-
Notifications
You must be signed in to change notification settings - Fork 17
/
client.go
158 lines (132 loc) · 3.72 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
package fastws
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/tls"
"errors"
"net"
"github.com/valyala/fasthttp"
)
var (
// ErrCannotUpgrade shows up when an error ocurred when upgrading a connection.
ErrCannotUpgrade = errors.New("cannot upgrade connection")
)
// Client returns Conn using existing connection.
//
// url must be complete URL format i.e. http://localhost:8080/ws
func Client(c net.Conn, url string) (*Conn, error) {
return client(c, url, nil)
}
// ClientWithHeaders returns a Conn using existing connection and sending personalized headers.
func ClientWithHeaders(c net.Conn, url string, req *fasthttp.Request) (*Conn, error) {
return client(c, url, req)
}
// UpgradeAsClient will upgrade the connection as a client
//
// r can be nil.
func UpgradeAsClient(c net.Conn, url string, r *fasthttp.Request) error {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
defer fasthttp.ReleaseURI(uri)
uri.Update(url)
origin := bytePool.Get().([]byte)
key := bytePool.Get().([]byte)
defer bytePool.Put(origin)
defer bytePool.Put(key)
origin = prepareOrigin(origin, uri)
key = makeRandKey(key[:0])
if r != nil {
r.CopyTo(req)
}
req.Header.SetMethod("GET")
req.Header.AddBytesKV(originString, origin)
req.Header.AddBytesKV(connectionString, upgradeString)
req.Header.AddBytesKV(upgradeString, websocketString)
req.Header.AddBytesKV(wsHeaderVersion, supportedVersions[0])
req.Header.AddBytesKV(wsHeaderKey, key)
// TODO: Add compression
req.SetRequestURIBytes(uri.FullURI())
br := bufio.NewReader(c)
bw := bufio.NewWriter(c)
req.Write(bw)
bw.Flush()
err := res.Read(br)
if err == nil {
if res.StatusCode() != 101 ||
!equalsFold(res.Header.PeekBytes(upgradeString), websocketString) {
err = ErrCannotUpgrade
}
}
return err
}
func client(c net.Conn, url string, r *fasthttp.Request) (conn *Conn, err error) {
err = UpgradeAsClient(c, url, r)
if err == nil {
conn = acquireConn(c)
conn.server = false
}
return conn, err
}
// Dial establishes a websocket connection as client.
//
// url parameter must follow WebSocket URL format i.e. ws://host:port/path
func Dial(url string) (*Conn, error) {
cnf := &tls.Config{
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS11,
}
return dial(url, cnf, nil)
}
// DialTLS establishes a websocket connection as client with the
// parsed tls.Config. The config will be used if the URL is wss:// like.
func DialTLS(url string, cnf *tls.Config) (*Conn, error) {
return dial(url, cnf, nil)
}
// DialWithHeaders establishes a websocket connection as client sending a personalized request.
func DialWithHeaders(url string, req *fasthttp.Request) (*Conn, error) {
cnf := &tls.Config{
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS11,
}
return dial(url, cnf, req)
}
func dial(url string, cnf *tls.Config, req *fasthttp.Request) (conn *Conn, err error) {
uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseURI(uri)
uri.Update(url)
scheme := "https"
port := ":443"
if bytes.Equal(uri.Scheme(), wsString) {
scheme, port = "http", ":80"
}
uri.SetScheme(scheme)
addr := bytePool.Get().([]byte)
defer bytePool.Put(addr)
addr = append(addr[:0], uri.Host()...)
if n := bytes.LastIndexByte(addr, ':'); n == -1 {
addr = append(addr, port...)
}
var c net.Conn
if scheme == "http" {
c, err = net.Dial("tcp", b2s(addr))
} else {
c, err = tls.Dial("tcp", b2s(addr), cnf)
}
if err == nil {
conn, err = client(c, uri.String(), req)
if err != nil {
c.Close()
}
}
return conn, err
}
func makeRandKey(b []byte) []byte {
b = extendByteSlice(b, 16)
rand.Read(b[:16])
b = appendEncode(base64, b[:0], b[:16])
return b
}