ddan-js 2.6.29 → 2.6.31

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.
@@ -5,6 +5,7 @@ const net_1 = require("net");
5
5
  const socks_1 = require("socks");
6
6
  const uuid_1 = require("../crypto/uuid");
7
7
  const mapping_1 = require("../../class/mapping");
8
+ const index_1 = require("../qs/index");
8
9
  // interface IConnectionPool {
9
10
  // [key: string]: Socket // Key为目标地址:端口组合
10
11
  // }
@@ -61,7 +62,7 @@ class Socks5 {
61
62
  this.__port = port;
62
63
  this.server = net_1.default.createServer((socket) => this.handleSocksConnection(socket));
63
64
  this.server.listen(port, () => {
64
- this.__debug && console.log(`[socks] server is running on port ${port}`);
65
+ this.__debug && console.info(`[socks] server is running on port ${port}`);
65
66
  });
66
67
  this.server.on('error', (err) => {
67
68
  console.error('[socks] server error:', err);
@@ -123,17 +124,17 @@ class Socks5 {
123
124
  if (this.isAllowedDomain(destination.addr)) {
124
125
  // 走上游代理
125
126
  const upstreamSocket = await this.connectToUpstreamProxy(destination);
126
- this.__debug && console.log(`[socks] handle connection upstream`, destination.addr);
127
+ this.__debug && console.info(`[socks] handle connection upstream`, destination.addr);
127
128
  this.setupDataForwarding(clientSocket, upstreamSocket);
128
129
  }
129
130
  else if (this.useSystemProxy && this.systemProxy) {
130
131
  // 走系统代理
131
132
  const systemSocket = await this.connectToSystemProxy(destination);
132
- this.__debug && console.log(`[socks] handle connection system`, destination.addr);
133
+ this.__debug && console.info(`[socks] handle connection system`, destination.addr);
133
134
  this.setupDataForwarding(clientSocket, systemSocket);
134
135
  }
135
136
  else {
136
- this.__debug && console.log(`[socks] handle connection local`, destination.addr);
137
+ this.__debug && console.info(`[socks] handle connection local`, destination.addr);
137
138
  // 本地连接
138
139
  const localSocket = await this.connectToLocal(destination);
139
140
  this.setupDataForwarding(clientSocket, localSocket);
@@ -215,6 +216,7 @@ class Socks5 {
215
216
  this.connectionPool.set(destKey, upstreamSocket);
216
217
  upstreamSocket.on('close', () => {
217
218
  // delete this.connectionPool[destKey]
219
+ this.__debug && console.info('[socks] connection pool delete', destKey);
218
220
  this.connectionPool.delete(destKey);
219
221
  });
220
222
  return upstreamSocket;
@@ -223,7 +225,7 @@ class Socks5 {
223
225
  throw new Error('Failed to connect to upstream proxy: ' + err);
224
226
  }
225
227
  }
226
- // 上游代理连接
228
+ // 系统代理连接
227
229
  async connectToSystemProxy(destination) {
228
230
  const destKey = `${destination.addr}:${destination.port}`;
229
231
  const cacheConnection = this.getPoolConnection(destKey);
@@ -243,12 +245,13 @@ class Socks5 {
243
245
  this.connectionPool.set(destKey, upstreamSocket);
244
246
  upstreamSocket.on('close', () => {
245
247
  // delete this.connectionPool[destKey]
248
+ this.__debug && console.info('[socks] connection pool delete', destKey);
246
249
  this.connectionPool.delete(destKey);
247
250
  });
248
251
  return upstreamSocket;
249
252
  }
250
253
  catch (err) {
251
- throw new Error('Failed to connect to upstream proxy: ' + err);
254
+ throw new Error('Failed to connect to system proxy: ' + err);
252
255
  }
253
256
  }
254
257
  // 本地连接
@@ -265,6 +268,7 @@ class Socks5 {
265
268
  this.connectionPool.set(destKey, localSocket);
266
269
  localSocket.on('close', () => {
267
270
  // delete this.connectionPool[destKey]
271
+ this.__debug && console.info('[socks] connection pool delete', destKey);
268
272
  this.connectionPool.delete(destKey);
269
273
  });
270
274
  localSocket.on('error', (err) => {
@@ -276,16 +280,14 @@ class Socks5 {
276
280
  clientSocket.write(new Uint8Array([0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]));
277
281
  clientSocket.pipe(targetSocket);
278
282
  targetSocket.pipe(clientSocket);
279
- this.__debug &&
280
- console.log(`[socks] setupDataForwarding ${targetSocket.localAddress}:${targetSocket.localPort}`);
283
+ const localIpPort = `${targetSocket.localAddress}:${targetSocket.localPort}`;
284
+ this.__debug && console.info(`[socks] setupDataForwarding ${localIpPort}`);
281
285
  clientSocket.on('close', () => {
282
- this.__debug &&
283
- console.error('[socks] client socket close', targetSocket?.remoteAddress || '');
286
+ this.__debug && console.info('[socks] client socket close', localIpPort);
284
287
  targetSocket.end();
285
288
  });
286
289
  targetSocket.on('close', () => {
287
- this.__debug &&
288
- console.error('[socks] target socket close', targetSocket?.remoteAddress || targetSocket?.localAddress || '');
290
+ this.__debug && console.info('[socks] target socket close', localIpPort);
289
291
  clientSocket.end();
290
292
  });
291
293
  clientSocket.on('error', (err) => {
@@ -300,10 +302,10 @@ class Socks5 {
300
302
  isAllowedDomain(addr) {
301
303
  // return this.allowedDomains.has(addr);
302
304
  // 只匹配主域名,忽略子域名
303
- for (const domain of this.allowedDomains) {
304
- if (domain === '*')
305
+ for (const pattern of this.allowedDomains) {
306
+ if (pattern === '*')
305
307
  return true;
306
- if (addr.includes(domain))
308
+ if (index_1.default.shExpMatch(addr, pattern))
307
309
  return true;
308
310
  }
309
311
  return false;
@@ -312,7 +314,7 @@ class Socks5 {
312
314
  try {
313
315
  if (!this.server)
314
316
  return;
315
- this.__debug && console.log('[socks] closing SOCKS5 proxy server...');
317
+ this.__debug && console.info('[socks] closing SOCKS5 proxy server...');
316
318
  this.server.close((err) => {
317
319
  if (err) {
318
320
  this.__debug && console.error('[socks] closing the server failed:', err);
@@ -332,7 +334,7 @@ class Socks5 {
332
334
  showPool() {
333
335
  Array.from(this.connectionPool.values).forEach((kv) => {
334
336
  if (kv && kv.value) {
335
- console.log(`[socks] ${kv.value.remoteAddress} ${kv.value.localAddress}:${kv.value.localPort}`);
337
+ console.info(`[socks] ${kv.value.remoteAddress} ${kv.value.localAddress}:${kv.value.localPort}`);
336
338
  }
337
339
  });
338
340
  }
@@ -74,4 +74,30 @@ const parseHost = (url) => {
74
74
  result.query = arr.length > 1 ? arr[1] : '';
75
75
  return result;
76
76
  };
77
- exports.default = { parse: decode_1.default, stringify: encode_1.default, https, parseUrl, parsePath, parseHost, join };
77
+ const shExpMatch = (text, pattern) => {
78
+ if (text === pattern)
79
+ return true;
80
+ if (!text)
81
+ return false;
82
+ if (!pattern)
83
+ return false;
84
+ if (pattern === "*")
85
+ return true;
86
+ // 将模式转换为正则表达式
87
+ const regexPattern = pattern
88
+ .replace(/\./g, '\\.') // 转义 .
89
+ .replace(/\*/g, '.*') // 将 * 转换为 .*
90
+ .replace(/\?/g, '.'); // 将 ? 转换为 .
91
+ const regex = new RegExp(regexPattern);
92
+ return regex.test(text);
93
+ };
94
+ exports.default = {
95
+ parse: decode_1.default,
96
+ stringify: encode_1.default,
97
+ https,
98
+ parseUrl,
99
+ parsePath,
100
+ parseHost,
101
+ join,
102
+ shExpMatch,
103
+ };
@@ -212,8 +212,14 @@ declare const dUtil: {
212
212
  };
213
213
  };
214
214
  declare const dHook: {
215
+ singleton: <T>() => {
216
+ new (): {};
217
+ __instance__: any;
218
+ readonly Instance: T;
219
+ readonly I: T;
220
+ };
215
221
  sleep: (ms?: number) => Promise<unknown>;
216
- run: <T = any>(task?: import("./typings").Ddan.PFunction<T> | undefined, wait?: number) => Promise<[any, undefined] | [null, T]>;
222
+ run: <T_1 = any>(task?: import("./typings").Ddan.PFunction<T_1> | undefined, wait?: number) => Promise<[any, undefined] | [null, T_1]>;
217
223
  exec: (func: import("./typings").Ddan.Function, taskId?: string) => import("./typings").Ddan.PSafeResult<any>;
218
224
  debounce: typeof import("./modules/hook/modules/debounce").default;
219
225
  throttle: typeof import("./modules/hook/modules/throttle").default;
@@ -223,10 +229,10 @@ declare const dHook: {
223
229
  pipe: (func: import("./typings").Ddan.Function, callback?: ((result: import("./typings").Ddan.SafeResult<any>) => void) | undefined) => import("./modules/hook/modules/pipeline").default;
224
230
  pipeline: (max?: number) => import("./modules/hook/modules/pipeline").default;
225
231
  safeTask: (func: import("./typings").Ddan.Function, callback?: ((result: import("./typings").Ddan.SafeResult<any>) => void) | undefined) => import("./modules/hook/modules/safeTask").default;
226
- to: <T_1 = any, U extends object = any>(promise: Promise<T_1>, errorExt?: object | undefined) => Promise<[null, T_1] | [U, undefined]>;
227
- go: <T_2 = any>(task?: import("./typings").Ddan.PFunction<T_2> | undefined) => Promise<[any, undefined] | [null, T_2]>;
232
+ to: <T_2 = any, U extends object = any>(promise: Promise<T_2>, errorExt?: object | undefined) => Promise<[null, T_2] | [U, undefined]>;
233
+ go: <T_3 = any>(task?: import("./typings").Ddan.PFunction<T_3> | undefined) => Promise<[any, undefined] | [null, T_3]>;
228
234
  delay: (ms?: number) => Promise<unknown>;
229
- safeRun: <T_3 = any>(func: any) => Promise<[any, undefined] | [null, T_3]>;
235
+ safeRun: <T_4 = any>(func: any) => Promise<[any, undefined] | [null, T_4]>;
230
236
  base64: {
231
237
  encode: (input: string) => string;
232
238
  decode: (base64Str: string) => string;
@@ -325,6 +331,7 @@ declare const dMini: {
325
331
  };
326
332
  parseHost: (url: string) => import("./typings").Ddan.IHttpHost;
327
333
  join: (...args: string[]) => string;
334
+ shExpMatch: (text: string, pattern: string) => boolean;
328
335
  };
329
336
  icon: import("./class/icon").DIcon;
330
337
  html: {
@@ -464,6 +471,7 @@ declare const dWeb: {
464
471
  };
465
472
  parseHost: (url: string) => import("./typings").Ddan.IHttpHost;
466
473
  join: (...args: string[]) => string;
474
+ shExpMatch: (text: string, pattern: string) => boolean;
467
475
  };
468
476
  icon: import("./class/icon").DIcon;
469
477
  html: {
@@ -510,7 +518,7 @@ declare const dNode: {
510
518
  ipaddress: string;
511
519
  port: number;
512
520
  } | undefined> | undefined;
513
- getPacDataUrl: ({ proxy, defProxy, rules }: {
521
+ getPacScript: ({ proxy, defProxy, rules }: {
514
522
  proxy?: string | undefined;
515
523
  defProxy?: string | undefined;
516
524
  rules?: string[] | undefined;
@@ -754,6 +762,7 @@ declare const _default: {
754
762
  };
755
763
  parseHost: (url: string) => import("./typings").Ddan.IHttpHost;
756
764
  join: (...args: string[]) => string;
765
+ shExpMatch: (text: string, pattern: string) => boolean;
757
766
  };
758
767
  css: {
759
768
  stringify: (styleObj: Record<string, string | number>) => string;
@@ -9,7 +9,7 @@ declare const _default: {
9
9
  ipaddress: string;
10
10
  port: number;
11
11
  } | undefined> | undefined;
12
- getPacDataUrl: ({ proxy, defProxy, rules }: {
12
+ getPacScript: ({ proxy, defProxy, rules }: {
13
13
  proxy?: string | undefined;
14
14
  defProxy?: string | undefined;
15
15
  rules?: string[] | undefined;
@@ -3,7 +3,7 @@ declare const _default: {
3
3
  ipaddress: string;
4
4
  port: number;
5
5
  } | undefined> | undefined;
6
- getPacDataUrl: ({ proxy, defProxy, rules }: {
6
+ getPacScript: ({ proxy, defProxy, rules }: {
7
7
  proxy?: string | undefined;
8
8
  defProxy?: string | undefined;
9
9
  rules?: string[] | undefined;
@@ -44,5 +44,6 @@ declare const _default: {
44
44
  };
45
45
  parseHost: (url: string) => Ddan.IHttpHost;
46
46
  join: (...args: string[]) => string;
47
+ shExpMatch: (text: string, pattern: string) => boolean;
47
48
  };
48
49
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.6.29",
3
+ "version": "2.6.31",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",