-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtunat_linux.go
65 lines (60 loc) · 1.33 KB
/
tunat_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tunat
import (
"errors"
"net"
"net/netip"
"github.com/FH0/tunat/device"
)
// NewFromUnixSocket new a Tunat from unix
func NewFromUnixSocket(path string,
ipv4Prefix,
ipv6Prefix netip.Prefix,
bufLen int,
preCommands []string,
postCommands []string,
) (tunat *Tunat, err error) {
tunat = &Tunat{
udpChan: make(chan udpData, 100),
bufLen: bufLen,
}
err = excuteCommands(preCommands)
if err != nil {
return
}
tunat.file, err = device.NewFromUnixSocket(path)
if err != nil {
return
}
err = excuteCommands(postCommands)
if err != nil {
return
}
tunat.tcpListener, err = net.Listen("tcp", "[::]:0")
if err != nil {
return
}
if ipv4Prefix.IsValid() {
tunat.ipv4TCPListenerAddrPort = netip.AddrPortFrom(
ipv4Prefix.Addr(),
uint16(tunat.tcpListener.Addr().(*net.TCPAddr).Port),
)
tunat.fakeIPv4Addr = ipv4Prefix.Addr().Next()
if !ipv4Prefix.Contains(tunat.fakeIPv4Addr) {
err = errors.New("ipv4 next address is out of CIDR")
return
}
}
if ipv6Prefix.IsValid() {
tunat.ipv6TCPListenerAddrPort = netip.AddrPortFrom(
ipv6Prefix.Addr(),
uint16(tunat.tcpListener.Addr().(*net.TCPAddr).Port),
)
tunat.fakeIPv6Addr = ipv6Prefix.Addr().Next()
if !ipv6Prefix.Contains(tunat.fakeIPv6Addr) {
err = errors.New("ipv6 next address is out of CIDR")
return
}
}
go tunat.start()
return
}