ddan-js 2.6.35 → 2.6.37

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.
@@ -6,6 +6,10 @@ const socks_1 = require("socks");
6
6
  const uuid_1 = require("../crypto/uuid");
7
7
  const mapping_1 = require("../../class/mapping");
8
8
  const index_1 = require("../qs/index");
9
+ const event_1 = require("../../class/event");
10
+ const Socks5Event = {
11
+ Error: 'error',
12
+ };
9
13
  class Socks5 {
10
14
  upstreamProxy;
11
15
  server = null;
@@ -17,7 +21,9 @@ class Socks5 {
17
21
  __debug = false;
18
22
  __uuid;
19
23
  __port = 0;
24
+ __event;
20
25
  constructor(upstreamProxy, allowedDomains = ['*'], debug = false) {
26
+ this.__event = new event_1.default();
21
27
  this.upstreamProxy = upstreamProxy;
22
28
  this.allowedDomains = new Set(allowedDomains);
23
29
  this.__debug = debug;
@@ -46,12 +52,12 @@ class Socks5 {
46
52
  }
47
53
  return '';
48
54
  }
49
- async start(startPort = 9968) {
55
+ async start(startPort = 8838) {
50
56
  if (this.validateProxyConfig())
51
57
  return 0;
52
58
  if (this.server) {
53
- console.log(`[socks] server is already running`);
54
- return 0;
59
+ this.__debug && console.info(`[socks] server is already running`);
60
+ return this.__port;
55
61
  }
56
62
  const port = await this.findAvailablePort(startPort);
57
63
  if (!port)
@@ -114,7 +120,7 @@ class Socks5 {
114
120
  this.clientSockets.delete(clientSocket);
115
121
  clientSocket?.destroy();
116
122
  });
117
- let addrport = "";
123
+ let addrport = '';
118
124
  try {
119
125
  this.connectionPool.clean(100, (kv) => kv?.destroy());
120
126
  await this.performHandshake(clientSocket);
@@ -143,8 +149,25 @@ class Socks5 {
143
149
  this.__debug && console.error('[socks] connection failed:', err);
144
150
  // this.__event.emit(Socks5Event.Error, err, addrport)
145
151
  clientSocket.end(new Uint8Array([0x05, 0x01]));
152
+ this._emit('error', err, addrport);
146
153
  }
147
154
  }
155
+ /**
156
+ * 回复客户端请求响应数据
157
+ * VER是SOCKS版本,这里应该是0x05;
158
+ * REP应答字段:
159
+ * 0x00表示成功
160
+ * 0x01普通SOCKS服务器连接失败
161
+ * 0x02现有规则不允许连接
162
+ * 0x03网络不可达
163
+ * 0x04主机不可达
164
+ * 0x05连接被拒
165
+ * 0x06 TTL超时
166
+ * 0x07不支持的命令
167
+ * 0x08不支持的地址类型
168
+ * 0x09 - 0xFF未定义
169
+ */
170
+ /** */
148
171
  performHandshake(clientSocket) {
149
172
  return new Promise((resolve, reject) => {
150
173
  clientSocket.once('data', (data) => {
@@ -164,6 +187,8 @@ class Socks5 {
164
187
  if (data[1] !== 0x01) {
165
188
  return reject(new Error('Unsupported command'));
166
189
  }
190
+ // 3 为 addrtype
191
+ // remotePort = data.readUInt16BE(data.length - 2); //最后两位为端口值
167
192
  if (data[3] === 0x01) {
168
193
  // IPv4 地址
169
194
  addr = data.slice(4, 8).join('.');
@@ -321,21 +346,47 @@ class Socks5 {
321
346
  });
322
347
  // 销毁客户端 socket
323
348
  this.clientSockets.forEach((socket) => socket?.destroy());
349
+ this.clientSockets.clear();
324
350
  // 销毁所有连接池中的 socket
325
351
  // Object.values(this.connectionPool).forEach((socket) => socket?.destroy())
326
- Array.from(this.connectionPool.values).forEach((kv) => kv?.value?.destroy());
352
+ this.connectionPool.values.forEach((kv) => kv?.value?.destroy());
353
+ this.connectionPool.clear();
327
354
  this.server = null;
355
+ this.__port = 0;
328
356
  }
329
357
  catch (error) {
330
358
  this.__debug && console.error('[socks] close errot:', error);
331
359
  }
332
360
  }
333
- showPool() {
334
- Array.from(this.connectionPool.values).forEach((kv) => {
335
- if (kv && kv.value) {
336
- console.info(`[socks] ${kv.value.remoteAddress} ${kv.value.localAddress}:${kv.value.localPort}`);
361
+ showPools() {
362
+ this.connectionPool.values.forEach((kv) => {
363
+ const socket = kv?.value;
364
+ const key = kv?.key;
365
+ if (key && socket) {
366
+ console.info(`[socks] ${key} ${socket.localAddress}:${socket.localPort}`);
337
367
  }
338
368
  });
339
369
  }
370
+ on(name, listener) {
371
+ if (!name || !listener)
372
+ return;
373
+ this.__event.off(name, listener);
374
+ this.__event.on(name, listener);
375
+ }
376
+ off(name, listener) {
377
+ if (!name)
378
+ return;
379
+ this.__event.off(name, listener);
380
+ }
381
+ _emit(name, ...args) {
382
+ try {
383
+ if (!name)
384
+ return;
385
+ this.__event.emit(name, ...args);
386
+ }
387
+ catch (error) {
388
+ // nothing
389
+ }
390
+ }
340
391
  }
341
392
  exports.Socks5 = Socks5;
@@ -2,8 +2,8 @@ import KValue from './kvalue';
2
2
  export default class Mapping<T = any> {
3
3
  _map: Map<string, KValue<T>>;
4
4
  constructor();
5
- get keys(): IterableIterator<string>;
6
- get values(): IterableIterator<KValue<T>>;
5
+ get keys(): string[];
6
+ get values(): KValue<T>[];
7
7
  get(key: string): T | undefined;
8
8
  set(key: string, value?: T): void;
9
9
  delete(key: string, cb?: (value?: T, key?: string) => void): void;
@@ -565,9 +565,9 @@ declare const dNode: {
565
565
  ipaddress: string;
566
566
  port: number;
567
567
  } | undefined> | undefined;
568
- toPacScript: ({ proxy, defProxy, rules }: {
568
+ toPacScript: ({ proxy, system, rules }: {
569
569
  proxy?: string | undefined;
570
- defProxy?: string | undefined;
570
+ system?: string | undefined;
571
571
  rules?: string[] | undefined;
572
572
  }) => string;
573
573
  toPacProxy: (ipaddress: string, port: number, options?: {
@@ -9,9 +9,9 @@ declare const _default: {
9
9
  ipaddress: string;
10
10
  port: number;
11
11
  } | undefined> | undefined;
12
- toPacScript: ({ proxy, defProxy, rules }: {
12
+ toPacScript: ({ proxy, system, rules }: {
13
13
  proxy?: string | undefined;
14
- defProxy?: string | undefined;
14
+ system?: string | undefined;
15
15
  rules?: string[] | undefined;
16
16
  }) => string;
17
17
  toPacProxy: (ipaddress: string, port: number, options?: {
@@ -3,9 +3,9 @@ declare const _default: {
3
3
  ipaddress: string;
4
4
  port: number;
5
5
  } | undefined> | undefined;
6
- toPacScript: ({ proxy, defProxy, rules }: {
6
+ toPacScript: ({ proxy, system, rules }: {
7
7
  proxy?: string | undefined;
8
- defProxy?: string | undefined;
8
+ system?: string | undefined;
9
9
  rules?: string[] | undefined;
10
10
  }) => string;
11
11
  toPacProxy: (ipaddress: string, port: number, options?: {
@@ -1,3 +1,4 @@
1
+ import DEvent from '../../class/event';
1
2
  interface IProxyConfig {
2
3
  ipaddress: string;
3
4
  port: number;
@@ -5,6 +6,10 @@ interface IProxyConfig {
5
6
  userId?: string;
6
7
  password?: string;
7
8
  }
9
+ declare const Socks5Event: {
10
+ readonly Error: "error";
11
+ };
12
+ type Socks5EventType = (typeof Socks5Event)[keyof typeof Socks5Event];
8
13
  export declare class Socks5 {
9
14
  private upstreamProxy;
10
15
  private server;
@@ -16,6 +21,7 @@ export declare class Socks5 {
16
21
  __debug: boolean;
17
22
  __uuid: string;
18
23
  __port: number;
24
+ __event: DEvent;
19
25
  constructor(upstreamProxy: IProxyConfig, allowedDomains?: string[] | ['*'], debug?: boolean);
20
26
  get id(): string;
21
27
  get port(): number;
@@ -26,6 +32,22 @@ export declare class Socks5 {
26
32
  setAllowedDomains(allowedDomains?: string[]): void;
27
33
  private findAvailablePort;
28
34
  private handleSocksConnection;
35
+ /**
36
+ * 回复客户端请求响应数据
37
+ * VER是SOCKS版本,这里应该是0x05;
38
+ * REP应答字段:
39
+ * 0x00表示成功
40
+ * 0x01普通SOCKS服务器连接失败
41
+ * 0x02现有规则不允许连接
42
+ * 0x03网络不可达
43
+ * 0x04主机不可达
44
+ * 0x05连接被拒
45
+ * 0x06 TTL超时
46
+ * 0x07不支持的命令
47
+ * 0x08不支持的地址类型
48
+ * 0x09 - 0xFF未定义
49
+ */
50
+ /** */
29
51
  private performHandshake;
30
52
  private parseClientRequest;
31
53
  private getPoolConnection;
@@ -35,6 +57,9 @@ export declare class Socks5 {
35
57
  private setupDataForwarding;
36
58
  private isAllowedDomain;
37
59
  close(): void;
38
- showPool(): void;
60
+ showPools(): void;
61
+ on(name: Socks5EventType, listener: (...args: any[]) => void): void;
62
+ off(name: Socks5EventType, listener: (...args: any[]) => void): void;
63
+ private _emit;
39
64
  }
40
65
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.6.35",
3
+ "version": "2.6.37",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",