ddan-js 2.6.24 → 2.6.26

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.
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const child_1 = require("./child");
4
+ const convert_1 = require("../convert");
5
+ const getSystemProxy = () => {
6
+ try {
7
+ if (process.platform === 'win32') {
8
+ return getWindowsSystemProxy();
9
+ }
10
+ else if (process.platform === 'darwin') {
11
+ return getMacOSSystemProxy();
12
+ }
13
+ else {
14
+ return undefined;
15
+ }
16
+ }
17
+ catch (error) {
18
+ return undefined;
19
+ }
20
+ };
21
+ const getWindowsSystemProxy = async () => {
22
+ do {
23
+ let err, text;
24
+ [err, text] = await child_1.default.child_exec(`reg query "HKCU\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"`);
25
+ if (err)
26
+ break;
27
+ const proxyEnabledLine = text.split('\n').find((line) => line.includes('ProxyEnable'));
28
+ const ProxyEnable = proxyEnabledLine && proxyEnabledLine.trim().endsWith('1');
29
+ if (!ProxyEnable)
30
+ break;
31
+ const proxyServerLine = text.split('\n').find((line) => line.includes('ProxyServer'));
32
+ const proxyServer = proxyServerLine ? proxyServerLine.split(' ').filter(Boolean).pop() : '';
33
+ if (!proxyServer)
34
+ break;
35
+ // 解析 IP 和端口
36
+ const proxyParts = proxyServer.split(':');
37
+ if (proxyParts.length <= 1)
38
+ break;
39
+ const ipaddress = proxyParts[0];
40
+ const port = +(proxyParts[1] || '');
41
+ if (!ipaddress || !port)
42
+ break;
43
+ return { ipaddress, port };
44
+ } while (false);
45
+ return undefined;
46
+ };
47
+ const getMacOSSystemProxy = async () => {
48
+ do {
49
+ let err, text;
50
+ [err, text] = await child_1.default.child_exec(`networksetup -getwebproxy "Wi-Fi"`);
51
+ if (err)
52
+ break;
53
+ const outputLines = text.split('\n');
54
+ const proxyEnabledLine = outputLines.find((line) => line.includes('Enabled'));
55
+ const isEnabled = proxyEnabledLine && proxyEnabledLine.includes('Yes');
56
+ if (!isEnabled)
57
+ break;
58
+ const proxyServerLine = outputLines.find((line) => line.includes('Server'));
59
+ const proxyPortLine = outputLines.find((line) => line.includes('Port'));
60
+ const ipaddress = proxyServerLine ? proxyServerLine.split(':')[1]?.trim() : null;
61
+ const proxyPort = proxyPortLine ? proxyPortLine.split(':')[1]?.trim() : null;
62
+ const port = +(proxyPort || '');
63
+ if (!ipaddress || !port)
64
+ break;
65
+ return { ipaddress, port };
66
+ } while (false);
67
+ return undefined;
68
+ };
69
+ // if (host === 'singapore-upgrade.geelark.com') {
70
+ // return 'SOCKS5 127.0.0.1:${port}';
71
+ // }
72
+ // { proxy = 'DIRECT'; default = 'DIRECT', rules: string[]
73
+ const getPacDataUrl = ({ proxy = 'DIRECT', defProxy = 'DIRECT', rules = ['*'] }) => {
74
+ const _proxy = proxy || defProxy || "DIRECT";
75
+ const content = `
76
+ function FindProxyForURL(url, host) {
77
+ if (isInNet(host, "127.0.0.1", "255.255.255.255") ||
78
+ shExpMatch(host, "localhost") ||
79
+ shExpMatch(host, "<local>")) {
80
+ return "DIRECT";
81
+ }
82
+
83
+ for (let rule of ${rules}) {
84
+ if (rule && shExpMatch(host, rule)) {
85
+ return "${_proxy}";
86
+ }
87
+ }
88
+ return "${defProxy}";
89
+ }
90
+ `;
91
+ return convert_1.default.toDataUrl(content);
92
+ };
93
+ exports.default = { getSystemProxy, getPacDataUrl };
@@ -14,7 +14,8 @@ class Socks5 {
14
14
  useSystemProxy = false;
15
15
  __debug = false;
16
16
  __uuid;
17
- constructor(upstreamProxy, allowedDomains = [], debug = false) {
17
+ __port = 0;
18
+ constructor(upstreamProxy, allowedDomains = ['*'], debug = false) {
18
19
  this.upstreamProxy = upstreamProxy;
19
20
  this.allowedDomains = new Set(allowedDomains);
20
21
  this.__debug = debug;
@@ -23,6 +24,9 @@ class Socks5 {
23
24
  get id() {
24
25
  return this.__uuid;
25
26
  }
27
+ get port() {
28
+ return this.__port;
29
+ }
26
30
  validateProxyConfig(proxyConfig = this.upstreamProxy) {
27
31
  const { ipaddress, port, userId, password } = proxyConfig || {};
28
32
  if (!ipaddress || typeof ipaddress !== 'string') {
@@ -49,6 +53,7 @@ class Socks5 {
49
53
  const port = await this.findAvailablePort(startPort);
50
54
  if (!port)
51
55
  return port;
56
+ this.__port = port;
52
57
  this.server = net_1.default.createServer((socket) => this.handleSocksConnection(socket));
53
58
  this.server.listen(port, () => {
54
59
  this.__debug && console.log(`[socks] server is running on port ${port}`);
@@ -251,11 +256,12 @@ class Socks5 {
251
256
  clientSocket.pipe(targetSocket);
252
257
  targetSocket.pipe(clientSocket);
253
258
  clientSocket.on('close', () => {
254
- this.__debug && console.error('[socks] client socket close', targetSocket?.remoteAddress);
259
+ this.__debug && console.error('[socks] client socket close', targetSocket?.remoteAddress || "");
255
260
  targetSocket.end();
256
261
  });
257
262
  targetSocket.on('close', () => {
258
- this.__debug && console.error('[socks] target socket close', targetSocket?.remoteAddress);
263
+ this.__debug &&
264
+ console.error('[socks] target socket close', targetSocket?.remoteAddress || targetSocket?.localAddress || '');
259
265
  clientSocket.end();
260
266
  });
261
267
  clientSocket.on('error', (err) => {
@@ -279,7 +285,9 @@ class Socks5 {
279
285
  return false;
280
286
  }
281
287
  close() {
282
- if (this.server) {
288
+ try {
289
+ if (!this.server)
290
+ return;
283
291
  console.log('[socks] closing SOCKS5 proxy server...');
284
292
  this.server.close((err) => {
285
293
  if (err) {
@@ -292,6 +300,9 @@ class Socks5 {
292
300
  Object.values(this.connectionPool).forEach((socket) => socket.destroy());
293
301
  this.server = null;
294
302
  }
303
+ catch (error) {
304
+ console.error('[socks] close errot:', error);
305
+ }
295
306
  }
296
307
  }
297
308
  exports.Socks5 = Socks5;
@@ -79,6 +79,7 @@ declare const dUtil: {
79
79
  getRandomBytes: (length: number) => Uint8Array;
80
80
  textEncode: (text: string) => Uint8Array;
81
81
  textDecode: (buf: ArrayBuffer) => string;
82
+ toDataUrl: (textOrBuf: string | ArrayBuffer, contentType?: string) => string;
82
83
  toBase64: (input?: string) => string;
83
84
  fromBase64: (input?: string) => string;
84
85
  bytesToBase64: (bytes: Uint8Array) => string;
@@ -247,6 +248,7 @@ declare const dHook: {
247
248
  getRandomBytes: (length: number) => Uint8Array;
248
249
  textEncode: (text: string) => Uint8Array;
249
250
  textDecode: (buf: ArrayBuffer) => string;
251
+ toDataUrl: (textOrBuf: string | ArrayBuffer, contentType?: string) => string;
250
252
  toBase64: (input?: string) => string;
251
253
  fromBase64: (input?: string) => string;
252
254
  bytesToBase64: (bytes: Uint8Array) => string;
@@ -502,6 +504,16 @@ declare const dNode: {
502
504
  Ecdh: typeof import("./modules/node/ecdh").default;
503
505
  EcdhSpki: typeof import("./modules/node/ecdh-spki").default;
504
506
  Socks5: typeof import("./modules/node/socks5").Socks5;
507
+ getSystemProxy: () => Promise<{
508
+ ipaddress: string;
509
+ port: number;
510
+ } | undefined> | undefined;
511
+ getPacDataUrl: ({ proxy, defProxy, rules }: {
512
+ proxy?: string | undefined;
513
+ defProxy?: string | undefined;
514
+ rules?: string[] | undefined;
515
+ }) => string;
516
+ child_exec: (cmd: string) => Promise<[any, string]>;
505
517
  brotliCompress: typeof import("./modules/node/brotli").brotliCompress;
506
518
  brotliDecompress: typeof import("./modules/node/brotli").brotliDecompress;
507
519
  };
@@ -829,6 +841,7 @@ declare const _default: {
829
841
  getRandomBytes: (length: number) => Uint8Array;
830
842
  textEncode: (text: string) => Uint8Array;
831
843
  textDecode: (buf: ArrayBuffer) => string;
844
+ toDataUrl: (textOrBuf: string | ArrayBuffer, contentType?: string) => string;
832
845
  toBase64: (input?: string) => string;
833
846
  fromBase64: (input?: string) => string;
834
847
  bytesToBase64: (bytes: Uint8Array) => string;
@@ -10,6 +10,7 @@ declare const _default: {
10
10
  getRandomBytes: (length: number) => Uint8Array;
11
11
  textEncode: (text: string) => Uint8Array;
12
12
  textDecode: (buf: ArrayBuffer) => string;
13
+ toDataUrl: (textOrBuf: string | ArrayBuffer, contentType?: string) => string;
13
14
  toBase64: (input?: string) => string;
14
15
  fromBase64: (input?: string) => string;
15
16
  bytesToBase64: (bytes: Uint8Array) => string;
@@ -0,0 +1,4 @@
1
+ declare const _default: {
2
+ child_exec: (cmd: string) => Promise<[any, string]>;
3
+ };
4
+ export default _default;
@@ -1,10 +1,20 @@
1
- import Ecdh from "./ecdh";
2
- import EcdhSpki from "./ecdh-spki";
3
- import { Socks5 } from "./socks5";
1
+ import Ecdh from './ecdh';
2
+ import EcdhSpki from './ecdh-spki';
3
+ import { Socks5 } from './socks5';
4
4
  declare const _default: {
5
5
  Ecdh: typeof Ecdh;
6
6
  EcdhSpki: typeof EcdhSpki;
7
7
  Socks5: typeof Socks5;
8
+ getSystemProxy: () => Promise<{
9
+ ipaddress: string;
10
+ port: number;
11
+ } | undefined> | undefined;
12
+ getPacDataUrl: ({ proxy, defProxy, rules }: {
13
+ proxy?: string | undefined;
14
+ defProxy?: string | undefined;
15
+ rules?: string[] | undefined;
16
+ }) => string;
17
+ child_exec: (cmd: string) => Promise<[any, string]>;
8
18
  brotliCompress: typeof import("./brotli").brotliCompress;
9
19
  brotliDecompress: typeof import("./brotli").brotliDecompress;
10
20
  };
@@ -0,0 +1,12 @@
1
+ declare const _default: {
2
+ getSystemProxy: () => Promise<{
3
+ ipaddress: string;
4
+ port: number;
5
+ } | undefined> | undefined;
6
+ getPacDataUrl: ({ proxy, defProxy, rules }: {
7
+ proxy?: string | undefined;
8
+ defProxy?: string | undefined;
9
+ rules?: string[] | undefined;
10
+ }) => string;
11
+ };
12
+ export default _default;
@@ -15,8 +15,10 @@ export declare class Socks5 {
15
15
  private useSystemProxy;
16
16
  __debug: boolean;
17
17
  __uuid: string;
18
- constructor(upstreamProxy: IProxyConfig, allowedDomains?: string[], debug?: boolean);
18
+ __port: number;
19
+ constructor(upstreamProxy: IProxyConfig, allowedDomains?: string[] | ['*'], debug?: boolean);
19
20
  get id(): string;
21
+ get port(): number;
20
22
  validateProxyConfig(proxyConfig?: IProxyConfig): "" | "无效的上游代理 IP 地址" | "无效的上游代理端口" | "无效的上游代理用户名" | "无效的上游代理密码";
21
23
  start(startPort?: number): Promise<number>;
22
24
  setUpstreamProxy(upstreamProxy: IProxyConfig): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.6.24",
3
+ "version": "2.6.26",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",