-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
111 lines (100 loc) · 2.52 KB
/
hub.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
package tcp
import (
"context"
gotls "crypto/tls"
"net"
"time"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/retry"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"
)
type TCPListener struct {
ctx context.Context
listener *net.TCPListener
tlsConfig *gotls.Config
authConfig internet.ConnectionAuthenticator
config *Config
conns chan<- internet.Connection
}
func ListenTCP(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- internet.Connection) (internet.Listener, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
log.Trace(newError("listening TCP on ", address, ":", port))
networkSettings := internet.TransportSettingsFromContext(ctx)
tcpSettings := networkSettings.(*Config)
l := &TCPListener{
ctx: ctx,
listener: listener,
config: tcpSettings,
conns: conns,
}
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*tls.Config)
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
}
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, newError("invalid header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, newError("invalid header settings.").Base(err).AtError()
}
l.authConfig = auth
}
go l.KeepAccepting()
return l, nil
}
func (v *TCPListener) KeepAccepting() {
for {
select {
case <-v.ctx.Done():
return
default:
}
var conn net.Conn
err := retry.ExponentialBackoff(5, 200).On(func() error {
rawConn, err := v.listener.Accept()
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Trace(newError("failed to accepted raw connections").Base(err).AtWarning())
continue
}
if v.tlsConfig != nil {
conn = tls.Server(conn, v.tlsConfig)
}
if v.authConfig != nil {
conn = v.authConfig.Server(conn)
}
select {
case v.conns <- internet.Connection(conn):
case <-time.After(time.Second * 5):
conn.Close()
}
}
}
func (v *TCPListener) Addr() net.Addr {
return v.listener.Addr()
}
func (v *TCPListener) Close() error {
return v.listener.Close()
}
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_TCP, ListenTCP))
}