ddan-js 2.7.6 → 2.7.8

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,143 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const convert_1 = require("../convert");
4
+ const RSAPublicKey = `MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApiLWsPmoaiGdr02AmPYS
5
+ 4/VgxyJNNJSK2VICirXl4P8qAP3b/6GIs7Hwnsmmuj7gledJ0XiZodFjDNgQEZEG
6
+ j4K9KX8NixcrJwnTQV5UebiERdj+64ihwqPVWo9dIOj43ZPJ8f4dritJv5tnQaBb
7
+ b9zTkU3Ofzk1g8RU00Ith6quOGZcU2IWC4OQ+5YiZvzrCBKEvjFwK2OvGq43tI51
8
+ GaZdI9H09jisypqefsMvv6esjEQQOtl+WGlI5fof7KZUt3dppvuDI4CYgeXIoKKL
9
+ 9T9qDl8F9aNSs3XHx32YyA7L1FraFD6PjqRjueIFp2emsXQcIMeMqqgVyV+4qb+0
10
+ uwIDAQAB`;
11
+ const RSAPrivateKey = `MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmItaw+ahqIZ2v
12
+ TYCY9hLj9WDHIk00lIrZUgKKteXg/yoA/dv/oYizsfCeyaa6PuCV50nReJmh0WMM
13
+ 2BARkQaPgr0pfw2LFysnCdNBXlR5uIRF2P7riKHCo9Vaj10g6Pjdk8nx/h2uK0m/
14
+ m2dBoFtv3NORTc5/OTWDxFTTQi2Hqq44ZlxTYhYLg5D7liJm/OsIEoS+MXArY68a
15
+ rje0jnUZpl0j0fT2OKzKmp5+wy+/p6yMRBA62X5YaUjl+h/splS3d2mm+4MjgJiB
16
+ 5cigoov1P2oOXwX1o1KzdcfHfZjIDsvUWtoUPo+OpGO54gWnZ6axdBwgx4yqqBXJ
17
+ X7ipv7S7AgMBAAECggEAE1kuWLkSsJ2Oo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Ygsu4BrJkK02wMuydbxczIW9U6bTaeCFG7PAoGAN3oHYzTEBwZ7ZKv11QiQ
34
+ Ep/HeHBvD8fH/e1uVyBel4Co7NO5pV2s8qjS8+3+jxumKLNXCirjqwHo03YCdzxi
35
+ cy4GFDsukHmDQ7FF9VC4QV0fJ0F7jGYE9nNZ+K9qwS5KrihOSBPxj0HptrF6TSOw
36
+ BTSwr0byL3TC0m0StkSNKpo=`;
37
+ const generateKeys = async (subtle) => {
38
+ try {
39
+ // 生成RSA密钥对
40
+ const keyPair = await subtle.generateKey({
41
+ name: 'RSA-OAEP',
42
+ modulusLength: 2048,
43
+ publicExponent: new Uint8Array([1, 0, 1]),
44
+ hash: 'SHA-256',
45
+ }, true, // 是否可导出密钥
46
+ ['encrypt', 'decrypt']);
47
+ // 导出公钥为SPKI格式(可共享)
48
+ const publicBuffer = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
49
+ const publicKey = format_pem_content(publicBuffer);
50
+ const publicKeyPem = format_pem_key(publicKey, 'PUBLIC KEY');
51
+ // 导出私钥为PKCS#8格式(需保护)
52
+ const privateBuffer = await window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
53
+ const privateKey = format_pem_content(privateBuffer);
54
+ const privateKeyPem = format_pem_key(privateKey, 'PRIVATE KEY');
55
+ return {
56
+ publicKey,
57
+ publicKeyPem,
58
+ privateKey,
59
+ privateKeyPem,
60
+ };
61
+ }
62
+ catch (err) {
63
+ console.error('generateKeys failed:', err);
64
+ return {
65
+ publicKey: '',
66
+ publicKeyPem: '',
67
+ privateKey: '',
68
+ privateKeyPem: '',
69
+ };
70
+ }
71
+ };
72
+ const format_pem_content = (buffer) => {
73
+ try {
74
+ const base64String = convert_1.default.ab2str(buffer, true);
75
+ return base64String.match(/.{1,64}/g)?.join('\n') || '';
76
+ }
77
+ catch (error) {
78
+ return '';
79
+ }
80
+ };
81
+ const format_pem_key = (pemContent = '', label = 'PUBLIC KEY') => {
82
+ return `-----BEGIN ${label}-----\n${pemContent}\n-----END ${label}-----`;
83
+ };
84
+ // SHA256: 256 - 2 - 2*32 = 190
85
+ // SHA1: 256 - 2 - 2*20 = 214
86
+ const maxChunkSize = 190;
87
+ const encrypt = async (subtle, data, publicKey) => {
88
+ try {
89
+ const bytes = new TextEncoder().encode(data);
90
+ const key = await subtle.importKey('spki', convert_1.default.str2ab(publicKey, true), {
91
+ name: 'RSA-OAEP',
92
+ hash: 'SHA-256',
93
+ }, true, ['encrypt']);
94
+ const encryptedChunks = [];
95
+ for (let i = 0; i < bytes.length; i += maxChunkSize) {
96
+ let chunk = bytes.slice(i, i + maxChunkSize);
97
+ let encryptedChunk = await subtle.encrypt({
98
+ name: 'RSA-OAEP',
99
+ }, key, chunk);
100
+ encryptedChunks.push(new Uint8Array(encryptedChunk));
101
+ }
102
+ const enctryptedBytes = convert_1.default.concatBytes(...encryptedChunks);
103
+ return convert_1.default.bytesToBase64(enctryptedBytes);
104
+ }
105
+ catch (error) {
106
+ console.error(`[debug] encrypt`, error);
107
+ return '';
108
+ }
109
+ };
110
+ const decrypt = async (subtle, encryptedBase64, privateKey) => {
111
+ try {
112
+ const encrypted = convert_1.default.base64ToBytes(encryptedBase64);
113
+ const key = await subtle.importKey('pkcs8', convert_1.default.str2ab(privateKey, true), {
114
+ name: 'RSA-OAEP',
115
+ hash: 'SHA-256',
116
+ }, true, ['decrypt']);
117
+ const decryptedChunks = [];
118
+ const chunkSize = 256; // 每个加密块的大小(2048位 RSA 的加密输出为 256 字节)
119
+ for (let i = 0; i < encrypted.length; i += chunkSize) {
120
+ let chunk = encrypted.slice(i, i + chunkSize);
121
+ let decryptedChunk = await subtle.decrypt({
122
+ name: 'RSA-OAEP',
123
+ }, key, chunk);
124
+ // 添加解密后的数据
125
+ decryptedChunks.push(new Uint8Array(decryptedChunk));
126
+ }
127
+ const encryptedBytes = convert_1.default.concatBytes(...decryptedChunks);
128
+ return convert_1.default.bytesToStr(encryptedBytes);
129
+ }
130
+ catch (err) {
131
+ console.error('decrypt failed:', err);
132
+ return '';
133
+ }
134
+ };
135
+ exports.default = {
136
+ RSAPublicKey,
137
+ RSAPrivateKey,
138
+ generateKeys,
139
+ format_pem_key,
140
+ format_pem_content,
141
+ encrypt,
142
+ decrypt,
143
+ };
@@ -6,4 +6,5 @@ const socks5_1 = require("./socks5");
6
6
  const child_1 = require("./child");
7
7
  const proxy_1 = require("./proxy");
8
8
  const ecdh_web_1 = require("./ecdh-web");
9
- exports.default = { ...brotli_1.default, ...child_1.default, ...proxy_1.default, Ecdh: ecdh_1.default, EcdhWeb: ecdh_web_1.default, Socks5: socks5_1.Socks5 };
9
+ const rsa_web_1 = require("./rsa-web");
10
+ exports.default = { ...brotli_1.default, ...child_1.default, ...proxy_1.default, Ecdh: ecdh_1.default, EcdhWeb: ecdh_web_1.default, Socks5: socks5_1.Socks5, rsa: rsa_web_1.default };
@@ -135,13 +135,13 @@ const pacScript = async (host, port, options) => {
135
135
  const pac_proxy = toPacProxy(host, port, { type });
136
136
  if (!pac_proxy)
137
137
  return "";
138
- let pac_sys = undefined;
138
+ let pac_default = undefined;
139
139
  if (useSystem) {
140
140
  const sysproxy = await getSystemProxy();
141
141
  if (sysproxy) {
142
- pac_sys = toPacProxy(sysproxy?.ipaddress || '', sysproxy?.port || 0, { direct: true });
142
+ pac_default = toPacProxy(sysproxy?.ipaddress || '', sysproxy?.port || 0, { direct: true });
143
143
  }
144
144
  }
145
- return toPacScript(pac_proxy, rules || [], pac_sys);
145
+ return toPacScript(pac_proxy, rules || [], pac_default);
146
146
  };
147
147
  exports.default = { getSystemProxy, toPacScript, toPacProxy, pacScript };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rsa_1 = require("../crypto/rsa");
4
+ const crypto_1 = require("crypto");
5
+ const getSubtle = () => crypto_1.default.webcrypto.subtle;
6
+ const generateKeys = () => rsa_1.default.generateKeys(getSubtle());
7
+ // SHA256: 256 - 2 - 2*32 = 190
8
+ // SHA1: 256 - 2 - 2*20 = 214
9
+ const encrypt = async (data, publicKey) => rsa_1.default.encrypt(getSubtle(), data, publicKey);
10
+ const decrypt = async (encryptedBase64, privateKey) => rsa_1.default.decrypt(getSubtle(), encryptedBase64, privateKey);
11
+ const encode = async (data) => rsa_1.default.encrypt(getSubtle(), data, rsa_1.default.RSAPublicKey);
12
+ const decode = async (encryptedBase64) => rsa_1.default.decrypt(getSubtle(), encryptedBase64, rsa_1.default.RSAPrivateKey);
13
+ exports.default = {
14
+ generateKeys,
15
+ encrypt,
16
+ decrypt,
17
+ encode,
18
+ decode,
19
+ };
@@ -1,92 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const convert_1 = require("./convert");
4
- const generateKeys = async () => {
5
- try {
6
- // 生成RSA密钥对
7
- const keyPair = await window.crypto.subtle.generateKey({
8
- name: 'RSA-OAEP',
9
- modulusLength: 2048,
10
- publicExponent: new Uint8Array([1, 0, 1]),
11
- hash: 'SHA-256',
12
- }, true, // 是否可导出密钥
13
- ['encrypt', 'decrypt']);
14
- // 导出公钥为SPKI格式(可共享)
15
- const publicBuffer = await window.crypto.subtle.exportKey('spki', keyPair.publicKey);
16
- const publicKey = format_pem_content(publicBuffer);
17
- const publicKeyPem = format_pem_key(publicKey, 'PUBLIC KEY');
18
- // 导出私钥为PKCS#8格式(需保护)
19
- const privateBuffer = await window.crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
20
- const privateKey = format_pem_content(privateBuffer);
21
- const privateKeyPem = format_pem_key(privateKey, 'PRIVATE KEY');
22
- return {
23
- publicKey,
24
- publicKeyPem,
25
- privateKey,
26
- privateKeyPem,
27
- };
28
- }
29
- catch (err) {
30
- console.error('generateKeys failed:', err);
31
- return {
32
- publicKey: "",
33
- publicKeyPem: "",
34
- privateKey: "",
35
- privateKeyPem: ""
36
- };
37
- }
38
- };
39
- const format_pem_content = (buffer) => {
40
- try {
41
- const base64String = convert_1.default.ab2str(buffer, true);
42
- return base64String.match(/.{1,64}/g)?.join('\n') || '';
43
- }
44
- catch (error) {
45
- return '';
46
- }
47
- };
48
- const format_pem_key = (pemContent = '', label = 'PUBLIC KEY') => {
49
- return `-----BEGIN ${label}-----\n${pemContent}\n-----END ${label}-----`;
50
- };
51
- const encrypt = async (data, publicKey) => {
52
- try {
53
- const encodedData = new TextEncoder().encode(data);
54
- const key = await window.crypto.subtle.importKey('spki', convert_1.default.str2ab(publicKey, true), {
55
- name: 'RSA-OAEP',
56
- hash: 'SHA-256',
57
- }, true, ['encrypt']);
58
- const encrypted = await window.crypto.subtle.encrypt({
59
- name: 'RSA-OAEP',
60
- }, key, encodedData);
61
- return convert_1.default.ab2str(encrypted, true);
62
- }
63
- catch (err) {
64
- console.error('encrypt failed:', err);
65
- return '';
66
- }
67
- };
68
- const decrypt = async (encryptedData, privateKey) => {
69
- try {
70
- const encrypted = convert_1.default.str2ab(encryptedData, true);
71
- const key = await window.crypto.subtle.importKey('pkcs8', convert_1.default.str2ab(privateKey, true), {
72
- name: 'RSA-OAEP',
73
- hash: 'SHA-256',
74
- }, true, ['decrypt']);
75
- const decrypted = await window.crypto.subtle.decrypt({
76
- name: 'RSA-OAEP',
77
- }, key, encrypted);
78
- const decodedData = new TextDecoder().decode(decrypted);
79
- return decodedData;
80
- }
81
- catch (err) {
82
- console.error('decrypt failed:', err);
83
- return '';
84
- }
85
- };
3
+ const rsa_1 = require("./crypto/rsa");
4
+ const __subtle = ((typeof window !== 'undefined') ? window.crypto.subtle : {});
5
+ const generateKeys = () => rsa_1.default.generateKeys(__subtle);
6
+ // SHA256: 256 - 2 - 2*32 = 190
7
+ // SHA1: 256 - 2 - 2*20 = 214
8
+ const encrypt = async (data, publicKey) => rsa_1.default.encrypt(__subtle, data, publicKey);
9
+ const decrypt = async (encryptedBase64, privateKey) => rsa_1.default.decrypt(__subtle, encryptedBase64, privateKey);
10
+ const encode = async (data) => rsa_1.default.encrypt(__subtle, data, rsa_1.default.RSAPublicKey);
11
+ const decode = async (encryptedBase64) => rsa_1.default.decrypt(__subtle, encryptedBase64, rsa_1.default.RSAPrivateKey);
86
12
  exports.default = {
87
13
  generateKeys,
88
- format_pem_key,
89
- format_pem_content,
90
14
  encrypt,
91
15
  decrypt,
16
+ encode,
17
+ decode,
92
18
  };
@@ -5,6 +5,7 @@ export default class Mapping<T = any> {
5
5
  get keys(): string[];
6
6
  get values(): KValue<T>[];
7
7
  get(key: string): T | undefined;
8
+ getKV(key: string): KValue<T> | undefined;
8
9
  set(key: string, value?: T): void;
9
10
  delete(key: string, cb?: (value?: T, key?: string) => void): void;
10
11
  clear(): void;
@@ -557,16 +557,28 @@ declare const dWeb: {
557
557
  privateKey: string;
558
558
  privateKeyPem: string;
559
559
  }>;
560
- format_pem_key: (pemContent?: string, label?: string) => string;
561
- format_pem_content: (buffer: ArrayBuffer) => string;
562
560
  encrypt: (data: string, publicKey: string) => Promise<string>;
563
- decrypt: (encryptedData: string, privateKey: string) => Promise<string>;
561
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
562
+ encode: (data: string) => Promise<string>;
563
+ decode: (encryptedBase64: string) => Promise<string>;
564
564
  };
565
565
  };
566
566
  declare const dNode: {
567
567
  Ecdh: typeof import("./modules/node/ecdh").default;
568
568
  EcdhWeb: typeof import("./modules/node/ecdh-web").default;
569
569
  Socks5: typeof import("./modules/node/socks5").Socks5;
570
+ rsa: {
571
+ generateKeys: () => Promise<{
572
+ publicKey: string;
573
+ publicKeyPem: string;
574
+ privateKey: string;
575
+ privateKeyPem: string;
576
+ }>;
577
+ encrypt: (data: string, publicKey: string) => Promise<string>;
578
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
579
+ encode: (data: string) => Promise<string>;
580
+ decode: (encryptedBase64: string) => Promise<string>;
581
+ };
570
582
  getSystemProxy: () => Promise<{
571
583
  ipaddress: string;
572
584
  port: number;
@@ -968,10 +980,10 @@ declare const _default: {
968
980
  privateKey: string;
969
981
  privateKeyPem: string;
970
982
  }>;
971
- format_pem_key: (pemContent?: string, label?: string) => string;
972
- format_pem_content: (buffer: ArrayBuffer) => string;
973
983
  encrypt: (data: string, publicKey: string) => Promise<string>;
974
- decrypt: (encryptedData: string, privateKey: string) => Promise<string>;
984
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
985
+ encode: (data: string) => Promise<string>;
986
+ decode: (encryptedBase64: string) => Promise<string>;
975
987
  };
976
988
  };
977
989
  export default _default;
@@ -0,0 +1,15 @@
1
+ declare const _default: {
2
+ RSAPublicKey: string;
3
+ RSAPrivateKey: string;
4
+ generateKeys: (subtle: any) => Promise<{
5
+ publicKey: string;
6
+ publicKeyPem: string;
7
+ privateKey: string;
8
+ privateKeyPem: string;
9
+ }>;
10
+ format_pem_key: (pemContent?: string, label?: string) => string;
11
+ format_pem_content: (buffer: ArrayBuffer) => string;
12
+ encrypt: (subtle: any, data: string, publicKey: string) => Promise<string>;
13
+ decrypt: (subtle: any, encryptedBase64: string, privateKey: string) => Promise<string>;
14
+ };
15
+ export default _default;
@@ -5,6 +5,18 @@ declare const _default: {
5
5
  Ecdh: typeof Ecdh;
6
6
  EcdhWeb: typeof EcdhWeb;
7
7
  Socks5: typeof Socks5;
8
+ rsa: {
9
+ generateKeys: () => Promise<{
10
+ publicKey: string;
11
+ publicKeyPem: string;
12
+ privateKey: string;
13
+ privateKeyPem: string;
14
+ }>;
15
+ encrypt: (data: string, publicKey: string) => Promise<string>;
16
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
17
+ encode: (data: string) => Promise<string>;
18
+ decode: (encryptedBase64: string) => Promise<string>;
19
+ };
8
20
  getSystemProxy: () => Promise<{
9
21
  ipaddress: string;
10
22
  port: number;
@@ -0,0 +1,13 @@
1
+ declare const _default: {
2
+ generateKeys: () => Promise<{
3
+ publicKey: string;
4
+ publicKeyPem: string;
5
+ privateKey: string;
6
+ privateKeyPem: string;
7
+ }>;
8
+ encrypt: (data: string, publicKey: string) => Promise<string>;
9
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
10
+ encode: (data: string) => Promise<string>;
11
+ decode: (encryptedBase64: string) => Promise<string>;
12
+ };
13
+ export default _default;
@@ -5,9 +5,9 @@ declare const _default: {
5
5
  privateKey: string;
6
6
  privateKeyPem: string;
7
7
  }>;
8
- format_pem_key: (pemContent?: string, label?: string) => string;
9
- format_pem_content: (buffer: ArrayBuffer) => string;
10
8
  encrypt: (data: string, publicKey: string) => Promise<string>;
11
- decrypt: (encryptedData: string, privateKey: string) => Promise<string>;
9
+ decrypt: (encryptedBase64: string, privateKey: string) => Promise<string>;
10
+ encode: (data: string) => Promise<string>;
11
+ decode: (encryptedBase64: string) => Promise<string>;
12
12
  };
13
13
  export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddan-js",
3
- "version": "2.7.6",
3
+ "version": "2.7.8",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "ddan-js",