-
Notifications
You must be signed in to change notification settings - Fork 0
/
binder.go
266 lines (237 loc) · 5.87 KB
/
binder.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package socksy5
import (
"net"
"os"
"runtime"
"sync"
"time"
)
// Binder handles the BND requests.
//
// Binder can listen for inbounds for different requests on the same port.
// Thus it is totally ok if you want to handle for multiple requests on one
// fixed port. Although, please note that, if Binder is not listening for
// any inbound on a port, the listener on that port will be closed,
// and will only be created again when next time Binder is required to listen
// on that port.
//
// Currently if req is to be denied, only [RepGeneralFailure]
// will be replied.
type Binder struct {
// Hostname of the server, not to be confused with listening address.
// This is the address that will be sent in the first BND reply.
// RFC 1928 states that the addresses in replies to BIND requests shall
// be IP addresses, but a host name is considered valid here.
// Do not modify this field when Binder is running.
Hostname string
mux sync.Mutex
dispatchers map[string]*tcpDispatcher
}
// Handle handles the BND request, blocks until error or successful bind.
// It can be called simultainously.
//
// addr represents the address to listen at, and it can be empty,
// in this case Handle will listen on 0.0.0.0 and :: with one single
// system allocated port.
// If the host part in addr is a host name,
// Handle will resolve it to IP addresses and listen on all of them.
// If the port in addr is 0,
// Handle will try to use one single system allocated port for all of them.
//
// Handle doesn't wait for the data transmission after the 2nd reply to finish.
//
// timeout is disabled if it's 0.
func (b *Binder) Handle(req *BindRequest, addr string, timeout time.Duration) error {
cancel := make(chan struct{})
if timeout != 0 {
time.AfterFunc(timeout, func() {
close(cancel)
})
}
b.mux.Lock()
if b.dispatchers == nil {
b.dispatchers = make(map[string]*tcpDispatcher)
}
b.mux.Unlock()
dstIPs, err := net.LookupIP(req.Dst().Host())
if err != nil {
req.Deny(RepGeneralFailure, "")
return err
}
select {
case <-cancel:
req.Deny(RepGeneralFailure, "")
return os.ErrDeadlineExceeded
default:
}
dispatchers, err := b.getDispatchers(addr)
if err != nil {
req.Deny(RepGeneralFailure, "")
return err
}
_, port, _ := net.SplitHostPort(dispatchers[0].listener.Addr().String())
ready := make(chan struct{})
var conn net.Conn
connChan := make(chan net.Conn)
errChan := make(chan error)
defer close(connChan)
go func() {
conn = <-connChan
close(ready)
}()
err = nil
for _, l := range dispatchers {
for _, ip := range dstIPs {
addr := net.JoinHostPort(ip.String(), port)
if existed := l.subscribe(addr, connChan, errChan); existed {
err = ErrDuplicatedRequest
continue
}
defer l.unsubscribe(addr)
}
}
if err != nil {
req.Deny(RepGeneralFailure, "")
return err
}
if ok := req.Accept(net.JoinHostPort(b.Hostname, port)); !ok {
return ErrAcceptOrDenyFailed
}
err = nil
select {
case <-cancel:
err = os.ErrDeadlineExceeded
case err = <-errChan:
case <-ready:
}
if err != nil {
go func() {
<-ready
if conn != nil {
conn.Close()
}
}()
req.Deny(RepGeneralFailure, "")
return err
}
if ok := req.Bind(conn); !ok {
return ErrAcceptOrDenyFailed
}
return nil
}
func (b *Binder) getDispatchers(addr string) ([]*tcpDispatcher, error) {
var ips []net.IP
var port string
if addr == "" {
if runtime.GOOS == "dragonfly" || runtime.GOOS == "openbsd" {
ips = []net.IP{net.IPv4zero, net.IPv6zero}
} else {
ips = []net.IP{net.IPv4zero}
}
port = "0"
} else {
var host string
var err error
host, port, err = net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ips, err = net.LookupIP(host)
if err != nil {
return nil, err
}
}
b.mux.Lock()
defer b.mux.Unlock()
result := make([]*tcpDispatcher, 0, len(ips))
newDispatchers := make([]*tcpDispatcher, 0, 4)
var err error
for _, ip := range ips {
d := b.dispatchers[net.JoinHostPort(ip.String(), port)]
if d != nil {
result = append(result, d)
continue
}
var l net.Listener
l, err = net.Listen("tcp", net.JoinHostPort(ip.String(), port))
if err != nil {
break
}
d = &tcpDispatcher{
listener: l,
connChanByAddr: make(map[string]chan<- net.Conn),
errChanByAddr: make(map[string]chan<- error),
binder: b,
mux: &b.mux,
}
result = append(result, d)
newDispatchers = append(newDispatchers, d)
if port == "0" {
_, port, _ = net.SplitHostPort(l.Addr().String())
}
}
if err != nil {
for _, d := range newDispatchers {
d.listener.Close()
}
return nil, err
}
for _, d := range newDispatchers {
b.dispatchers[d.listener.Addr().String()] = d
go d.run()
}
return result, nil
}
type tcpDispatcher struct {
listener net.Listener
connChanByAddr map[string]chan<- net.Conn
errChanByAddr map[string]chan<- error
mux *sync.Mutex
binder *Binder
}
func (d *tcpDispatcher) subscribe(addr string, connChan chan<- net.Conn, errChan chan<- error) (existed bool) {
d.mux.Lock()
defer d.mux.Unlock()
if ch := d.connChanByAddr[addr]; ch != nil {
return true
}
d.connChanByAddr[addr] = connChan
d.errChanByAddr[addr] = errChan
return false
}
func (d *tcpDispatcher) unsubscribe(addr string) {
d.mux.Lock()
defer d.mux.Unlock()
delete(d.connChanByAddr, addr)
delete(d.errChanByAddr, addr)
if len(d.connChanByAddr) == 0 {
d.listener.Close()
delete(d.binder.dispatchers, d.listener.Addr().String())
}
}
func (d *tcpDispatcher) run() {
for {
conn, err := d.listener.Accept()
d.mux.Lock()
if err != nil {
for _, errChan := range d.errChanByAddr {
select {
case errChan <- err:
default:
}
}
d.mux.Unlock()
return
}
if connChan := d.connChanByAddr[conn.RemoteAddr().String()]; connChan != nil {
select {
case connChan <- conn:
d.mux.Unlock()
continue
default:
}
}
conn.Close()
d.mux.Unlock()
}
}