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 pathmethods.go
189 lines (151 loc) · 4.15 KB
/
methods.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
package p2pforwarder
import (
"context"
"errors"
"strconv"
"sync"
"github.com/libp2p/go-libp2p-core/peer"
)
var (
// ErrMaxConnections = error "Max connections reached"
ErrMaxConnections = errors.New("Max connections reached")
// ErrPortAlreadyOpened = error "Port already opened"
ErrPortAlreadyOpened = errors.New("Port already opened")
// ErrUnknownNetworkType = error "Unknown network type, it must be \"tcp\" or \"udp\""
ErrUnknownNetworkType = errors.New("Unknown network type, it must be \"tcp\" or \"udp\"")
// ErrConnectionExists = error "You are already connected to specified host"
ErrConnectionExists = errors.New("You are already connected to specified host")
)
// OpenPort opens port in specified networkType - "tcp" or "udp"
func (f *Forwarder) OpenPort(networkType string, port uint16) (cancel func(), err error) {
switch networkType {
case "tcp":
cancel, err = f.addOpenPort(f.openPorts.tcp, port)
case "udp":
cancel, err = f.addOpenPort(f.openPorts.udp, port)
default:
cancel, err = nil, ErrUnknownNetworkType
return
}
if err == nil {
go f.publishOpenPortsManifest()
}
return cancel, err
}
func (f *Forwarder) addOpenPort(portsMap *openPortsStoreMap, port uint16) (cancel func(), err error) {
portsMap.mux.Lock()
if portsMap.ports[port] != nil {
portsMap.mux.Unlock()
return nil, ErrPortAlreadyOpened
}
var cancelfn func()
portsMap.ports[port], cancelfn = context.WithCancel(context.Background())
portsMap.mux.Unlock()
cancel = func() {
portsMap.mux.Lock()
cancelfn()
delete(portsMap.ports, port)
portsMap.mux.Unlock()
go f.publishOpenPortsManifest()
}
return cancel, nil
}
var (
listenIPks = make([]bool, 255)
listenIPksMux sync.Mutex
)
// Connect starts forwarding connections to `listenip`:`PORT` to passed id`:`PORT`
func (f *Forwarder) Connect(id string) (listenip string, cancel context.CancelFunc, err error) {
peerid, err := peer.IDB58Decode(id)
if err != nil {
return "", nil, err
}
// Getting free ip part
listenIPksMux.Lock()
lIPk := -1
for k, v := range listenIPks {
if v {
continue
}
lIPk = k
listenIPks[lIPk] = true
break
}
if lIPk == -1 {
return "", nil, ErrMaxConnections
}
listenip = "127.0.89." + strconv.Itoa(lIPk)
listenIPksMux.Unlock()
// Registering subscription
f.portsSubscriptionsMux.Lock()
if _, ok := f.portsSubscriptions[peerid]; ok {
f.portsSubscriptionsMux.Unlock()
listenIPksMux.Lock()
listenIPks[lIPk] = false
listenIPksMux.Unlock()
return "", nil, ErrConnectionExists
}
subCh := make(chan *portsManifest, 5)
f.portsSubscriptions[peerid] = subCh
f.portsSubscriptionsMux.Unlock()
ctx, cancel := context.WithCancel(context.Background())
go func() {
var (
tcpPortsOld = make(map[uint16]func())
udpPortsOld = make(map[uint16]func())
)
loop:
for {
select {
case <-ctx.Done():
f.portsSubscriptionsMux.Lock()
delete(f.portsSubscriptions, peerid)
close(subCh)
f.portsSubscriptionsMux.Unlock()
listenIPksMux.Lock()
listenIPks[lIPk] = false
listenIPksMux.Unlock()
break loop
case portsM := <-subCh:
if portsM.tcp != nil {
f.updatePortsListening(ctx, protocolTypeTCP, portsM.tcp, &tcpPortsOld, peerid, listenip)
}
if portsM.udp != nil {
f.updatePortsListening(ctx, protocolTypeUDP, portsM.udp, &udpPortsOld, peerid, listenip)
}
}
}
}()
s, err := f.host.NewStream(ctx, peerid, portssubProtID)
if err != nil {
cancel()
return "", nil, err
}
// This starts subscription
_, err = s.Write([]byte{portssubModeSubscribe})
if err != nil {
s.Reset()
cancel()
return "", nil, err
}
s.Close()
return listenip, cancel, nil
}
func (f *Forwarder) updatePortsListening(parentCtx context.Context, protocolType byte, portsArr []uint16, portsOld *map[uint16]func(), peerid peer.ID, listenip string) {
ports := make(map[uint16]func())
for _, port := range portsArr {
cancel, ok := (*portsOld)[port]
if ok {
ports[port] = cancel
delete(*portsOld, port)
continue
}
var ctx context.Context
ctx, ports[port] = context.WithCancel(parentCtx)
go f.dial(ctx, peerid, protocolType, listenip, port)
}
for _, v := range *portsOld {
v()
}
*portsOld = ports
}