mqtt 5.10.4 → 5.11.0
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/README.md +4 -0
- package/build/lib/client.d.ts +2 -0
- package/build/lib/client.js.map +1 -1
- package/build/lib/connect/index.js +7 -0
- package/build/lib/connect/index.js.map +1 -1
- package/build/lib/connect/socks.d.ts +8 -0
- package/build/lib/connect/socks.js +183 -0
- package/build/lib/connect/socks.js.map +1 -0
- package/build/lib/connect/tcp.js +6 -0
- package/build/lib/connect/tcp.js.map +1 -1
- package/build/lib/connect/tls.js +20 -1
- package/build/lib/connect/tls.js.map +1 -1
- package/build/lib/validations.d.ts +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/dist/mqtt.esm.js +5 -5
- package/dist/mqtt.js +45 -4
- package/dist/mqtt.min.js +5 -5
- package/package.json +4 -3
- package/src/lib/client.ts +4 -0
- package/src/lib/connect/index.ts +9 -0
- package/src/lib/connect/socks.ts +236 -0
- package/src/lib/connect/tcp.ts +7 -0
- package/src/lib/connect/tls.ts +19 -2
- package/src/lib/validations.ts +1 -1
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mqtt",
|
|
3
3
|
"description": "A library for the MQTT protocol",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.11.0",
|
|
5
5
|
"contributors": [
|
|
6
6
|
"Adam Rudd <adamvrr@gmail.com>",
|
|
7
7
|
"Matteo Collina <matteo.collina@gmail.com> (https://github.com/mcollina)",
|
|
@@ -119,11 +119,12 @@
|
|
|
119
119
|
"help-me": "^5.0.0",
|
|
120
120
|
"lru-cache": "^10.4.3",
|
|
121
121
|
"minimist": "^1.2.8",
|
|
122
|
-
"mqtt-packet": "^9.0.
|
|
122
|
+
"mqtt-packet": "^9.0.2",
|
|
123
123
|
"number-allocator": "^1.0.14",
|
|
124
124
|
"readable-stream": "^4.7.0",
|
|
125
125
|
"reinterval": "^1.1.0",
|
|
126
126
|
"rfdc": "^1.4.1",
|
|
127
|
+
"socks": "^2.8.3",
|
|
127
128
|
"split2": "^4.2.0",
|
|
128
129
|
"worker-timers": "^7.1.8",
|
|
129
130
|
"ws": "^8.18.0"
|
|
@@ -144,7 +145,7 @@
|
|
|
144
145
|
"chokidar": "^3.6.0",
|
|
145
146
|
"conventional-changelog-cli": "^4.1.0",
|
|
146
147
|
"end-of-stream": "^1.4.4",
|
|
147
|
-
"esbuild": "^0.
|
|
148
|
+
"esbuild": "^0.25.0",
|
|
148
149
|
"esbuild-plugin-polyfill-node": "^0.3.0",
|
|
149
150
|
"esbuild-register": "^3.6.0",
|
|
150
151
|
"eslint": "^8.57.1",
|
package/src/lib/client.ts
CHANGED
|
@@ -140,6 +140,10 @@ export interface IClientOptions extends ISecureClientOptions {
|
|
|
140
140
|
query?: Record<string, string>
|
|
141
141
|
/** Auth string in the format <username>:<password> */
|
|
142
142
|
auth?: string
|
|
143
|
+
/** Optional SOCKS proxy to use for TCP / TLS connections , i.e. socks5://localhost:1333, socks4://localhost:1333, socks5h://localhost:1333 . Default is socks5h. */
|
|
144
|
+
socksProxy?: string
|
|
145
|
+
/** Timeout for establishing a socks connection */
|
|
146
|
+
socksTimeout?: number
|
|
143
147
|
/** Custom ack handler */
|
|
144
148
|
customHandleAcks?: AckHandler
|
|
145
149
|
/** Broker port */
|
package/src/lib/connect/index.ts
CHANGED
|
@@ -104,6 +104,15 @@ function connect(
|
|
|
104
104
|
opts.clientId = opts.query.clientId
|
|
105
105
|
}
|
|
106
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
|
+
|
|
107
116
|
if (opts.cert && opts.key) {
|
|
108
117
|
if (opts.protocol) {
|
|
109
118
|
if (['mqtts', 'wss', 'wxs', 'alis'].indexOf(opts.protocol) === -1) {
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import _debug from 'debug'
|
|
2
|
+
import { Duplex } from 'stream'
|
|
3
|
+
import { SocksClient, SocksProxy } from 'socks'
|
|
4
|
+
import * as dns from 'dns'
|
|
5
|
+
import { SocksProxyType } from 'socks/typings/common/constants'
|
|
6
|
+
import { IStream } from '../shared'
|
|
7
|
+
import { promisify } from 'util'
|
|
8
|
+
import { 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
CHANGED
|
@@ -2,6 +2,7 @@ import { StreamBuilder } from '../shared'
|
|
|
2
2
|
|
|
3
3
|
import net from 'net'
|
|
4
4
|
import _debug from 'debug'
|
|
5
|
+
import openSocks from './socks'
|
|
5
6
|
|
|
6
7
|
const debug = _debug('mqttjs:tcp')
|
|
7
8
|
/*
|
|
@@ -12,6 +13,12 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
12
13
|
opts.port = opts.port || 1883
|
|
13
14
|
opts.hostname = opts.hostname || opts.host || 'localhost'
|
|
14
15
|
|
|
16
|
+
if (opts.socksProxy) {
|
|
17
|
+
return openSocks(opts.hostname, opts.port, opts.socksProxy, {
|
|
18
|
+
timeout: opts.socksTimeout,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
15
22
|
const { port, path } = opts
|
|
16
23
|
const host = opts.hostname
|
|
17
24
|
|
package/src/lib/connect/tls.ts
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
|
-
import tls from 'tls'
|
|
1
|
+
import tls, { TLSSocket } from 'tls'
|
|
2
2
|
import net from 'net'
|
|
3
3
|
import _debug from 'debug'
|
|
4
4
|
import { StreamBuilder } from '../shared'
|
|
5
|
+
import { IClientOptions } from '../client'
|
|
6
|
+
import openSocks from './socks'
|
|
5
7
|
|
|
6
8
|
const debug = _debug('mqttjs:tls')
|
|
7
9
|
|
|
10
|
+
function connect(opts: IClientOptions): TLSSocket {
|
|
11
|
+
const { host, port, socksProxy, ...rest } = opts
|
|
12
|
+
|
|
13
|
+
return tls.connect(
|
|
14
|
+
socksProxy
|
|
15
|
+
? {
|
|
16
|
+
...rest,
|
|
17
|
+
socket: openSocks(host, port, socksProxy, {
|
|
18
|
+
timeout: opts.socksTimeout,
|
|
19
|
+
}),
|
|
20
|
+
}
|
|
21
|
+
: opts,
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
8
25
|
const buildStream: StreamBuilder = (client, opts) => {
|
|
9
26
|
opts.port = opts.port || 8883
|
|
10
27
|
opts.host = opts.hostname || opts.host || 'localhost'
|
|
@@ -24,7 +41,7 @@ const buildStream: StreamBuilder = (client, opts) => {
|
|
|
24
41
|
opts.rejectUnauthorized,
|
|
25
42
|
)
|
|
26
43
|
|
|
27
|
-
const connection =
|
|
44
|
+
const connection = connect(opts)
|
|
28
45
|
connection.on('secureConnect', () => {
|
|
29
46
|
if (opts.rejectUnauthorized && !connection.authorized) {
|
|
30
47
|
connection.emit('error', new Error('TLS not authorized'))
|
package/src/lib/validations.ts
CHANGED
|
@@ -33,7 +33,7 @@ export function validateTopic(topic: string): boolean {
|
|
|
33
33
|
* @param {Array} topics - Array of topics
|
|
34
34
|
* @returns {String} If the topics is valid, returns null. Otherwise, returns the invalid one
|
|
35
35
|
*/
|
|
36
|
-
export function validateTopics(topics: string[]): string {
|
|
36
|
+
export function validateTopics(topics: string[]): string | null {
|
|
37
37
|
if (topics.length === 0) {
|
|
38
38
|
return 'empty_topic_list'
|
|
39
39
|
}
|