-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunnel.go
203 lines (193 loc) · 4.22 KB
/
tunnel.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package tunnel
import (
"bufio"
"context"
"fmt"
"io"
"net"
"os"
"time"
)
type Tunnel struct {
ctx context.Context
conn net.PacketConn
localAddr net.UDPAddr
remoteAddr net.UDPAddr
localNAT *NATDetail
remoteNAT *NATDetail
signal Signal
cancelFunc context.CancelFunc
}
func NewTunnel(ctx context.Context, signal Signal) (*Tunnel, error) {
ctx, cancelFunc := context.WithCancel(ctx)
return &Tunnel{
ctx: ctx,
signal: signal,
cancelFunc: cancelFunc,
}, nil
}
func (t *Tunnel) Connect() error {
err := t.initTunnel()
if err != nil {
return err
}
c := handshake(t)
err, notClosed := <-c
if !notClosed {
log.Debugln("tunnel hole punch success")
log.Debugf("local addr: %s, remote addr: %s\n", t.localAddr.String(), t.remoteAddr.String())
return nil
}
return err
}
// TODO: temporary code ↓
func (t *Tunnel) Test(mode string) {
if mode == "server" {
t.runHandler()
} else {
go t.keepAlive()
for {
select {
case <-t.ctx.Done():
return
default:
fmt.Printf("Message: ")
r := bufio.NewReader(os.Stdin)
var in []byte
for {
var err error
in, err = r.ReadBytes('\n')
if err != io.EOF {
if err != nil {
break
}
}
if len(in) > 0 {
break
}
}
msg, err := NewDataMessage(t.localNAT.Token, in).Marshal()
if err != nil {
log.Debugf("marshal message error: %s\n", err)
continue
}
_, err = t.conn.WriteTo(msg, &t.remoteAddr)
if err != nil {
log.Debugf("send message error: %s\n", err)
continue
}
}
}
}
}
func (t *Tunnel) runHandler() {
bytes := make([]byte, 1024)
timeout := time.NewTimer(time.Second * 10)
for {
select {
case <-t.ctx.Done():
return
case <-timeout.C:
t.Close()
return
default:
err := t.conn.SetReadDeadline(time.Now().Add(time.Second * 5))
if err != nil {
log.Debugf("set read deadline error: %s\n", err)
continue
}
n, addr, err := t.conn.ReadFrom(bytes)
if err != nil {
log.Debugf("read from conn error: %s\n", err)
continue
}
if addr.String() != t.remoteAddr.String() {
log.Debugf("received message from unexpected addr: %s\n", addr.String())
continue
}
msg, err := UnmarshalMessage(bytes[:n])
if err != nil {
log.Debugf("unmarshal message error: %s\n", err)
continue
}
if msg.token != t.remoteNAT.Token {
log.Debugf("received message with unexpected token: %s\n", msg.token)
continue
}
switch msg.mType {
case MessageTypeHandshake:
log.Debugf("received handshake message from %s\n", addr.String())
case MessageTypePing:
if !timeout.Stop() {
<-timeout.C
}
timeout.Reset(time.Second * 10)
case MessageTypeData:
log.Debugf("received data message from %s, payload: %s\n", addr.String(), string(msg.payload))
}
}
}
}
func (t *Tunnel) keepAlive() {
// send and wait receive ping message
pingMsg, err := NewPingMessage(t.localNAT.Token).Marshal()
if err != nil {
return
}
tick := time.NewTicker(time.Second)
for {
select {
case <-t.ctx.Done():
return
case <-tick.C:
_, _ = t.conn.WriteTo(pingMsg, &t.remoteAddr)
}
}
}
// TODO: temporary code ↑
func (t *Tunnel) initTunnel() error {
conn, err := net.ListenPacket("udp4", "0.0.0.0:0")
if err != nil {
return err
}
defer func() {
_ = conn.Close()
}()
resolver, err := NewResolver(conn)
if err != nil {
return err
}
defer resolver.Close()
localNAT, err := resolver.Resolve()
if err != nil {
return err
}
t.localNAT = localNAT
err = t.signal.SendSignal(localNAT)
if err != nil {
return err
}
remoteNAT, err := t.signal.ReadSignal()
if err != nil {
return err
}
log.Debugf("remote nat type: %d, token: %s, addr: %s\n", remoteNAT.NATType, remoteNAT.Token, remoteNAT.Addr)
// if both NATs are symmetric, we can't do anything
if remoteNAT.NATType == NATTypeSymmetric && localNAT.NATType == NATTypeSymmetric {
return fmt.Errorf("symmetric NAT not supported")
}
t.localNAT = localNAT
t.remoteNAT = remoteNAT
t.localAddr = net.UDPAddr{
IP: net.IPv4zero,
Port: conn.LocalAddr().(*net.UDPAddr).Port,
}
return nil
}
func (t *Tunnel) Close() {
t.cancelFunc()
err := t.conn.Close()
if err != nil {
log.Debugf("close tunnel error: %s\n", err)
}
}