mqtt 5.13.0 → 5.13.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mqtt.esm.js +1 -1
- package/dist/mqtt.js +1 -1
- package/dist/mqtt.min.js +1 -1
- package/package.json +4 -5
- package/src/bin/mqtt.ts +0 -39
- package/src/bin/pub.ts +0 -174
- package/src/bin/sub.ts +0 -146
- package/src/index.ts +0 -4
- package/src/lib/BufferedDuplex.ts +0 -115
- package/src/lib/KeepaliveManager.ts +0 -112
- package/src/lib/TypedEmitter.ts +0 -71
- package/src/lib/client.ts +0 -2428
- package/src/lib/connect/ali.ts +0 -128
- package/src/lib/connect/index.ts +0 -294
- package/src/lib/connect/socks.ts +0 -236
- package/src/lib/connect/tcp.ts +0 -29
- package/src/lib/connect/tls.ts +0 -71
- package/src/lib/connect/ws.ts +0 -307
- package/src/lib/connect/wx.ts +0 -143
- package/src/lib/default-message-id-provider.ts +0 -99
- package/src/lib/get-timer.ts +0 -40
- package/src/lib/handlers/ack.ts +0 -159
- package/src/lib/handlers/auth.ts +0 -44
- package/src/lib/handlers/connack.ts +0 -58
- package/src/lib/handlers/index.ts +0 -74
- package/src/lib/handlers/publish.ts +0 -183
- package/src/lib/handlers/pubrel.ts +0 -31
- package/src/lib/is-browser.ts +0 -46
- package/src/lib/shared.ts +0 -100
- package/src/lib/store.ts +0 -166
- package/src/lib/topic-alias-recv.ts +0 -48
- package/src/lib/topic-alias-send.ts +0 -96
- package/src/lib/unique-message-id-provider.ts +0 -64
- package/src/lib/validations.ts +0 -46
- package/src/mqtt.ts +0 -28
package/src/lib/connect/ali.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'buffer'
|
|
2
|
-
import { Transform } from 'readable-stream'
|
|
3
|
-
import { type StreamBuilder } from '../shared'
|
|
4
|
-
import { type IClientOptions } from '../client'
|
|
5
|
-
import type MqttClient from '../client'
|
|
6
|
-
import { BufferedDuplex } from '../BufferedDuplex'
|
|
7
|
-
|
|
8
|
-
let my: any
|
|
9
|
-
let proxy: Transform
|
|
10
|
-
let stream: BufferedDuplex
|
|
11
|
-
let isInitialized = false
|
|
12
|
-
|
|
13
|
-
function buildProxy() {
|
|
14
|
-
const _proxy = new Transform()
|
|
15
|
-
_proxy._write = (chunk, encoding, next) => {
|
|
16
|
-
my.sendSocketMessage({
|
|
17
|
-
data: chunk.buffer,
|
|
18
|
-
success() {
|
|
19
|
-
next()
|
|
20
|
-
},
|
|
21
|
-
fail() {
|
|
22
|
-
next(new Error())
|
|
23
|
-
},
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
_proxy._flush = (done) => {
|
|
27
|
-
my.closeSocket({
|
|
28
|
-
success() {
|
|
29
|
-
done()
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return _proxy
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function setDefaultOpts(opts: IClientOptions) {
|
|
38
|
-
if (!opts.hostname) {
|
|
39
|
-
opts.hostname = 'localhost'
|
|
40
|
-
}
|
|
41
|
-
if (!opts.path) {
|
|
42
|
-
opts.path = '/'
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (!opts.wsOptions) {
|
|
46
|
-
opts.wsOptions = {}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function buildUrl(opts: IClientOptions, client: MqttClient) {
|
|
51
|
-
const protocol = opts.protocol === 'alis' ? 'wss' : 'ws'
|
|
52
|
-
let url = `${protocol}://${opts.hostname}${opts.path}`
|
|
53
|
-
if (opts.port && opts.port !== 80 && opts.port !== 443) {
|
|
54
|
-
url = `${protocol}://${opts.hostname}:${opts.port}${opts.path}`
|
|
55
|
-
}
|
|
56
|
-
if (typeof opts.transformWsUrl === 'function') {
|
|
57
|
-
url = opts.transformWsUrl(url, opts, client)
|
|
58
|
-
}
|
|
59
|
-
return url
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function bindEventHandler() {
|
|
63
|
-
if (isInitialized) return
|
|
64
|
-
|
|
65
|
-
isInitialized = true
|
|
66
|
-
|
|
67
|
-
my.onSocketOpen(() => {
|
|
68
|
-
stream.socketReady()
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
my.onSocketMessage((res) => {
|
|
72
|
-
if (typeof res.data === 'string') {
|
|
73
|
-
const buffer = Buffer.from(res.data, 'base64')
|
|
74
|
-
proxy.push(buffer)
|
|
75
|
-
} else {
|
|
76
|
-
const reader = new FileReader()
|
|
77
|
-
reader.addEventListener('load', () => {
|
|
78
|
-
let data = reader.result
|
|
79
|
-
|
|
80
|
-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
|
|
81
|
-
else data = Buffer.from(data, 'utf8')
|
|
82
|
-
proxy.push(data)
|
|
83
|
-
})
|
|
84
|
-
reader.readAsArrayBuffer(res.data)
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
my.onSocketClose(() => {
|
|
89
|
-
stream.end()
|
|
90
|
-
stream.destroy()
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
my.onSocketError((err) => {
|
|
94
|
-
stream.destroy(err)
|
|
95
|
-
})
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const buildStream: StreamBuilder = (client, opts) => {
|
|
99
|
-
opts.hostname = opts.hostname || opts.host
|
|
100
|
-
|
|
101
|
-
if (!opts.hostname) {
|
|
102
|
-
throw new Error('Could not determine host. Specify host manually.')
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const websocketSubProtocol =
|
|
106
|
-
opts.protocolId === 'MQIsdp' && opts.protocolVersion === 3
|
|
107
|
-
? 'mqttv3.1'
|
|
108
|
-
: 'mqtt'
|
|
109
|
-
|
|
110
|
-
setDefaultOpts(opts)
|
|
111
|
-
|
|
112
|
-
const url = buildUrl(opts, client)
|
|
113
|
-
my = opts.my
|
|
114
|
-
// https://miniprogram.alipay.com/docs/miniprogram/mpdev/api_network_connectsocket
|
|
115
|
-
my.connectSocket({
|
|
116
|
-
url,
|
|
117
|
-
protocols: websocketSubProtocol,
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
proxy = buildProxy()
|
|
121
|
-
stream = new BufferedDuplex(opts, proxy, my)
|
|
122
|
-
|
|
123
|
-
bindEventHandler()
|
|
124
|
-
|
|
125
|
-
return stream
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export default buildStream
|
package/src/lib/connect/index.ts
DELETED
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
-
import _debug from 'debug'
|
|
3
|
-
import url from 'url'
|
|
4
|
-
import MqttClient, {
|
|
5
|
-
type IClientOptions,
|
|
6
|
-
type MqttClientEventCallbacks,
|
|
7
|
-
type MqttProtocol,
|
|
8
|
-
} from '../client'
|
|
9
|
-
import isBrowser from '../is-browser'
|
|
10
|
-
import { type StreamBuilder } from '../shared'
|
|
11
|
-
|
|
12
|
-
// Handling the process.nextTick is not a function error in react-native applications.
|
|
13
|
-
if (typeof process?.nextTick !== 'function') {
|
|
14
|
-
process.nextTick = setImmediate
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const debug = _debug('mqttjs')
|
|
18
|
-
|
|
19
|
-
let protocols: Record<string, StreamBuilder> = null
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Parse the auth attribute and merge username and password in the options object.
|
|
23
|
-
*
|
|
24
|
-
* @param {Object} [opts] option object
|
|
25
|
-
*/
|
|
26
|
-
function parseAuthOptions(opts: IClientOptions) {
|
|
27
|
-
let matches: RegExpMatchArray | null
|
|
28
|
-
if (opts.auth) {
|
|
29
|
-
matches = opts.auth.match(/^(.+):(.+)$/)
|
|
30
|
-
if (matches) {
|
|
31
|
-
opts.username = matches[1]
|
|
32
|
-
opts.password = matches[2]
|
|
33
|
-
} else {
|
|
34
|
-
opts.username = opts.auth
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* connect - connect to an MQTT broker.
|
|
41
|
-
*/
|
|
42
|
-
function connect(brokerUrl: string): MqttClient
|
|
43
|
-
function connect(opts: IClientOptions): MqttClient
|
|
44
|
-
function connect(brokerUrl: string, opts?: IClientOptions): MqttClient
|
|
45
|
-
function connect(
|
|
46
|
-
brokerUrl: string | IClientOptions,
|
|
47
|
-
opts?: IClientOptions,
|
|
48
|
-
): MqttClient {
|
|
49
|
-
debug('connecting to an MQTT broker...')
|
|
50
|
-
if (typeof brokerUrl === 'object' && !opts) {
|
|
51
|
-
opts = brokerUrl
|
|
52
|
-
brokerUrl = ''
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
opts = opts || {}
|
|
56
|
-
|
|
57
|
-
// try to parse the broker url
|
|
58
|
-
if (brokerUrl && typeof brokerUrl === 'string') {
|
|
59
|
-
// eslint-disable-next-line
|
|
60
|
-
const parsedUrl = url.parse(brokerUrl, true)
|
|
61
|
-
const parsedOptions: Partial<IClientOptions> = {}
|
|
62
|
-
|
|
63
|
-
if (parsedUrl.port != null) {
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
65
|
-
// @ts-ignore
|
|
66
|
-
parsedOptions.port = Number(parsedUrl.port)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
parsedOptions.host = parsedUrl.hostname
|
|
70
|
-
parsedOptions.query = parsedUrl.query as Record<string, string>
|
|
71
|
-
parsedOptions.auth = parsedUrl.auth
|
|
72
|
-
parsedOptions.protocol = parsedUrl.protocol as MqttProtocol
|
|
73
|
-
parsedOptions.path = parsedUrl.path
|
|
74
|
-
|
|
75
|
-
opts = { ...parsedOptions, ...opts }
|
|
76
|
-
|
|
77
|
-
// when parsing an url expect the protocol to be set
|
|
78
|
-
if (!opts.protocol) {
|
|
79
|
-
throw new Error('Missing protocol')
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
opts.protocol = opts.protocol.replace(/:$/, '') as MqttProtocol
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
opts.unixSocket = opts.unixSocket || opts.protocol?.includes('+unix')
|
|
86
|
-
|
|
87
|
-
if (opts.unixSocket) {
|
|
88
|
-
opts.protocol = opts.protocol.replace('+unix', '') as MqttProtocol
|
|
89
|
-
} else if (
|
|
90
|
-
!opts.protocol?.startsWith('ws') &&
|
|
91
|
-
!opts.protocol?.startsWith('wx')
|
|
92
|
-
) {
|
|
93
|
-
// consider path only with ws protocol or unix socket
|
|
94
|
-
// url.parse could return path (for example when url ends with a `/`)
|
|
95
|
-
// that could break the connection. See https://github.com/mqttjs/MQTT.js/pull/1874
|
|
96
|
-
delete opts.path
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// merge in the auth options if supplied
|
|
100
|
-
parseAuthOptions(opts)
|
|
101
|
-
|
|
102
|
-
// support clientId passed in the query string of the url
|
|
103
|
-
if (opts.query && typeof opts.query.clientId === 'string') {
|
|
104
|
-
opts.clientId = opts.query.clientId
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (isBrowser || opts.unixSocket) {
|
|
108
|
-
opts.socksProxy = undefined
|
|
109
|
-
} else if (
|
|
110
|
-
opts.socksProxy === undefined &&
|
|
111
|
-
typeof process !== 'undefined'
|
|
112
|
-
) {
|
|
113
|
-
opts.socksProxy = process.env['MQTTJS_SOCKS_PROXY']
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (opts.cert && opts.key) {
|
|
117
|
-
if (opts.protocol) {
|
|
118
|
-
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
119
|
-
switch (opts.protocol) {
|
|
120
|
-
case 'mqtt':
|
|
121
|
-
opts.protocol = 'mqtts'
|
|
122
|
-
break
|
|
123
|
-
case 'ws':
|
|
124
|
-
opts.protocol = 'wss'
|
|
125
|
-
break
|
|
126
|
-
case 'wx':
|
|
127
|
-
opts.protocol = 'wxs'
|
|
128
|
-
break
|
|
129
|
-
case 'ali':
|
|
130
|
-
opts.protocol = 'alis'
|
|
131
|
-
break
|
|
132
|
-
default:
|
|
133
|
-
throw new Error(
|
|
134
|
-
`Unknown protocol for secure connection: "${opts.protocol}"!`,
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
} else {
|
|
139
|
-
// A cert and key was provided, however no protocol was specified, so we will throw an error.
|
|
140
|
-
throw new Error('Missing secure protocol key')
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// only loads the protocols once
|
|
145
|
-
if (!protocols) {
|
|
146
|
-
protocols = {}
|
|
147
|
-
if (!isBrowser && !opts.forceNativeWebSocket) {
|
|
148
|
-
protocols.ws = require('./ws').streamBuilder
|
|
149
|
-
protocols.wss = require('./ws').streamBuilder
|
|
150
|
-
|
|
151
|
-
protocols.mqtt = require('./tcp').default
|
|
152
|
-
protocols.tcp = require('./tcp').default
|
|
153
|
-
protocols.ssl = require('./tls').default
|
|
154
|
-
protocols.tls = protocols.ssl
|
|
155
|
-
protocols.mqtts = require('./tls').default
|
|
156
|
-
} else {
|
|
157
|
-
protocols.ws = require('./ws').browserStreamBuilder
|
|
158
|
-
protocols.wss = require('./ws').browserStreamBuilder
|
|
159
|
-
|
|
160
|
-
protocols.wx = require('./wx').default
|
|
161
|
-
protocols.wxs = require('./wx').default
|
|
162
|
-
|
|
163
|
-
protocols.ali = require('./ali').default
|
|
164
|
-
protocols.alis = require('./ali').default
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (!protocols[opts.protocol]) {
|
|
169
|
-
const isSecure = ['mqtts', 'wss'].indexOf(opts.protocol) !== -1
|
|
170
|
-
// returns the first available protocol based on available protocols (that depends on environment)
|
|
171
|
-
// if no protocol is specified this will return mqtt on node and ws on browser
|
|
172
|
-
// if secure it will return mqtts on node and wss on browser
|
|
173
|
-
opts.protocol = [
|
|
174
|
-
'mqtt',
|
|
175
|
-
'mqtts',
|
|
176
|
-
'ws',
|
|
177
|
-
'wss',
|
|
178
|
-
'wx',
|
|
179
|
-
'wxs',
|
|
180
|
-
'ali',
|
|
181
|
-
'alis',
|
|
182
|
-
].filter((key, index) => {
|
|
183
|
-
if (isSecure && index % 2 === 0) {
|
|
184
|
-
// Skip insecure protocols when requesting a secure one.
|
|
185
|
-
return false
|
|
186
|
-
}
|
|
187
|
-
return typeof protocols[key] === 'function'
|
|
188
|
-
})[0] as MqttProtocol
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (opts.clean === false && !opts.clientId) {
|
|
192
|
-
throw new Error('Missing clientId for unclean clients')
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (opts.protocol) {
|
|
196
|
-
opts.defaultProtocol = opts.protocol
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function wrapper(client: MqttClient) {
|
|
200
|
-
if (opts.servers) {
|
|
201
|
-
if (
|
|
202
|
-
!client._reconnectCount ||
|
|
203
|
-
client._reconnectCount === opts.servers.length
|
|
204
|
-
) {
|
|
205
|
-
client._reconnectCount = 0
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
opts.host = opts.servers[client._reconnectCount].host
|
|
209
|
-
opts.port = opts.servers[client._reconnectCount].port
|
|
210
|
-
opts.protocol = !opts.servers[client._reconnectCount].protocol
|
|
211
|
-
? opts.defaultProtocol
|
|
212
|
-
: opts.servers[client._reconnectCount].protocol
|
|
213
|
-
opts.hostname = opts.host
|
|
214
|
-
|
|
215
|
-
client._reconnectCount++
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
debug('calling streambuilder for', opts.protocol)
|
|
219
|
-
return protocols[opts.protocol](client, opts)
|
|
220
|
-
}
|
|
221
|
-
const client = new MqttClient(wrapper, opts)
|
|
222
|
-
client.on('error', () => {
|
|
223
|
-
/* Automatically set up client error handling */
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
return client
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function connectAsync(brokerUrl: string): Promise<MqttClient>
|
|
230
|
-
function connectAsync(opts: IClientOptions): Promise<MqttClient>
|
|
231
|
-
function connectAsync(
|
|
232
|
-
brokerUrl: string,
|
|
233
|
-
opts?: IClientOptions,
|
|
234
|
-
): Promise<MqttClient>
|
|
235
|
-
function connectAsync(
|
|
236
|
-
brokerUrl: string,
|
|
237
|
-
opts: IClientOptions,
|
|
238
|
-
allowRetries: boolean,
|
|
239
|
-
): Promise<MqttClient>
|
|
240
|
-
function connectAsync(
|
|
241
|
-
brokerUrl: string | IClientOptions,
|
|
242
|
-
opts?: IClientOptions,
|
|
243
|
-
allowRetries = true,
|
|
244
|
-
): Promise<MqttClient> {
|
|
245
|
-
return new Promise((resolve, reject) => {
|
|
246
|
-
const client = connect(brokerUrl as string, opts)
|
|
247
|
-
|
|
248
|
-
const promiseResolutionListeners: Partial<MqttClientEventCallbacks> = {
|
|
249
|
-
connect: (connack) => {
|
|
250
|
-
removePromiseResolutionListeners()
|
|
251
|
-
resolve(client) // Resolve on connect
|
|
252
|
-
},
|
|
253
|
-
end: () => {
|
|
254
|
-
removePromiseResolutionListeners()
|
|
255
|
-
resolve(client) // Resolve on end
|
|
256
|
-
},
|
|
257
|
-
error: (err) => {
|
|
258
|
-
removePromiseResolutionListeners()
|
|
259
|
-
client.end()
|
|
260
|
-
reject(err) // Reject on error
|
|
261
|
-
},
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// If retries are not allowed, reject on close
|
|
265
|
-
if (allowRetries === false) {
|
|
266
|
-
promiseResolutionListeners.close = () => {
|
|
267
|
-
promiseResolutionListeners.error(
|
|
268
|
-
new Error("Couldn't connect to server"),
|
|
269
|
-
)
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// Remove listeners added to client by this promise
|
|
274
|
-
function removePromiseResolutionListeners() {
|
|
275
|
-
Object.keys(promiseResolutionListeners).forEach((eventName) => {
|
|
276
|
-
client.off(
|
|
277
|
-
eventName as keyof MqttClientEventCallbacks,
|
|
278
|
-
promiseResolutionListeners[eventName],
|
|
279
|
-
)
|
|
280
|
-
})
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Add listeners to client
|
|
284
|
-
Object.keys(promiseResolutionListeners).forEach((eventName) => {
|
|
285
|
-
client.on(
|
|
286
|
-
eventName as keyof MqttClientEventCallbacks,
|
|
287
|
-
promiseResolutionListeners[eventName],
|
|
288
|
-
)
|
|
289
|
-
})
|
|
290
|
-
})
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
export default connect
|
|
294
|
-
export { connectAsync }
|
package/src/lib/connect/socks.ts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import _debug from 'debug'
|
|
2
|
-
import { Duplex } from 'stream'
|
|
3
|
-
import { SocksClient, type SocksProxy } from 'socks'
|
|
4
|
-
import * as dns from 'dns'
|
|
5
|
-
import { type SocksProxyType } from 'socks/typings/common/constants'
|
|
6
|
-
import { type IStream } from '../shared'
|
|
7
|
-
import { promisify } from 'util'
|
|
8
|
-
import { type Socket } from 'net'
|
|
9
|
-
import assert from 'assert'
|
|
10
|
-
|
|
11
|
-
const debug = _debug('mqttjs:socks')
|
|
12
|
-
|
|
13
|
-
export interface SocksConnectionOptions {
|
|
14
|
-
timeout?: number
|
|
15
|
-
lookup?: (hostname: string) => Promise<{ address: string }>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class ProxyStream extends Duplex {
|
|
19
|
-
private _flowing = false
|
|
20
|
-
|
|
21
|
-
private _socket?: Socket
|
|
22
|
-
|
|
23
|
-
constructor() {
|
|
24
|
-
super({ autoDestroy: false })
|
|
25
|
-
|
|
26
|
-
this.cork()
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
_start(socket: Socket): void {
|
|
30
|
-
debug('proxy stream started')
|
|
31
|
-
|
|
32
|
-
assert(!this._socket)
|
|
33
|
-
|
|
34
|
-
if (this.destroyed) {
|
|
35
|
-
socket.destroy(this.errored)
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
this._socket = socket
|
|
40
|
-
|
|
41
|
-
if (!this._flowing) socket.pause()
|
|
42
|
-
|
|
43
|
-
socket.on('data', this._onData)
|
|
44
|
-
socket.on('end', this._onEnd)
|
|
45
|
-
socket.on('error', this._onError)
|
|
46
|
-
socket.on('close', this._onClose)
|
|
47
|
-
|
|
48
|
-
socket.emit('connect')
|
|
49
|
-
|
|
50
|
-
this.uncork()
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
_write(
|
|
54
|
-
chunk: any,
|
|
55
|
-
encoding: BufferEncoding,
|
|
56
|
-
callback: (error?: Error | null) => void,
|
|
57
|
-
): void {
|
|
58
|
-
assert(this._socket)
|
|
59
|
-
|
|
60
|
-
this._socket.write(chunk, callback)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
_read(size: number): void {
|
|
64
|
-
this._flowing = true
|
|
65
|
-
|
|
66
|
-
this._socket?.resume?.()
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
_destroy(
|
|
70
|
-
error: Error | null,
|
|
71
|
-
callback: (error?: Error | null) => void,
|
|
72
|
-
): void {
|
|
73
|
-
this._socket?.destroy?.(error)
|
|
74
|
-
|
|
75
|
-
callback(error)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private _onData = (chunk: any): void => {
|
|
79
|
-
assert(this._socket)
|
|
80
|
-
|
|
81
|
-
this._flowing = this.push(chunk)
|
|
82
|
-
if (!this._flowing) this._socket.pause()
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private _onEnd = (): void => {
|
|
86
|
-
debug('proxy stream received EOF')
|
|
87
|
-
|
|
88
|
-
this.push(null)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
private _onClose = (): void => {
|
|
92
|
-
debug('proxy stream closed')
|
|
93
|
-
|
|
94
|
-
this.destroy()
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private _onError = (err: any): void => {
|
|
98
|
-
debug('proxy stream died with error %s', err)
|
|
99
|
-
|
|
100
|
-
this.destroy(err)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function fatal<T>(e: T): T {
|
|
105
|
-
try {
|
|
106
|
-
if ((e as any).code === undefined) (e as any).code = 'SOCKS'
|
|
107
|
-
return e
|
|
108
|
-
} catch {
|
|
109
|
-
return e
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function typeFromProtocol(
|
|
114
|
-
proto: string,
|
|
115
|
-
): [SocksProxyType | undefined, boolean] {
|
|
116
|
-
switch (proto) {
|
|
117
|
-
case 'socks5h:':
|
|
118
|
-
return [5, true]
|
|
119
|
-
|
|
120
|
-
case 'socks4a:':
|
|
121
|
-
return [4, true]
|
|
122
|
-
|
|
123
|
-
case 'socks5:':
|
|
124
|
-
return [5, false]
|
|
125
|
-
|
|
126
|
-
case 'socks4:':
|
|
127
|
-
return [4, false]
|
|
128
|
-
|
|
129
|
-
default:
|
|
130
|
-
return [undefined, false]
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function parseSocksUrl(url: string): [SocksProxy, boolean] {
|
|
135
|
-
const parsedUrl = new URL(url)
|
|
136
|
-
|
|
137
|
-
if (parsedUrl.pathname || parsedUrl.hash || parsedUrl.search) {
|
|
138
|
-
throw fatal(new Error('bad SOCKS URL'))
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const [type, resolveThroughProxy] = typeFromProtocol(parsedUrl.protocol)
|
|
142
|
-
if (!type) {
|
|
143
|
-
throw fatal(new Error('bad SOCKS URL: invalid protocol'))
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const port = parseInt(parsedUrl.port, 10)
|
|
147
|
-
if (Number.isNaN(port)) {
|
|
148
|
-
throw fatal(new Error('bad SOCKS URL: invalid port'))
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const proxy: SocksProxy = {
|
|
152
|
-
host: parsedUrl.hostname,
|
|
153
|
-
port,
|
|
154
|
-
type,
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return [proxy, resolveThroughProxy]
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async function connectSocks(
|
|
161
|
-
destinationHost: string,
|
|
162
|
-
destinationPort: number,
|
|
163
|
-
socksUrl: string,
|
|
164
|
-
stream: ProxyStream,
|
|
165
|
-
options: SocksConnectionOptions = {},
|
|
166
|
-
): Promise<void> {
|
|
167
|
-
const lookup = options.lookup ?? promisify(dns.lookup)
|
|
168
|
-
|
|
169
|
-
const [proxy, resolveThroughProxy] = parseSocksUrl(socksUrl)
|
|
170
|
-
|
|
171
|
-
if (!resolveThroughProxy) {
|
|
172
|
-
debug('resolving %s locally', destinationHost)
|
|
173
|
-
|
|
174
|
-
destinationHost = (
|
|
175
|
-
await lookup(destinationHost, {
|
|
176
|
-
family: proxy.type === 4 ? 4 : 0,
|
|
177
|
-
})
|
|
178
|
-
).address
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
debug(
|
|
182
|
-
'establishing SOCKS%d connection to %s:%d via %s:%d',
|
|
183
|
-
proxy.type,
|
|
184
|
-
destinationHost,
|
|
185
|
-
destinationPort,
|
|
186
|
-
proxy.host,
|
|
187
|
-
proxy.port,
|
|
188
|
-
)
|
|
189
|
-
|
|
190
|
-
const socksClient = new SocksClient({
|
|
191
|
-
command: 'connect',
|
|
192
|
-
destination: {
|
|
193
|
-
host: destinationHost,
|
|
194
|
-
port: destinationPort,
|
|
195
|
-
},
|
|
196
|
-
proxy: { ...proxy },
|
|
197
|
-
timeout: options.timeout,
|
|
198
|
-
})
|
|
199
|
-
socksClient.connect()
|
|
200
|
-
|
|
201
|
-
socksClient.on('established', ({ socket }) => stream._start(socket))
|
|
202
|
-
|
|
203
|
-
socksClient.on('error', (e) => {
|
|
204
|
-
debug('SOCKS failed: %s', e)
|
|
205
|
-
stream.destroy(fatal(e))
|
|
206
|
-
})
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export default function openSocks(
|
|
210
|
-
destinationHost: string,
|
|
211
|
-
destinationPort: number,
|
|
212
|
-
socksUrl: string,
|
|
213
|
-
options?: SocksConnectionOptions,
|
|
214
|
-
): IStream {
|
|
215
|
-
debug(
|
|
216
|
-
'SOCKS connection to %s:%d via %s',
|
|
217
|
-
destinationHost,
|
|
218
|
-
destinationPort,
|
|
219
|
-
socksUrl,
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
const stream = new ProxyStream()
|
|
223
|
-
|
|
224
|
-
connectSocks(
|
|
225
|
-
destinationHost,
|
|
226
|
-
destinationPort,
|
|
227
|
-
socksUrl,
|
|
228
|
-
stream,
|
|
229
|
-
options,
|
|
230
|
-
).catch((e) => {
|
|
231
|
-
debug('SOCKS failed: %s', e)
|
|
232
|
-
stream.destroy(e)
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
return stream
|
|
236
|
-
}
|
package/src/lib/connect/tcp.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { type StreamBuilder } from '../shared'
|
|
2
|
-
|
|
3
|
-
import net from 'net'
|
|
4
|
-
import _debug from 'debug'
|
|
5
|
-
import openSocks from './socks'
|
|
6
|
-
|
|
7
|
-
const debug = _debug('mqttjs:tcp')
|
|
8
|
-
/*
|
|
9
|
-
variables port and host can be removed since
|
|
10
|
-
you have all required information in opts object
|
|
11
|
-
*/
|
|
12
|
-
const buildStream: StreamBuilder = (client, opts) => {
|
|
13
|
-
opts.port = opts.port || 1883
|
|
14
|
-
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
15
|
-
|
|
16
|
-
if (opts.socksProxy) {
|
|
17
|
-
return openSocks(opts.hostname, opts.port, opts.socksProxy, {
|
|
18
|
-
timeout: opts.socksTimeout,
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const { port, path } = opts
|
|
23
|
-
const host = opts.hostname
|
|
24
|
-
|
|
25
|
-
debug('port %d and host %s', port, host)
|
|
26
|
-
return net.createConnection({ port, host, path })
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export default buildStream
|