forked from v2ray/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhub.go
143 lines (122 loc) · 3.32 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
package websocket
import (
"context"
"crypto/tls"
"net"
"net/http"
"strconv"
"sync"
"time"
"github.com/gorilla/websocket"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
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 requestHandler struct {
path string
ln *Listener
}
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != h.path {
writer.WriteHeader(http.StatusNotFound)
return
}
conn, err := converttovws(writer, request)
if err != nil {
log.Info("WebSocket|Listener: Failed to convert to WebSocket connection: ", err)
return
}
select {
case <-h.ln.ctx.Done():
conn.Close()
case h.ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, h.ln, internal.ReuseConnection(h.ln.config.IsConnectionReuse())):
case <-time.After(time.Second * 5):
conn.Close()
}
}
type Listener struct {
sync.Mutex
ctx context.Context
listener net.Listener
tlsConfig *tls.Config
config *Config
conns chan<- internet.Connection
}
func ListenWS(ctx context.Context, address v2net.Address, port v2net.Port, conns chan<- internet.Connection) (internet.Listener, error) {
networkSettings := internet.TransportSettingsFromContext(ctx)
wsSettings := networkSettings.(*Config)
l := &Listener{
ctx: ctx,
config: wsSettings,
conns: conns,
}
if securitySettings := internet.SecuritySettingsFromContext(ctx); securitySettings != nil {
tlsConfig, ok := securitySettings.(*v2tls.Config)
if ok {
l.tlsConfig = tlsConfig.GetTLSConfig()
}
}
err := l.listenws(address, port)
return l, err
}
func (ln *Listener) listenws(address v2net.Address, port v2net.Port) error {
netAddr := address.String() + ":" + strconv.Itoa(int(port.Value()))
var listener net.Listener
if ln.tlsConfig == nil {
l, err := net.Listen("tcp", netAddr)
if err != nil {
return errors.Base(err).Message("WebSocket|Listener: Failed to listen TCP ", netAddr)
}
listener = l
} else {
l, err := tls.Listen("tcp", netAddr, ln.tlsConfig)
if err != nil {
return errors.Base(err).Message("WebSocket|Listener: Failed to listen TLS ", netAddr)
}
listener = l
}
ln.listener = listener
go func() {
http.Serve(listener, &requestHandler{
path: ln.config.GetNormailzedPath(),
ln: ln,
})
}()
return nil
}
func converttovws(w http.ResponseWriter, r *http.Request) (*connection, error) {
var upgrader = websocket.Upgrader{
ReadBufferSize: 32 * 1024,
WriteBufferSize: 32 * 1024,
}
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return nil, err
}
return &connection{wsc: conn}, nil
}
func (ln *Listener) Put(id internal.ConnectionID, conn net.Conn) {
select {
case <-ln.ctx.Done():
conn.Close()
case ln.conns <- internal.NewConnection(internal.ConnectionID{}, conn, ln, internal.ReuseConnection(ln.config.IsConnectionReuse())):
case <-time.After(time.Second * 5):
conn.Close()
}
}
func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr()
}
func (ln *Listener) Close() error {
return ln.listener.Close()
}
func init() {
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_WebSocket, ListenWS))
}