forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
202 lines (184 loc) · 4.26 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
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
package tcp
import (
"crypto/tls"
"net"
"sync"
"time"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/internal"
v2tls "v2ray.com/core/transport/internet/tls"
)
var (
ErrClosedListener = errors.New("Listener is closed.")
)
type ConnectionWithError struct {
conn net.Conn
err error
}
type TCPListener struct {
sync.Mutex
acccepting bool
listener *net.TCPListener
awaitingConns chan *ConnectionWithError
tlsConfig *tls.Config
authConfig internet.ConnectionAuthenticator
config *Config
}
func ListenTCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
if err != nil {
return nil, err
}
tcpSettings := networkSettings.(*Config)
l := &TCPListener{
acccepting: true,
listener: listener,
awaitingConns: make(chan *ConnectionWithError, 32),
config: tcpSettings,
}
if options.Stream != nil && options.Stream.HasSecuritySettings() {
securitySettings, err := options.Stream.GetEffectiveSecuritySettings()
if err != nil {
log.Error("TCP: Failed to get security config: ", err)
return nil, err
}
tlsConfig, ok := securitySettings.(*v2tls.Config)
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
}
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, errors.Base(err).Message("Internet|TCP: Invalid header settings.")
}
auth, err := internet.CreateConnectionAuthenticator(tcpSettings.HeaderSettings.Type, headerConfig)
if err != nil {
return nil, errors.Base(err).Message("Internet|TCP: Invalid header settings.")
}
l.authConfig = auth
}
go l.KeepAccepting()
return l, nil
}
func (v *TCPListener) Accept() (internet.Connection, error) {
for v.acccepting {
select {
case connErr, open := <-v.awaitingConns:
if !open {
return nil, ErrClosedListener
}
if connErr.err != nil {
return nil, connErr.err
}
conn := connErr.conn
return NewConnection(internal.ConnectionId{}, conn, v, v.config), nil
case <-time.After(time.Second * 2):
}
}
return nil, ErrClosedListener
}
func (v *TCPListener) KeepAccepting() {
for v.acccepting {
conn, err := v.listener.Accept()
v.Lock()
if !v.acccepting {
v.Unlock()
break
}
if v.tlsConfig != nil {
conn = tls.Server(conn, v.tlsConfig)
}
if v.authConfig != nil {
conn = v.authConfig.Server(conn)
}
select {
case v.awaitingConns <- &ConnectionWithError{
conn: conn,
err: err,
}:
default:
if conn != nil {
conn.Close()
}
}
v.Unlock()
}
}
func (v *TCPListener) Put(id internal.ConnectionId, conn net.Conn) {
v.Lock()
defer v.Unlock()
if !v.acccepting {
return
}
select {
case v.awaitingConns <- &ConnectionWithError{conn: conn}:
default:
conn.Close()
}
}
func (v *TCPListener) Addr() net.Addr {
return v.listener.Addr()
}
func (v *TCPListener) Close() error {
v.Lock()
defer v.Unlock()
v.acccepting = false
v.listener.Close()
close(v.awaitingConns)
for connErr := range v.awaitingConns {
if connErr.conn != nil {
go connErr.conn.Close()
}
}
return nil
}
type RawTCPListener struct {
accepting bool
listener *net.TCPListener
}
func (v *RawTCPListener) Accept() (internet.Connection, error) {
conn, err := v.listener.AcceptTCP()
if err != nil {
return nil, err
}
return &RawConnection{
TCPConn: *conn,
}, nil
}
func (v *RawTCPListener) Addr() net.Addr {
return v.listener.Addr()
}
func (v *RawTCPListener) Close() error {
v.accepting = false
v.listener.Close()
return nil
}
func ListenRawTCP(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: address.IP(),
Port: int(port),
})
if err != nil {
return nil, err
}
// TODO: handle listen options
return &RawTCPListener{
accepting: true,
listener: listener,
}, nil
}
func init() {
internet.TCPListenFunc = ListenTCP
internet.RawTCPListenFunc = ListenRawTCP
}