This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprotdial.go
242 lines (193 loc) · 4.82 KB
/
protdial.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package p2pforwarder
import (
"context"
"encoding/binary"
"fmt"
"io"
"math/rand"
"net"
"strconv"
"sync"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/pion/udp"
)
const dialProtID protocol.ID = "/p2pforwarder/dial/1.0.0"
var dialsIP = "127.0.88.89"
func setDialHandler(f *Forwarder) {
f.host.SetStreamHandler(dialProtID, func(s network.Stream) {
onInfoFn("'dial' from " + s.Conn().RemotePeer().Pretty())
portBytes := make([]byte, 3)
_, err := io.ReadFull(s, portBytes)
if err != nil {
s.Reset()
onErrFn(fmt.Errorf("dial handler: %s", err))
return
}
protocolType := portBytes[0]
port := binary.BigEndian.Uint16(portBytes[1:])
portInt := int(port)
var (
addr string
portsMap *openPortsStoreMap
)
switch protocolType {
case protocolTypeTCP:
addr = "tcp:" + strconv.Itoa(portInt)
portsMap = f.openPorts.tcp
case protocolTypeUDP:
addr = "udp:" + strconv.Itoa(portInt)
portsMap = f.openPorts.udp
default:
s.Reset()
return
}
onInfoFn("Dialing to " + addr + " from " + s.Conn().RemotePeer().Pretty())
defer onInfoFn("Closed dial to " + addr + " from " + s.Conn().RemotePeer().Pretty())
portsMap.mux.Lock()
portContext := portsMap.ports[port]
portsMap.mux.Unlock()
if portContext == nil {
s.Reset()
return
}
var conn net.Conn
switch protocolType {
case protocolTypeTCP:
conn, err = net.DialTCP("tcp", &net.TCPAddr{
IP: net.ParseIP(dialsIP),
Port: 0,
}, &net.TCPAddr{
IP: nil,
Port: portInt,
})
case protocolTypeUDP:
conn, err = net.DialUDP("udp", &net.UDPAddr{
IP: net.ParseIP(dialsIP),
Port: 0,
}, &net.UDPAddr{
IP: nil,
Port: portInt,
})
}
if err != nil {
s.Reset()
onErrFn(fmt.Errorf("dial handler: %s", err))
return
}
pipeBothIOsAndClose(portContext, s, conn)
})
}
func createAddrInfoString(network string, listenip string, lport int, port int) string {
return network + " " + listenip + ":" + strconv.Itoa(lport) + " -> " + strconv.Itoa(port)
}
func (f *Forwarder) dial(ctx context.Context, peerid peer.ID, protocolType byte, listenip string, port uint16) {
lport := int(port)
var addressinfostr string
var listenfunc func(lip net.IP, port int) (net.Listener, error)
switch protocolType {
case protocolTypeTCP:
addressinfostr = createAddrInfoString("tcp", listenip, lport, int(port))
listenfunc = func(lip net.IP, port int) (net.Listener, error) {
return net.ListenTCP("tcp", &net.TCPAddr{
IP: lip,
Port: port,
})
}
case protocolTypeUDP:
addressinfostr = createAddrInfoString("udp", listenip, lport, int(port))
listenfunc = func(lip net.IP, port int) (net.Listener, error) {
return udp.Listen("udp", &net.UDPAddr{
IP: lip,
Port: port,
})
}
}
lip := net.ParseIP(listenip)
ln, err := listenfunc(lip, lport)
if err != nil {
onErrFn(fmt.Errorf("dial: %s", err))
for i := 0; i < 4; i++ {
lport = rand.Intn(65535-1024) + 1024
ln, err = listenfunc(lip, lport)
if err != nil {
onErrFn(fmt.Errorf("dial: %s", err))
} else {
break
}
}
if err != nil {
return
}
}
onInfoFn("Listening " + addressinfostr)
go func() {
loop:
for {
conn, err := ln.Accept()
if err != nil {
onErrFn(fmt.Errorf("dial: %s", err))
select {
case <-ctx.Done():
break loop
default:
continue loop
}
}
onInfoFn("Accepted " + ln.Addr().Network() + " connection from " + conn.RemoteAddr().String() + " on " + ln.Addr().String())
go func() {
defer onInfoFn("Closed " + ln.Addr().Network() + " connection from " + conn.RemoteAddr().String() + " on " + ln.Addr().String())
s, err := f.host.NewStream(ctx, peerid, dialProtID)
if err != nil {
conn.Close()
onErrFn(fmt.Errorf("dial: %s", err))
return
}
p := make([]byte, 3)
p[0] = protocolType
binary.BigEndian.PutUint16(p[1:3], port)
_, err = s.Write(p)
if err != nil {
s.Reset()
conn.Close()
onErrFn(fmt.Errorf("dial: %s", err))
return
}
pipeBothIOsAndClose(ctx, conn, s)
}()
}
}()
<-ctx.Done()
ln.Close()
onInfoFn("Closed " + addressinfostr)
}
// pipeBothIOsAndClose pipes `a` and `b` in both directions and closes them in the end
func pipeBothIOsAndClose(parentctx context.Context, a io.ReadWriteCloser, b io.ReadWriteCloser) {
ctx, cancel := context.WithCancel(parentctx)
var wg sync.WaitGroup
wg.Add(2)
go func() {
wg.Wait()
cancel()
}()
go func() {
_, err := io.Copy(b, a)
wg.Done()
if err != nil {
onErrFn(fmt.Errorf("pipeBothIOsAndClose b<-a: %s", err))
cancel()
}
}()
go func() {
_, err := io.Copy(a, b)
wg.Done()
if err != nil {
onErrFn(fmt.Errorf("pipeBothIOsAndClose a<-b: %s", err))
cancel()
}
}()
<-ctx.Done()
a.Close()
b.Close()
}