@mtkruto/browser 0.132.1 → 0.134.0

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.
Files changed (69) hide show
  1. package/esm/0_deps.d.ts +1 -0
  2. package/esm/0_deps.d.ts.map +1 -1
  3. package/esm/0_deps.js +1 -0
  4. package/esm/1_utilities.d.ts +1 -0
  5. package/esm/1_utilities.d.ts.map +1 -1
  6. package/esm/1_utilities.js +1 -0
  7. package/esm/2_connection.d.ts +1 -0
  8. package/esm/2_connection.d.ts.map +1 -1
  9. package/esm/2_connection.js +1 -0
  10. package/esm/3_transport.d.ts +1 -0
  11. package/esm/3_transport.d.ts.map +1 -1
  12. package/esm/3_transport.js +1 -0
  13. package/esm/connection/1_connection_socks5.d.ts +36 -0
  14. package/esm/connection/1_connection_socks5.d.ts.map +1 -0
  15. package/esm/connection/1_connection_socks5.js +192 -0
  16. package/esm/connection/1_connection_socks5.node.d.ts +35 -0
  17. package/esm/connection/1_connection_socks5.node.d.ts.map +1 -0
  18. package/esm/connection/1_connection_socks5.node.js +214 -0
  19. package/esm/connection/1_connection_tcp.d.ts.map +1 -1
  20. package/esm/connection/1_connection_tcp.js +0 -1
  21. package/esm/connection/1_connection_tls.js +1 -1
  22. package/esm/deps/jsr.io/@std/net/1.0.6/unstable_ip.d.ts +103 -0
  23. package/esm/deps/jsr.io/@std/net/1.0.6/unstable_ip.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@std/net/1.0.6/unstable_ip.js +296 -0
  25. package/esm/transport/2_transport_provider_mtproxy.d.ts +1 -1
  26. package/esm/transport/2_transport_provider_mtproxy.d.ts.map +1 -1
  27. package/esm/transport/2_transport_provider_mtproxy.js +3 -3
  28. package/esm/transport/2_transport_provider_socks5.d.ts +27 -0
  29. package/esm/transport/2_transport_provider_socks5.d.ts.map +1 -0
  30. package/esm/transport/2_transport_provider_socks5.js +45 -0
  31. package/esm/types/5_star_transaction.js +2 -2
  32. package/esm/utilities/0_ip.d.ts +22 -0
  33. package/esm/utilities/0_ip.d.ts.map +1 -0
  34. package/esm/utilities/0_ip.js +73 -0
  35. package/package.json +1 -1
  36. package/script/0_deps.d.ts +1 -0
  37. package/script/0_deps.d.ts.map +1 -1
  38. package/script/0_deps.js +4 -1
  39. package/script/1_utilities.d.ts +1 -0
  40. package/script/1_utilities.d.ts.map +1 -1
  41. package/script/1_utilities.js +1 -0
  42. package/script/2_connection.d.ts +1 -0
  43. package/script/2_connection.d.ts.map +1 -1
  44. package/script/2_connection.js +1 -0
  45. package/script/3_transport.d.ts +1 -0
  46. package/script/3_transport.d.ts.map +1 -1
  47. package/script/3_transport.js +1 -0
  48. package/script/connection/1_connection_socks5.d.ts +36 -0
  49. package/script/connection/1_connection_socks5.d.ts.map +1 -0
  50. package/script/connection/1_connection_socks5.js +196 -0
  51. package/script/connection/1_connection_socks5.node.d.ts +35 -0
  52. package/script/connection/1_connection_socks5.node.d.ts.map +1 -0
  53. package/script/connection/1_connection_socks5.node.js +218 -0
  54. package/script/connection/1_connection_tcp.d.ts.map +1 -1
  55. package/script/connection/1_connection_tcp.js +0 -1
  56. package/script/connection/1_connection_tls.js +1 -1
  57. package/script/deps/jsr.io/@std/net/1.0.6/unstable_ip.d.ts +103 -0
  58. package/script/deps/jsr.io/@std/net/1.0.6/unstable_ip.d.ts.map +1 -0
  59. package/script/deps/jsr.io/@std/net/1.0.6/unstable_ip.js +303 -0
  60. package/script/transport/2_transport_provider_mtproxy.d.ts +1 -1
  61. package/script/transport/2_transport_provider_mtproxy.d.ts.map +1 -1
  62. package/script/transport/2_transport_provider_mtproxy.js +3 -3
  63. package/script/transport/2_transport_provider_socks5.d.ts +27 -0
  64. package/script/transport/2_transport_provider_socks5.d.ts.map +1 -0
  65. package/script/transport/2_transport_provider_socks5.js +48 -0
  66. package/script/types/5_star_transaction.js +1 -1
  67. package/script/utilities/0_ip.d.ts +22 -0
  68. package/script/utilities/0_ip.d.ts.map +1 -0
  69. package/script/utilities/0_ip.js +77 -0
package/esm/0_deps.d.ts CHANGED
@@ -32,6 +32,7 @@ export { pooledMap } from "./deps/jsr.io/@std/async/1.2.0/pool.js";
32
32
  export { concat } from "./deps/jsr.io/@std/bytes/1.0.6/concat.js";
33
33
  export { equals } from "./deps/jsr.io/@std/bytes/1.0.6/equals.js";
34
34
  export { startsWith } from "./deps/jsr.io/@std/bytes/1.0.6/starts_with.js";
35
+ export { isIPv4, isIPv6 } from "./deps/jsr.io/@std/net/1.0.6/unstable_ip.js";
35
36
  export { LruCache } from "./deps/jsr.io/@std/cache/0.2.2/lru_cache.js";
36
37
  export { writeAll } from "./deps/jsr.io/@std/io/0.225.3/write_all.js";
37
38
  export { format } from "./deps/jsr.io/@std/datetime/0.225.7/format.js";
@@ -1 +1 @@
1
- {"version":3,"file":"0_deps.d.ts","sourceRoot":"","sources":["../src/0_deps.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,4CAA4C,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,2CAA2C,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,4CAA4C,CAAC;AAC1E,OAAO,EAAE,WAAW,EAAE,MAAM,iDAAiD,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,qDAAqD,CAAC;AAErF,OAAO,EAAE,IAAI,EAAE,MAAM,uCAAuC,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,0CAA0C,CAAC;AACnE,OAAO,EAAE,QAAQ,EAAE,MAAM,2CAA2C,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,MAAM,8CAA8C,CAAC;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,8CAA8C,CAAC;AAE1E,OAAO,EAAE,KAAK,EAAE,MAAM,yCAAyC,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,wCAAwC,CAAC;AAEnE,OAAO,EAAE,MAAM,EAAE,MAAM,0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ytes/1.0.6/concat.js";
33
33
  export { equals } from "./deps/jsr.io/@std/bytes/1.0.6/equals.js";
34
34
  export { startsWith } from "./deps/jsr.io/@std/bytes/1.0.6/starts_with.js";
35
+ export { isIPv4, isIPv6 } from "./deps/jsr.io/@std/net/1.0.6/unstable_ip.js";
35
36
  export { LruCache } from "./deps/jsr.io/@std/cache/0.2.2/lru_cache.js";
36
37
  export { writeAll } from "./deps/jsr.io/@std/io/0.225.3/write_all.js";
37
38
  export { format } from "./deps/jsr.io/@std/datetime/0.225.7/format.js";
@@ -19,6 +19,7 @@
19
19
  */
20
20
  import "./_dnt.polyfills.js";
21
21
  export * from "./utilities/0_int.js";
22
+ export * from "./utilities/0_ip.js";
22
23
  export * from "./utilities/0_color.js";
23
24
  export * from "./utilities/0_gzip.js";
24
25
  export * from "./utilities/0_hash.js";
@@ -1 +1 @@
1
- {"version":3,"file":"1_utilities.d.ts","sourceRoot":"","sources":["../src/1_utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,qBAAqB,CAAC;AAG7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"1_utilities.d.ts","sourceRoot":"","sources":["../src/1_utilities.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,qBAAqB,CAAC;AAG7B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,yBAAyB,CAAC;AACxC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,sBAAsB,CAAC;AACrC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,wBAAwB,CAAC"}
@@ -19,6 +19,7 @@
19
19
  */
20
20
  import "./_dnt.polyfills.js";
21
21
  export * from "./utilities/0_int.js";
22
+ export * from "./utilities/0_ip.js";
22
23
  export * from "./utilities/0_color.js";
23
24
  export * from "./utilities/0_gzip.js";
24
25
  export * from "./utilities/0_hash.js";
@@ -18,6 +18,7 @@
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
  */
20
20
  export * from "./connection/0_connection.js";
21
+ export * from "./connection/1_connection_socks5.js";
21
22
  export * from "./connection/1_connection_tcp.js";
22
23
  export * from "./connection/1_connection_tls.js";
23
24
  export * from "./connection/1_connection_web_socket.js";
@@ -1 +1 @@
1
- {"version":3,"file":"2_connection.d.ts","sourceRoot":"","sources":["../src/2_connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,cAAc,8BAA8B,CAAC;AAC7C,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC"}
1
+ {"version":3,"file":"2_connection.d.ts","sourceRoot":"","sources":["../src/2_connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,cAAc,8BAA8B,CAAC;AAC7C,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kCAAkC,CAAC;AACjD,cAAc,yCAAyC,CAAC"}
@@ -18,6 +18,7 @@
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
19
  */
20
20
  export * from "./connection/0_connection.js";
21
+ export * from "./connection/1_connection_socks5.js";
21
22
  export * from "./connection/1_connection_tcp.js";
22
23
  export * from "./connection/1_connection_tls.js";
23
24
  export * from "./connection/1_connection_web_socket.js";
@@ -22,6 +22,7 @@ export * from "./transport/1_transport_abridged.js";
22
22
  export * from "./transport/1_transport_intermediate.js";
23
23
  export * from "./transport/1_transport_provider.js";
24
24
  export * from "./transport/2_transport_provider_mtproxy.js";
25
+ export * from "./transport/2_transport_provider_socks5.js";
25
26
  export * from "./transport/2_transport_provider_tcp.js";
26
27
  export * from "./transport/2_transport_provider_web_socket.js";
27
28
  //# sourceMappingURL=3_transport.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"3_transport.d.ts","sourceRoot":"","sources":["../src/3_transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,qCAAqC,CAAC;AACpD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC"}
1
+ {"version":3,"file":"3_transport.d.ts","sourceRoot":"","sources":["../src/3_transport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,cAAc,4BAA4B,CAAC;AAC3C,cAAc,qCAAqC,CAAC;AACpD,cAAc,yCAAyC,CAAC;AACxD,cAAc,qCAAqC,CAAC;AACpD,cAAc,6CAA6C,CAAC;AAC5D,cAAc,4CAA4C,CAAC;AAC3D,cAAc,yCAAyC,CAAC;AACxD,cAAc,gDAAgD,CAAC"}
@@ -22,5 +22,6 @@ export * from "./transport/1_transport_abridged.js";
22
22
  export * from "./transport/1_transport_intermediate.js";
23
23
  export * from "./transport/1_transport_provider.js";
24
24
  export * from "./transport/2_transport_provider_mtproxy.js";
25
+ export * from "./transport/2_transport_provider_socks5.js";
25
26
  export * from "./transport/2_transport_provider_tcp.js";
26
27
  export * from "./transport/2_transport_provider_web_socket.js";
@@ -0,0 +1,36 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2026 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+ import type { Connection } from "./0_connection.js";
21
+ export interface ConnectionSocks5Params {
22
+ username: string;
23
+ password: string;
24
+ }
25
+ export declare class ConnectionSocks5 implements Connection {
26
+ #private;
27
+ stateChangeHandler?: Connection["stateChangeHandler"];
28
+ callback?: Connection["callback"];
29
+ constructor(hostname: string, port: number, socks5Hostname: string, socks5Port: number, params?: ConnectionSocks5Params);
30
+ get isConnected(): boolean;
31
+ open(): Promise<void>;
32
+ read(p: Uint8Array): Promise<void>;
33
+ write(p: Uint8Array): Promise<void>;
34
+ close(): void;
35
+ }
36
+ //# sourceMappingURL=1_connection_socks5.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1_connection_socks5.d.ts","sourceRoot":"","sources":["../../src/connection/1_connection_socks5.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAiBpD,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,gBAAiB,YAAW,UAAU;;IAYjD,kBAAkB,CAAC,EAAE,UAAU,CAAC,oBAAoB,CAAC,CAAC;IACtD,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC;gBAEtB,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,sBAAsB;IAQvH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAQK,IAAI;IAuFJ,IAAI,CAAC,CAAC,EAAE,UAAU;IAyBlB,KAAK,CAAC,CAAC,EAAE,UAAU;IAsBzB,KAAK;CAMN"}
@@ -0,0 +1,192 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2026 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+ import { concat, isIPv4, isIPv6 } from "../0_deps.js";
21
+ import { ConnectionError } from "../0_errors.js";
22
+ import { encodeText, getLogger, ipv4ToBytes, ipv6ToBytes, Mutex } from "../1_utilities.js";
23
+ const VERSION_SOCKS = 5;
24
+ const VERSION_USERNAME_PASSWORD_AUTH = 1;
25
+ const AUTH_MEHTOD_NONE = 0;
26
+ const AUTH_METHOD_USERNAME_PASSWORD = 2;
27
+ const RESULT_SUCCESS = 0;
28
+ const COMMAND_CONNECT = 1;
29
+ const ADDRESS_TYPE_4 = 1;
30
+ const ADDRESS_TYPE_DOMAIN_NAME = 3;
31
+ const ADDRESS_TYPE_6 = 4;
32
+ const L = getLogger("ConnectionSocks5");
33
+ export class ConnectionSocks5 {
34
+ #hostname;
35
+ #port;
36
+ #socks5Hostname;
37
+ #socks5Port;
38
+ #credentials;
39
+ #connection;
40
+ #rMutex = new Mutex();
41
+ #wMutex = new Mutex();
42
+ #canRead = false;
43
+ #canWrite = false;
44
+ stateChangeHandler;
45
+ callback;
46
+ constructor(hostname, port, socks5Hostname, socks5Port, params) {
47
+ this.#hostname = hostname.slice(0, 255);
48
+ this.#port = port;
49
+ this.#socks5Hostname = socks5Hostname;
50
+ this.#socks5Port = socks5Port;
51
+ this.#credentials = params?.username && params.password ? { username: params.username.slice(0, 255), password: params.password.slice(0, 255) } : undefined;
52
+ }
53
+ get isConnected() {
54
+ return !!this.#connection && this.#canRead && this.#canWrite;
55
+ }
56
+ #assertConnected() {
57
+ if (!this.isConnected) {
58
+ throw new ConnectionError("The connection is not open.");
59
+ }
60
+ }
61
+ async open() {
62
+ if (this.isConnected) {
63
+ return;
64
+ }
65
+ const connection = await Deno.connect({
66
+ hostname: this.#socks5Hostname,
67
+ port: this.#socks5Port,
68
+ });
69
+ connection.setNoDelay(true);
70
+ connection.setKeepAlive(true);
71
+ this.#canRead = this.#canWrite = true;
72
+ this.#connection = connection;
73
+ const header = new Uint8Array([VERSION_SOCKS, 2, AUTH_MEHTOD_NONE, AUTH_METHOD_USERNAME_PASSWORD]);
74
+ await this.write(header);
75
+ const negotiation = new Uint8Array(2);
76
+ await this.read(negotiation);
77
+ if (negotiation[0] !== VERSION_SOCKS || (negotiation[1] !== AUTH_MEHTOD_NONE && negotiation[1] !== AUTH_METHOD_USERNAME_PASSWORD)) {
78
+ throw new ConnectionError("Negotiation with SOCKS5 server failed.");
79
+ }
80
+ if (negotiation[1] === AUTH_METHOD_USERNAME_PASSWORD && !this.#credentials) {
81
+ throw new ConnectionError("Username and password are required for connecting to SOCKS5 server.");
82
+ }
83
+ else if (this.#credentials) {
84
+ await this.write(concat([new Uint8Array([VERSION_USERNAME_PASSWORD_AUTH, this.#credentials.username.length]), encodeText(this.#credentials.username), new Uint8Array([this.#credentials.password.length]), encodeText(this.#credentials.password)]));
85
+ const status = new Uint8Array(2);
86
+ await this.read(status);
87
+ if (status[0] !== VERSION_USERNAME_PASSWORD_AUTH || status[1] !== RESULT_SUCCESS) {
88
+ throw new ConnectionError("SOCKS5 username and password authentication failed.");
89
+ }
90
+ }
91
+ let hostnameType;
92
+ let hostname;
93
+ if (isIPv4(this.#hostname)) {
94
+ hostnameType = ADDRESS_TYPE_4;
95
+ hostname = new Uint8Array(ipv4ToBytes(this.#hostname));
96
+ }
97
+ else if (isIPv6(this.#hostname)) {
98
+ hostnameType = ADDRESS_TYPE_6;
99
+ hostname = new Uint8Array(ipv6ToBytes(this.#hostname));
100
+ }
101
+ else {
102
+ hostnameType = ADDRESS_TYPE_DOMAIN_NAME;
103
+ hostname = concat([new Uint8Array([this.#hostname.length]), encodeText(this.#hostname)]);
104
+ }
105
+ const address = concat([new Uint8Array([hostnameType]), hostname, new Uint8Array(2)]);
106
+ new DataView(address.buffer).setUint16(hostname.byteLength + 1, this.#port);
107
+ await this.write(concat([new Uint8Array([VERSION_SOCKS, COMMAND_CONNECT, 0]), address]));
108
+ const status = new Uint8Array(3);
109
+ await this.read(status);
110
+ if (status[0] !== VERSION_SOCKS || status[1] !== RESULT_SUCCESS) {
111
+ throw new ConnectionError("Connection with SOCKS5 server failed.");
112
+ }
113
+ const addressType = new Uint8Array(1);
114
+ await this.read(addressType);
115
+ switch (addressType[0]) {
116
+ case ADDRESS_TYPE_4: {
117
+ const ipv4 = new Uint8Array(4);
118
+ await this.read(ipv4);
119
+ break;
120
+ }
121
+ case ADDRESS_TYPE_6: {
122
+ const ipv4 = new Uint8Array(16);
123
+ await this.read(ipv4);
124
+ break;
125
+ }
126
+ case ADDRESS_TYPE_DOMAIN_NAME: {
127
+ const length = new Uint8Array(1);
128
+ await this.read(length);
129
+ const domainName = new Uint8Array(length[0]);
130
+ await this.read(domainName);
131
+ break;
132
+ }
133
+ }
134
+ const port = new Uint8Array(2);
135
+ await this.read(port);
136
+ this.stateChangeHandler?.(true);
137
+ L.debug("connected to", this.#hostname, "port", this.#port, "through socks5 server", this.#socks5Hostname, "port", this.#socks5Port);
138
+ }
139
+ async read(p) {
140
+ this.#assertConnected();
141
+ const unlock = await this.#rMutex.lock();
142
+ let offset = 0;
143
+ try {
144
+ do {
145
+ const read = await this.#connection.read(p.subarray(offset));
146
+ if (read === null) {
147
+ this.#canRead = false;
148
+ this.stateChangeHandler?.(false);
149
+ throw new ConnectionError("The connection was closed.");
150
+ }
151
+ offset += read;
152
+ } while (offset < p.byteLength);
153
+ }
154
+ catch {
155
+ this.#canRead = false;
156
+ this.stateChangeHandler?.(false);
157
+ throw new ConnectionError("The connection was closed.");
158
+ }
159
+ finally {
160
+ unlock();
161
+ }
162
+ }
163
+ async write(p) {
164
+ this.#assertConnected();
165
+ const unlock = await this.#wMutex.lock();
166
+ try {
167
+ this.#assertConnected();
168
+ let written = 0;
169
+ while (written < p.byteLength) {
170
+ try {
171
+ const wrote = await this.#connection.write(p.subarray(written));
172
+ this.callback?.write(wrote);
173
+ written += wrote;
174
+ }
175
+ catch {
176
+ this.#canWrite = false;
177
+ this.stateChangeHandler?.(false);
178
+ throw new ConnectionError("The connection was closed.");
179
+ }
180
+ }
181
+ }
182
+ finally {
183
+ unlock();
184
+ }
185
+ }
186
+ close() {
187
+ this.#assertConnected();
188
+ this.#connection.close();
189
+ this.#canRead = this.#canWrite = false;
190
+ this.#connection = undefined;
191
+ }
192
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2026 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+ import type { Connection } from "./0_connection.js";
21
+ export interface ConnectionSocks5Params {
22
+ username: string;
23
+ password: string;
24
+ }
25
+ export declare class ConnectionSocks5 implements Connection {
26
+ #private;
27
+ stateChangeHandler?: Connection["stateChangeHandler"];
28
+ constructor(hostname: string, port: number, socks5Hostname: string, socks5Port: number, params?: ConnectionSocks5Params);
29
+ open(): Promise<void> | undefined;
30
+ get isConnected(): boolean;
31
+ read(p: Uint8Array): Promise<void>;
32
+ write(p: Uint8Array): Promise<void>;
33
+ close(): void;
34
+ }
35
+ //# sourceMappingURL=1_connection_socks5.node.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1_connection_socks5.node.d.ts","sourceRoot":"","sources":["../../src/connection/1_connection_socks5.node.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAMH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAkBpD,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,gBAAiB,YAAW,UAAU;;IAejD,kBAAkB,CAAC,EAAE,UAAU,CAAC,oBAAoB,CAAC,CAAC;gBAE1C,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,sBAAsB;IAevH,IAAI;IAoHJ,IAAI,WAAW,YAEd;IAQK,IAAI,CAAC,CAAC,EAAE,UAAU;IAclB,KAAK,CAAC,CAAC,EAAE,UAAU;IA0BzB,KAAK;CAKN"}
@@ -0,0 +1,214 @@
1
+ /**
2
+ * MTKruto - Cross-runtime JavaScript library for building Telegram clients
3
+ * Copyright (C) 2023-2026 Roj <https://roj.im/>
4
+ *
5
+ * This file is part of MTKruto.
6
+ *
7
+ * This program is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU Lesser General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public License
18
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
+ */
20
+ import { Socket } from "node:net";
21
+ import { concat, isIPv4, isIPv6 } from "../0_deps.js";
22
+ import { ConnectionError } from "../0_errors.js";
23
+ import { encodeText, getLogger, ipv4ToBytes, ipv6ToBytes, Mutex } from "../1_utilities.js";
24
+ const VERSION_SOCKS = 5;
25
+ const VERSION_USERNAME_PASSWORD_AUTH = 1;
26
+ const AUTH_MEHTOD_NONE = 0;
27
+ const AUTH_METHOD_USERNAME_PASSWORD = 2;
28
+ const RESULT_SUCCESS = 0;
29
+ const COMMAND_CONNECT = 1;
30
+ const ADDRESS_TYPE_4 = 1;
31
+ const ADDRESS_TYPE_DOMAIN_NAME = 3;
32
+ const ADDRESS_TYPE_6 = 4;
33
+ const L = getLogger("ConnectionTCP");
34
+ const errConnectionNotOpen = new ConnectionError("The connection is not open.");
35
+ export class ConnectionSocks5 {
36
+ #hostname;
37
+ #port;
38
+ #socks5Hostname;
39
+ #socks5Port;
40
+ #credentials;
41
+ #socket;
42
+ #rMutex = new Mutex();
43
+ #wMutex = new Mutex();
44
+ #buffer = new Array();
45
+ #nextResolve = null;
46
+ stateChangeHandler;
47
+ constructor(hostname, port, socks5Hostname, socks5Port, params) {
48
+ this.#hostname = hostname.slice(0, 255);
49
+ this.#port = port;
50
+ this.#socks5Hostname = socks5Hostname;
51
+ this.#socks5Port = socks5Port;
52
+ this.#credentials = params?.username && params.password ? { username: params.username.slice(0, 255), password: params.password.slice(0, 255) } : undefined;
53
+ }
54
+ #rejectRead() {
55
+ if (this.#nextResolve !== null) {
56
+ this.#nextResolve[1].reject(errConnectionNotOpen);
57
+ this.#nextResolve = null;
58
+ }
59
+ }
60
+ open() {
61
+ if (this.isConnected) {
62
+ return;
63
+ }
64
+ this.#socket = new Socket();
65
+ this.#socket.on("close", () => {
66
+ this.#rejectRead();
67
+ this.stateChangeHandler?.(false);
68
+ });
69
+ this.#socket.on("data", (data) => {
70
+ if (typeof data === "string") {
71
+ return;
72
+ }
73
+ for (const byte of data) {
74
+ this.#buffer.push(byte);
75
+ }
76
+ if (this.#nextResolve !== null && this.#buffer.length >= this.#nextResolve[0]) {
77
+ const resolve = this.#nextResolve[1].resolve;
78
+ this.#nextResolve = null;
79
+ resolve();
80
+ }
81
+ });
82
+ return new Promise((resolve, reject) => {
83
+ this.#socket.connect(this.#socks5Port, this.#socks5Hostname);
84
+ this.#socket.once("error", reject);
85
+ this.#socket.once("connect", async () => {
86
+ this.#socket.off("error", reject);
87
+ try {
88
+ const header = new Uint8Array([VERSION_SOCKS, 2, AUTH_MEHTOD_NONE, AUTH_METHOD_USERNAME_PASSWORD]);
89
+ await this.write(header);
90
+ const negotiation = new Uint8Array(2);
91
+ await this.read(negotiation);
92
+ if (negotiation[0] !== VERSION_SOCKS || (negotiation[1] !== AUTH_MEHTOD_NONE && negotiation[1] !== AUTH_METHOD_USERNAME_PASSWORD)) {
93
+ throw new ConnectionError("Negotiation with SOCKS5 server failed.");
94
+ }
95
+ if (negotiation[1] === AUTH_METHOD_USERNAME_PASSWORD && !this.#credentials) {
96
+ throw new ConnectionError("Username and password are required for connecting to SOCKS5 server.");
97
+ }
98
+ else if (this.#credentials) {
99
+ await this.write(concat([new Uint8Array([VERSION_USERNAME_PASSWORD_AUTH, this.#credentials.username.length]), encodeText(this.#credentials.username), new Uint8Array([this.#credentials.password.length]), encodeText(this.#credentials.password)]));
100
+ const status = new Uint8Array(2);
101
+ await this.read(status);
102
+ if (status[0] !== VERSION_USERNAME_PASSWORD_AUTH || status[1] !== RESULT_SUCCESS) {
103
+ throw new ConnectionError("SOCKS5 username and password authentication failed.");
104
+ }
105
+ }
106
+ let hostnameType;
107
+ let hostname;
108
+ if (isIPv4(this.#hostname)) {
109
+ hostnameType = ADDRESS_TYPE_4;
110
+ hostname = new Uint8Array(ipv4ToBytes(this.#hostname));
111
+ }
112
+ else if (isIPv6(this.#hostname)) {
113
+ hostnameType = ADDRESS_TYPE_6;
114
+ hostname = new Uint8Array(ipv6ToBytes(this.#hostname));
115
+ }
116
+ else {
117
+ hostnameType = ADDRESS_TYPE_DOMAIN_NAME;
118
+ hostname = concat([new Uint8Array([this.#hostname.length]), encodeText(this.#hostname)]);
119
+ }
120
+ const address = concat([new Uint8Array([hostnameType]), hostname, new Uint8Array(2)]);
121
+ new DataView(address.buffer).setUint16(hostname.byteLength + 1, this.#port);
122
+ await this.write(concat([new Uint8Array([VERSION_SOCKS, COMMAND_CONNECT, 0]), address]));
123
+ const status = new Uint8Array(3);
124
+ await this.read(status);
125
+ if (status[0] !== VERSION_SOCKS || status[1] !== RESULT_SUCCESS) {
126
+ throw new ConnectionError("Connection with SOCKS5 server failed.");
127
+ }
128
+ const addressType = new Uint8Array(1);
129
+ await this.read(addressType);
130
+ switch (addressType[0]) {
131
+ case ADDRESS_TYPE_4: {
132
+ const ipv4 = new Uint8Array(4);
133
+ await this.read(ipv4);
134
+ break;
135
+ }
136
+ case ADDRESS_TYPE_6: {
137
+ const ipv4 = new Uint8Array(16);
138
+ await this.read(ipv4);
139
+ break;
140
+ }
141
+ case ADDRESS_TYPE_DOMAIN_NAME: {
142
+ const length = new Uint8Array(1);
143
+ await this.read(length);
144
+ const domainName = new Uint8Array(length[0]);
145
+ await this.read(domainName);
146
+ break;
147
+ }
148
+ }
149
+ const port = new Uint8Array(2);
150
+ await this.read(port);
151
+ }
152
+ catch (err) {
153
+ reject(err);
154
+ return;
155
+ }
156
+ resolve();
157
+ this.stateChangeHandler?.(true);
158
+ L.debug("connected to", this.#hostname, "port", this.#port, "through socks5 server", this.#socks5Hostname, "port", this.#socks5Port);
159
+ });
160
+ });
161
+ }
162
+ get isConnected() {
163
+ return this.#socket?.readyState === "open";
164
+ }
165
+ #assertConnected() {
166
+ if (!this.isConnected) {
167
+ throw errConnectionNotOpen;
168
+ }
169
+ }
170
+ async read(p) {
171
+ this.#assertConnected();
172
+ const unlock = await this.#rMutex.lock();
173
+ try {
174
+ this.#assertConnected();
175
+ if (this.#buffer.length < p.length) {
176
+ await new Promise((resolve, reject) => this.#nextResolve = [p.length, { resolve, reject }]);
177
+ }
178
+ p.set(this.#buffer.splice(0, p.length));
179
+ }
180
+ finally {
181
+ unlock();
182
+ }
183
+ }
184
+ async write(p) {
185
+ this.#assertConnected();
186
+ const unlock = await this.#wMutex.lock();
187
+ try {
188
+ this.#assertConnected();
189
+ try {
190
+ await new Promise((resolve, reject) => {
191
+ this.#socket.write(p, (err) => {
192
+ (err === undefined || err === null) ? resolve() : reject(err);
193
+ });
194
+ });
195
+ }
196
+ catch (err) {
197
+ if (!this.isConnected) {
198
+ throw errConnectionNotOpen;
199
+ }
200
+ else {
201
+ throw err;
202
+ }
203
+ }
204
+ }
205
+ finally {
206
+ unlock();
207
+ }
208
+ }
209
+ close() {
210
+ this.#assertConnected();
211
+ this.#socket.destroy();
212
+ this.#socket = undefined;
213
+ }
214
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"1_connection_tcp.d.ts","sourceRoot":"","sources":["../../src/connection/1_connection_tcp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD,qBAAa,aAAc,YAAW,UAAU;;IAQ9C,OAAO,EAAE,OAAO,IAAI,CAAC,OAAO,CAAgB;IAC5C,kBAAkB,CAAC,EAAE,UAAU,CAAC,oBAAoB,CAAC,CAAC;IACtD,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC;gBAEtB,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAK1C,IAAI,WAAW,IAAI,OAAO,CAGzB;IAQK,IAAI;IAiBJ,IAAI,CAAC,CAAC,EAAE,UAAU;IAyBlB,KAAK,CAAC,CAAC,EAAE,UAAU;IAsBzB,KAAK;CAMN"}
1
+ {"version":3,"file":"1_connection_tcp.d.ts","sourceRoot":"","sources":["../../src/connection/1_connection_tcp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAIpD,qBAAa,aAAc,YAAW,UAAU;;IAQ9C,OAAO,EAAE,OAAO,IAAI,CAAC,OAAO,CAAgB;IAC5C,kBAAkB,CAAC,EAAE,UAAU,CAAC,oBAAoB,CAAC,CAAC;IACtD,QAAQ,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC;gBAEtB,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAK1C,IAAI,WAAW,IAAI,OAAO,CAEzB;IAQK,IAAI;IAiBJ,IAAI,CAAC,CAAC,EAAE,UAAU;IAyBlB,KAAK,CAAC,CAAC,EAAE,UAAU;IAsBzB,KAAK;CAMN"}
@@ -36,7 +36,6 @@ export class ConnectionTCP {
36
36
  this.#port = port;
37
37
  }
38
38
  get isConnected() {
39
- Deno.errors.BrokenPipe;
40
39
  return !!this.#connection && this.#canRead && this.#canWrite;
41
40
  }
42
41
  #assertConnected() {
@@ -41,7 +41,7 @@ export class ConnectionTLS {
41
41
  constructor(hostname, port, secret) {
42
42
  this.#hostname = hostname;
43
43
  this.#port = port;
44
- this.#secret = (secret[0] === 0xDD || secret[0] === 0xEE) ? secret.slice(1) : secret;
44
+ this.#secret = secret.byteLength > 16 ? secret.slice(1) : secret;
45
45
  }
46
46
  get isConnected() {
47
47
  return !!this.#connection && this.#canRead && this.#canWrite;