forked from antlinker/libmqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_connector.go
180 lines (145 loc) · 4.27 KB
/
client_connector.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
/*
* Copyright Go-IIoT (https://github.com/goiiot)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package libmqtt
import (
"bytes"
"context"
"crypto/tls"
"errors"
"net"
"net/http"
"strings"
"time"
"nhooyr.io/websocket"
)
type Connector func(ctx context.Context, address string, timeout time.Duration, tlsConfig *tls.Config) (net.Conn, error)
func WithTCPConnector(handshakeTimeout time.Duration) Option {
return func(c *AsyncClient, options *connectOptions) error {
options.newConnection = func(ctx context.Context, address string, timeout time.Duration, tlsConfig *tls.Config) (conn net.Conn, e error) {
return tcpConnect(ctx, address, timeout, handshakeTimeout, tlsConfig)
}
return nil
}
}
func WithWebSocketConnector(handshakeTimeout time.Duration, headers http.Header) Option {
return func(c *AsyncClient, options *connectOptions) error {
options.newConnection = func(ctx context.Context, address string, timeout time.Duration, tlsConfig *tls.Config) (conn net.Conn, e error) {
return websocketConnect(ctx, address, timeout, handshakeTimeout, headers, tlsConfig)
}
return nil
}
}
func WithCustomConnector(connector Connector) Option {
return func(c *AsyncClient, options *connectOptions) error {
options.newConnection = connector
return nil
}
}
type tlsTimeoutError struct{}
func (tlsTimeoutError) Error() string { return "tls: timed out" }
func (tlsTimeoutError) Timeout() bool { return true }
func (tlsTimeoutError) Temporary() bool { return true }
func tcpConnect(ctx context.Context, address string, timeout, handshakeTimeout time.Duration, tlsConfig *tls.Config) (net.Conn, error) {
dialer := &net.Dialer{
Timeout: timeout,
}
conn, err := dialer.DialContext(ctx, "tcp", address)
if err != nil {
return nil, err
}
if tlsConfig != nil {
// code copied from golang standard library tls.DialWithDialer
errChannel := make(chan error, 2)
if handshakeTimeout != 0 {
time.AfterFunc(handshakeTimeout, func() {
errChannel <- tlsTimeoutError{}
})
}
colonPos := strings.LastIndex(address, ":")
if colonPos == -1 {
colonPos = len(address)
}
hostname := address[:colonPos]
// If no ServerName is set, infer the ServerName
// from the hostname we're connecting to.
if tlsConfig.ServerName == "" {
// Make a copy to avoid polluting argument or default.
c := tlsConfig.Clone()
c.ServerName = hostname
tlsConfig = c
}
tlsConn := tls.Client(conn, tlsConfig)
if timeout == 0 {
err = tlsConn.Handshake()
} else {
go func() {
errChannel <- tlsConn.Handshake()
}()
err = <-errChannel
}
if err != nil {
_ = conn.Close()
return nil, err
}
conn = tlsConn
}
return conn, err
}
var errNotSupported = errors.New("operation not supported")
type wsConn struct {
ctx context.Context
conn *websocket.Conn
localAddr net.Addr
remoteAddr net.Addr
readBuf *bytes.Buffer
}
func (c *wsConn) Read(b []byte) (int, error) {
_, data, err := c.conn.Read(c.ctx)
if err != nil {
return 0, err
}
c.readBuf.Write(data)
return c.readBuf.Read(b)
}
func (c *wsConn) Write(b []byte) (int, error) {
err := c.conn.Write(c.ctx, websocket.MessageBinary, b)
if err != nil {
return 0, err
}
return len(b), nil
}
func (c *wsConn) Close() error {
return c.conn.Close(websocket.StatusGoingAway, "")
}
func (c *wsConn) LocalAddr() net.Addr {
return c.localAddr
}
func (c *wsConn) RemoteAddr() net.Addr {
return c.remoteAddr
}
func (c *wsConn) SetDeadline(t time.Time) error {
return errNotSupported
}
func (c *wsConn) SetReadDeadline(t time.Time) error {
return errNotSupported
}
func (c *wsConn) SetWriteDeadline(t time.Time) error {
return errNotSupported
}
func quicConnector(address string, timeout, handshakeTimeout time.Duration, tlsConfig *tls.Config) (net.Conn, error) {
// TODO: TBD
return nil, nil
}