-
Notifications
You must be signed in to change notification settings - Fork 93
/
client.go
213 lines (193 loc) · 6.21 KB
/
client.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
package gws
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"encoding/binary"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/lxzan/gws/internal"
)
type Dialer interface {
Dial(network, addr string) (c net.Conn, err error)
}
type connector struct {
option *ClientOption
conn net.Conn
eventHandler Event
secWebsocketKey string
}
// NewClient 创建客户端
// Create New client
func NewClient(handler Event, option *ClientOption) (*Conn, *http.Response, error) {
option = initClientOption(option)
c := &connector{option: option, eventHandler: handler}
URL, err := url.Parse(option.Addr)
if err != nil {
return nil, nil, err
}
if URL.Scheme != "ws" && URL.Scheme != "wss" {
return nil, nil, ErrUnsupportedProtocol
}
var tlsEnabled = URL.Scheme == "wss"
dialer, err := option.NewDialer()
if err != nil {
return nil, nil, err
}
port := internal.SelectValue(URL.Port() == "", internal.SelectValue(tlsEnabled, "443", "80"), URL.Port())
hp := internal.SelectValue(URL.Hostname() == "", "127.0.0.1", URL.Hostname()) + ":" + port
c.conn, err = dialer.Dial("tcp", hp)
if err != nil {
return nil, nil, err
}
if tlsEnabled {
if option.TlsConfig == nil {
option.TlsConfig = &tls.Config{}
}
if option.TlsConfig.ServerName == "" {
option.TlsConfig.ServerName = URL.Host
}
c.conn = tls.Client(c.conn, option.TlsConfig)
}
client, resp, err := c.handshake()
if err != nil {
_ = c.conn.Close()
}
return client, resp, err
}
// NewClientFromConn 通过外部连接创建客户端, 支持 TCP/KCP/Unix Domain Socket
// Create New client via external connection, supports TCP/KCP/Unix Domain Socket.
func NewClientFromConn(handler Event, option *ClientOption, conn net.Conn) (*Conn, *http.Response, error) {
option = initClientOption(option)
c := &connector{option: option, conn: conn, eventHandler: handler}
client, resp, err := c.handshake()
if err != nil {
_ = c.conn.Close()
}
return client, resp, err
}
func (c *connector) request() (*http.Response, *bufio.Reader, error) {
_ = c.conn.SetDeadline(time.Now().Add(c.option.HandshakeTimeout))
ctx, cancel := context.WithTimeout(context.Background(), c.option.HandshakeTimeout)
defer cancel()
// 构建请求
r, err := http.NewRequestWithContext(ctx, http.MethodGet, c.option.Addr, nil)
if err != nil {
return nil, nil, err
}
for k, v := range c.option.RequestHeader {
r.Header[k] = v
}
r.Header.Set(internal.Connection.Key, internal.Connection.Val)
r.Header.Set(internal.Upgrade.Key, internal.Upgrade.Val)
r.Header.Set(internal.SecWebSocketVersion.Key, internal.SecWebSocketVersion.Val)
if c.option.PermessageDeflate.Enabled {
r.Header.Set(internal.SecWebSocketExtensions.Key, c.option.PermessageDeflate.genRequestHeader())
}
if c.secWebsocketKey == "" {
var key [16]byte
binary.BigEndian.PutUint64(key[0:8], internal.AlphabetNumeric.Uint64())
binary.BigEndian.PutUint64(key[8:16], internal.AlphabetNumeric.Uint64())
c.secWebsocketKey = base64.StdEncoding.EncodeToString(key[0:])
r.Header.Set(internal.SecWebSocketKey.Key, c.secWebsocketKey)
}
var ch = make(chan error)
// 发送请求
go func() { ch <- r.Write(c.conn) }()
// 同步等待请求是否发送成功
select {
case err = <-ch:
case <-ctx.Done():
err = ctx.Err()
}
if err != nil {
return nil, nil, err
}
// 读取响应结果
br := bufio.NewReaderSize(c.conn, c.option.ReadBufferSize)
resp, err := http.ReadResponse(br, r)
return resp, br, err
}
func (c *connector) getPermessageDeflate(extensions string) PermessageDeflate {
serverPD := permessageNegotiation(extensions)
clientPD := c.option.PermessageDeflate
return PermessageDeflate{
Enabled: clientPD.Enabled && strings.Contains(extensions, internal.PermessageDeflate),
Threshold: clientPD.Threshold,
Level: clientPD.Level,
PoolSize: clientPD.PoolSize,
ServerContextTakeover: serverPD.ServerContextTakeover,
ClientContextTakeover: serverPD.ClientContextTakeover,
ServerMaxWindowBits: serverPD.ServerMaxWindowBits,
ClientMaxWindowBits: serverPD.ClientMaxWindowBits,
}
}
func (c *connector) handshake() (*Conn, *http.Response, error) {
resp, br, err := c.request()
if err != nil {
return nil, resp, err
}
if err = c.checkHeaders(resp); err != nil {
return nil, resp, err
}
subprotocol, err := c.getSubProtocol(resp)
if err != nil {
return nil, resp, err
}
var extensions = resp.Header.Get(internal.SecWebSocketExtensions.Key)
var pd = c.getPermessageDeflate(extensions)
socket := &Conn{
ss: c.option.NewSession(),
isServer: false,
subprotocol: subprotocol,
pd: pd,
conn: c.conn,
config: c.option.getConfig(),
br: br,
continuationFrame: continuationFrame{},
fh: frameHeader{},
handler: c.eventHandler,
closed: 0,
deflater: new(deflater),
writeQueue: workerQueue{maxConcurrency: 1},
readQueue: make(channel, c.option.ParallelGolimit),
}
if pd.Enabled {
socket.deflater.initialize(false, pd)
if pd.ServerContextTakeover {
socket.dpsWindow.initialize(nil, pd.ServerMaxWindowBits)
}
if pd.ClientContextTakeover {
socket.cpsWindow.initialize(nil, pd.ClientMaxWindowBits)
}
}
return socket, resp, c.conn.SetDeadline(time.Time{})
}
func (c *connector) getSubProtocol(resp *http.Response) (string, error) {
a := internal.Split(c.option.RequestHeader.Get(internal.SecWebSocketProtocol.Key), ",")
b := internal.Split(resp.Header.Get(internal.SecWebSocketProtocol.Key), ",")
subprotocol := internal.GetIntersectionElem(a, b)
if len(a) > 0 && subprotocol == "" {
return "", ErrSubprotocolNegotiation
}
return subprotocol, nil
}
func (c *connector) checkHeaders(resp *http.Response) error {
if resp.StatusCode != http.StatusSwitchingProtocols {
return ErrHandshake
}
if !internal.HttpHeaderContains(resp.Header.Get(internal.Connection.Key), internal.Connection.Val) {
return ErrHandshake
}
if !strings.EqualFold(resp.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
return ErrHandshake
}
if resp.Header.Get(internal.SecWebSocketAccept.Key) != internal.ComputeAcceptKey(c.secWebsocketKey) {
return ErrHandshake
}
return nil
}