-
Notifications
You must be signed in to change notification settings - Fork 3
/
tunat.go
129 lines (119 loc) · 2.75 KB
/
tunat.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package tunat
import (
"errors"
"fmt"
"io"
"net"
"net/netip"
"os/exec"
"sync"
"github.com/FH0/tunat/device"
"gvisor.dev/gvisor/pkg/tcpip/header"
)
// Tunat main struct
type Tunat struct {
file io.ReadWriteCloser
tcpListener net.Listener
ipv4TCPListenerAddrPort netip.AddrPort
ipv6TCPListenerAddrPort netip.AddrPort
fakeIPv4Addr netip.Addr
fakeIPv6Addr netip.Addr
udpChan chan udpData
bufLen int
tcpMap sync.Map
}
// New new a Tunat
func New(name 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.New(name)
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
}
// Close close tun device
func (t *Tunat) Close() (err error) {
return t.file.Close()
}
func (t *Tunat) start() {
buf := make([]byte, t.bufLen)
for {
nread, err := t.file.Read(buf)
if err != nil {
fmt.Printf("tunat read error: %v\n", err)
return
}
packet := buf[:nread]
switch header.IPVersion(packet) {
case header.IPv4Version:
ipHeader := header.IPv4(packet)
switch ipHeader.TransportProtocol() {
case header.TCPProtocolNumber:
t.handleIPv4TCP(ipHeader, ipHeader.Payload())
case header.UDPProtocolNumber:
t.handleIPv4UDP(ipHeader, ipHeader.Payload())
}
case header.IPv6Version:
ipHeader := header.IPv6(packet)
switch ipHeader.TransportProtocol() {
case header.TCPProtocolNumber:
t.handleIPv6TCP(ipHeader, ipHeader.Payload())
case header.UDPProtocolNumber:
t.handleIPv6UDP(ipHeader, ipHeader.Payload())
}
}
}
}
func excuteCommands(commands []string) (err error) {
for _, cmd := range commands {
err = exec.Command("bash", "-c", cmd).Run()
if err != nil {
return
}
}
return
}