-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
hub.go
121 lines (99 loc) · 2.89 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
package websocket
import (
"context"
"crypto/tls"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"v2ray.com/core/common"
"v2ray.com/core/common/net"
http_proto "v2ray.com/core/common/protocol/http"
"v2ray.com/core/common/session"
"v2ray.com/core/transport/internet"
v2tls "v2ray.com/core/transport/internet/tls"
)
type requestHandler struct {
path string
ln *Listener
}
var upgrader = &websocket.Upgrader{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: time.Second * 8,
}
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
newError("failed to convert to WebSocket connection").Base(err).WriteToLog()
return
}
forwardedAddrs := http_proto.ParseXForwardedFor(request.Header)
remoteAddr := conn.RemoteAddr()
if len(forwardedAddrs) > 0 && forwardedAddrs[0].Family().IsIP() {
remoteAddr.(*net.TCPAddr).IP = forwardedAddrs[0].IP()
}
h.ln.addConn(newConnection(conn, remoteAddr))
}
type Listener struct {
sync.Mutex
listener net.Listener
config *Config
addConn internet.ConnHandler
}
func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, addConn internet.ConnHandler) (internet.Listener, error) {
wsSettings := streamSettings.ProtocolSettings.(*Config)
var tlsConfig *tls.Config
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
tlsConfig = config.GetTLSConfig()
}
listener, err := listenTCP(ctx, address, port, tlsConfig, streamSettings.SocketSettings)
if err != nil {
return nil, err
}
l := &Listener{
config: wsSettings,
addConn: addConn,
listener: listener,
}
go func() {
if err := l.serve(); err != nil {
newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
}
}()
return l, err
}
func listenTCP(ctx context.Context, address net.Address, port net.Port, tlsConfig *tls.Config, sockopt *internet.SocketConfig) (net.Listener, error) {
listener, err := internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, sockopt)
if err != nil {
return nil, newError("failed to listen TCP on", address, ":", port).Base(err)
}
if tlsConfig != nil {
return tls.NewListener(listener, tlsConfig), nil
}
return listener, nil
}
func (ln *Listener) serve() error {
return http.Serve(ln.listener, &requestHandler{
path: ln.config.GetNormalizedPath(),
ln: ln,
})
}
// Addr implements net.Listener.Addr().
func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr()
}
// Close implements net.Listener.Close().
func (ln *Listener) Close() error {
return ln.listener.Close()
}
func init() {
common.Must(internet.RegisterTransportListener(protocolName, ListenWS))
}