ddan-js 2.6.41 → 2.6.42
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/LICENSE +7 -7
- package/README.md +10 -10
- package/bin/ddan-js.esm.js +1 -1
- package/bin/ddan-js.js +1 -1
- package/bin/lib/modules/node/socks5.js +18 -67
- package/bin/lib/modules/rule/async-validator/rule/url.js +11 -11
- package/bin/types/modules/node/socks5.d.ts +1 -3
- package/bin/types/modules/rule/async-validator/index.d.ts +0 -6
- package/package.json +1 -1
|
@@ -4,7 +4,6 @@ exports.Socks5 = void 0;
|
|
|
4
4
|
const net_1 = require("net");
|
|
5
5
|
const socks_1 = require("socks");
|
|
6
6
|
const uuid_1 = require("../crypto/uuid");
|
|
7
|
-
const mapping_1 = require("../../class/mapping");
|
|
8
7
|
const index_1 = require("../qs/index");
|
|
9
8
|
const event_1 = require("../../class/event");
|
|
10
9
|
const pipeline_1 = require("../hook/modules/pipeline");
|
|
@@ -16,7 +15,6 @@ class Socks5 {
|
|
|
16
15
|
server = null;
|
|
17
16
|
clientSockets = new Set();
|
|
18
17
|
allowedDomains;
|
|
19
|
-
connectionPool;
|
|
20
18
|
systemProxy = null;
|
|
21
19
|
useSystemProxy = false;
|
|
22
20
|
__debug = false;
|
|
@@ -30,7 +28,6 @@ class Socks5 {
|
|
|
30
28
|
this.allowedDomains = new Set(allowedDomains);
|
|
31
29
|
this.__debug = debug;
|
|
32
30
|
this.__uuid = (0, uuid_1.default)();
|
|
33
|
-
this.connectionPool = new mapping_1.default();
|
|
34
31
|
this.__pipeline = new pipeline_1.default(1);
|
|
35
32
|
}
|
|
36
33
|
get id() {
|
|
@@ -64,7 +61,7 @@ class Socks5 {
|
|
|
64
61
|
}
|
|
65
62
|
const port = await this.findAvailablePort(startPort);
|
|
66
63
|
if (!port)
|
|
67
|
-
return
|
|
64
|
+
return 0;
|
|
68
65
|
this.__port = port;
|
|
69
66
|
this.server = net_1.default.createServer((socket) => this.handleSocksConnection(socket));
|
|
70
67
|
this.server.listen(port, () => {
|
|
@@ -118,7 +115,14 @@ class Socks5 {
|
|
|
118
115
|
return 0;
|
|
119
116
|
}
|
|
120
117
|
async handleSocksConnection(clientSocket) {
|
|
121
|
-
|
|
118
|
+
const id = (0, uuid_1.default)();
|
|
119
|
+
this.__pipeline.push(async () => {
|
|
120
|
+
this.__debug &&
|
|
121
|
+
console.info(`[socks] pipeline ${clientSocket.remoteAddress}:${clientSocket.remotePort} ${id}`);
|
|
122
|
+
await this._handleSocksConnection(clientSocket);
|
|
123
|
+
}, () => {
|
|
124
|
+
console.info(`[socks] pipeline ${id} end`);
|
|
125
|
+
});
|
|
122
126
|
}
|
|
123
127
|
async _handleSocksConnection(clientSocket) {
|
|
124
128
|
if (!clientSocket)
|
|
@@ -130,25 +134,23 @@ class Socks5 {
|
|
|
130
134
|
});
|
|
131
135
|
let addrport = '';
|
|
132
136
|
try {
|
|
133
|
-
this.connectionPool.clean(100, (kv) => kv?.destroy());
|
|
134
137
|
await this.performHandshake(clientSocket);
|
|
135
138
|
const destination = await this.parseClientRequest(clientSocket);
|
|
136
139
|
addrport = `${destination.addr}:${destination.port}`;
|
|
137
|
-
const clienAddrport = `${clientSocket.localAddress}:${clientSocket.localPort}`;
|
|
138
140
|
if (this.isAllowedDomain(destination.addr)) {
|
|
139
141
|
// 走上游代理
|
|
140
142
|
const upstreamSocket = await this.connectToUpstreamProxy(destination);
|
|
141
|
-
this.__debug && console.info(`[socks] handle connection upstream`, addrport
|
|
143
|
+
this.__debug && console.info(`[socks] handle connection upstream`, addrport);
|
|
142
144
|
this.setupDataForwarding(clientSocket, upstreamSocket);
|
|
143
145
|
}
|
|
144
146
|
else if (this.useSystemProxy && this.systemProxy) {
|
|
145
147
|
// 走系统代理
|
|
146
148
|
const systemSocket = await this.connectToSystemProxy(destination);
|
|
147
|
-
this.__debug && console.info(`[socks] handle connection system`, addrport
|
|
149
|
+
this.__debug && console.info(`[socks] handle connection system`, addrport);
|
|
148
150
|
this.setupDataForwarding(clientSocket, systemSocket);
|
|
149
151
|
}
|
|
150
152
|
else {
|
|
151
|
-
this.__debug && console.info(`[socks] handle connection local`, addrport
|
|
153
|
+
this.__debug && console.info(`[socks] handle connection local`, addrport);
|
|
152
154
|
// 本地连接
|
|
153
155
|
const localSocket = await this.connectToLocal(destination);
|
|
154
156
|
this.setupDataForwarding(clientSocket, localSocket);
|
|
@@ -216,22 +218,8 @@ class Socks5 {
|
|
|
216
218
|
});
|
|
217
219
|
});
|
|
218
220
|
}
|
|
219
|
-
getPoolConnection(key) {
|
|
220
|
-
if (!key)
|
|
221
|
-
return undefined;
|
|
222
|
-
const connection = this.connectionPool.get(key);
|
|
223
|
-
// 如果已有连接在池中,复用
|
|
224
|
-
if (connection && !connection.destroyed) {
|
|
225
|
-
return connection;
|
|
226
|
-
}
|
|
227
|
-
return undefined;
|
|
228
|
-
}
|
|
229
221
|
// 上游代理连接
|
|
230
222
|
async connectToUpstreamProxy(destination) {
|
|
231
|
-
const destKey = `${destination.addr}:${destination.port}`;
|
|
232
|
-
const cacheConnection = this.getPoolConnection(destKey);
|
|
233
|
-
if (cacheConnection)
|
|
234
|
-
return cacheConnection;
|
|
235
223
|
const options = {
|
|
236
224
|
proxy: { ...this.upstreamProxy, type: 5 },
|
|
237
225
|
command: 'connect',
|
|
@@ -241,15 +229,7 @@ class Socks5 {
|
|
|
241
229
|
},
|
|
242
230
|
};
|
|
243
231
|
try {
|
|
244
|
-
|
|
245
|
-
// this.connectionPool[destKey] = upstreamSocket
|
|
246
|
-
this.connectionPool.set(destKey, upstreamSocket);
|
|
247
|
-
upstreamSocket.on('close', () => {
|
|
248
|
-
// delete this.connectionPool[destKey]
|
|
249
|
-
this.__debug && console.info('[socks] connection pool delete', destKey);
|
|
250
|
-
this.connectionPool.delete(destKey);
|
|
251
|
-
});
|
|
252
|
-
return upstreamSocket;
|
|
232
|
+
return await this.connectToProxy(options);
|
|
253
233
|
}
|
|
254
234
|
catch (err) {
|
|
255
235
|
throw new Error('Failed to connect to upstream proxy: ' + err);
|
|
@@ -257,10 +237,6 @@ class Socks5 {
|
|
|
257
237
|
}
|
|
258
238
|
// 系统代理连接
|
|
259
239
|
async connectToSystemProxy(destination) {
|
|
260
|
-
const destKey = `${destination.addr}:${destination.port}`;
|
|
261
|
-
const cacheConnection = this.getPoolConnection(destKey);
|
|
262
|
-
if (cacheConnection)
|
|
263
|
-
return cacheConnection;
|
|
264
240
|
const options = {
|
|
265
241
|
proxy: { ...this.systemProxy, type: 5 },
|
|
266
242
|
command: 'connect',
|
|
@@ -270,36 +246,22 @@ class Socks5 {
|
|
|
270
246
|
},
|
|
271
247
|
};
|
|
272
248
|
try {
|
|
273
|
-
|
|
274
|
-
this.connectionPool.set(destKey, systemSocket);
|
|
275
|
-
systemSocket.on('close', () => {
|
|
276
|
-
// delete this.connectionPool[destKey]
|
|
277
|
-
this.__debug && console.info('[socks] connection pool delete', destKey);
|
|
278
|
-
this.connectionPool.delete(destKey);
|
|
279
|
-
});
|
|
280
|
-
return systemSocket;
|
|
249
|
+
return await this.connectToProxy(options);
|
|
281
250
|
}
|
|
282
251
|
catch (err) {
|
|
283
252
|
throw new Error('Failed to connect to system proxy: ' + err);
|
|
284
253
|
}
|
|
285
254
|
}
|
|
255
|
+
async connectToProxy(options) {
|
|
256
|
+
const { socket } = await socks_1.SocksClient.createConnection(options);
|
|
257
|
+
return socket;
|
|
258
|
+
}
|
|
286
259
|
// 本地连接
|
|
287
260
|
async connectToLocal(destination) {
|
|
288
|
-
const destKey = `${destination.addr}:${destination.port}`;
|
|
289
|
-
const cacheConnection = this.getPoolConnection(destKey);
|
|
290
|
-
if (cacheConnection)
|
|
291
|
-
return cacheConnection;
|
|
292
261
|
return new Promise((resolve, reject) => {
|
|
293
262
|
const localSocket = net_1.default.createConnection(destination.port, destination.addr, () => {
|
|
294
263
|
resolve(localSocket);
|
|
295
264
|
});
|
|
296
|
-
// this.connectionPool[destKey] = localSocket
|
|
297
|
-
this.connectionPool.set(destKey, localSocket);
|
|
298
|
-
localSocket.on('close', () => {
|
|
299
|
-
// delete this.connectionPool[destKey]
|
|
300
|
-
this.__debug && console.info('[socks] connection pool delete', destKey);
|
|
301
|
-
this.connectionPool.delete(destKey);
|
|
302
|
-
});
|
|
303
265
|
localSocket.on('error', (err) => {
|
|
304
266
|
reject(err);
|
|
305
267
|
});
|
|
@@ -354,8 +316,6 @@ class Socks5 {
|
|
|
354
316
|
this.clientSockets.clear();
|
|
355
317
|
// 销毁所有连接池中的 socket
|
|
356
318
|
// Object.values(this.connectionPool).forEach((socket) => socket?.destroy())
|
|
357
|
-
this.connectionPool.values.forEach((kv) => kv?.value?.destroy());
|
|
358
|
-
this.connectionPool.clear();
|
|
359
319
|
this.server = null;
|
|
360
320
|
this.__port = 0;
|
|
361
321
|
}
|
|
@@ -363,15 +323,6 @@ class Socks5 {
|
|
|
363
323
|
this.__debug && console.error('[socks] close errot:', error);
|
|
364
324
|
}
|
|
365
325
|
}
|
|
366
|
-
showPools() {
|
|
367
|
-
this.connectionPool.values.forEach((kv) => {
|
|
368
|
-
const socket = kv?.value;
|
|
369
|
-
const key = kv?.key;
|
|
370
|
-
if (key && socket) {
|
|
371
|
-
console.info(`[socks] ${key} ${socket.localAddress}:${socket.localPort}`);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
326
|
on(name, listener) {
|
|
376
327
|
if (!name || !listener)
|
|
377
328
|
return;
|
|
@@ -12,17 +12,17 @@ exports.default = () => {
|
|
|
12
12
|
: '';
|
|
13
13
|
const v4 = '(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}';
|
|
14
14
|
const v6seg = '[a-fA-F\\d]{1,4}';
|
|
15
|
-
const v6 = `
|
|
16
|
-
(?:
|
|
17
|
-
(?:${v6seg}:){7}(?:${v6seg}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
|
|
18
|
-
(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
|
|
19
|
-
(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
|
|
20
|
-
(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
|
|
21
|
-
(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
|
|
22
|
-
(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
|
|
23
|
-
(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
|
|
24
|
-
(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
|
|
25
|
-
)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
|
|
15
|
+
const v6 = `
|
|
16
|
+
(?:
|
|
17
|
+
(?:${v6seg}:){7}(?:${v6seg}|:)| // 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:8
|
|
18
|
+
(?:${v6seg}:){6}(?:${v4}|:${v6seg}|:)| // 1:2:3:4:5:6:: 1:2:3:4:5:6::8 1:2:3:4:5:6::8 1:2:3:4:5:6::1.2.3.4
|
|
19
|
+
(?:${v6seg}:){5}(?::${v4}|(?::${v6seg}){1,2}|:)| // 1:2:3:4:5:: 1:2:3:4:5::7:8 1:2:3:4:5::8 1:2:3:4:5::7:1.2.3.4
|
|
20
|
+
(?:${v6seg}:){4}(?:(?::${v6seg}){0,1}:${v4}|(?::${v6seg}){1,3}|:)| // 1:2:3:4:: 1:2:3:4::6:7:8 1:2:3:4::8 1:2:3:4::6:7:1.2.3.4
|
|
21
|
+
(?:${v6seg}:){3}(?:(?::${v6seg}){0,2}:${v4}|(?::${v6seg}){1,4}|:)| // 1:2:3:: 1:2:3::5:6:7:8 1:2:3::8 1:2:3::5:6:7:1.2.3.4
|
|
22
|
+
(?:${v6seg}:){2}(?:(?::${v6seg}){0,3}:${v4}|(?::${v6seg}){1,5}|:)| // 1:2:: 1:2::4:5:6:7:8 1:2::8 1:2::4:5:6:7:1.2.3.4
|
|
23
|
+
(?:${v6seg}:){1}(?:(?::${v6seg}){0,4}:${v4}|(?::${v6seg}){1,6}|:)| // 1:: 1::3:4:5:6:7:8 1::8 1::3:4:5:6:7:1.2.3.4
|
|
24
|
+
(?::(?:(?::${v6seg}){0,5}:${v4}|(?::${v6seg}){1,7}|:)) // ::2:3:4:5:6:7:8 ::2:3:4:5:6:7:8 ::8 ::1.2.3.4
|
|
25
|
+
)(?:%[0-9a-zA-Z]{1,})? // %eth0 %1
|
|
26
26
|
`
|
|
27
27
|
.replace(/\s*\/\/.*$/gm, '')
|
|
28
28
|
.replace(/\n/g, '')
|
|
@@ -16,7 +16,6 @@ export declare class Socks5 {
|
|
|
16
16
|
private server;
|
|
17
17
|
private clientSockets;
|
|
18
18
|
private allowedDomains;
|
|
19
|
-
private connectionPool;
|
|
20
19
|
private systemProxy;
|
|
21
20
|
private useSystemProxy;
|
|
22
21
|
__debug: boolean;
|
|
@@ -53,14 +52,13 @@ export declare class Socks5 {
|
|
|
53
52
|
/** */
|
|
54
53
|
private performHandshake;
|
|
55
54
|
private parseClientRequest;
|
|
56
|
-
private getPoolConnection;
|
|
57
55
|
private connectToUpstreamProxy;
|
|
58
56
|
private connectToSystemProxy;
|
|
57
|
+
private connectToProxy;
|
|
59
58
|
private connectToLocal;
|
|
60
59
|
private setupDataForwarding;
|
|
61
60
|
private isAllowedDomain;
|
|
62
61
|
close(): void;
|
|
63
|
-
showPools(): void;
|
|
64
62
|
on(name: Socks5EventType, listener: (...args: any[]) => void): void;
|
|
65
63
|
off(name: Socks5EventType, listener: (...args: any[]) => void): void;
|
|
66
64
|
private _emit;
|
|
@@ -14,12 +14,6 @@ declare class Schema {
|
|
|
14
14
|
string: import("./interface").ExecuteValidator;
|
|
15
15
|
method: import("./interface").ExecuteValidator;
|
|
16
16
|
number: import("./interface").ExecuteValidator;
|
|
17
|
-
/**
|
|
18
|
-
* Encapsulates a validation schema.
|
|
19
|
-
*
|
|
20
|
-
* @param descriptor An object declaring validation rules
|
|
21
|
-
* for this schema.
|
|
22
|
-
*/
|
|
23
17
|
boolean: import("./interface").ExecuteValidator;
|
|
24
18
|
regexp: import("./interface").ExecuteValidator;
|
|
25
19
|
integer: import("./interface").ExecuteValidator;
|