@sip-protocol/sdk 0.7.3 → 0.7.4
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.
- package/LICENSE +21 -0
- package/README.md +267 -0
- package/dist/{TransportWebUSB-TQ7WZ4LE.mjs → TransportWebUSB-YQMAGJAJ.mjs} +12 -9
- package/dist/browser.d.mts +10 -4
- package/dist/browser.d.ts +10 -4
- package/dist/browser.js +47556 -19603
- package/dist/browser.mjs +628 -48
- package/dist/chunk-4GRJ5MAW.mjs +152 -0
- package/dist/chunk-5D7A3L3W.mjs +717 -0
- package/dist/chunk-64AYA5F5.mjs +7834 -0
- package/dist/chunk-GMDGB22A.mjs +379 -0
- package/dist/chunk-I534WKN7.mjs +328 -0
- package/dist/chunk-IBZVA5Y7.mjs +1003 -0
- package/dist/chunk-PRRZAWJE.mjs +223 -0
- package/dist/{chunk-UJCSKKID.mjs → chunk-XGB3TDIC.mjs} +13 -1
- package/dist/{chunk-3M3HNQCW.mjs → chunk-YWGJ77A2.mjs} +28656 -13103
- package/dist/{chunk-6WGN57S2.mjs → chunk-Z3K7W5S3.mjs} +48 -0
- package/dist/constants-LHAAUC2T.mjs +51 -0
- package/dist/dist-2OGQ7FED.mjs +3957 -0
- package/dist/dist-IFHPYLDX.mjs +254 -0
- package/dist/fulfillment_proof-ANHVPKTB.mjs +21 -0
- package/dist/funding_proof-ICFZ5LHY.mjs +21 -0
- package/dist/{index-DIBZHOOQ.d.ts → index-DXh2IGkz.d.ts} +21239 -10304
- package/dist/{index-8MQz13eJ.d.mts → index-DeE1ZzA4.d.mts} +21239 -10304
- package/dist/index.d.mts +9 -3
- package/dist/index.d.ts +9 -3
- package/dist/index.js +48396 -19623
- package/dist/index.mjs +537 -19
- package/dist/interface-Bf7w1PLW.d.mts +679 -0
- package/dist/interface-Bf7w1PLW.d.ts +679 -0
- package/dist/{noir-DKfEzWy9.d.mts → noir-kzbLVTei.d.mts} +31 -21
- package/dist/{noir-DKfEzWy9.d.ts → noir-kzbLVTei.d.ts} +31 -21
- package/dist/proofs/halo2.d.mts +151 -0
- package/dist/proofs/halo2.d.ts +151 -0
- package/dist/proofs/halo2.js +350 -0
- package/dist/proofs/halo2.mjs +11 -0
- package/dist/proofs/kimchi.d.mts +160 -0
- package/dist/proofs/kimchi.d.ts +160 -0
- package/dist/proofs/kimchi.js +431 -0
- package/dist/proofs/kimchi.mjs +13 -0
- package/dist/proofs/noir.d.mts +1 -1
- package/dist/proofs/noir.d.ts +1 -1
- package/dist/proofs/noir.js +74 -18
- package/dist/proofs/noir.mjs +84 -24
- package/dist/solana-U3MEGU7W.mjs +280 -0
- package/dist/validity_proof-3POXLPNY.mjs +21 -0
- package/package.json +54 -21
- package/src/adapters/index.ts +41 -0
- package/src/adapters/jupiter.ts +571 -0
- package/src/adapters/near-intents.ts +135 -0
- package/src/advisor/advisor.ts +653 -0
- package/src/advisor/index.ts +54 -0
- package/src/advisor/tools.ts +303 -0
- package/src/advisor/types.ts +164 -0
- package/src/chains/ethereum/announcement.ts +536 -0
- package/src/chains/ethereum/bnb-optimizations.ts +474 -0
- package/src/chains/ethereum/commitment.ts +522 -0
- package/src/chains/ethereum/constants.ts +462 -0
- package/src/chains/ethereum/deployment.ts +596 -0
- package/src/chains/ethereum/gas-estimation.ts +538 -0
- package/src/chains/ethereum/index.ts +268 -0
- package/src/chains/ethereum/optimizations.ts +614 -0
- package/src/chains/ethereum/privacy-adapter.ts +855 -0
- package/src/chains/ethereum/registry.ts +584 -0
- package/src/chains/ethereum/rpc.ts +905 -0
- package/src/chains/ethereum/stealth.ts +491 -0
- package/src/chains/ethereum/token.ts +790 -0
- package/src/chains/ethereum/transfer.ts +637 -0
- package/src/chains/ethereum/types.ts +456 -0
- package/src/chains/ethereum/viewing-key.ts +455 -0
- package/src/chains/near/commitment.ts +608 -0
- package/src/chains/near/constants.ts +284 -0
- package/src/chains/near/function-call.ts +871 -0
- package/src/chains/near/history.ts +654 -0
- package/src/chains/near/implicit-account.ts +840 -0
- package/src/chains/near/index.ts +393 -0
- package/src/chains/near/native-transfer.ts +658 -0
- package/src/chains/near/nep141.ts +775 -0
- package/src/chains/near/privacy-adapter.ts +889 -0
- package/src/chains/near/resolver.ts +971 -0
- package/src/chains/near/rpc.ts +1016 -0
- package/src/chains/near/stealth.ts +419 -0
- package/src/chains/near/types.ts +317 -0
- package/src/chains/near/viewing-key.ts +876 -0
- package/src/chains/solana/anchor-transfer.ts +386 -0
- package/src/chains/solana/commitment.ts +577 -0
- package/src/chains/solana/constants.ts +126 -12
- package/src/chains/solana/ephemeral-keys.ts +543 -0
- package/src/chains/solana/index.ts +252 -1
- package/src/chains/solana/key-derivation.ts +418 -0
- package/src/chains/solana/kit-compat.ts +334 -0
- package/src/chains/solana/optimizations.ts +560 -0
- package/src/chains/solana/privacy-adapter.ts +605 -0
- package/src/chains/solana/providers/generic.ts +47 -6
- package/src/chains/solana/providers/helius-enhanced-types.ts +336 -0
- package/src/chains/solana/providers/helius-enhanced.ts +623 -0
- package/src/chains/solana/providers/helius.ts +186 -33
- package/src/chains/solana/providers/index.ts +31 -0
- package/src/chains/solana/providers/interface.ts +61 -18
- package/src/chains/solana/providers/quicknode.ts +409 -0
- package/src/chains/solana/providers/triton.ts +426 -0
- package/src/chains/solana/providers/webhook.ts +338 -67
- package/src/chains/solana/rpc-client.ts +1150 -0
- package/src/chains/solana/scan.ts +83 -66
- package/src/chains/solana/sol-transfer.ts +732 -0
- package/src/chains/solana/spl-transfer.ts +886 -0
- package/src/chains/solana/stealth-scanner.ts +703 -0
- package/src/chains/solana/sunspot-verifier.ts +453 -0
- package/src/chains/solana/transaction-builder.ts +755 -0
- package/src/chains/solana/transfer.ts +74 -5
- package/src/chains/solana/types.ts +57 -6
- package/src/chains/solana/utils.ts +110 -0
- package/src/chains/solana/viewing-key.ts +807 -0
- package/src/compliance/fireblocks.ts +921 -0
- package/src/compliance/index.ts +23 -0
- package/src/compliance/range-sas.ts +398 -33
- package/src/config/endpoints.ts +100 -0
- package/src/crypto.ts +11 -8
- package/src/errors.ts +82 -0
- package/src/evm/erc4337-relayer.ts +830 -0
- package/src/evm/index.ts +47 -0
- package/src/fees/calculator.ts +396 -0
- package/src/fees/index.ts +87 -0
- package/src/fees/near-contract.ts +429 -0
- package/src/fees/types.ts +268 -0
- package/src/index.ts +686 -1
- package/src/intent.ts +6 -3
- package/src/logger.ts +324 -0
- package/src/network/index.ts +80 -0
- package/src/network/proxy.ts +691 -0
- package/src/optimizations/index.ts +541 -0
- package/src/oracle/types.ts +1 -0
- package/src/privacy-backends/arcium-types.ts +727 -0
- package/src/privacy-backends/arcium.ts +719 -0
- package/src/privacy-backends/combined-privacy.ts +866 -0
- package/src/privacy-backends/cspl-token.ts +595 -0
- package/src/privacy-backends/cspl-types.ts +512 -0
- package/src/privacy-backends/cspl.ts +907 -0
- package/src/privacy-backends/health.ts +488 -0
- package/src/privacy-backends/inco-types.ts +323 -0
- package/src/privacy-backends/inco.ts +616 -0
- package/src/privacy-backends/index.ts +254 -4
- package/src/privacy-backends/interface.ts +649 -6
- package/src/privacy-backends/lru-cache.ts +343 -0
- package/src/privacy-backends/magicblock.ts +458 -0
- package/src/privacy-backends/mock.ts +258 -0
- package/src/privacy-backends/privacycash.ts +13 -17
- package/src/privacy-backends/private-swap.ts +570 -0
- package/src/privacy-backends/rate-limiter.ts +683 -0
- package/src/privacy-backends/registry.ts +414 -2
- package/src/privacy-backends/router.ts +283 -3
- package/src/privacy-backends/shadowwire.ts +449 -0
- package/src/privacy-backends/sip-native.ts +3 -0
- package/src/privacy-logger.ts +191 -0
- package/src/production-safety.ts +373 -0
- package/src/proofs/aggregator.ts +1029 -0
- package/src/proofs/browser-composer.ts +1150 -0
- package/src/proofs/browser.ts +113 -25
- package/src/proofs/cache/index.ts +127 -0
- package/src/proofs/cache/interface.ts +545 -0
- package/src/proofs/cache/key-generator.ts +188 -0
- package/src/proofs/cache/lru-cache.ts +481 -0
- package/src/proofs/cache/multi-tier-cache.ts +575 -0
- package/src/proofs/cache/persistent-cache.ts +788 -0
- package/src/proofs/compliance-proof.ts +872 -0
- package/src/proofs/composer/base.ts +923 -0
- package/src/proofs/composer/index.ts +25 -0
- package/src/proofs/composer/interface.ts +518 -0
- package/src/proofs/composer/types.ts +383 -0
- package/src/proofs/converters/halo2.ts +452 -0
- package/src/proofs/converters/index.ts +208 -0
- package/src/proofs/converters/interface.ts +363 -0
- package/src/proofs/converters/kimchi.ts +462 -0
- package/src/proofs/converters/noir.ts +451 -0
- package/src/proofs/fallback.ts +888 -0
- package/src/proofs/halo2.ts +42 -0
- package/src/proofs/index.ts +471 -0
- package/src/proofs/interface.ts +13 -0
- package/src/proofs/kimchi.ts +42 -0
- package/src/proofs/lazy.ts +1004 -0
- package/src/proofs/mock.ts +25 -1
- package/src/proofs/noir.ts +110 -29
- package/src/proofs/orchestrator.ts +960 -0
- package/src/proofs/parallel/concurrency.ts +297 -0
- package/src/proofs/parallel/dependency-graph.ts +602 -0
- package/src/proofs/parallel/executor.ts +420 -0
- package/src/proofs/parallel/index.ts +131 -0
- package/src/proofs/parallel/interface.ts +685 -0
- package/src/proofs/parallel/worker-pool.ts +644 -0
- package/src/proofs/providers/halo2.ts +560 -0
- package/src/proofs/providers/index.ts +34 -0
- package/src/proofs/providers/kimchi.ts +641 -0
- package/src/proofs/validator.ts +881 -0
- package/src/proofs/verifier.ts +867 -0
- package/src/quantum/index.ts +112 -0
- package/src/quantum/winternitz-vault.ts +639 -0
- package/src/quantum/wots.ts +611 -0
- package/src/settlement/backends/direct-chain.ts +1 -0
- package/src/settlement/index.ts +9 -0
- package/src/settlement/router.ts +732 -46
- package/src/solana/index.ts +72 -0
- package/src/solana/jito-relayer.ts +687 -0
- package/src/solana/noir-verifier-types.ts +430 -0
- package/src/solana/noir-verifier.ts +816 -0
- package/src/stealth/address-derivation.ts +193 -0
- package/src/stealth/ed25519.ts +431 -0
- package/src/stealth/index.ts +233 -0
- package/src/stealth/meta-address.ts +221 -0
- package/src/stealth/secp256k1.ts +368 -0
- package/src/stealth/utils.ts +194 -0
- package/src/stealth.ts +50 -1504
- package/src/sync/index.ts +106 -0
- package/src/sync/manager.ts +504 -0
- package/src/sync/mock-provider.ts +318 -0
- package/src/sync/oblivious.ts +625 -0
- package/src/tokens/index.ts +15 -0
- package/src/tokens/registry.ts +301 -0
- package/src/utils/deprecation.ts +94 -0
- package/src/utils/index.ts +9 -0
- package/src/wallet/ethereum/index.ts +68 -0
- package/src/wallet/ethereum/metamask-privacy.ts +420 -0
- package/src/wallet/ethereum/multi-wallet.ts +646 -0
- package/src/wallet/ethereum/privacy-adapter.ts +700 -0
- package/src/wallet/ethereum/types.ts +3 -1
- package/src/wallet/ethereum/walletconnect-adapter.ts +675 -0
- package/src/wallet/hardware/index.ts +10 -0
- package/src/wallet/hardware/ledger-privacy.ts +414 -0
- package/src/wallet/index.ts +71 -0
- package/src/wallet/near/adapter.ts +626 -0
- package/src/wallet/near/index.ts +86 -0
- package/src/wallet/near/meteor-wallet.ts +1153 -0
- package/src/wallet/near/my-near-wallet.ts +790 -0
- package/src/wallet/near/wallet-selector.ts +702 -0
- package/src/wallet/solana/adapter.ts +6 -4
- package/src/wallet/solana/index.ts +13 -0
- package/src/wallet/solana/privacy-adapter.ts +567 -0
- package/src/wallet/sui/types.ts +6 -4
- package/src/zcash/rpc-client.ts +13 -6
- package/dist/chunk-2XIVXWHA.mjs +0 -1930
- package/dist/chunk-3INS3PR5.mjs +0 -884
- package/dist/chunk-3OVABDRH.mjs +0 -17096
- package/dist/chunk-7RFRWDCW.mjs +0 -1504
- package/dist/chunk-DLDWZFYC.mjs +0 -1495
- package/dist/chunk-E6SZWREQ.mjs +0 -57
- package/dist/chunk-F6F73W35.mjs +0 -16166
- package/dist/chunk-G33LB27A.mjs +0 -16166
- package/dist/chunk-HGU6HZRC.mjs +0 -231
- package/dist/chunk-L2K34JCU.mjs +0 -1496
- package/dist/chunk-OFDBEIEK.mjs +0 -16166
- package/dist/chunk-SF7YSLF5.mjs +0 -1515
- package/dist/chunk-SN4ZDTVW.mjs +0 -16166
- package/dist/chunk-WWUSGOXE.mjs +0 -17129
- package/dist/constants-VOI7BSLK.mjs +0 -27
- package/dist/index-B71aXVzk.d.ts +0 -13264
- package/dist/index-BYZbDjal.d.ts +0 -11390
- package/dist/index-CHB3KuOB.d.mts +0 -11859
- package/dist/index-CzWPI6Le.d.ts +0 -11859
- package/dist/index-pOIIuwfV.d.mts +0 -13264
- package/dist/index-xbWjohNq.d.mts +0 -11390
- package/dist/solana-4O4K45VU.mjs +0 -46
- package/dist/solana-5EMCTPTS.mjs +0 -46
- package/dist/solana-NDABAZ6P.mjs +0 -56
- package/dist/solana-Q4NAVBTS.mjs +0 -46
- package/dist/solana-ZYO63LY5.mjs +0 -46
|
@@ -0,0 +1,3957 @@
|
|
|
1
|
+
import {
|
|
2
|
+
require_dist,
|
|
3
|
+
require_src
|
|
4
|
+
} from "./chunk-IBZVA5Y7.mjs";
|
|
5
|
+
import {
|
|
6
|
+
__commonJS,
|
|
7
|
+
__require
|
|
8
|
+
} from "./chunk-XGB3TDIC.mjs";
|
|
9
|
+
|
|
10
|
+
// ../../node_modules/.pnpm/smart-buffer@4.2.0/node_modules/smart-buffer/build/utils.js
|
|
11
|
+
var require_utils = __commonJS({
|
|
12
|
+
"../../node_modules/.pnpm/smart-buffer@4.2.0/node_modules/smart-buffer/build/utils.js"(exports) {
|
|
13
|
+
"use strict";
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
var buffer_1 = __require("buffer");
|
|
16
|
+
var ERRORS = {
|
|
17
|
+
INVALID_ENCODING: "Invalid encoding provided. Please specify a valid encoding the internal Node.js Buffer supports.",
|
|
18
|
+
INVALID_SMARTBUFFER_SIZE: "Invalid size provided. Size must be a valid integer greater than zero.",
|
|
19
|
+
INVALID_SMARTBUFFER_BUFFER: "Invalid Buffer provided in SmartBufferOptions.",
|
|
20
|
+
INVALID_SMARTBUFFER_OBJECT: "Invalid SmartBufferOptions object supplied to SmartBuffer constructor or factory methods.",
|
|
21
|
+
INVALID_OFFSET: "An invalid offset value was provided.",
|
|
22
|
+
INVALID_OFFSET_NON_NUMBER: "An invalid offset value was provided. A numeric value is required.",
|
|
23
|
+
INVALID_LENGTH: "An invalid length value was provided.",
|
|
24
|
+
INVALID_LENGTH_NON_NUMBER: "An invalid length value was provived. A numeric value is required.",
|
|
25
|
+
INVALID_TARGET_OFFSET: "Target offset is beyond the bounds of the internal SmartBuffer data.",
|
|
26
|
+
INVALID_TARGET_LENGTH: "Specified length value moves cursor beyong the bounds of the internal SmartBuffer data.",
|
|
27
|
+
INVALID_READ_BEYOND_BOUNDS: "Attempted to read beyond the bounds of the managed data.",
|
|
28
|
+
INVALID_WRITE_BEYOND_BOUNDS: "Attempted to write beyond the bounds of the managed data."
|
|
29
|
+
};
|
|
30
|
+
exports.ERRORS = ERRORS;
|
|
31
|
+
function checkEncoding(encoding) {
|
|
32
|
+
if (!buffer_1.Buffer.isEncoding(encoding)) {
|
|
33
|
+
throw new Error(ERRORS.INVALID_ENCODING);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.checkEncoding = checkEncoding;
|
|
37
|
+
function isFiniteInteger(value) {
|
|
38
|
+
return typeof value === "number" && isFinite(value) && isInteger(value);
|
|
39
|
+
}
|
|
40
|
+
exports.isFiniteInteger = isFiniteInteger;
|
|
41
|
+
function checkOffsetOrLengthValue(value, offset) {
|
|
42
|
+
if (typeof value === "number") {
|
|
43
|
+
if (!isFiniteInteger(value) || value < 0) {
|
|
44
|
+
throw new Error(offset ? ERRORS.INVALID_OFFSET : ERRORS.INVALID_LENGTH);
|
|
45
|
+
}
|
|
46
|
+
} else {
|
|
47
|
+
throw new Error(offset ? ERRORS.INVALID_OFFSET_NON_NUMBER : ERRORS.INVALID_LENGTH_NON_NUMBER);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function checkLengthValue(length) {
|
|
51
|
+
checkOffsetOrLengthValue(length, false);
|
|
52
|
+
}
|
|
53
|
+
exports.checkLengthValue = checkLengthValue;
|
|
54
|
+
function checkOffsetValue(offset) {
|
|
55
|
+
checkOffsetOrLengthValue(offset, true);
|
|
56
|
+
}
|
|
57
|
+
exports.checkOffsetValue = checkOffsetValue;
|
|
58
|
+
function checkTargetOffset(offset, buff) {
|
|
59
|
+
if (offset < 0 || offset > buff.length) {
|
|
60
|
+
throw new Error(ERRORS.INVALID_TARGET_OFFSET);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.checkTargetOffset = checkTargetOffset;
|
|
64
|
+
function isInteger(value) {
|
|
65
|
+
return typeof value === "number" && isFinite(value) && Math.floor(value) === value;
|
|
66
|
+
}
|
|
67
|
+
function bigIntAndBufferInt64Check(bufferMethod) {
|
|
68
|
+
if (typeof BigInt === "undefined") {
|
|
69
|
+
throw new Error("Platform does not support JS BigInt type.");
|
|
70
|
+
}
|
|
71
|
+
if (typeof buffer_1.Buffer.prototype[bufferMethod] === "undefined") {
|
|
72
|
+
throw new Error(`Platform does not support Buffer.prototype.${bufferMethod}.`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.bigIntAndBufferInt64Check = bigIntAndBufferInt64Check;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// ../../node_modules/.pnpm/smart-buffer@4.2.0/node_modules/smart-buffer/build/smartbuffer.js
|
|
80
|
+
var require_smartbuffer = __commonJS({
|
|
81
|
+
"../../node_modules/.pnpm/smart-buffer@4.2.0/node_modules/smart-buffer/build/smartbuffer.js"(exports) {
|
|
82
|
+
"use strict";
|
|
83
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
84
|
+
var utils_1 = require_utils();
|
|
85
|
+
var DEFAULT_SMARTBUFFER_SIZE = 4096;
|
|
86
|
+
var DEFAULT_SMARTBUFFER_ENCODING = "utf8";
|
|
87
|
+
var SmartBuffer = class _SmartBuffer {
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new SmartBuffer instance.
|
|
90
|
+
*
|
|
91
|
+
* @param options { SmartBufferOptions } The SmartBufferOptions to apply to this instance.
|
|
92
|
+
*/
|
|
93
|
+
constructor(options) {
|
|
94
|
+
this.length = 0;
|
|
95
|
+
this._encoding = DEFAULT_SMARTBUFFER_ENCODING;
|
|
96
|
+
this._writeOffset = 0;
|
|
97
|
+
this._readOffset = 0;
|
|
98
|
+
if (_SmartBuffer.isSmartBufferOptions(options)) {
|
|
99
|
+
if (options.encoding) {
|
|
100
|
+
utils_1.checkEncoding(options.encoding);
|
|
101
|
+
this._encoding = options.encoding;
|
|
102
|
+
}
|
|
103
|
+
if (options.size) {
|
|
104
|
+
if (utils_1.isFiniteInteger(options.size) && options.size > 0) {
|
|
105
|
+
this._buff = Buffer.allocUnsafe(options.size);
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_SIZE);
|
|
108
|
+
}
|
|
109
|
+
} else if (options.buff) {
|
|
110
|
+
if (Buffer.isBuffer(options.buff)) {
|
|
111
|
+
this._buff = options.buff;
|
|
112
|
+
this.length = options.buff.length;
|
|
113
|
+
} else {
|
|
114
|
+
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_BUFFER);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
|
|
118
|
+
}
|
|
119
|
+
} else {
|
|
120
|
+
if (typeof options !== "undefined") {
|
|
121
|
+
throw new Error(utils_1.ERRORS.INVALID_SMARTBUFFER_OBJECT);
|
|
122
|
+
}
|
|
123
|
+
this._buff = Buffer.allocUnsafe(DEFAULT_SMARTBUFFER_SIZE);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Creates a new SmartBuffer instance with the provided internal Buffer size and optional encoding.
|
|
128
|
+
*
|
|
129
|
+
* @param size { Number } The size of the internal Buffer.
|
|
130
|
+
* @param encoding { String } The BufferEncoding to use for strings.
|
|
131
|
+
*
|
|
132
|
+
* @return { SmartBuffer }
|
|
133
|
+
*/
|
|
134
|
+
static fromSize(size, encoding) {
|
|
135
|
+
return new this({
|
|
136
|
+
size,
|
|
137
|
+
encoding
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Creates a new SmartBuffer instance with the provided Buffer and optional encoding.
|
|
142
|
+
*
|
|
143
|
+
* @param buffer { Buffer } The Buffer to use as the internal Buffer value.
|
|
144
|
+
* @param encoding { String } The BufferEncoding to use for strings.
|
|
145
|
+
*
|
|
146
|
+
* @return { SmartBuffer }
|
|
147
|
+
*/
|
|
148
|
+
static fromBuffer(buff, encoding) {
|
|
149
|
+
return new this({
|
|
150
|
+
buff,
|
|
151
|
+
encoding
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new SmartBuffer instance with the provided SmartBufferOptions options.
|
|
156
|
+
*
|
|
157
|
+
* @param options { SmartBufferOptions } The options to use when creating the SmartBuffer instance.
|
|
158
|
+
*/
|
|
159
|
+
static fromOptions(options) {
|
|
160
|
+
return new this(options);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Type checking function that determines if an object is a SmartBufferOptions object.
|
|
164
|
+
*/
|
|
165
|
+
static isSmartBufferOptions(options) {
|
|
166
|
+
const castOptions = options;
|
|
167
|
+
return castOptions && (castOptions.encoding !== void 0 || castOptions.size !== void 0 || castOptions.buff !== void 0);
|
|
168
|
+
}
|
|
169
|
+
// Signed integers
|
|
170
|
+
/**
|
|
171
|
+
* Reads an Int8 value from the current read position or an optionally provided offset.
|
|
172
|
+
*
|
|
173
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
174
|
+
* @return { Number }
|
|
175
|
+
*/
|
|
176
|
+
readInt8(offset) {
|
|
177
|
+
return this._readNumberValue(Buffer.prototype.readInt8, 1, offset);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Reads an Int16BE value from the current read position or an optionally provided offset.
|
|
181
|
+
*
|
|
182
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
183
|
+
* @return { Number }
|
|
184
|
+
*/
|
|
185
|
+
readInt16BE(offset) {
|
|
186
|
+
return this._readNumberValue(Buffer.prototype.readInt16BE, 2, offset);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Reads an Int16LE value from the current read position or an optionally provided offset.
|
|
190
|
+
*
|
|
191
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
192
|
+
* @return { Number }
|
|
193
|
+
*/
|
|
194
|
+
readInt16LE(offset) {
|
|
195
|
+
return this._readNumberValue(Buffer.prototype.readInt16LE, 2, offset);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Reads an Int32BE value from the current read position or an optionally provided offset.
|
|
199
|
+
*
|
|
200
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
201
|
+
* @return { Number }
|
|
202
|
+
*/
|
|
203
|
+
readInt32BE(offset) {
|
|
204
|
+
return this._readNumberValue(Buffer.prototype.readInt32BE, 4, offset);
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Reads an Int32LE value from the current read position or an optionally provided offset.
|
|
208
|
+
*
|
|
209
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
210
|
+
* @return { Number }
|
|
211
|
+
*/
|
|
212
|
+
readInt32LE(offset) {
|
|
213
|
+
return this._readNumberValue(Buffer.prototype.readInt32LE, 4, offset);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Reads a BigInt64BE value from the current read position or an optionally provided offset.
|
|
217
|
+
*
|
|
218
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
219
|
+
* @return { BigInt }
|
|
220
|
+
*/
|
|
221
|
+
readBigInt64BE(offset) {
|
|
222
|
+
utils_1.bigIntAndBufferInt64Check("readBigInt64BE");
|
|
223
|
+
return this._readNumberValue(Buffer.prototype.readBigInt64BE, 8, offset);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Reads a BigInt64LE value from the current read position or an optionally provided offset.
|
|
227
|
+
*
|
|
228
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
229
|
+
* @return { BigInt }
|
|
230
|
+
*/
|
|
231
|
+
readBigInt64LE(offset) {
|
|
232
|
+
utils_1.bigIntAndBufferInt64Check("readBigInt64LE");
|
|
233
|
+
return this._readNumberValue(Buffer.prototype.readBigInt64LE, 8, offset);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Writes an Int8 value to the current write position (or at optional offset).
|
|
237
|
+
*
|
|
238
|
+
* @param value { Number } The value to write.
|
|
239
|
+
* @param offset { Number } The offset to write the value at.
|
|
240
|
+
*
|
|
241
|
+
* @return this
|
|
242
|
+
*/
|
|
243
|
+
writeInt8(value, offset) {
|
|
244
|
+
this._writeNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Inserts an Int8 value at the given offset value.
|
|
249
|
+
*
|
|
250
|
+
* @param value { Number } The value to insert.
|
|
251
|
+
* @param offset { Number } The offset to insert the value at.
|
|
252
|
+
*
|
|
253
|
+
* @return this
|
|
254
|
+
*/
|
|
255
|
+
insertInt8(value, offset) {
|
|
256
|
+
return this._insertNumberValue(Buffer.prototype.writeInt8, 1, value, offset);
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Writes an Int16BE value to the current write position (or at optional offset).
|
|
260
|
+
*
|
|
261
|
+
* @param value { Number } The value to write.
|
|
262
|
+
* @param offset { Number } The offset to write the value at.
|
|
263
|
+
*
|
|
264
|
+
* @return this
|
|
265
|
+
*/
|
|
266
|
+
writeInt16BE(value, offset) {
|
|
267
|
+
return this._writeNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Inserts an Int16BE value at the given offset value.
|
|
271
|
+
*
|
|
272
|
+
* @param value { Number } The value to insert.
|
|
273
|
+
* @param offset { Number } The offset to insert the value at.
|
|
274
|
+
*
|
|
275
|
+
* @return this
|
|
276
|
+
*/
|
|
277
|
+
insertInt16BE(value, offset) {
|
|
278
|
+
return this._insertNumberValue(Buffer.prototype.writeInt16BE, 2, value, offset);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Writes an Int16LE value to the current write position (or at optional offset).
|
|
282
|
+
*
|
|
283
|
+
* @param value { Number } The value to write.
|
|
284
|
+
* @param offset { Number } The offset to write the value at.
|
|
285
|
+
*
|
|
286
|
+
* @return this
|
|
287
|
+
*/
|
|
288
|
+
writeInt16LE(value, offset) {
|
|
289
|
+
return this._writeNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Inserts an Int16LE value at the given offset value.
|
|
293
|
+
*
|
|
294
|
+
* @param value { Number } The value to insert.
|
|
295
|
+
* @param offset { Number } The offset to insert the value at.
|
|
296
|
+
*
|
|
297
|
+
* @return this
|
|
298
|
+
*/
|
|
299
|
+
insertInt16LE(value, offset) {
|
|
300
|
+
return this._insertNumberValue(Buffer.prototype.writeInt16LE, 2, value, offset);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Writes an Int32BE value to the current write position (or at optional offset).
|
|
304
|
+
*
|
|
305
|
+
* @param value { Number } The value to write.
|
|
306
|
+
* @param offset { Number } The offset to write the value at.
|
|
307
|
+
*
|
|
308
|
+
* @return this
|
|
309
|
+
*/
|
|
310
|
+
writeInt32BE(value, offset) {
|
|
311
|
+
return this._writeNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Inserts an Int32BE value at the given offset value.
|
|
315
|
+
*
|
|
316
|
+
* @param value { Number } The value to insert.
|
|
317
|
+
* @param offset { Number } The offset to insert the value at.
|
|
318
|
+
*
|
|
319
|
+
* @return this
|
|
320
|
+
*/
|
|
321
|
+
insertInt32BE(value, offset) {
|
|
322
|
+
return this._insertNumberValue(Buffer.prototype.writeInt32BE, 4, value, offset);
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Writes an Int32LE value to the current write position (or at optional offset).
|
|
326
|
+
*
|
|
327
|
+
* @param value { Number } The value to write.
|
|
328
|
+
* @param offset { Number } The offset to write the value at.
|
|
329
|
+
*
|
|
330
|
+
* @return this
|
|
331
|
+
*/
|
|
332
|
+
writeInt32LE(value, offset) {
|
|
333
|
+
return this._writeNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Inserts an Int32LE value at the given offset value.
|
|
337
|
+
*
|
|
338
|
+
* @param value { Number } The value to insert.
|
|
339
|
+
* @param offset { Number } The offset to insert the value at.
|
|
340
|
+
*
|
|
341
|
+
* @return this
|
|
342
|
+
*/
|
|
343
|
+
insertInt32LE(value, offset) {
|
|
344
|
+
return this._insertNumberValue(Buffer.prototype.writeInt32LE, 4, value, offset);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Writes a BigInt64BE value to the current write position (or at optional offset).
|
|
348
|
+
*
|
|
349
|
+
* @param value { BigInt } The value to write.
|
|
350
|
+
* @param offset { Number } The offset to write the value at.
|
|
351
|
+
*
|
|
352
|
+
* @return this
|
|
353
|
+
*/
|
|
354
|
+
writeBigInt64BE(value, offset) {
|
|
355
|
+
utils_1.bigIntAndBufferInt64Check("writeBigInt64BE");
|
|
356
|
+
return this._writeNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset);
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Inserts a BigInt64BE value at the given offset value.
|
|
360
|
+
*
|
|
361
|
+
* @param value { BigInt } The value to insert.
|
|
362
|
+
* @param offset { Number } The offset to insert the value at.
|
|
363
|
+
*
|
|
364
|
+
* @return this
|
|
365
|
+
*/
|
|
366
|
+
insertBigInt64BE(value, offset) {
|
|
367
|
+
utils_1.bigIntAndBufferInt64Check("writeBigInt64BE");
|
|
368
|
+
return this._insertNumberValue(Buffer.prototype.writeBigInt64BE, 8, value, offset);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Writes a BigInt64LE value to the current write position (or at optional offset).
|
|
372
|
+
*
|
|
373
|
+
* @param value { BigInt } The value to write.
|
|
374
|
+
* @param offset { Number } The offset to write the value at.
|
|
375
|
+
*
|
|
376
|
+
* @return this
|
|
377
|
+
*/
|
|
378
|
+
writeBigInt64LE(value, offset) {
|
|
379
|
+
utils_1.bigIntAndBufferInt64Check("writeBigInt64LE");
|
|
380
|
+
return this._writeNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Inserts a Int64LE value at the given offset value.
|
|
384
|
+
*
|
|
385
|
+
* @param value { BigInt } The value to insert.
|
|
386
|
+
* @param offset { Number } The offset to insert the value at.
|
|
387
|
+
*
|
|
388
|
+
* @return this
|
|
389
|
+
*/
|
|
390
|
+
insertBigInt64LE(value, offset) {
|
|
391
|
+
utils_1.bigIntAndBufferInt64Check("writeBigInt64LE");
|
|
392
|
+
return this._insertNumberValue(Buffer.prototype.writeBigInt64LE, 8, value, offset);
|
|
393
|
+
}
|
|
394
|
+
// Unsigned Integers
|
|
395
|
+
/**
|
|
396
|
+
* Reads an UInt8 value from the current read position or an optionally provided offset.
|
|
397
|
+
*
|
|
398
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
399
|
+
* @return { Number }
|
|
400
|
+
*/
|
|
401
|
+
readUInt8(offset) {
|
|
402
|
+
return this._readNumberValue(Buffer.prototype.readUInt8, 1, offset);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Reads an UInt16BE value from the current read position or an optionally provided offset.
|
|
406
|
+
*
|
|
407
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
408
|
+
* @return { Number }
|
|
409
|
+
*/
|
|
410
|
+
readUInt16BE(offset) {
|
|
411
|
+
return this._readNumberValue(Buffer.prototype.readUInt16BE, 2, offset);
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Reads an UInt16LE value from the current read position or an optionally provided offset.
|
|
415
|
+
*
|
|
416
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
417
|
+
* @return { Number }
|
|
418
|
+
*/
|
|
419
|
+
readUInt16LE(offset) {
|
|
420
|
+
return this._readNumberValue(Buffer.prototype.readUInt16LE, 2, offset);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Reads an UInt32BE value from the current read position or an optionally provided offset.
|
|
424
|
+
*
|
|
425
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
426
|
+
* @return { Number }
|
|
427
|
+
*/
|
|
428
|
+
readUInt32BE(offset) {
|
|
429
|
+
return this._readNumberValue(Buffer.prototype.readUInt32BE, 4, offset);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Reads an UInt32LE value from the current read position or an optionally provided offset.
|
|
433
|
+
*
|
|
434
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
435
|
+
* @return { Number }
|
|
436
|
+
*/
|
|
437
|
+
readUInt32LE(offset) {
|
|
438
|
+
return this._readNumberValue(Buffer.prototype.readUInt32LE, 4, offset);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Reads a BigUInt64BE value from the current read position or an optionally provided offset.
|
|
442
|
+
*
|
|
443
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
444
|
+
* @return { BigInt }
|
|
445
|
+
*/
|
|
446
|
+
readBigUInt64BE(offset) {
|
|
447
|
+
utils_1.bigIntAndBufferInt64Check("readBigUInt64BE");
|
|
448
|
+
return this._readNumberValue(Buffer.prototype.readBigUInt64BE, 8, offset);
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Reads a BigUInt64LE value from the current read position or an optionally provided offset.
|
|
452
|
+
*
|
|
453
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
454
|
+
* @return { BigInt }
|
|
455
|
+
*/
|
|
456
|
+
readBigUInt64LE(offset) {
|
|
457
|
+
utils_1.bigIntAndBufferInt64Check("readBigUInt64LE");
|
|
458
|
+
return this._readNumberValue(Buffer.prototype.readBigUInt64LE, 8, offset);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Writes an UInt8 value to the current write position (or at optional offset).
|
|
462
|
+
*
|
|
463
|
+
* @param value { Number } The value to write.
|
|
464
|
+
* @param offset { Number } The offset to write the value at.
|
|
465
|
+
*
|
|
466
|
+
* @return this
|
|
467
|
+
*/
|
|
468
|
+
writeUInt8(value, offset) {
|
|
469
|
+
return this._writeNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Inserts an UInt8 value at the given offset value.
|
|
473
|
+
*
|
|
474
|
+
* @param value { Number } The value to insert.
|
|
475
|
+
* @param offset { Number } The offset to insert the value at.
|
|
476
|
+
*
|
|
477
|
+
* @return this
|
|
478
|
+
*/
|
|
479
|
+
insertUInt8(value, offset) {
|
|
480
|
+
return this._insertNumberValue(Buffer.prototype.writeUInt8, 1, value, offset);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Writes an UInt16BE value to the current write position (or at optional offset).
|
|
484
|
+
*
|
|
485
|
+
* @param value { Number } The value to write.
|
|
486
|
+
* @param offset { Number } The offset to write the value at.
|
|
487
|
+
*
|
|
488
|
+
* @return this
|
|
489
|
+
*/
|
|
490
|
+
writeUInt16BE(value, offset) {
|
|
491
|
+
return this._writeNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Inserts an UInt16BE value at the given offset value.
|
|
495
|
+
*
|
|
496
|
+
* @param value { Number } The value to insert.
|
|
497
|
+
* @param offset { Number } The offset to insert the value at.
|
|
498
|
+
*
|
|
499
|
+
* @return this
|
|
500
|
+
*/
|
|
501
|
+
insertUInt16BE(value, offset) {
|
|
502
|
+
return this._insertNumberValue(Buffer.prototype.writeUInt16BE, 2, value, offset);
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Writes an UInt16LE value to the current write position (or at optional offset).
|
|
506
|
+
*
|
|
507
|
+
* @param value { Number } The value to write.
|
|
508
|
+
* @param offset { Number } The offset to write the value at.
|
|
509
|
+
*
|
|
510
|
+
* @return this
|
|
511
|
+
*/
|
|
512
|
+
writeUInt16LE(value, offset) {
|
|
513
|
+
return this._writeNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Inserts an UInt16LE value at the given offset value.
|
|
517
|
+
*
|
|
518
|
+
* @param value { Number } The value to insert.
|
|
519
|
+
* @param offset { Number } The offset to insert the value at.
|
|
520
|
+
*
|
|
521
|
+
* @return this
|
|
522
|
+
*/
|
|
523
|
+
insertUInt16LE(value, offset) {
|
|
524
|
+
return this._insertNumberValue(Buffer.prototype.writeUInt16LE, 2, value, offset);
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Writes an UInt32BE value to the current write position (or at optional offset).
|
|
528
|
+
*
|
|
529
|
+
* @param value { Number } The value to write.
|
|
530
|
+
* @param offset { Number } The offset to write the value at.
|
|
531
|
+
*
|
|
532
|
+
* @return this
|
|
533
|
+
*/
|
|
534
|
+
writeUInt32BE(value, offset) {
|
|
535
|
+
return this._writeNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Inserts an UInt32BE value at the given offset value.
|
|
539
|
+
*
|
|
540
|
+
* @param value { Number } The value to insert.
|
|
541
|
+
* @param offset { Number } The offset to insert the value at.
|
|
542
|
+
*
|
|
543
|
+
* @return this
|
|
544
|
+
*/
|
|
545
|
+
insertUInt32BE(value, offset) {
|
|
546
|
+
return this._insertNumberValue(Buffer.prototype.writeUInt32BE, 4, value, offset);
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Writes an UInt32LE value to the current write position (or at optional offset).
|
|
550
|
+
*
|
|
551
|
+
* @param value { Number } The value to write.
|
|
552
|
+
* @param offset { Number } The offset to write the value at.
|
|
553
|
+
*
|
|
554
|
+
* @return this
|
|
555
|
+
*/
|
|
556
|
+
writeUInt32LE(value, offset) {
|
|
557
|
+
return this._writeNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Inserts an UInt32LE value at the given offset value.
|
|
561
|
+
*
|
|
562
|
+
* @param value { Number } The value to insert.
|
|
563
|
+
* @param offset { Number } The offset to insert the value at.
|
|
564
|
+
*
|
|
565
|
+
* @return this
|
|
566
|
+
*/
|
|
567
|
+
insertUInt32LE(value, offset) {
|
|
568
|
+
return this._insertNumberValue(Buffer.prototype.writeUInt32LE, 4, value, offset);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Writes a BigUInt64BE value to the current write position (or at optional offset).
|
|
572
|
+
*
|
|
573
|
+
* @param value { Number } The value to write.
|
|
574
|
+
* @param offset { Number } The offset to write the value at.
|
|
575
|
+
*
|
|
576
|
+
* @return this
|
|
577
|
+
*/
|
|
578
|
+
writeBigUInt64BE(value, offset) {
|
|
579
|
+
utils_1.bigIntAndBufferInt64Check("writeBigUInt64BE");
|
|
580
|
+
return this._writeNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Inserts a BigUInt64BE value at the given offset value.
|
|
584
|
+
*
|
|
585
|
+
* @param value { Number } The value to insert.
|
|
586
|
+
* @param offset { Number } The offset to insert the value at.
|
|
587
|
+
*
|
|
588
|
+
* @return this
|
|
589
|
+
*/
|
|
590
|
+
insertBigUInt64BE(value, offset) {
|
|
591
|
+
utils_1.bigIntAndBufferInt64Check("writeBigUInt64BE");
|
|
592
|
+
return this._insertNumberValue(Buffer.prototype.writeBigUInt64BE, 8, value, offset);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Writes a BigUInt64LE value to the current write position (or at optional offset).
|
|
596
|
+
*
|
|
597
|
+
* @param value { Number } The value to write.
|
|
598
|
+
* @param offset { Number } The offset to write the value at.
|
|
599
|
+
*
|
|
600
|
+
* @return this
|
|
601
|
+
*/
|
|
602
|
+
writeBigUInt64LE(value, offset) {
|
|
603
|
+
utils_1.bigIntAndBufferInt64Check("writeBigUInt64LE");
|
|
604
|
+
return this._writeNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Inserts a BigUInt64LE value at the given offset value.
|
|
608
|
+
*
|
|
609
|
+
* @param value { Number } The value to insert.
|
|
610
|
+
* @param offset { Number } The offset to insert the value at.
|
|
611
|
+
*
|
|
612
|
+
* @return this
|
|
613
|
+
*/
|
|
614
|
+
insertBigUInt64LE(value, offset) {
|
|
615
|
+
utils_1.bigIntAndBufferInt64Check("writeBigUInt64LE");
|
|
616
|
+
return this._insertNumberValue(Buffer.prototype.writeBigUInt64LE, 8, value, offset);
|
|
617
|
+
}
|
|
618
|
+
// Floating Point
|
|
619
|
+
/**
|
|
620
|
+
* Reads an FloatBE value from the current read position or an optionally provided offset.
|
|
621
|
+
*
|
|
622
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
623
|
+
* @return { Number }
|
|
624
|
+
*/
|
|
625
|
+
readFloatBE(offset) {
|
|
626
|
+
return this._readNumberValue(Buffer.prototype.readFloatBE, 4, offset);
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Reads an FloatLE value from the current read position or an optionally provided offset.
|
|
630
|
+
*
|
|
631
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
632
|
+
* @return { Number }
|
|
633
|
+
*/
|
|
634
|
+
readFloatLE(offset) {
|
|
635
|
+
return this._readNumberValue(Buffer.prototype.readFloatLE, 4, offset);
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Writes a FloatBE value to the current write position (or at optional offset).
|
|
639
|
+
*
|
|
640
|
+
* @param value { Number } The value to write.
|
|
641
|
+
* @param offset { Number } The offset to write the value at.
|
|
642
|
+
*
|
|
643
|
+
* @return this
|
|
644
|
+
*/
|
|
645
|
+
writeFloatBE(value, offset) {
|
|
646
|
+
return this._writeNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Inserts a FloatBE value at the given offset value.
|
|
650
|
+
*
|
|
651
|
+
* @param value { Number } The value to insert.
|
|
652
|
+
* @param offset { Number } The offset to insert the value at.
|
|
653
|
+
*
|
|
654
|
+
* @return this
|
|
655
|
+
*/
|
|
656
|
+
insertFloatBE(value, offset) {
|
|
657
|
+
return this._insertNumberValue(Buffer.prototype.writeFloatBE, 4, value, offset);
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Writes a FloatLE value to the current write position (or at optional offset).
|
|
661
|
+
*
|
|
662
|
+
* @param value { Number } The value to write.
|
|
663
|
+
* @param offset { Number } The offset to write the value at.
|
|
664
|
+
*
|
|
665
|
+
* @return this
|
|
666
|
+
*/
|
|
667
|
+
writeFloatLE(value, offset) {
|
|
668
|
+
return this._writeNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Inserts a FloatLE value at the given offset value.
|
|
672
|
+
*
|
|
673
|
+
* @param value { Number } The value to insert.
|
|
674
|
+
* @param offset { Number } The offset to insert the value at.
|
|
675
|
+
*
|
|
676
|
+
* @return this
|
|
677
|
+
*/
|
|
678
|
+
insertFloatLE(value, offset) {
|
|
679
|
+
return this._insertNumberValue(Buffer.prototype.writeFloatLE, 4, value, offset);
|
|
680
|
+
}
|
|
681
|
+
// Double Floating Point
|
|
682
|
+
/**
|
|
683
|
+
* Reads an DoublEBE value from the current read position or an optionally provided offset.
|
|
684
|
+
*
|
|
685
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
686
|
+
* @return { Number }
|
|
687
|
+
*/
|
|
688
|
+
readDoubleBE(offset) {
|
|
689
|
+
return this._readNumberValue(Buffer.prototype.readDoubleBE, 8, offset);
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Reads an DoubleLE value from the current read position or an optionally provided offset.
|
|
693
|
+
*
|
|
694
|
+
* @param offset { Number } The offset to read data from (optional)
|
|
695
|
+
* @return { Number }
|
|
696
|
+
*/
|
|
697
|
+
readDoubleLE(offset) {
|
|
698
|
+
return this._readNumberValue(Buffer.prototype.readDoubleLE, 8, offset);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Writes a DoubleBE value to the current write position (or at optional offset).
|
|
702
|
+
*
|
|
703
|
+
* @param value { Number } The value to write.
|
|
704
|
+
* @param offset { Number } The offset to write the value at.
|
|
705
|
+
*
|
|
706
|
+
* @return this
|
|
707
|
+
*/
|
|
708
|
+
writeDoubleBE(value, offset) {
|
|
709
|
+
return this._writeNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Inserts a DoubleBE value at the given offset value.
|
|
713
|
+
*
|
|
714
|
+
* @param value { Number } The value to insert.
|
|
715
|
+
* @param offset { Number } The offset to insert the value at.
|
|
716
|
+
*
|
|
717
|
+
* @return this
|
|
718
|
+
*/
|
|
719
|
+
insertDoubleBE(value, offset) {
|
|
720
|
+
return this._insertNumberValue(Buffer.prototype.writeDoubleBE, 8, value, offset);
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Writes a DoubleLE value to the current write position (or at optional offset).
|
|
724
|
+
*
|
|
725
|
+
* @param value { Number } The value to write.
|
|
726
|
+
* @param offset { Number } The offset to write the value at.
|
|
727
|
+
*
|
|
728
|
+
* @return this
|
|
729
|
+
*/
|
|
730
|
+
writeDoubleLE(value, offset) {
|
|
731
|
+
return this._writeNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Inserts a DoubleLE value at the given offset value.
|
|
735
|
+
*
|
|
736
|
+
* @param value { Number } The value to insert.
|
|
737
|
+
* @param offset { Number } The offset to insert the value at.
|
|
738
|
+
*
|
|
739
|
+
* @return this
|
|
740
|
+
*/
|
|
741
|
+
insertDoubleLE(value, offset) {
|
|
742
|
+
return this._insertNumberValue(Buffer.prototype.writeDoubleLE, 8, value, offset);
|
|
743
|
+
}
|
|
744
|
+
// Strings
|
|
745
|
+
/**
|
|
746
|
+
* Reads a String from the current read position.
|
|
747
|
+
*
|
|
748
|
+
* @param arg1 { Number | String } The number of bytes to read as a String, or the BufferEncoding to use for
|
|
749
|
+
* the string (Defaults to instance level encoding).
|
|
750
|
+
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
|
|
751
|
+
*
|
|
752
|
+
* @return { String }
|
|
753
|
+
*/
|
|
754
|
+
readString(arg1, encoding) {
|
|
755
|
+
let lengthVal;
|
|
756
|
+
if (typeof arg1 === "number") {
|
|
757
|
+
utils_1.checkLengthValue(arg1);
|
|
758
|
+
lengthVal = Math.min(arg1, this.length - this._readOffset);
|
|
759
|
+
} else {
|
|
760
|
+
encoding = arg1;
|
|
761
|
+
lengthVal = this.length - this._readOffset;
|
|
762
|
+
}
|
|
763
|
+
if (typeof encoding !== "undefined") {
|
|
764
|
+
utils_1.checkEncoding(encoding);
|
|
765
|
+
}
|
|
766
|
+
const value = this._buff.slice(this._readOffset, this._readOffset + lengthVal).toString(encoding || this._encoding);
|
|
767
|
+
this._readOffset += lengthVal;
|
|
768
|
+
return value;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Inserts a String
|
|
772
|
+
*
|
|
773
|
+
* @param value { String } The String value to insert.
|
|
774
|
+
* @param offset { Number } The offset to insert the string at.
|
|
775
|
+
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
|
|
776
|
+
*
|
|
777
|
+
* @return this
|
|
778
|
+
*/
|
|
779
|
+
insertString(value, offset, encoding) {
|
|
780
|
+
utils_1.checkOffsetValue(offset);
|
|
781
|
+
return this._handleString(value, true, offset, encoding);
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Writes a String
|
|
785
|
+
*
|
|
786
|
+
* @param value { String } The String value to write.
|
|
787
|
+
* @param arg2 { Number | String } The offset to write the string at, or the BufferEncoding to use.
|
|
788
|
+
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
|
|
789
|
+
*
|
|
790
|
+
* @return this
|
|
791
|
+
*/
|
|
792
|
+
writeString(value, arg2, encoding) {
|
|
793
|
+
return this._handleString(value, false, arg2, encoding);
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Reads a null-terminated String from the current read position.
|
|
797
|
+
*
|
|
798
|
+
* @param encoding { String } The BufferEncoding to use for the string (Defaults to instance level encoding).
|
|
799
|
+
*
|
|
800
|
+
* @return { String }
|
|
801
|
+
*/
|
|
802
|
+
readStringNT(encoding) {
|
|
803
|
+
if (typeof encoding !== "undefined") {
|
|
804
|
+
utils_1.checkEncoding(encoding);
|
|
805
|
+
}
|
|
806
|
+
let nullPos = this.length;
|
|
807
|
+
for (let i = this._readOffset; i < this.length; i++) {
|
|
808
|
+
if (this._buff[i] === 0) {
|
|
809
|
+
nullPos = i;
|
|
810
|
+
break;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
const value = this._buff.slice(this._readOffset, nullPos);
|
|
814
|
+
this._readOffset = nullPos + 1;
|
|
815
|
+
return value.toString(encoding || this._encoding);
|
|
816
|
+
}
|
|
817
|
+
/**
|
|
818
|
+
* Inserts a null-terminated String.
|
|
819
|
+
*
|
|
820
|
+
* @param value { String } The String value to write.
|
|
821
|
+
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
|
|
822
|
+
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
|
|
823
|
+
*
|
|
824
|
+
* @return this
|
|
825
|
+
*/
|
|
826
|
+
insertStringNT(value, offset, encoding) {
|
|
827
|
+
utils_1.checkOffsetValue(offset);
|
|
828
|
+
this.insertString(value, offset, encoding);
|
|
829
|
+
this.insertUInt8(0, offset + value.length);
|
|
830
|
+
return this;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Writes a null-terminated String.
|
|
834
|
+
*
|
|
835
|
+
* @param value { String } The String value to write.
|
|
836
|
+
* @param arg2 { Number | String } The offset to write the string to, or the BufferEncoding to use.
|
|
837
|
+
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
|
|
838
|
+
*
|
|
839
|
+
* @return this
|
|
840
|
+
*/
|
|
841
|
+
writeStringNT(value, arg2, encoding) {
|
|
842
|
+
this.writeString(value, arg2, encoding);
|
|
843
|
+
this.writeUInt8(0, typeof arg2 === "number" ? arg2 + value.length : this.writeOffset);
|
|
844
|
+
return this;
|
|
845
|
+
}
|
|
846
|
+
// Buffers
|
|
847
|
+
/**
|
|
848
|
+
* Reads a Buffer from the internal read position.
|
|
849
|
+
*
|
|
850
|
+
* @param length { Number } The length of data to read as a Buffer.
|
|
851
|
+
*
|
|
852
|
+
* @return { Buffer }
|
|
853
|
+
*/
|
|
854
|
+
readBuffer(length) {
|
|
855
|
+
if (typeof length !== "undefined") {
|
|
856
|
+
utils_1.checkLengthValue(length);
|
|
857
|
+
}
|
|
858
|
+
const lengthVal = typeof length === "number" ? length : this.length;
|
|
859
|
+
const endPoint = Math.min(this.length, this._readOffset + lengthVal);
|
|
860
|
+
const value = this._buff.slice(this._readOffset, endPoint);
|
|
861
|
+
this._readOffset = endPoint;
|
|
862
|
+
return value;
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Writes a Buffer to the current write position.
|
|
866
|
+
*
|
|
867
|
+
* @param value { Buffer } The Buffer to write.
|
|
868
|
+
* @param offset { Number } The offset to write the Buffer to.
|
|
869
|
+
*
|
|
870
|
+
* @return this
|
|
871
|
+
*/
|
|
872
|
+
insertBuffer(value, offset) {
|
|
873
|
+
utils_1.checkOffsetValue(offset);
|
|
874
|
+
return this._handleBuffer(value, true, offset);
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Writes a Buffer to the current write position.
|
|
878
|
+
*
|
|
879
|
+
* @param value { Buffer } The Buffer to write.
|
|
880
|
+
* @param offset { Number } The offset to write the Buffer to.
|
|
881
|
+
*
|
|
882
|
+
* @return this
|
|
883
|
+
*/
|
|
884
|
+
writeBuffer(value, offset) {
|
|
885
|
+
return this._handleBuffer(value, false, offset);
|
|
886
|
+
}
|
|
887
|
+
/**
|
|
888
|
+
* Reads a null-terminated Buffer from the current read poisiton.
|
|
889
|
+
*
|
|
890
|
+
* @return { Buffer }
|
|
891
|
+
*/
|
|
892
|
+
readBufferNT() {
|
|
893
|
+
let nullPos = this.length;
|
|
894
|
+
for (let i = this._readOffset; i < this.length; i++) {
|
|
895
|
+
if (this._buff[i] === 0) {
|
|
896
|
+
nullPos = i;
|
|
897
|
+
break;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const value = this._buff.slice(this._readOffset, nullPos);
|
|
901
|
+
this._readOffset = nullPos + 1;
|
|
902
|
+
return value;
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Inserts a null-terminated Buffer.
|
|
906
|
+
*
|
|
907
|
+
* @param value { Buffer } The Buffer to write.
|
|
908
|
+
* @param offset { Number } The offset to write the Buffer to.
|
|
909
|
+
*
|
|
910
|
+
* @return this
|
|
911
|
+
*/
|
|
912
|
+
insertBufferNT(value, offset) {
|
|
913
|
+
utils_1.checkOffsetValue(offset);
|
|
914
|
+
this.insertBuffer(value, offset);
|
|
915
|
+
this.insertUInt8(0, offset + value.length);
|
|
916
|
+
return this;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* Writes a null-terminated Buffer.
|
|
920
|
+
*
|
|
921
|
+
* @param value { Buffer } The Buffer to write.
|
|
922
|
+
* @param offset { Number } The offset to write the Buffer to.
|
|
923
|
+
*
|
|
924
|
+
* @return this
|
|
925
|
+
*/
|
|
926
|
+
writeBufferNT(value, offset) {
|
|
927
|
+
if (typeof offset !== "undefined") {
|
|
928
|
+
utils_1.checkOffsetValue(offset);
|
|
929
|
+
}
|
|
930
|
+
this.writeBuffer(value, offset);
|
|
931
|
+
this.writeUInt8(0, typeof offset === "number" ? offset + value.length : this._writeOffset);
|
|
932
|
+
return this;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Clears the SmartBuffer instance to its original empty state.
|
|
936
|
+
*/
|
|
937
|
+
clear() {
|
|
938
|
+
this._writeOffset = 0;
|
|
939
|
+
this._readOffset = 0;
|
|
940
|
+
this.length = 0;
|
|
941
|
+
return this;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Gets the remaining data left to be read from the SmartBuffer instance.
|
|
945
|
+
*
|
|
946
|
+
* @return { Number }
|
|
947
|
+
*/
|
|
948
|
+
remaining() {
|
|
949
|
+
return this.length - this._readOffset;
|
|
950
|
+
}
|
|
951
|
+
/**
|
|
952
|
+
* Gets the current read offset value of the SmartBuffer instance.
|
|
953
|
+
*
|
|
954
|
+
* @return { Number }
|
|
955
|
+
*/
|
|
956
|
+
get readOffset() {
|
|
957
|
+
return this._readOffset;
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Sets the read offset value of the SmartBuffer instance.
|
|
961
|
+
*
|
|
962
|
+
* @param offset { Number } - The offset value to set.
|
|
963
|
+
*/
|
|
964
|
+
set readOffset(offset) {
|
|
965
|
+
utils_1.checkOffsetValue(offset);
|
|
966
|
+
utils_1.checkTargetOffset(offset, this);
|
|
967
|
+
this._readOffset = offset;
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* Gets the current write offset value of the SmartBuffer instance.
|
|
971
|
+
*
|
|
972
|
+
* @return { Number }
|
|
973
|
+
*/
|
|
974
|
+
get writeOffset() {
|
|
975
|
+
return this._writeOffset;
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Sets the write offset value of the SmartBuffer instance.
|
|
979
|
+
*
|
|
980
|
+
* @param offset { Number } - The offset value to set.
|
|
981
|
+
*/
|
|
982
|
+
set writeOffset(offset) {
|
|
983
|
+
utils_1.checkOffsetValue(offset);
|
|
984
|
+
utils_1.checkTargetOffset(offset, this);
|
|
985
|
+
this._writeOffset = offset;
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Gets the currently set string encoding of the SmartBuffer instance.
|
|
989
|
+
*
|
|
990
|
+
* @return { BufferEncoding } The string Buffer encoding currently set.
|
|
991
|
+
*/
|
|
992
|
+
get encoding() {
|
|
993
|
+
return this._encoding;
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Sets the string encoding of the SmartBuffer instance.
|
|
997
|
+
*
|
|
998
|
+
* @param encoding { BufferEncoding } The string Buffer encoding to set.
|
|
999
|
+
*/
|
|
1000
|
+
set encoding(encoding) {
|
|
1001
|
+
utils_1.checkEncoding(encoding);
|
|
1002
|
+
this._encoding = encoding;
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Gets the underlying internal Buffer. (This includes unmanaged data in the Buffer)
|
|
1006
|
+
*
|
|
1007
|
+
* @return { Buffer } The Buffer value.
|
|
1008
|
+
*/
|
|
1009
|
+
get internalBuffer() {
|
|
1010
|
+
return this._buff;
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Gets the value of the internal managed Buffer (Includes managed data only)
|
|
1014
|
+
*
|
|
1015
|
+
* @param { Buffer }
|
|
1016
|
+
*/
|
|
1017
|
+
toBuffer() {
|
|
1018
|
+
return this._buff.slice(0, this.length);
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Gets the String value of the internal managed Buffer
|
|
1022
|
+
*
|
|
1023
|
+
* @param encoding { String } The BufferEncoding to display the Buffer as (defaults to instance level encoding).
|
|
1024
|
+
*/
|
|
1025
|
+
toString(encoding) {
|
|
1026
|
+
const encodingVal = typeof encoding === "string" ? encoding : this._encoding;
|
|
1027
|
+
utils_1.checkEncoding(encodingVal);
|
|
1028
|
+
return this._buff.toString(encodingVal, 0, this.length);
|
|
1029
|
+
}
|
|
1030
|
+
/**
|
|
1031
|
+
* Destroys the SmartBuffer instance.
|
|
1032
|
+
*/
|
|
1033
|
+
destroy() {
|
|
1034
|
+
this.clear();
|
|
1035
|
+
return this;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Handles inserting and writing strings.
|
|
1039
|
+
*
|
|
1040
|
+
* @param value { String } The String value to insert.
|
|
1041
|
+
* @param isInsert { Boolean } True if inserting a string, false if writing.
|
|
1042
|
+
* @param arg2 { Number | String } The offset to insert the string at, or the BufferEncoding to use.
|
|
1043
|
+
* @param encoding { String } The BufferEncoding to use for writing strings (defaults to instance encoding).
|
|
1044
|
+
*/
|
|
1045
|
+
_handleString(value, isInsert, arg3, encoding) {
|
|
1046
|
+
let offsetVal = this._writeOffset;
|
|
1047
|
+
let encodingVal = this._encoding;
|
|
1048
|
+
if (typeof arg3 === "number") {
|
|
1049
|
+
offsetVal = arg3;
|
|
1050
|
+
} else if (typeof arg3 === "string") {
|
|
1051
|
+
utils_1.checkEncoding(arg3);
|
|
1052
|
+
encodingVal = arg3;
|
|
1053
|
+
}
|
|
1054
|
+
if (typeof encoding === "string") {
|
|
1055
|
+
utils_1.checkEncoding(encoding);
|
|
1056
|
+
encodingVal = encoding;
|
|
1057
|
+
}
|
|
1058
|
+
const byteLength = Buffer.byteLength(value, encodingVal);
|
|
1059
|
+
if (isInsert) {
|
|
1060
|
+
this.ensureInsertable(byteLength, offsetVal);
|
|
1061
|
+
} else {
|
|
1062
|
+
this._ensureWriteable(byteLength, offsetVal);
|
|
1063
|
+
}
|
|
1064
|
+
this._buff.write(value, offsetVal, byteLength, encodingVal);
|
|
1065
|
+
if (isInsert) {
|
|
1066
|
+
this._writeOffset += byteLength;
|
|
1067
|
+
} else {
|
|
1068
|
+
if (typeof arg3 === "number") {
|
|
1069
|
+
this._writeOffset = Math.max(this._writeOffset, offsetVal + byteLength);
|
|
1070
|
+
} else {
|
|
1071
|
+
this._writeOffset += byteLength;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
return this;
|
|
1075
|
+
}
|
|
1076
|
+
/**
|
|
1077
|
+
* Handles writing or insert of a Buffer.
|
|
1078
|
+
*
|
|
1079
|
+
* @param value { Buffer } The Buffer to write.
|
|
1080
|
+
* @param offset { Number } The offset to write the Buffer to.
|
|
1081
|
+
*/
|
|
1082
|
+
_handleBuffer(value, isInsert, offset) {
|
|
1083
|
+
const offsetVal = typeof offset === "number" ? offset : this._writeOffset;
|
|
1084
|
+
if (isInsert) {
|
|
1085
|
+
this.ensureInsertable(value.length, offsetVal);
|
|
1086
|
+
} else {
|
|
1087
|
+
this._ensureWriteable(value.length, offsetVal);
|
|
1088
|
+
}
|
|
1089
|
+
value.copy(this._buff, offsetVal);
|
|
1090
|
+
if (isInsert) {
|
|
1091
|
+
this._writeOffset += value.length;
|
|
1092
|
+
} else {
|
|
1093
|
+
if (typeof offset === "number") {
|
|
1094
|
+
this._writeOffset = Math.max(this._writeOffset, offsetVal + value.length);
|
|
1095
|
+
} else {
|
|
1096
|
+
this._writeOffset += value.length;
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
return this;
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Ensures that the internal Buffer is large enough to read data.
|
|
1103
|
+
*
|
|
1104
|
+
* @param length { Number } The length of the data that needs to be read.
|
|
1105
|
+
* @param offset { Number } The offset of the data that needs to be read.
|
|
1106
|
+
*/
|
|
1107
|
+
ensureReadable(length, offset) {
|
|
1108
|
+
let offsetVal = this._readOffset;
|
|
1109
|
+
if (typeof offset !== "undefined") {
|
|
1110
|
+
utils_1.checkOffsetValue(offset);
|
|
1111
|
+
offsetVal = offset;
|
|
1112
|
+
}
|
|
1113
|
+
if (offsetVal < 0 || offsetVal + length > this.length) {
|
|
1114
|
+
throw new Error(utils_1.ERRORS.INVALID_READ_BEYOND_BOUNDS);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Ensures that the internal Buffer is large enough to insert data.
|
|
1119
|
+
*
|
|
1120
|
+
* @param dataLength { Number } The length of the data that needs to be written.
|
|
1121
|
+
* @param offset { Number } The offset of the data to be written.
|
|
1122
|
+
*/
|
|
1123
|
+
ensureInsertable(dataLength, offset) {
|
|
1124
|
+
utils_1.checkOffsetValue(offset);
|
|
1125
|
+
this._ensureCapacity(this.length + dataLength);
|
|
1126
|
+
if (offset < this.length) {
|
|
1127
|
+
this._buff.copy(this._buff, offset + dataLength, offset, this._buff.length);
|
|
1128
|
+
}
|
|
1129
|
+
if (offset + dataLength > this.length) {
|
|
1130
|
+
this.length = offset + dataLength;
|
|
1131
|
+
} else {
|
|
1132
|
+
this.length += dataLength;
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Ensures that the internal Buffer is large enough to write data.
|
|
1137
|
+
*
|
|
1138
|
+
* @param dataLength { Number } The length of the data that needs to be written.
|
|
1139
|
+
* @param offset { Number } The offset of the data to be written (defaults to writeOffset).
|
|
1140
|
+
*/
|
|
1141
|
+
_ensureWriteable(dataLength, offset) {
|
|
1142
|
+
const offsetVal = typeof offset === "number" ? offset : this._writeOffset;
|
|
1143
|
+
this._ensureCapacity(offsetVal + dataLength);
|
|
1144
|
+
if (offsetVal + dataLength > this.length) {
|
|
1145
|
+
this.length = offsetVal + dataLength;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Ensures that the internal Buffer is large enough to write at least the given amount of data.
|
|
1150
|
+
*
|
|
1151
|
+
* @param minLength { Number } The minimum length of the data needs to be written.
|
|
1152
|
+
*/
|
|
1153
|
+
_ensureCapacity(minLength) {
|
|
1154
|
+
const oldLength = this._buff.length;
|
|
1155
|
+
if (minLength > oldLength) {
|
|
1156
|
+
let data = this._buff;
|
|
1157
|
+
let newLength = oldLength * 3 / 2 + 1;
|
|
1158
|
+
if (newLength < minLength) {
|
|
1159
|
+
newLength = minLength;
|
|
1160
|
+
}
|
|
1161
|
+
this._buff = Buffer.allocUnsafe(newLength);
|
|
1162
|
+
data.copy(this._buff, 0, 0, oldLength);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
/**
|
|
1166
|
+
* Reads a numeric number value using the provided function.
|
|
1167
|
+
*
|
|
1168
|
+
* @typeparam T { number | bigint } The type of the value to be read
|
|
1169
|
+
*
|
|
1170
|
+
* @param func { Function(offset: number) => number } The function to read data on the internal Buffer with.
|
|
1171
|
+
* @param byteSize { Number } The number of bytes read.
|
|
1172
|
+
* @param offset { Number } The offset to read from (optional). When this is not provided, the managed readOffset is used instead.
|
|
1173
|
+
*
|
|
1174
|
+
* @returns { T } the number value
|
|
1175
|
+
*/
|
|
1176
|
+
_readNumberValue(func, byteSize, offset) {
|
|
1177
|
+
this.ensureReadable(byteSize, offset);
|
|
1178
|
+
const value = func.call(this._buff, typeof offset === "number" ? offset : this._readOffset);
|
|
1179
|
+
if (typeof offset === "undefined") {
|
|
1180
|
+
this._readOffset += byteSize;
|
|
1181
|
+
}
|
|
1182
|
+
return value;
|
|
1183
|
+
}
|
|
1184
|
+
/**
|
|
1185
|
+
* Inserts a numeric number value based on the given offset and value.
|
|
1186
|
+
*
|
|
1187
|
+
* @typeparam T { number | bigint } The type of the value to be written
|
|
1188
|
+
*
|
|
1189
|
+
* @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with.
|
|
1190
|
+
* @param byteSize { Number } The number of bytes written.
|
|
1191
|
+
* @param value { T } The number value to write.
|
|
1192
|
+
* @param offset { Number } the offset to write the number at (REQUIRED).
|
|
1193
|
+
*
|
|
1194
|
+
* @returns SmartBuffer this buffer
|
|
1195
|
+
*/
|
|
1196
|
+
_insertNumberValue(func, byteSize, value, offset) {
|
|
1197
|
+
utils_1.checkOffsetValue(offset);
|
|
1198
|
+
this.ensureInsertable(byteSize, offset);
|
|
1199
|
+
func.call(this._buff, value, offset);
|
|
1200
|
+
this._writeOffset += byteSize;
|
|
1201
|
+
return this;
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Writes a numeric number value based on the given offset and value.
|
|
1205
|
+
*
|
|
1206
|
+
* @typeparam T { number | bigint } The type of the value to be written
|
|
1207
|
+
*
|
|
1208
|
+
* @param func { Function(offset: T, offset?) => number} The function to write data on the internal Buffer with.
|
|
1209
|
+
* @param byteSize { Number } The number of bytes written.
|
|
1210
|
+
* @param value { T } The number value to write.
|
|
1211
|
+
* @param offset { Number } the offset to write the number at (REQUIRED).
|
|
1212
|
+
*
|
|
1213
|
+
* @returns SmartBuffer this buffer
|
|
1214
|
+
*/
|
|
1215
|
+
_writeNumberValue(func, byteSize, value, offset) {
|
|
1216
|
+
if (typeof offset === "number") {
|
|
1217
|
+
if (offset < 0) {
|
|
1218
|
+
throw new Error(utils_1.ERRORS.INVALID_WRITE_BEYOND_BOUNDS);
|
|
1219
|
+
}
|
|
1220
|
+
utils_1.checkOffsetValue(offset);
|
|
1221
|
+
}
|
|
1222
|
+
const offsetVal = typeof offset === "number" ? offset : this._writeOffset;
|
|
1223
|
+
this._ensureWriteable(byteSize, offsetVal);
|
|
1224
|
+
func.call(this._buff, value, offsetVal);
|
|
1225
|
+
if (typeof offset === "number") {
|
|
1226
|
+
this._writeOffset = Math.max(this._writeOffset, offsetVal + byteSize);
|
|
1227
|
+
} else {
|
|
1228
|
+
this._writeOffset += byteSize;
|
|
1229
|
+
}
|
|
1230
|
+
return this;
|
|
1231
|
+
}
|
|
1232
|
+
};
|
|
1233
|
+
exports.SmartBuffer = SmartBuffer;
|
|
1234
|
+
}
|
|
1235
|
+
});
|
|
1236
|
+
|
|
1237
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/constants.js
|
|
1238
|
+
var require_constants = __commonJS({
|
|
1239
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/constants.js"(exports) {
|
|
1240
|
+
"use strict";
|
|
1241
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1242
|
+
exports.SOCKS5_NO_ACCEPTABLE_AUTH = exports.SOCKS5_CUSTOM_AUTH_END = exports.SOCKS5_CUSTOM_AUTH_START = exports.SOCKS_INCOMING_PACKET_SIZES = exports.SocksClientState = exports.Socks5Response = exports.Socks5HostType = exports.Socks5Auth = exports.Socks4Response = exports.SocksCommand = exports.ERRORS = exports.DEFAULT_TIMEOUT = void 0;
|
|
1243
|
+
var DEFAULT_TIMEOUT = 3e4;
|
|
1244
|
+
exports.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
|
|
1245
|
+
var ERRORS = {
|
|
1246
|
+
InvalidSocksCommand: "An invalid SOCKS command was provided. Valid options are connect, bind, and associate.",
|
|
1247
|
+
InvalidSocksCommandForOperation: "An invalid SOCKS command was provided. Only a subset of commands are supported for this operation.",
|
|
1248
|
+
InvalidSocksCommandChain: "An invalid SOCKS command was provided. Chaining currently only supports the connect command.",
|
|
1249
|
+
InvalidSocksClientOptionsDestination: "An invalid destination host was provided.",
|
|
1250
|
+
InvalidSocksClientOptionsExistingSocket: "An invalid existing socket was provided. This should be an instance of stream.Duplex.",
|
|
1251
|
+
InvalidSocksClientOptionsProxy: "Invalid SOCKS proxy details were provided.",
|
|
1252
|
+
InvalidSocksClientOptionsTimeout: "An invalid timeout value was provided. Please enter a value above 0 (in ms).",
|
|
1253
|
+
InvalidSocksClientOptionsProxiesLength: "At least two socks proxies must be provided for chaining.",
|
|
1254
|
+
InvalidSocksClientOptionsCustomAuthRange: "Custom auth must be a value between 0x80 and 0xFE.",
|
|
1255
|
+
InvalidSocksClientOptionsCustomAuthOptions: "When a custom_auth_method is provided, custom_auth_request_handler, custom_auth_response_size, and custom_auth_response_handler must also be provided and valid.",
|
|
1256
|
+
NegotiationError: "Negotiation error",
|
|
1257
|
+
SocketClosed: "Socket closed",
|
|
1258
|
+
ProxyConnectionTimedOut: "Proxy connection timed out",
|
|
1259
|
+
InternalError: "SocksClient internal error (this should not happen)",
|
|
1260
|
+
InvalidSocks4HandshakeResponse: "Received invalid Socks4 handshake response",
|
|
1261
|
+
Socks4ProxyRejectedConnection: "Socks4 Proxy rejected connection",
|
|
1262
|
+
InvalidSocks4IncomingConnectionResponse: "Socks4 invalid incoming connection response",
|
|
1263
|
+
Socks4ProxyRejectedIncomingBoundConnection: "Socks4 Proxy rejected incoming bound connection",
|
|
1264
|
+
InvalidSocks5InitialHandshakeResponse: "Received invalid Socks5 initial handshake response",
|
|
1265
|
+
InvalidSocks5IntiailHandshakeSocksVersion: "Received invalid Socks5 initial handshake (invalid socks version)",
|
|
1266
|
+
InvalidSocks5InitialHandshakeNoAcceptedAuthType: "Received invalid Socks5 initial handshake (no accepted authentication type)",
|
|
1267
|
+
InvalidSocks5InitialHandshakeUnknownAuthType: "Received invalid Socks5 initial handshake (unknown authentication type)",
|
|
1268
|
+
Socks5AuthenticationFailed: "Socks5 Authentication failed",
|
|
1269
|
+
InvalidSocks5FinalHandshake: "Received invalid Socks5 final handshake response",
|
|
1270
|
+
InvalidSocks5FinalHandshakeRejected: "Socks5 proxy rejected connection",
|
|
1271
|
+
InvalidSocks5IncomingConnectionResponse: "Received invalid Socks5 incoming connection response",
|
|
1272
|
+
Socks5ProxyRejectedIncomingBoundConnection: "Socks5 Proxy rejected incoming bound connection"
|
|
1273
|
+
};
|
|
1274
|
+
exports.ERRORS = ERRORS;
|
|
1275
|
+
var SOCKS_INCOMING_PACKET_SIZES = {
|
|
1276
|
+
Socks5InitialHandshakeResponse: 2,
|
|
1277
|
+
Socks5UserPassAuthenticationResponse: 2,
|
|
1278
|
+
// Command response + incoming connection (bind)
|
|
1279
|
+
Socks5ResponseHeader: 5,
|
|
1280
|
+
// We need at least 5 to read the hostname length, then we wait for the address+port information.
|
|
1281
|
+
Socks5ResponseIPv4: 10,
|
|
1282
|
+
// 4 header + 4 ip + 2 port
|
|
1283
|
+
Socks5ResponseIPv6: 22,
|
|
1284
|
+
// 4 header + 16 ip + 2 port
|
|
1285
|
+
Socks5ResponseHostname: (hostNameLength) => hostNameLength + 7,
|
|
1286
|
+
// 4 header + 1 host length + host + 2 port
|
|
1287
|
+
// Command response + incoming connection (bind)
|
|
1288
|
+
Socks4Response: 8
|
|
1289
|
+
// 2 header + 2 port + 4 ip
|
|
1290
|
+
};
|
|
1291
|
+
exports.SOCKS_INCOMING_PACKET_SIZES = SOCKS_INCOMING_PACKET_SIZES;
|
|
1292
|
+
var SocksCommand;
|
|
1293
|
+
(function(SocksCommand2) {
|
|
1294
|
+
SocksCommand2[SocksCommand2["connect"] = 1] = "connect";
|
|
1295
|
+
SocksCommand2[SocksCommand2["bind"] = 2] = "bind";
|
|
1296
|
+
SocksCommand2[SocksCommand2["associate"] = 3] = "associate";
|
|
1297
|
+
})(SocksCommand || (exports.SocksCommand = SocksCommand = {}));
|
|
1298
|
+
var Socks4Response;
|
|
1299
|
+
(function(Socks4Response2) {
|
|
1300
|
+
Socks4Response2[Socks4Response2["Granted"] = 90] = "Granted";
|
|
1301
|
+
Socks4Response2[Socks4Response2["Failed"] = 91] = "Failed";
|
|
1302
|
+
Socks4Response2[Socks4Response2["Rejected"] = 92] = "Rejected";
|
|
1303
|
+
Socks4Response2[Socks4Response2["RejectedIdent"] = 93] = "RejectedIdent";
|
|
1304
|
+
})(Socks4Response || (exports.Socks4Response = Socks4Response = {}));
|
|
1305
|
+
var Socks5Auth;
|
|
1306
|
+
(function(Socks5Auth2) {
|
|
1307
|
+
Socks5Auth2[Socks5Auth2["NoAuth"] = 0] = "NoAuth";
|
|
1308
|
+
Socks5Auth2[Socks5Auth2["GSSApi"] = 1] = "GSSApi";
|
|
1309
|
+
Socks5Auth2[Socks5Auth2["UserPass"] = 2] = "UserPass";
|
|
1310
|
+
})(Socks5Auth || (exports.Socks5Auth = Socks5Auth = {}));
|
|
1311
|
+
var SOCKS5_CUSTOM_AUTH_START = 128;
|
|
1312
|
+
exports.SOCKS5_CUSTOM_AUTH_START = SOCKS5_CUSTOM_AUTH_START;
|
|
1313
|
+
var SOCKS5_CUSTOM_AUTH_END = 254;
|
|
1314
|
+
exports.SOCKS5_CUSTOM_AUTH_END = SOCKS5_CUSTOM_AUTH_END;
|
|
1315
|
+
var SOCKS5_NO_ACCEPTABLE_AUTH = 255;
|
|
1316
|
+
exports.SOCKS5_NO_ACCEPTABLE_AUTH = SOCKS5_NO_ACCEPTABLE_AUTH;
|
|
1317
|
+
var Socks5Response;
|
|
1318
|
+
(function(Socks5Response2) {
|
|
1319
|
+
Socks5Response2[Socks5Response2["Granted"] = 0] = "Granted";
|
|
1320
|
+
Socks5Response2[Socks5Response2["Failure"] = 1] = "Failure";
|
|
1321
|
+
Socks5Response2[Socks5Response2["NotAllowed"] = 2] = "NotAllowed";
|
|
1322
|
+
Socks5Response2[Socks5Response2["NetworkUnreachable"] = 3] = "NetworkUnreachable";
|
|
1323
|
+
Socks5Response2[Socks5Response2["HostUnreachable"] = 4] = "HostUnreachable";
|
|
1324
|
+
Socks5Response2[Socks5Response2["ConnectionRefused"] = 5] = "ConnectionRefused";
|
|
1325
|
+
Socks5Response2[Socks5Response2["TTLExpired"] = 6] = "TTLExpired";
|
|
1326
|
+
Socks5Response2[Socks5Response2["CommandNotSupported"] = 7] = "CommandNotSupported";
|
|
1327
|
+
Socks5Response2[Socks5Response2["AddressNotSupported"] = 8] = "AddressNotSupported";
|
|
1328
|
+
})(Socks5Response || (exports.Socks5Response = Socks5Response = {}));
|
|
1329
|
+
var Socks5HostType;
|
|
1330
|
+
(function(Socks5HostType2) {
|
|
1331
|
+
Socks5HostType2[Socks5HostType2["IPv4"] = 1] = "IPv4";
|
|
1332
|
+
Socks5HostType2[Socks5HostType2["Hostname"] = 3] = "Hostname";
|
|
1333
|
+
Socks5HostType2[Socks5HostType2["IPv6"] = 4] = "IPv6";
|
|
1334
|
+
})(Socks5HostType || (exports.Socks5HostType = Socks5HostType = {}));
|
|
1335
|
+
var SocksClientState;
|
|
1336
|
+
(function(SocksClientState2) {
|
|
1337
|
+
SocksClientState2[SocksClientState2["Created"] = 0] = "Created";
|
|
1338
|
+
SocksClientState2[SocksClientState2["Connecting"] = 1] = "Connecting";
|
|
1339
|
+
SocksClientState2[SocksClientState2["Connected"] = 2] = "Connected";
|
|
1340
|
+
SocksClientState2[SocksClientState2["SentInitialHandshake"] = 3] = "SentInitialHandshake";
|
|
1341
|
+
SocksClientState2[SocksClientState2["ReceivedInitialHandshakeResponse"] = 4] = "ReceivedInitialHandshakeResponse";
|
|
1342
|
+
SocksClientState2[SocksClientState2["SentAuthentication"] = 5] = "SentAuthentication";
|
|
1343
|
+
SocksClientState2[SocksClientState2["ReceivedAuthenticationResponse"] = 6] = "ReceivedAuthenticationResponse";
|
|
1344
|
+
SocksClientState2[SocksClientState2["SentFinalHandshake"] = 7] = "SentFinalHandshake";
|
|
1345
|
+
SocksClientState2[SocksClientState2["ReceivedFinalResponse"] = 8] = "ReceivedFinalResponse";
|
|
1346
|
+
SocksClientState2[SocksClientState2["BoundWaitingForConnection"] = 9] = "BoundWaitingForConnection";
|
|
1347
|
+
SocksClientState2[SocksClientState2["Established"] = 10] = "Established";
|
|
1348
|
+
SocksClientState2[SocksClientState2["Disconnected"] = 11] = "Disconnected";
|
|
1349
|
+
SocksClientState2[SocksClientState2["Error"] = 99] = "Error";
|
|
1350
|
+
})(SocksClientState || (exports.SocksClientState = SocksClientState = {}));
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
|
|
1354
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/util.js
|
|
1355
|
+
var require_util = __commonJS({
|
|
1356
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/util.js"(exports) {
|
|
1357
|
+
"use strict";
|
|
1358
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1359
|
+
exports.shuffleArray = exports.SocksClientError = void 0;
|
|
1360
|
+
var SocksClientError = class extends Error {
|
|
1361
|
+
constructor(message, options) {
|
|
1362
|
+
super(message);
|
|
1363
|
+
this.options = options;
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
1366
|
+
exports.SocksClientError = SocksClientError;
|
|
1367
|
+
function shuffleArray(array) {
|
|
1368
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
1369
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
1370
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
exports.shuffleArray = shuffleArray;
|
|
1374
|
+
}
|
|
1375
|
+
});
|
|
1376
|
+
|
|
1377
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/common.js
|
|
1378
|
+
var require_common = __commonJS({
|
|
1379
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/common.js"(exports) {
|
|
1380
|
+
"use strict";
|
|
1381
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1382
|
+
exports.isInSubnet = isInSubnet;
|
|
1383
|
+
exports.isCorrect = isCorrect;
|
|
1384
|
+
exports.numberToPaddedHex = numberToPaddedHex;
|
|
1385
|
+
exports.stringToPaddedHex = stringToPaddedHex;
|
|
1386
|
+
exports.testBit = testBit;
|
|
1387
|
+
function isInSubnet(address) {
|
|
1388
|
+
if (this.subnetMask < address.subnetMask) {
|
|
1389
|
+
return false;
|
|
1390
|
+
}
|
|
1391
|
+
if (this.mask(address.subnetMask) === address.mask()) {
|
|
1392
|
+
return true;
|
|
1393
|
+
}
|
|
1394
|
+
return false;
|
|
1395
|
+
}
|
|
1396
|
+
function isCorrect(defaultBits) {
|
|
1397
|
+
return function() {
|
|
1398
|
+
if (this.addressMinusSuffix !== this.correctForm()) {
|
|
1399
|
+
return false;
|
|
1400
|
+
}
|
|
1401
|
+
if (this.subnetMask === defaultBits && !this.parsedSubnet) {
|
|
1402
|
+
return true;
|
|
1403
|
+
}
|
|
1404
|
+
return this.parsedSubnet === String(this.subnetMask);
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1407
|
+
function numberToPaddedHex(number) {
|
|
1408
|
+
return number.toString(16).padStart(2, "0");
|
|
1409
|
+
}
|
|
1410
|
+
function stringToPaddedHex(numberString) {
|
|
1411
|
+
return numberToPaddedHex(parseInt(numberString, 10));
|
|
1412
|
+
}
|
|
1413
|
+
function testBit(binaryValue, position) {
|
|
1414
|
+
const { length } = binaryValue;
|
|
1415
|
+
if (position > length) {
|
|
1416
|
+
return false;
|
|
1417
|
+
}
|
|
1418
|
+
const positionInString = length - position;
|
|
1419
|
+
return binaryValue.substring(positionInString, positionInString + 1) === "1";
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
});
|
|
1423
|
+
|
|
1424
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v4/constants.js
|
|
1425
|
+
var require_constants2 = __commonJS({
|
|
1426
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v4/constants.js"(exports) {
|
|
1427
|
+
"use strict";
|
|
1428
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1429
|
+
exports.RE_SUBNET_STRING = exports.RE_ADDRESS = exports.GROUPS = exports.BITS = void 0;
|
|
1430
|
+
exports.BITS = 32;
|
|
1431
|
+
exports.GROUPS = 4;
|
|
1432
|
+
exports.RE_ADDRESS = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/g;
|
|
1433
|
+
exports.RE_SUBNET_STRING = /\/\d{1,2}$/;
|
|
1434
|
+
}
|
|
1435
|
+
});
|
|
1436
|
+
|
|
1437
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/address-error.js
|
|
1438
|
+
var require_address_error = __commonJS({
|
|
1439
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/address-error.js"(exports) {
|
|
1440
|
+
"use strict";
|
|
1441
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1442
|
+
exports.AddressError = void 0;
|
|
1443
|
+
var AddressError = class extends Error {
|
|
1444
|
+
constructor(message, parseMessage) {
|
|
1445
|
+
super(message);
|
|
1446
|
+
this.name = "AddressError";
|
|
1447
|
+
this.parseMessage = parseMessage;
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
exports.AddressError = AddressError;
|
|
1451
|
+
}
|
|
1452
|
+
});
|
|
1453
|
+
|
|
1454
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ipv4.js
|
|
1455
|
+
var require_ipv4 = __commonJS({
|
|
1456
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ipv4.js"(exports) {
|
|
1457
|
+
"use strict";
|
|
1458
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
1459
|
+
if (k2 === void 0) k2 = k;
|
|
1460
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1461
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1462
|
+
desc = { enumerable: true, get: function() {
|
|
1463
|
+
return m[k];
|
|
1464
|
+
} };
|
|
1465
|
+
}
|
|
1466
|
+
Object.defineProperty(o, k2, desc);
|
|
1467
|
+
}) : (function(o, m, k, k2) {
|
|
1468
|
+
if (k2 === void 0) k2 = k;
|
|
1469
|
+
o[k2] = m[k];
|
|
1470
|
+
}));
|
|
1471
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
1472
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
1473
|
+
}) : function(o, v) {
|
|
1474
|
+
o["default"] = v;
|
|
1475
|
+
});
|
|
1476
|
+
var __importStar = exports && exports.__importStar || function(mod) {
|
|
1477
|
+
if (mod && mod.__esModule) return mod;
|
|
1478
|
+
var result = {};
|
|
1479
|
+
if (mod != null) {
|
|
1480
|
+
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
1481
|
+
}
|
|
1482
|
+
__setModuleDefault(result, mod);
|
|
1483
|
+
return result;
|
|
1484
|
+
};
|
|
1485
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1486
|
+
exports.Address4 = void 0;
|
|
1487
|
+
var common = __importStar(require_common());
|
|
1488
|
+
var constants = __importStar(require_constants2());
|
|
1489
|
+
var address_error_1 = require_address_error();
|
|
1490
|
+
var Address4 = class _Address4 {
|
|
1491
|
+
constructor(address) {
|
|
1492
|
+
this.groups = constants.GROUPS;
|
|
1493
|
+
this.parsedAddress = [];
|
|
1494
|
+
this.parsedSubnet = "";
|
|
1495
|
+
this.subnet = "/32";
|
|
1496
|
+
this.subnetMask = 32;
|
|
1497
|
+
this.v4 = true;
|
|
1498
|
+
this.isCorrect = common.isCorrect(constants.BITS);
|
|
1499
|
+
this.isInSubnet = common.isInSubnet;
|
|
1500
|
+
this.address = address;
|
|
1501
|
+
const subnet = constants.RE_SUBNET_STRING.exec(address);
|
|
1502
|
+
if (subnet) {
|
|
1503
|
+
this.parsedSubnet = subnet[0].replace("/", "");
|
|
1504
|
+
this.subnetMask = parseInt(this.parsedSubnet, 10);
|
|
1505
|
+
this.subnet = `/${this.subnetMask}`;
|
|
1506
|
+
if (this.subnetMask < 0 || this.subnetMask > constants.BITS) {
|
|
1507
|
+
throw new address_error_1.AddressError("Invalid subnet mask.");
|
|
1508
|
+
}
|
|
1509
|
+
address = address.replace(constants.RE_SUBNET_STRING, "");
|
|
1510
|
+
}
|
|
1511
|
+
this.addressMinusSuffix = address;
|
|
1512
|
+
this.parsedAddress = this.parse(address);
|
|
1513
|
+
}
|
|
1514
|
+
static isValid(address) {
|
|
1515
|
+
try {
|
|
1516
|
+
new _Address4(address);
|
|
1517
|
+
return true;
|
|
1518
|
+
} catch (e) {
|
|
1519
|
+
return false;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
/*
|
|
1523
|
+
* Parses a v4 address
|
|
1524
|
+
*/
|
|
1525
|
+
parse(address) {
|
|
1526
|
+
const groups = address.split(".");
|
|
1527
|
+
if (!address.match(constants.RE_ADDRESS)) {
|
|
1528
|
+
throw new address_error_1.AddressError("Invalid IPv4 address.");
|
|
1529
|
+
}
|
|
1530
|
+
return groups;
|
|
1531
|
+
}
|
|
1532
|
+
/**
|
|
1533
|
+
* Returns the correct form of an address
|
|
1534
|
+
* @memberof Address4
|
|
1535
|
+
* @instance
|
|
1536
|
+
* @returns {String}
|
|
1537
|
+
*/
|
|
1538
|
+
correctForm() {
|
|
1539
|
+
return this.parsedAddress.map((part) => parseInt(part, 10)).join(".");
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* Converts a hex string to an IPv4 address object
|
|
1543
|
+
* @memberof Address4
|
|
1544
|
+
* @static
|
|
1545
|
+
* @param {string} hex - a hex string to convert
|
|
1546
|
+
* @returns {Address4}
|
|
1547
|
+
*/
|
|
1548
|
+
static fromHex(hex) {
|
|
1549
|
+
const padded = hex.replace(/:/g, "").padStart(8, "0");
|
|
1550
|
+
const groups = [];
|
|
1551
|
+
let i;
|
|
1552
|
+
for (i = 0; i < 8; i += 2) {
|
|
1553
|
+
const h = padded.slice(i, i + 2);
|
|
1554
|
+
groups.push(parseInt(h, 16));
|
|
1555
|
+
}
|
|
1556
|
+
return new _Address4(groups.join("."));
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Converts an integer into a IPv4 address object
|
|
1560
|
+
* @memberof Address4
|
|
1561
|
+
* @static
|
|
1562
|
+
* @param {integer} integer - a number to convert
|
|
1563
|
+
* @returns {Address4}
|
|
1564
|
+
*/
|
|
1565
|
+
static fromInteger(integer) {
|
|
1566
|
+
return _Address4.fromHex(integer.toString(16));
|
|
1567
|
+
}
|
|
1568
|
+
/**
|
|
1569
|
+
* Return an address from in-addr.arpa form
|
|
1570
|
+
* @memberof Address4
|
|
1571
|
+
* @static
|
|
1572
|
+
* @param {string} arpaFormAddress - an 'in-addr.arpa' form ipv4 address
|
|
1573
|
+
* @returns {Adress4}
|
|
1574
|
+
* @example
|
|
1575
|
+
* var address = Address4.fromArpa(42.2.0.192.in-addr.arpa.)
|
|
1576
|
+
* address.correctForm(); // '192.0.2.42'
|
|
1577
|
+
*/
|
|
1578
|
+
static fromArpa(arpaFormAddress) {
|
|
1579
|
+
const leader = arpaFormAddress.replace(/(\.in-addr\.arpa)?\.$/, "");
|
|
1580
|
+
const address = leader.split(".").reverse().join(".");
|
|
1581
|
+
return new _Address4(address);
|
|
1582
|
+
}
|
|
1583
|
+
/**
|
|
1584
|
+
* Converts an IPv4 address object to a hex string
|
|
1585
|
+
* @memberof Address4
|
|
1586
|
+
* @instance
|
|
1587
|
+
* @returns {String}
|
|
1588
|
+
*/
|
|
1589
|
+
toHex() {
|
|
1590
|
+
return this.parsedAddress.map((part) => common.stringToPaddedHex(part)).join(":");
|
|
1591
|
+
}
|
|
1592
|
+
/**
|
|
1593
|
+
* Converts an IPv4 address object to an array of bytes
|
|
1594
|
+
* @memberof Address4
|
|
1595
|
+
* @instance
|
|
1596
|
+
* @returns {Array}
|
|
1597
|
+
*/
|
|
1598
|
+
toArray() {
|
|
1599
|
+
return this.parsedAddress.map((part) => parseInt(part, 10));
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* Converts an IPv4 address object to an IPv6 address group
|
|
1603
|
+
* @memberof Address4
|
|
1604
|
+
* @instance
|
|
1605
|
+
* @returns {String}
|
|
1606
|
+
*/
|
|
1607
|
+
toGroup6() {
|
|
1608
|
+
const output = [];
|
|
1609
|
+
let i;
|
|
1610
|
+
for (i = 0; i < constants.GROUPS; i += 2) {
|
|
1611
|
+
output.push(`${common.stringToPaddedHex(this.parsedAddress[i])}${common.stringToPaddedHex(this.parsedAddress[i + 1])}`);
|
|
1612
|
+
}
|
|
1613
|
+
return output.join(":");
|
|
1614
|
+
}
|
|
1615
|
+
/**
|
|
1616
|
+
* Returns the address as a `bigint`
|
|
1617
|
+
* @memberof Address4
|
|
1618
|
+
* @instance
|
|
1619
|
+
* @returns {bigint}
|
|
1620
|
+
*/
|
|
1621
|
+
bigInt() {
|
|
1622
|
+
return BigInt(`0x${this.parsedAddress.map((n) => common.stringToPaddedHex(n)).join("")}`);
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Helper function getting start address.
|
|
1626
|
+
* @memberof Address4
|
|
1627
|
+
* @instance
|
|
1628
|
+
* @returns {bigint}
|
|
1629
|
+
*/
|
|
1630
|
+
_startAddress() {
|
|
1631
|
+
return BigInt(`0b${this.mask() + "0".repeat(constants.BITS - this.subnetMask)}`);
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* The first address in the range given by this address' subnet.
|
|
1635
|
+
* Often referred to as the Network Address.
|
|
1636
|
+
* @memberof Address4
|
|
1637
|
+
* @instance
|
|
1638
|
+
* @returns {Address4}
|
|
1639
|
+
*/
|
|
1640
|
+
startAddress() {
|
|
1641
|
+
return _Address4.fromBigInt(this._startAddress());
|
|
1642
|
+
}
|
|
1643
|
+
/**
|
|
1644
|
+
* The first host address in the range given by this address's subnet ie
|
|
1645
|
+
* the first address after the Network Address
|
|
1646
|
+
* @memberof Address4
|
|
1647
|
+
* @instance
|
|
1648
|
+
* @returns {Address4}
|
|
1649
|
+
*/
|
|
1650
|
+
startAddressExclusive() {
|
|
1651
|
+
const adjust = BigInt("1");
|
|
1652
|
+
return _Address4.fromBigInt(this._startAddress() + adjust);
|
|
1653
|
+
}
|
|
1654
|
+
/**
|
|
1655
|
+
* Helper function getting end address.
|
|
1656
|
+
* @memberof Address4
|
|
1657
|
+
* @instance
|
|
1658
|
+
* @returns {bigint}
|
|
1659
|
+
*/
|
|
1660
|
+
_endAddress() {
|
|
1661
|
+
return BigInt(`0b${this.mask() + "1".repeat(constants.BITS - this.subnetMask)}`);
|
|
1662
|
+
}
|
|
1663
|
+
/**
|
|
1664
|
+
* The last address in the range given by this address' subnet
|
|
1665
|
+
* Often referred to as the Broadcast
|
|
1666
|
+
* @memberof Address4
|
|
1667
|
+
* @instance
|
|
1668
|
+
* @returns {Address4}
|
|
1669
|
+
*/
|
|
1670
|
+
endAddress() {
|
|
1671
|
+
return _Address4.fromBigInt(this._endAddress());
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* The last host address in the range given by this address's subnet ie
|
|
1675
|
+
* the last address prior to the Broadcast Address
|
|
1676
|
+
* @memberof Address4
|
|
1677
|
+
* @instance
|
|
1678
|
+
* @returns {Address4}
|
|
1679
|
+
*/
|
|
1680
|
+
endAddressExclusive() {
|
|
1681
|
+
const adjust = BigInt("1");
|
|
1682
|
+
return _Address4.fromBigInt(this._endAddress() - adjust);
|
|
1683
|
+
}
|
|
1684
|
+
/**
|
|
1685
|
+
* Converts a BigInt to a v4 address object
|
|
1686
|
+
* @memberof Address4
|
|
1687
|
+
* @static
|
|
1688
|
+
* @param {bigint} bigInt - a BigInt to convert
|
|
1689
|
+
* @returns {Address4}
|
|
1690
|
+
*/
|
|
1691
|
+
static fromBigInt(bigInt) {
|
|
1692
|
+
return _Address4.fromHex(bigInt.toString(16));
|
|
1693
|
+
}
|
|
1694
|
+
/**
|
|
1695
|
+
* Returns the first n bits of the address, defaulting to the
|
|
1696
|
+
* subnet mask
|
|
1697
|
+
* @memberof Address4
|
|
1698
|
+
* @instance
|
|
1699
|
+
* @returns {String}
|
|
1700
|
+
*/
|
|
1701
|
+
mask(mask) {
|
|
1702
|
+
if (mask === void 0) {
|
|
1703
|
+
mask = this.subnetMask;
|
|
1704
|
+
}
|
|
1705
|
+
return this.getBitsBase2(0, mask);
|
|
1706
|
+
}
|
|
1707
|
+
/**
|
|
1708
|
+
* Returns the bits in the given range as a base-2 string
|
|
1709
|
+
* @memberof Address4
|
|
1710
|
+
* @instance
|
|
1711
|
+
* @returns {string}
|
|
1712
|
+
*/
|
|
1713
|
+
getBitsBase2(start, end) {
|
|
1714
|
+
return this.binaryZeroPad().slice(start, end);
|
|
1715
|
+
}
|
|
1716
|
+
/**
|
|
1717
|
+
* Return the reversed ip6.arpa form of the address
|
|
1718
|
+
* @memberof Address4
|
|
1719
|
+
* @param {Object} options
|
|
1720
|
+
* @param {boolean} options.omitSuffix - omit the "in-addr.arpa" suffix
|
|
1721
|
+
* @instance
|
|
1722
|
+
* @returns {String}
|
|
1723
|
+
*/
|
|
1724
|
+
reverseForm(options) {
|
|
1725
|
+
if (!options) {
|
|
1726
|
+
options = {};
|
|
1727
|
+
}
|
|
1728
|
+
const reversed = this.correctForm().split(".").reverse().join(".");
|
|
1729
|
+
if (options.omitSuffix) {
|
|
1730
|
+
return reversed;
|
|
1731
|
+
}
|
|
1732
|
+
return `${reversed}.in-addr.arpa.`;
|
|
1733
|
+
}
|
|
1734
|
+
/**
|
|
1735
|
+
* Returns true if the given address is a multicast address
|
|
1736
|
+
* @memberof Address4
|
|
1737
|
+
* @instance
|
|
1738
|
+
* @returns {boolean}
|
|
1739
|
+
*/
|
|
1740
|
+
isMulticast() {
|
|
1741
|
+
return this.isInSubnet(new _Address4("224.0.0.0/4"));
|
|
1742
|
+
}
|
|
1743
|
+
/**
|
|
1744
|
+
* Returns a zero-padded base-2 string representation of the address
|
|
1745
|
+
* @memberof Address4
|
|
1746
|
+
* @instance
|
|
1747
|
+
* @returns {string}
|
|
1748
|
+
*/
|
|
1749
|
+
binaryZeroPad() {
|
|
1750
|
+
return this.bigInt().toString(2).padStart(constants.BITS, "0");
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Groups an IPv4 address for inclusion at the end of an IPv6 address
|
|
1754
|
+
* @returns {String}
|
|
1755
|
+
*/
|
|
1756
|
+
groupForV6() {
|
|
1757
|
+
const segments = this.parsedAddress;
|
|
1758
|
+
return this.address.replace(constants.RE_ADDRESS, `<span class="hover-group group-v4 group-6">${segments.slice(0, 2).join(".")}</span>.<span class="hover-group group-v4 group-7">${segments.slice(2, 4).join(".")}</span>`);
|
|
1759
|
+
}
|
|
1760
|
+
};
|
|
1761
|
+
exports.Address4 = Address4;
|
|
1762
|
+
}
|
|
1763
|
+
});
|
|
1764
|
+
|
|
1765
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/constants.js
|
|
1766
|
+
var require_constants3 = __commonJS({
|
|
1767
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/constants.js"(exports) {
|
|
1768
|
+
"use strict";
|
|
1769
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1770
|
+
exports.RE_URL_WITH_PORT = exports.RE_URL = exports.RE_ZONE_STRING = exports.RE_SUBNET_STRING = exports.RE_BAD_ADDRESS = exports.RE_BAD_CHARACTERS = exports.TYPES = exports.SCOPES = exports.GROUPS = exports.BITS = void 0;
|
|
1771
|
+
exports.BITS = 128;
|
|
1772
|
+
exports.GROUPS = 8;
|
|
1773
|
+
exports.SCOPES = {
|
|
1774
|
+
0: "Reserved",
|
|
1775
|
+
1: "Interface local",
|
|
1776
|
+
2: "Link local",
|
|
1777
|
+
4: "Admin local",
|
|
1778
|
+
5: "Site local",
|
|
1779
|
+
8: "Organization local",
|
|
1780
|
+
14: "Global",
|
|
1781
|
+
15: "Reserved"
|
|
1782
|
+
};
|
|
1783
|
+
exports.TYPES = {
|
|
1784
|
+
"ff01::1/128": "Multicast (All nodes on this interface)",
|
|
1785
|
+
"ff01::2/128": "Multicast (All routers on this interface)",
|
|
1786
|
+
"ff02::1/128": "Multicast (All nodes on this link)",
|
|
1787
|
+
"ff02::2/128": "Multicast (All routers on this link)",
|
|
1788
|
+
"ff05::2/128": "Multicast (All routers in this site)",
|
|
1789
|
+
"ff02::5/128": "Multicast (OSPFv3 AllSPF routers)",
|
|
1790
|
+
"ff02::6/128": "Multicast (OSPFv3 AllDR routers)",
|
|
1791
|
+
"ff02::9/128": "Multicast (RIP routers)",
|
|
1792
|
+
"ff02::a/128": "Multicast (EIGRP routers)",
|
|
1793
|
+
"ff02::d/128": "Multicast (PIM routers)",
|
|
1794
|
+
"ff02::16/128": "Multicast (MLDv2 reports)",
|
|
1795
|
+
"ff01::fb/128": "Multicast (mDNSv6)",
|
|
1796
|
+
"ff02::fb/128": "Multicast (mDNSv6)",
|
|
1797
|
+
"ff05::fb/128": "Multicast (mDNSv6)",
|
|
1798
|
+
"ff02::1:2/128": "Multicast (All DHCP servers and relay agents on this link)",
|
|
1799
|
+
"ff05::1:2/128": "Multicast (All DHCP servers and relay agents in this site)",
|
|
1800
|
+
"ff02::1:3/128": "Multicast (All DHCP servers on this link)",
|
|
1801
|
+
"ff05::1:3/128": "Multicast (All DHCP servers in this site)",
|
|
1802
|
+
"::/128": "Unspecified",
|
|
1803
|
+
"::1/128": "Loopback",
|
|
1804
|
+
"ff00::/8": "Multicast",
|
|
1805
|
+
"fe80::/10": "Link-local unicast"
|
|
1806
|
+
};
|
|
1807
|
+
exports.RE_BAD_CHARACTERS = /([^0-9a-f:/%])/gi;
|
|
1808
|
+
exports.RE_BAD_ADDRESS = /([0-9a-f]{5,}|:{3,}|[^:]:$|^:[^:]|\/$)/gi;
|
|
1809
|
+
exports.RE_SUBNET_STRING = /\/\d{1,3}(?=%|$)/;
|
|
1810
|
+
exports.RE_ZONE_STRING = /%.*$/;
|
|
1811
|
+
exports.RE_URL = /^\[{0,1}([0-9a-f:]+)\]{0,1}/;
|
|
1812
|
+
exports.RE_URL_WITH_PORT = /\[([0-9a-f:]+)\]:([0-9]{1,5})/;
|
|
1813
|
+
}
|
|
1814
|
+
});
|
|
1815
|
+
|
|
1816
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/helpers.js
|
|
1817
|
+
var require_helpers = __commonJS({
|
|
1818
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/helpers.js"(exports) {
|
|
1819
|
+
"use strict";
|
|
1820
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1821
|
+
exports.spanAllZeroes = spanAllZeroes;
|
|
1822
|
+
exports.spanAll = spanAll;
|
|
1823
|
+
exports.spanLeadingZeroes = spanLeadingZeroes;
|
|
1824
|
+
exports.simpleGroup = simpleGroup;
|
|
1825
|
+
function spanAllZeroes(s) {
|
|
1826
|
+
return s.replace(/(0+)/g, '<span class="zero">$1</span>');
|
|
1827
|
+
}
|
|
1828
|
+
function spanAll(s, offset = 0) {
|
|
1829
|
+
const letters = s.split("");
|
|
1830
|
+
return letters.map((n, i) => `<span class="digit value-${n} position-${i + offset}">${spanAllZeroes(n)}</span>`).join("");
|
|
1831
|
+
}
|
|
1832
|
+
function spanLeadingZeroesSimple(group) {
|
|
1833
|
+
return group.replace(/^(0+)/, '<span class="zero">$1</span>');
|
|
1834
|
+
}
|
|
1835
|
+
function spanLeadingZeroes(address) {
|
|
1836
|
+
const groups = address.split(":");
|
|
1837
|
+
return groups.map((g) => spanLeadingZeroesSimple(g)).join(":");
|
|
1838
|
+
}
|
|
1839
|
+
function simpleGroup(addressString, offset = 0) {
|
|
1840
|
+
const groups = addressString.split(":");
|
|
1841
|
+
return groups.map((g, i) => {
|
|
1842
|
+
if (/group-v4/.test(g)) {
|
|
1843
|
+
return g;
|
|
1844
|
+
}
|
|
1845
|
+
return `<span class="hover-group group-${i + offset}">${spanLeadingZeroesSimple(g)}</span>`;
|
|
1846
|
+
});
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
});
|
|
1850
|
+
|
|
1851
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/regular-expressions.js
|
|
1852
|
+
var require_regular_expressions = __commonJS({
|
|
1853
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/v6/regular-expressions.js"(exports) {
|
|
1854
|
+
"use strict";
|
|
1855
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
1856
|
+
if (k2 === void 0) k2 = k;
|
|
1857
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1858
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1859
|
+
desc = { enumerable: true, get: function() {
|
|
1860
|
+
return m[k];
|
|
1861
|
+
} };
|
|
1862
|
+
}
|
|
1863
|
+
Object.defineProperty(o, k2, desc);
|
|
1864
|
+
}) : (function(o, m, k, k2) {
|
|
1865
|
+
if (k2 === void 0) k2 = k;
|
|
1866
|
+
o[k2] = m[k];
|
|
1867
|
+
}));
|
|
1868
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
1869
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
1870
|
+
}) : function(o, v) {
|
|
1871
|
+
o["default"] = v;
|
|
1872
|
+
});
|
|
1873
|
+
var __importStar = exports && exports.__importStar || function(mod) {
|
|
1874
|
+
if (mod && mod.__esModule) return mod;
|
|
1875
|
+
var result = {};
|
|
1876
|
+
if (mod != null) {
|
|
1877
|
+
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
1878
|
+
}
|
|
1879
|
+
__setModuleDefault(result, mod);
|
|
1880
|
+
return result;
|
|
1881
|
+
};
|
|
1882
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1883
|
+
exports.ADDRESS_BOUNDARY = void 0;
|
|
1884
|
+
exports.groupPossibilities = groupPossibilities;
|
|
1885
|
+
exports.padGroup = padGroup;
|
|
1886
|
+
exports.simpleRegularExpression = simpleRegularExpression;
|
|
1887
|
+
exports.possibleElisions = possibleElisions;
|
|
1888
|
+
var v6 = __importStar(require_constants3());
|
|
1889
|
+
function groupPossibilities(possibilities) {
|
|
1890
|
+
return `(${possibilities.join("|")})`;
|
|
1891
|
+
}
|
|
1892
|
+
function padGroup(group) {
|
|
1893
|
+
if (group.length < 4) {
|
|
1894
|
+
return `0{0,${4 - group.length}}${group}`;
|
|
1895
|
+
}
|
|
1896
|
+
return group;
|
|
1897
|
+
}
|
|
1898
|
+
exports.ADDRESS_BOUNDARY = "[^A-Fa-f0-9:]";
|
|
1899
|
+
function simpleRegularExpression(groups) {
|
|
1900
|
+
const zeroIndexes = [];
|
|
1901
|
+
groups.forEach((group, i) => {
|
|
1902
|
+
const groupInteger = parseInt(group, 16);
|
|
1903
|
+
if (groupInteger === 0) {
|
|
1904
|
+
zeroIndexes.push(i);
|
|
1905
|
+
}
|
|
1906
|
+
});
|
|
1907
|
+
const possibilities = zeroIndexes.map((zeroIndex) => groups.map((group, i) => {
|
|
1908
|
+
if (i === zeroIndex) {
|
|
1909
|
+
const elision = i === 0 || i === v6.GROUPS - 1 ? ":" : "";
|
|
1910
|
+
return groupPossibilities([padGroup(group), elision]);
|
|
1911
|
+
}
|
|
1912
|
+
return padGroup(group);
|
|
1913
|
+
}).join(":"));
|
|
1914
|
+
possibilities.push(groups.map(padGroup).join(":"));
|
|
1915
|
+
return groupPossibilities(possibilities);
|
|
1916
|
+
}
|
|
1917
|
+
function possibleElisions(elidedGroups, moreLeft, moreRight) {
|
|
1918
|
+
const left = moreLeft ? "" : ":";
|
|
1919
|
+
const right = moreRight ? "" : ":";
|
|
1920
|
+
const possibilities = [];
|
|
1921
|
+
if (!moreLeft && !moreRight) {
|
|
1922
|
+
possibilities.push("::");
|
|
1923
|
+
}
|
|
1924
|
+
if (moreLeft && moreRight) {
|
|
1925
|
+
possibilities.push("");
|
|
1926
|
+
}
|
|
1927
|
+
if (moreRight && !moreLeft || !moreRight && moreLeft) {
|
|
1928
|
+
possibilities.push(":");
|
|
1929
|
+
}
|
|
1930
|
+
possibilities.push(`${left}(:0{1,4}){1,${elidedGroups - 1}}`);
|
|
1931
|
+
possibilities.push(`(0{1,4}:){1,${elidedGroups - 1}}${right}`);
|
|
1932
|
+
possibilities.push(`(0{1,4}:){${elidedGroups - 1}}0{1,4}`);
|
|
1933
|
+
for (let groups = 1; groups < elidedGroups - 1; groups++) {
|
|
1934
|
+
for (let position = 1; position < elidedGroups - groups; position++) {
|
|
1935
|
+
possibilities.push(`(0{1,4}:){${position}}:(0{1,4}:){${elidedGroups - position - groups - 1}}0{1,4}`);
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
return groupPossibilities(possibilities);
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
});
|
|
1942
|
+
|
|
1943
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ipv6.js
|
|
1944
|
+
var require_ipv6 = __commonJS({
|
|
1945
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ipv6.js"(exports) {
|
|
1946
|
+
"use strict";
|
|
1947
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
1948
|
+
if (k2 === void 0) k2 = k;
|
|
1949
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1950
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1951
|
+
desc = { enumerable: true, get: function() {
|
|
1952
|
+
return m[k];
|
|
1953
|
+
} };
|
|
1954
|
+
}
|
|
1955
|
+
Object.defineProperty(o, k2, desc);
|
|
1956
|
+
}) : (function(o, m, k, k2) {
|
|
1957
|
+
if (k2 === void 0) k2 = k;
|
|
1958
|
+
o[k2] = m[k];
|
|
1959
|
+
}));
|
|
1960
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
1961
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
1962
|
+
}) : function(o, v) {
|
|
1963
|
+
o["default"] = v;
|
|
1964
|
+
});
|
|
1965
|
+
var __importStar = exports && exports.__importStar || function(mod) {
|
|
1966
|
+
if (mod && mod.__esModule) return mod;
|
|
1967
|
+
var result = {};
|
|
1968
|
+
if (mod != null) {
|
|
1969
|
+
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
1970
|
+
}
|
|
1971
|
+
__setModuleDefault(result, mod);
|
|
1972
|
+
return result;
|
|
1973
|
+
};
|
|
1974
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1975
|
+
exports.Address6 = void 0;
|
|
1976
|
+
var common = __importStar(require_common());
|
|
1977
|
+
var constants4 = __importStar(require_constants2());
|
|
1978
|
+
var constants6 = __importStar(require_constants3());
|
|
1979
|
+
var helpers = __importStar(require_helpers());
|
|
1980
|
+
var ipv4_1 = require_ipv4();
|
|
1981
|
+
var regular_expressions_1 = require_regular_expressions();
|
|
1982
|
+
var address_error_1 = require_address_error();
|
|
1983
|
+
var common_1 = require_common();
|
|
1984
|
+
function assert(condition) {
|
|
1985
|
+
if (!condition) {
|
|
1986
|
+
throw new Error("Assertion failed.");
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
function addCommas(number) {
|
|
1990
|
+
const r = /(\d+)(\d{3})/;
|
|
1991
|
+
while (r.test(number)) {
|
|
1992
|
+
number = number.replace(r, "$1,$2");
|
|
1993
|
+
}
|
|
1994
|
+
return number;
|
|
1995
|
+
}
|
|
1996
|
+
function spanLeadingZeroes4(n) {
|
|
1997
|
+
n = n.replace(/^(0{1,})([1-9]+)$/, '<span class="parse-error">$1</span>$2');
|
|
1998
|
+
n = n.replace(/^(0{1,})(0)$/, '<span class="parse-error">$1</span>$2');
|
|
1999
|
+
return n;
|
|
2000
|
+
}
|
|
2001
|
+
function compact(address, slice) {
|
|
2002
|
+
const s1 = [];
|
|
2003
|
+
const s2 = [];
|
|
2004
|
+
let i;
|
|
2005
|
+
for (i = 0; i < address.length; i++) {
|
|
2006
|
+
if (i < slice[0]) {
|
|
2007
|
+
s1.push(address[i]);
|
|
2008
|
+
} else if (i > slice[1]) {
|
|
2009
|
+
s2.push(address[i]);
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
return s1.concat(["compact"]).concat(s2);
|
|
2013
|
+
}
|
|
2014
|
+
function paddedHex(octet) {
|
|
2015
|
+
return parseInt(octet, 16).toString(16).padStart(4, "0");
|
|
2016
|
+
}
|
|
2017
|
+
function unsignByte(b) {
|
|
2018
|
+
return b & 255;
|
|
2019
|
+
}
|
|
2020
|
+
var Address6 = class _Address6 {
|
|
2021
|
+
constructor(address, optionalGroups) {
|
|
2022
|
+
this.addressMinusSuffix = "";
|
|
2023
|
+
this.parsedSubnet = "";
|
|
2024
|
+
this.subnet = "/128";
|
|
2025
|
+
this.subnetMask = 128;
|
|
2026
|
+
this.v4 = false;
|
|
2027
|
+
this.zone = "";
|
|
2028
|
+
this.isInSubnet = common.isInSubnet;
|
|
2029
|
+
this.isCorrect = common.isCorrect(constants6.BITS);
|
|
2030
|
+
if (optionalGroups === void 0) {
|
|
2031
|
+
this.groups = constants6.GROUPS;
|
|
2032
|
+
} else {
|
|
2033
|
+
this.groups = optionalGroups;
|
|
2034
|
+
}
|
|
2035
|
+
this.address = address;
|
|
2036
|
+
const subnet = constants6.RE_SUBNET_STRING.exec(address);
|
|
2037
|
+
if (subnet) {
|
|
2038
|
+
this.parsedSubnet = subnet[0].replace("/", "");
|
|
2039
|
+
this.subnetMask = parseInt(this.parsedSubnet, 10);
|
|
2040
|
+
this.subnet = `/${this.subnetMask}`;
|
|
2041
|
+
if (Number.isNaN(this.subnetMask) || this.subnetMask < 0 || this.subnetMask > constants6.BITS) {
|
|
2042
|
+
throw new address_error_1.AddressError("Invalid subnet mask.");
|
|
2043
|
+
}
|
|
2044
|
+
address = address.replace(constants6.RE_SUBNET_STRING, "");
|
|
2045
|
+
} else if (/\//.test(address)) {
|
|
2046
|
+
throw new address_error_1.AddressError("Invalid subnet mask.");
|
|
2047
|
+
}
|
|
2048
|
+
const zone = constants6.RE_ZONE_STRING.exec(address);
|
|
2049
|
+
if (zone) {
|
|
2050
|
+
this.zone = zone[0];
|
|
2051
|
+
address = address.replace(constants6.RE_ZONE_STRING, "");
|
|
2052
|
+
}
|
|
2053
|
+
this.addressMinusSuffix = address;
|
|
2054
|
+
this.parsedAddress = this.parse(this.addressMinusSuffix);
|
|
2055
|
+
}
|
|
2056
|
+
static isValid(address) {
|
|
2057
|
+
try {
|
|
2058
|
+
new _Address6(address);
|
|
2059
|
+
return true;
|
|
2060
|
+
} catch (e) {
|
|
2061
|
+
return false;
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
/**
|
|
2065
|
+
* Convert a BigInt to a v6 address object
|
|
2066
|
+
* @memberof Address6
|
|
2067
|
+
* @static
|
|
2068
|
+
* @param {bigint} bigInt - a BigInt to convert
|
|
2069
|
+
* @returns {Address6}
|
|
2070
|
+
* @example
|
|
2071
|
+
* var bigInt = BigInt('1000000000000');
|
|
2072
|
+
* var address = Address6.fromBigInt(bigInt);
|
|
2073
|
+
* address.correctForm(); // '::e8:d4a5:1000'
|
|
2074
|
+
*/
|
|
2075
|
+
static fromBigInt(bigInt) {
|
|
2076
|
+
const hex = bigInt.toString(16).padStart(32, "0");
|
|
2077
|
+
const groups = [];
|
|
2078
|
+
let i;
|
|
2079
|
+
for (i = 0; i < constants6.GROUPS; i++) {
|
|
2080
|
+
groups.push(hex.slice(i * 4, (i + 1) * 4));
|
|
2081
|
+
}
|
|
2082
|
+
return new _Address6(groups.join(":"));
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Convert a URL (with optional port number) to an address object
|
|
2086
|
+
* @memberof Address6
|
|
2087
|
+
* @static
|
|
2088
|
+
* @param {string} url - a URL with optional port number
|
|
2089
|
+
* @example
|
|
2090
|
+
* var addressAndPort = Address6.fromURL('http://[ffff::]:8080/foo/');
|
|
2091
|
+
* addressAndPort.address.correctForm(); // 'ffff::'
|
|
2092
|
+
* addressAndPort.port; // 8080
|
|
2093
|
+
*/
|
|
2094
|
+
static fromURL(url) {
|
|
2095
|
+
let host;
|
|
2096
|
+
let port = null;
|
|
2097
|
+
let result;
|
|
2098
|
+
if (url.indexOf("[") !== -1 && url.indexOf("]:") !== -1) {
|
|
2099
|
+
result = constants6.RE_URL_WITH_PORT.exec(url);
|
|
2100
|
+
if (result === null) {
|
|
2101
|
+
return {
|
|
2102
|
+
error: "failed to parse address with port",
|
|
2103
|
+
address: null,
|
|
2104
|
+
port: null
|
|
2105
|
+
};
|
|
2106
|
+
}
|
|
2107
|
+
host = result[1];
|
|
2108
|
+
port = result[2];
|
|
2109
|
+
} else if (url.indexOf("/") !== -1) {
|
|
2110
|
+
url = url.replace(/^[a-z0-9]+:\/\//, "");
|
|
2111
|
+
result = constants6.RE_URL.exec(url);
|
|
2112
|
+
if (result === null) {
|
|
2113
|
+
return {
|
|
2114
|
+
error: "failed to parse address from URL",
|
|
2115
|
+
address: null,
|
|
2116
|
+
port: null
|
|
2117
|
+
};
|
|
2118
|
+
}
|
|
2119
|
+
host = result[1];
|
|
2120
|
+
} else {
|
|
2121
|
+
host = url;
|
|
2122
|
+
}
|
|
2123
|
+
if (port) {
|
|
2124
|
+
port = parseInt(port, 10);
|
|
2125
|
+
if (port < 0 || port > 65536) {
|
|
2126
|
+
port = null;
|
|
2127
|
+
}
|
|
2128
|
+
} else {
|
|
2129
|
+
port = null;
|
|
2130
|
+
}
|
|
2131
|
+
return {
|
|
2132
|
+
address: new _Address6(host),
|
|
2133
|
+
port
|
|
2134
|
+
};
|
|
2135
|
+
}
|
|
2136
|
+
/**
|
|
2137
|
+
* Create an IPv6-mapped address given an IPv4 address
|
|
2138
|
+
* @memberof Address6
|
|
2139
|
+
* @static
|
|
2140
|
+
* @param {string} address - An IPv4 address string
|
|
2141
|
+
* @returns {Address6}
|
|
2142
|
+
* @example
|
|
2143
|
+
* var address = Address6.fromAddress4('192.168.0.1');
|
|
2144
|
+
* address.correctForm(); // '::ffff:c0a8:1'
|
|
2145
|
+
* address.to4in6(); // '::ffff:192.168.0.1'
|
|
2146
|
+
*/
|
|
2147
|
+
static fromAddress4(address) {
|
|
2148
|
+
const address4 = new ipv4_1.Address4(address);
|
|
2149
|
+
const mask6 = constants6.BITS - (constants4.BITS - address4.subnetMask);
|
|
2150
|
+
return new _Address6(`::ffff:${address4.correctForm()}/${mask6}`);
|
|
2151
|
+
}
|
|
2152
|
+
/**
|
|
2153
|
+
* Return an address from ip6.arpa form
|
|
2154
|
+
* @memberof Address6
|
|
2155
|
+
* @static
|
|
2156
|
+
* @param {string} arpaFormAddress - an 'ip6.arpa' form address
|
|
2157
|
+
* @returns {Adress6}
|
|
2158
|
+
* @example
|
|
2159
|
+
* var address = Address6.fromArpa(e.f.f.f.3.c.2.6.f.f.f.e.6.6.8.e.1.0.6.7.9.4.e.c.0.0.0.0.1.0.0.2.ip6.arpa.)
|
|
2160
|
+
* address.correctForm(); // '2001:0:ce49:7601:e866:efff:62c3:fffe'
|
|
2161
|
+
*/
|
|
2162
|
+
static fromArpa(arpaFormAddress) {
|
|
2163
|
+
let address = arpaFormAddress.replace(/(\.ip6\.arpa)?\.$/, "");
|
|
2164
|
+
const semicolonAmount = 7;
|
|
2165
|
+
if (address.length !== 63) {
|
|
2166
|
+
throw new address_error_1.AddressError("Invalid 'ip6.arpa' form.");
|
|
2167
|
+
}
|
|
2168
|
+
const parts = address.split(".").reverse();
|
|
2169
|
+
for (let i = semicolonAmount; i > 0; i--) {
|
|
2170
|
+
const insertIndex = i * 4;
|
|
2171
|
+
parts.splice(insertIndex, 0, ":");
|
|
2172
|
+
}
|
|
2173
|
+
address = parts.join("");
|
|
2174
|
+
return new _Address6(address);
|
|
2175
|
+
}
|
|
2176
|
+
/**
|
|
2177
|
+
* Return the Microsoft UNC transcription of the address
|
|
2178
|
+
* @memberof Address6
|
|
2179
|
+
* @instance
|
|
2180
|
+
* @returns {String} the Microsoft UNC transcription of the address
|
|
2181
|
+
*/
|
|
2182
|
+
microsoftTranscription() {
|
|
2183
|
+
return `${this.correctForm().replace(/:/g, "-")}.ipv6-literal.net`;
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Return the first n bits of the address, defaulting to the subnet mask
|
|
2187
|
+
* @memberof Address6
|
|
2188
|
+
* @instance
|
|
2189
|
+
* @param {number} [mask=subnet] - the number of bits to mask
|
|
2190
|
+
* @returns {String} the first n bits of the address as a string
|
|
2191
|
+
*/
|
|
2192
|
+
mask(mask = this.subnetMask) {
|
|
2193
|
+
return this.getBitsBase2(0, mask);
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* Return the number of possible subnets of a given size in the address
|
|
2197
|
+
* @memberof Address6
|
|
2198
|
+
* @instance
|
|
2199
|
+
* @param {number} [subnetSize=128] - the subnet size
|
|
2200
|
+
* @returns {String}
|
|
2201
|
+
*/
|
|
2202
|
+
// TODO: probably useful to have a numeric version of this too
|
|
2203
|
+
possibleSubnets(subnetSize = 128) {
|
|
2204
|
+
const availableBits = constants6.BITS - this.subnetMask;
|
|
2205
|
+
const subnetBits = Math.abs(subnetSize - constants6.BITS);
|
|
2206
|
+
const subnetPowers = availableBits - subnetBits;
|
|
2207
|
+
if (subnetPowers < 0) {
|
|
2208
|
+
return "0";
|
|
2209
|
+
}
|
|
2210
|
+
return addCommas((BigInt("2") ** BigInt(subnetPowers)).toString(10));
|
|
2211
|
+
}
|
|
2212
|
+
/**
|
|
2213
|
+
* Helper function getting start address.
|
|
2214
|
+
* @memberof Address6
|
|
2215
|
+
* @instance
|
|
2216
|
+
* @returns {bigint}
|
|
2217
|
+
*/
|
|
2218
|
+
_startAddress() {
|
|
2219
|
+
return BigInt(`0b${this.mask() + "0".repeat(constants6.BITS - this.subnetMask)}`);
|
|
2220
|
+
}
|
|
2221
|
+
/**
|
|
2222
|
+
* The first address in the range given by this address' subnet
|
|
2223
|
+
* Often referred to as the Network Address.
|
|
2224
|
+
* @memberof Address6
|
|
2225
|
+
* @instance
|
|
2226
|
+
* @returns {Address6}
|
|
2227
|
+
*/
|
|
2228
|
+
startAddress() {
|
|
2229
|
+
return _Address6.fromBigInt(this._startAddress());
|
|
2230
|
+
}
|
|
2231
|
+
/**
|
|
2232
|
+
* The first host address in the range given by this address's subnet ie
|
|
2233
|
+
* the first address after the Network Address
|
|
2234
|
+
* @memberof Address6
|
|
2235
|
+
* @instance
|
|
2236
|
+
* @returns {Address6}
|
|
2237
|
+
*/
|
|
2238
|
+
startAddressExclusive() {
|
|
2239
|
+
const adjust = BigInt("1");
|
|
2240
|
+
return _Address6.fromBigInt(this._startAddress() + adjust);
|
|
2241
|
+
}
|
|
2242
|
+
/**
|
|
2243
|
+
* Helper function getting end address.
|
|
2244
|
+
* @memberof Address6
|
|
2245
|
+
* @instance
|
|
2246
|
+
* @returns {bigint}
|
|
2247
|
+
*/
|
|
2248
|
+
_endAddress() {
|
|
2249
|
+
return BigInt(`0b${this.mask() + "1".repeat(constants6.BITS - this.subnetMask)}`);
|
|
2250
|
+
}
|
|
2251
|
+
/**
|
|
2252
|
+
* The last address in the range given by this address' subnet
|
|
2253
|
+
* Often referred to as the Broadcast
|
|
2254
|
+
* @memberof Address6
|
|
2255
|
+
* @instance
|
|
2256
|
+
* @returns {Address6}
|
|
2257
|
+
*/
|
|
2258
|
+
endAddress() {
|
|
2259
|
+
return _Address6.fromBigInt(this._endAddress());
|
|
2260
|
+
}
|
|
2261
|
+
/**
|
|
2262
|
+
* The last host address in the range given by this address's subnet ie
|
|
2263
|
+
* the last address prior to the Broadcast Address
|
|
2264
|
+
* @memberof Address6
|
|
2265
|
+
* @instance
|
|
2266
|
+
* @returns {Address6}
|
|
2267
|
+
*/
|
|
2268
|
+
endAddressExclusive() {
|
|
2269
|
+
const adjust = BigInt("1");
|
|
2270
|
+
return _Address6.fromBigInt(this._endAddress() - adjust);
|
|
2271
|
+
}
|
|
2272
|
+
/**
|
|
2273
|
+
* Return the scope of the address
|
|
2274
|
+
* @memberof Address6
|
|
2275
|
+
* @instance
|
|
2276
|
+
* @returns {String}
|
|
2277
|
+
*/
|
|
2278
|
+
getScope() {
|
|
2279
|
+
let scope = constants6.SCOPES[parseInt(this.getBits(12, 16).toString(10), 10)];
|
|
2280
|
+
if (this.getType() === "Global unicast" && scope !== "Link local") {
|
|
2281
|
+
scope = "Global";
|
|
2282
|
+
}
|
|
2283
|
+
return scope || "Unknown";
|
|
2284
|
+
}
|
|
2285
|
+
/**
|
|
2286
|
+
* Return the type of the address
|
|
2287
|
+
* @memberof Address6
|
|
2288
|
+
* @instance
|
|
2289
|
+
* @returns {String}
|
|
2290
|
+
*/
|
|
2291
|
+
getType() {
|
|
2292
|
+
for (const subnet of Object.keys(constants6.TYPES)) {
|
|
2293
|
+
if (this.isInSubnet(new _Address6(subnet))) {
|
|
2294
|
+
return constants6.TYPES[subnet];
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
return "Global unicast";
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* Return the bits in the given range as a BigInt
|
|
2301
|
+
* @memberof Address6
|
|
2302
|
+
* @instance
|
|
2303
|
+
* @returns {bigint}
|
|
2304
|
+
*/
|
|
2305
|
+
getBits(start, end) {
|
|
2306
|
+
return BigInt(`0b${this.getBitsBase2(start, end)}`);
|
|
2307
|
+
}
|
|
2308
|
+
/**
|
|
2309
|
+
* Return the bits in the given range as a base-2 string
|
|
2310
|
+
* @memberof Address6
|
|
2311
|
+
* @instance
|
|
2312
|
+
* @returns {String}
|
|
2313
|
+
*/
|
|
2314
|
+
getBitsBase2(start, end) {
|
|
2315
|
+
return this.binaryZeroPad().slice(start, end);
|
|
2316
|
+
}
|
|
2317
|
+
/**
|
|
2318
|
+
* Return the bits in the given range as a base-16 string
|
|
2319
|
+
* @memberof Address6
|
|
2320
|
+
* @instance
|
|
2321
|
+
* @returns {String}
|
|
2322
|
+
*/
|
|
2323
|
+
getBitsBase16(start, end) {
|
|
2324
|
+
const length = end - start;
|
|
2325
|
+
if (length % 4 !== 0) {
|
|
2326
|
+
throw new Error("Length of bits to retrieve must be divisible by four");
|
|
2327
|
+
}
|
|
2328
|
+
return this.getBits(start, end).toString(16).padStart(length / 4, "0");
|
|
2329
|
+
}
|
|
2330
|
+
/**
|
|
2331
|
+
* Return the bits that are set past the subnet mask length
|
|
2332
|
+
* @memberof Address6
|
|
2333
|
+
* @instance
|
|
2334
|
+
* @returns {String}
|
|
2335
|
+
*/
|
|
2336
|
+
getBitsPastSubnet() {
|
|
2337
|
+
return this.getBitsBase2(this.subnetMask, constants6.BITS);
|
|
2338
|
+
}
|
|
2339
|
+
/**
|
|
2340
|
+
* Return the reversed ip6.arpa form of the address
|
|
2341
|
+
* @memberof Address6
|
|
2342
|
+
* @param {Object} options
|
|
2343
|
+
* @param {boolean} options.omitSuffix - omit the "ip6.arpa" suffix
|
|
2344
|
+
* @instance
|
|
2345
|
+
* @returns {String}
|
|
2346
|
+
*/
|
|
2347
|
+
reverseForm(options) {
|
|
2348
|
+
if (!options) {
|
|
2349
|
+
options = {};
|
|
2350
|
+
}
|
|
2351
|
+
const characters = Math.floor(this.subnetMask / 4);
|
|
2352
|
+
const reversed = this.canonicalForm().replace(/:/g, "").split("").slice(0, characters).reverse().join(".");
|
|
2353
|
+
if (characters > 0) {
|
|
2354
|
+
if (options.omitSuffix) {
|
|
2355
|
+
return reversed;
|
|
2356
|
+
}
|
|
2357
|
+
return `${reversed}.ip6.arpa.`;
|
|
2358
|
+
}
|
|
2359
|
+
if (options.omitSuffix) {
|
|
2360
|
+
return "";
|
|
2361
|
+
}
|
|
2362
|
+
return "ip6.arpa.";
|
|
2363
|
+
}
|
|
2364
|
+
/**
|
|
2365
|
+
* Return the correct form of the address
|
|
2366
|
+
* @memberof Address6
|
|
2367
|
+
* @instance
|
|
2368
|
+
* @returns {String}
|
|
2369
|
+
*/
|
|
2370
|
+
correctForm() {
|
|
2371
|
+
let i;
|
|
2372
|
+
let groups = [];
|
|
2373
|
+
let zeroCounter = 0;
|
|
2374
|
+
const zeroes = [];
|
|
2375
|
+
for (i = 0; i < this.parsedAddress.length; i++) {
|
|
2376
|
+
const value = parseInt(this.parsedAddress[i], 16);
|
|
2377
|
+
if (value === 0) {
|
|
2378
|
+
zeroCounter++;
|
|
2379
|
+
}
|
|
2380
|
+
if (value !== 0 && zeroCounter > 0) {
|
|
2381
|
+
if (zeroCounter > 1) {
|
|
2382
|
+
zeroes.push([i - zeroCounter, i - 1]);
|
|
2383
|
+
}
|
|
2384
|
+
zeroCounter = 0;
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
if (zeroCounter > 1) {
|
|
2388
|
+
zeroes.push([this.parsedAddress.length - zeroCounter, this.parsedAddress.length - 1]);
|
|
2389
|
+
}
|
|
2390
|
+
const zeroLengths = zeroes.map((n) => n[1] - n[0] + 1);
|
|
2391
|
+
if (zeroes.length > 0) {
|
|
2392
|
+
const index = zeroLengths.indexOf(Math.max(...zeroLengths));
|
|
2393
|
+
groups = compact(this.parsedAddress, zeroes[index]);
|
|
2394
|
+
} else {
|
|
2395
|
+
groups = this.parsedAddress;
|
|
2396
|
+
}
|
|
2397
|
+
for (i = 0; i < groups.length; i++) {
|
|
2398
|
+
if (groups[i] !== "compact") {
|
|
2399
|
+
groups[i] = parseInt(groups[i], 16).toString(16);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
let correct = groups.join(":");
|
|
2403
|
+
correct = correct.replace(/^compact$/, "::");
|
|
2404
|
+
correct = correct.replace(/(^compact)|(compact$)/, ":");
|
|
2405
|
+
correct = correct.replace(/compact/, "");
|
|
2406
|
+
return correct;
|
|
2407
|
+
}
|
|
2408
|
+
/**
|
|
2409
|
+
* Return a zero-padded base-2 string representation of the address
|
|
2410
|
+
* @memberof Address6
|
|
2411
|
+
* @instance
|
|
2412
|
+
* @returns {String}
|
|
2413
|
+
* @example
|
|
2414
|
+
* var address = new Address6('2001:4860:4001:803::1011');
|
|
2415
|
+
* address.binaryZeroPad();
|
|
2416
|
+
* // '0010000000000001010010000110000001000000000000010000100000000011
|
|
2417
|
+
* // 0000000000000000000000000000000000000000000000000001000000010001'
|
|
2418
|
+
*/
|
|
2419
|
+
binaryZeroPad() {
|
|
2420
|
+
return this.bigInt().toString(2).padStart(constants6.BITS, "0");
|
|
2421
|
+
}
|
|
2422
|
+
// TODO: Improve the semantics of this helper function
|
|
2423
|
+
parse4in6(address) {
|
|
2424
|
+
const groups = address.split(":");
|
|
2425
|
+
const lastGroup = groups.slice(-1)[0];
|
|
2426
|
+
const address4 = lastGroup.match(constants4.RE_ADDRESS);
|
|
2427
|
+
if (address4) {
|
|
2428
|
+
this.parsedAddress4 = address4[0];
|
|
2429
|
+
this.address4 = new ipv4_1.Address4(this.parsedAddress4);
|
|
2430
|
+
for (let i = 0; i < this.address4.groups; i++) {
|
|
2431
|
+
if (/^0[0-9]+/.test(this.address4.parsedAddress[i])) {
|
|
2432
|
+
throw new address_error_1.AddressError("IPv4 addresses can't have leading zeroes.", address.replace(constants4.RE_ADDRESS, this.address4.parsedAddress.map(spanLeadingZeroes4).join(".")));
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
this.v4 = true;
|
|
2436
|
+
groups[groups.length - 1] = this.address4.toGroup6();
|
|
2437
|
+
address = groups.join(":");
|
|
2438
|
+
}
|
|
2439
|
+
return address;
|
|
2440
|
+
}
|
|
2441
|
+
// TODO: Make private?
|
|
2442
|
+
parse(address) {
|
|
2443
|
+
address = this.parse4in6(address);
|
|
2444
|
+
const badCharacters = address.match(constants6.RE_BAD_CHARACTERS);
|
|
2445
|
+
if (badCharacters) {
|
|
2446
|
+
throw new address_error_1.AddressError(`Bad character${badCharacters.length > 1 ? "s" : ""} detected in address: ${badCharacters.join("")}`, address.replace(constants6.RE_BAD_CHARACTERS, '<span class="parse-error">$1</span>'));
|
|
2447
|
+
}
|
|
2448
|
+
const badAddress = address.match(constants6.RE_BAD_ADDRESS);
|
|
2449
|
+
if (badAddress) {
|
|
2450
|
+
throw new address_error_1.AddressError(`Address failed regex: ${badAddress.join("")}`, address.replace(constants6.RE_BAD_ADDRESS, '<span class="parse-error">$1</span>'));
|
|
2451
|
+
}
|
|
2452
|
+
let groups = [];
|
|
2453
|
+
const halves = address.split("::");
|
|
2454
|
+
if (halves.length === 2) {
|
|
2455
|
+
let first = halves[0].split(":");
|
|
2456
|
+
let last = halves[1].split(":");
|
|
2457
|
+
if (first.length === 1 && first[0] === "") {
|
|
2458
|
+
first = [];
|
|
2459
|
+
}
|
|
2460
|
+
if (last.length === 1 && last[0] === "") {
|
|
2461
|
+
last = [];
|
|
2462
|
+
}
|
|
2463
|
+
const remaining = this.groups - (first.length + last.length);
|
|
2464
|
+
if (!remaining) {
|
|
2465
|
+
throw new address_error_1.AddressError("Error parsing groups");
|
|
2466
|
+
}
|
|
2467
|
+
this.elidedGroups = remaining;
|
|
2468
|
+
this.elisionBegin = first.length;
|
|
2469
|
+
this.elisionEnd = first.length + this.elidedGroups;
|
|
2470
|
+
groups = groups.concat(first);
|
|
2471
|
+
for (let i = 0; i < remaining; i++) {
|
|
2472
|
+
groups.push("0");
|
|
2473
|
+
}
|
|
2474
|
+
groups = groups.concat(last);
|
|
2475
|
+
} else if (halves.length === 1) {
|
|
2476
|
+
groups = address.split(":");
|
|
2477
|
+
this.elidedGroups = 0;
|
|
2478
|
+
} else {
|
|
2479
|
+
throw new address_error_1.AddressError("Too many :: groups found");
|
|
2480
|
+
}
|
|
2481
|
+
groups = groups.map((group) => parseInt(group, 16).toString(16));
|
|
2482
|
+
if (groups.length !== this.groups) {
|
|
2483
|
+
throw new address_error_1.AddressError("Incorrect number of groups found");
|
|
2484
|
+
}
|
|
2485
|
+
return groups;
|
|
2486
|
+
}
|
|
2487
|
+
/**
|
|
2488
|
+
* Return the canonical form of the address
|
|
2489
|
+
* @memberof Address6
|
|
2490
|
+
* @instance
|
|
2491
|
+
* @returns {String}
|
|
2492
|
+
*/
|
|
2493
|
+
canonicalForm() {
|
|
2494
|
+
return this.parsedAddress.map(paddedHex).join(":");
|
|
2495
|
+
}
|
|
2496
|
+
/**
|
|
2497
|
+
* Return the decimal form of the address
|
|
2498
|
+
* @memberof Address6
|
|
2499
|
+
* @instance
|
|
2500
|
+
* @returns {String}
|
|
2501
|
+
*/
|
|
2502
|
+
decimal() {
|
|
2503
|
+
return this.parsedAddress.map((n) => parseInt(n, 16).toString(10).padStart(5, "0")).join(":");
|
|
2504
|
+
}
|
|
2505
|
+
/**
|
|
2506
|
+
* Return the address as a BigInt
|
|
2507
|
+
* @memberof Address6
|
|
2508
|
+
* @instance
|
|
2509
|
+
* @returns {bigint}
|
|
2510
|
+
*/
|
|
2511
|
+
bigInt() {
|
|
2512
|
+
return BigInt(`0x${this.parsedAddress.map(paddedHex).join("")}`);
|
|
2513
|
+
}
|
|
2514
|
+
/**
|
|
2515
|
+
* Return the last two groups of this address as an IPv4 address string
|
|
2516
|
+
* @memberof Address6
|
|
2517
|
+
* @instance
|
|
2518
|
+
* @returns {Address4}
|
|
2519
|
+
* @example
|
|
2520
|
+
* var address = new Address6('2001:4860:4001::1825:bf11');
|
|
2521
|
+
* address.to4().correctForm(); // '24.37.191.17'
|
|
2522
|
+
*/
|
|
2523
|
+
to4() {
|
|
2524
|
+
const binary = this.binaryZeroPad().split("");
|
|
2525
|
+
return ipv4_1.Address4.fromHex(BigInt(`0b${binary.slice(96, 128).join("")}`).toString(16));
|
|
2526
|
+
}
|
|
2527
|
+
/**
|
|
2528
|
+
* Return the v4-in-v6 form of the address
|
|
2529
|
+
* @memberof Address6
|
|
2530
|
+
* @instance
|
|
2531
|
+
* @returns {String}
|
|
2532
|
+
*/
|
|
2533
|
+
to4in6() {
|
|
2534
|
+
const address4 = this.to4();
|
|
2535
|
+
const address6 = new _Address6(this.parsedAddress.slice(0, 6).join(":"), 6);
|
|
2536
|
+
const correct = address6.correctForm();
|
|
2537
|
+
let infix = "";
|
|
2538
|
+
if (!/:$/.test(correct)) {
|
|
2539
|
+
infix = ":";
|
|
2540
|
+
}
|
|
2541
|
+
return correct + infix + address4.address;
|
|
2542
|
+
}
|
|
2543
|
+
/**
|
|
2544
|
+
* Return an object containing the Teredo properties of the address
|
|
2545
|
+
* @memberof Address6
|
|
2546
|
+
* @instance
|
|
2547
|
+
* @returns {Object}
|
|
2548
|
+
*/
|
|
2549
|
+
inspectTeredo() {
|
|
2550
|
+
const prefix = this.getBitsBase16(0, 32);
|
|
2551
|
+
const bitsForUdpPort = this.getBits(80, 96);
|
|
2552
|
+
const udpPort = (bitsForUdpPort ^ BigInt("0xffff")).toString();
|
|
2553
|
+
const server4 = ipv4_1.Address4.fromHex(this.getBitsBase16(32, 64));
|
|
2554
|
+
const bitsForClient4 = this.getBits(96, 128);
|
|
2555
|
+
const client4 = ipv4_1.Address4.fromHex((bitsForClient4 ^ BigInt("0xffffffff")).toString(16));
|
|
2556
|
+
const flagsBase2 = this.getBitsBase2(64, 80);
|
|
2557
|
+
const coneNat = (0, common_1.testBit)(flagsBase2, 15);
|
|
2558
|
+
const reserved = (0, common_1.testBit)(flagsBase2, 14);
|
|
2559
|
+
const groupIndividual = (0, common_1.testBit)(flagsBase2, 8);
|
|
2560
|
+
const universalLocal = (0, common_1.testBit)(flagsBase2, 9);
|
|
2561
|
+
const nonce = BigInt(`0b${flagsBase2.slice(2, 6) + flagsBase2.slice(8, 16)}`).toString(10);
|
|
2562
|
+
return {
|
|
2563
|
+
prefix: `${prefix.slice(0, 4)}:${prefix.slice(4, 8)}`,
|
|
2564
|
+
server4: server4.address,
|
|
2565
|
+
client4: client4.address,
|
|
2566
|
+
flags: flagsBase2,
|
|
2567
|
+
coneNat,
|
|
2568
|
+
microsoft: {
|
|
2569
|
+
reserved,
|
|
2570
|
+
universalLocal,
|
|
2571
|
+
groupIndividual,
|
|
2572
|
+
nonce
|
|
2573
|
+
},
|
|
2574
|
+
udpPort
|
|
2575
|
+
};
|
|
2576
|
+
}
|
|
2577
|
+
/**
|
|
2578
|
+
* Return an object containing the 6to4 properties of the address
|
|
2579
|
+
* @memberof Address6
|
|
2580
|
+
* @instance
|
|
2581
|
+
* @returns {Object}
|
|
2582
|
+
*/
|
|
2583
|
+
inspect6to4() {
|
|
2584
|
+
const prefix = this.getBitsBase16(0, 16);
|
|
2585
|
+
const gateway = ipv4_1.Address4.fromHex(this.getBitsBase16(16, 48));
|
|
2586
|
+
return {
|
|
2587
|
+
prefix: prefix.slice(0, 4),
|
|
2588
|
+
gateway: gateway.address
|
|
2589
|
+
};
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Return a v6 6to4 address from a v6 v4inv6 address
|
|
2593
|
+
* @memberof Address6
|
|
2594
|
+
* @instance
|
|
2595
|
+
* @returns {Address6}
|
|
2596
|
+
*/
|
|
2597
|
+
to6to4() {
|
|
2598
|
+
if (!this.is4()) {
|
|
2599
|
+
return null;
|
|
2600
|
+
}
|
|
2601
|
+
const addr6to4 = [
|
|
2602
|
+
"2002",
|
|
2603
|
+
this.getBitsBase16(96, 112),
|
|
2604
|
+
this.getBitsBase16(112, 128),
|
|
2605
|
+
"",
|
|
2606
|
+
"/16"
|
|
2607
|
+
].join(":");
|
|
2608
|
+
return new _Address6(addr6to4);
|
|
2609
|
+
}
|
|
2610
|
+
/**
|
|
2611
|
+
* Return a byte array
|
|
2612
|
+
* @memberof Address6
|
|
2613
|
+
* @instance
|
|
2614
|
+
* @returns {Array}
|
|
2615
|
+
*/
|
|
2616
|
+
toByteArray() {
|
|
2617
|
+
const valueWithoutPadding = this.bigInt().toString(16);
|
|
2618
|
+
const leadingPad = "0".repeat(valueWithoutPadding.length % 2);
|
|
2619
|
+
const value = `${leadingPad}${valueWithoutPadding}`;
|
|
2620
|
+
const bytes = [];
|
|
2621
|
+
for (let i = 0, length = value.length; i < length; i += 2) {
|
|
2622
|
+
bytes.push(parseInt(value.substring(i, i + 2), 16));
|
|
2623
|
+
}
|
|
2624
|
+
return bytes;
|
|
2625
|
+
}
|
|
2626
|
+
/**
|
|
2627
|
+
* Return an unsigned byte array
|
|
2628
|
+
* @memberof Address6
|
|
2629
|
+
* @instance
|
|
2630
|
+
* @returns {Array}
|
|
2631
|
+
*/
|
|
2632
|
+
toUnsignedByteArray() {
|
|
2633
|
+
return this.toByteArray().map(unsignByte);
|
|
2634
|
+
}
|
|
2635
|
+
/**
|
|
2636
|
+
* Convert a byte array to an Address6 object
|
|
2637
|
+
* @memberof Address6
|
|
2638
|
+
* @static
|
|
2639
|
+
* @returns {Address6}
|
|
2640
|
+
*/
|
|
2641
|
+
static fromByteArray(bytes) {
|
|
2642
|
+
return this.fromUnsignedByteArray(bytes.map(unsignByte));
|
|
2643
|
+
}
|
|
2644
|
+
/**
|
|
2645
|
+
* Convert an unsigned byte array to an Address6 object
|
|
2646
|
+
* @memberof Address6
|
|
2647
|
+
* @static
|
|
2648
|
+
* @returns {Address6}
|
|
2649
|
+
*/
|
|
2650
|
+
static fromUnsignedByteArray(bytes) {
|
|
2651
|
+
const BYTE_MAX = BigInt("256");
|
|
2652
|
+
let result = BigInt("0");
|
|
2653
|
+
let multiplier = BigInt("1");
|
|
2654
|
+
for (let i = bytes.length - 1; i >= 0; i--) {
|
|
2655
|
+
result += multiplier * BigInt(bytes[i].toString(10));
|
|
2656
|
+
multiplier *= BYTE_MAX;
|
|
2657
|
+
}
|
|
2658
|
+
return _Address6.fromBigInt(result);
|
|
2659
|
+
}
|
|
2660
|
+
/**
|
|
2661
|
+
* Returns true if the address is in the canonical form, false otherwise
|
|
2662
|
+
* @memberof Address6
|
|
2663
|
+
* @instance
|
|
2664
|
+
* @returns {boolean}
|
|
2665
|
+
*/
|
|
2666
|
+
isCanonical() {
|
|
2667
|
+
return this.addressMinusSuffix === this.canonicalForm();
|
|
2668
|
+
}
|
|
2669
|
+
/**
|
|
2670
|
+
* Returns true if the address is a link local address, false otherwise
|
|
2671
|
+
* @memberof Address6
|
|
2672
|
+
* @instance
|
|
2673
|
+
* @returns {boolean}
|
|
2674
|
+
*/
|
|
2675
|
+
isLinkLocal() {
|
|
2676
|
+
if (this.getBitsBase2(0, 64) === "1111111010000000000000000000000000000000000000000000000000000000") {
|
|
2677
|
+
return true;
|
|
2678
|
+
}
|
|
2679
|
+
return false;
|
|
2680
|
+
}
|
|
2681
|
+
/**
|
|
2682
|
+
* Returns true if the address is a multicast address, false otherwise
|
|
2683
|
+
* @memberof Address6
|
|
2684
|
+
* @instance
|
|
2685
|
+
* @returns {boolean}
|
|
2686
|
+
*/
|
|
2687
|
+
isMulticast() {
|
|
2688
|
+
return this.getType() === "Multicast";
|
|
2689
|
+
}
|
|
2690
|
+
/**
|
|
2691
|
+
* Returns true if the address is a v4-in-v6 address, false otherwise
|
|
2692
|
+
* @memberof Address6
|
|
2693
|
+
* @instance
|
|
2694
|
+
* @returns {boolean}
|
|
2695
|
+
*/
|
|
2696
|
+
is4() {
|
|
2697
|
+
return this.v4;
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* Returns true if the address is a Teredo address, false otherwise
|
|
2701
|
+
* @memberof Address6
|
|
2702
|
+
* @instance
|
|
2703
|
+
* @returns {boolean}
|
|
2704
|
+
*/
|
|
2705
|
+
isTeredo() {
|
|
2706
|
+
return this.isInSubnet(new _Address6("2001::/32"));
|
|
2707
|
+
}
|
|
2708
|
+
/**
|
|
2709
|
+
* Returns true if the address is a 6to4 address, false otherwise
|
|
2710
|
+
* @memberof Address6
|
|
2711
|
+
* @instance
|
|
2712
|
+
* @returns {boolean}
|
|
2713
|
+
*/
|
|
2714
|
+
is6to4() {
|
|
2715
|
+
return this.isInSubnet(new _Address6("2002::/16"));
|
|
2716
|
+
}
|
|
2717
|
+
/**
|
|
2718
|
+
* Returns true if the address is a loopback address, false otherwise
|
|
2719
|
+
* @memberof Address6
|
|
2720
|
+
* @instance
|
|
2721
|
+
* @returns {boolean}
|
|
2722
|
+
*/
|
|
2723
|
+
isLoopback() {
|
|
2724
|
+
return this.getType() === "Loopback";
|
|
2725
|
+
}
|
|
2726
|
+
// #endregion
|
|
2727
|
+
// #region HTML
|
|
2728
|
+
/**
|
|
2729
|
+
* @returns {String} the address in link form with a default port of 80
|
|
2730
|
+
*/
|
|
2731
|
+
href(optionalPort) {
|
|
2732
|
+
if (optionalPort === void 0) {
|
|
2733
|
+
optionalPort = "";
|
|
2734
|
+
} else {
|
|
2735
|
+
optionalPort = `:${optionalPort}`;
|
|
2736
|
+
}
|
|
2737
|
+
return `http://[${this.correctForm()}]${optionalPort}/`;
|
|
2738
|
+
}
|
|
2739
|
+
/**
|
|
2740
|
+
* @returns {String} a link suitable for conveying the address via a URL hash
|
|
2741
|
+
*/
|
|
2742
|
+
link(options) {
|
|
2743
|
+
if (!options) {
|
|
2744
|
+
options = {};
|
|
2745
|
+
}
|
|
2746
|
+
if (options.className === void 0) {
|
|
2747
|
+
options.className = "";
|
|
2748
|
+
}
|
|
2749
|
+
if (options.prefix === void 0) {
|
|
2750
|
+
options.prefix = "/#address=";
|
|
2751
|
+
}
|
|
2752
|
+
if (options.v4 === void 0) {
|
|
2753
|
+
options.v4 = false;
|
|
2754
|
+
}
|
|
2755
|
+
let formFunction = this.correctForm;
|
|
2756
|
+
if (options.v4) {
|
|
2757
|
+
formFunction = this.to4in6;
|
|
2758
|
+
}
|
|
2759
|
+
const form = formFunction.call(this);
|
|
2760
|
+
if (options.className) {
|
|
2761
|
+
return `<a href="${options.prefix}${form}" class="${options.className}">${form}</a>`;
|
|
2762
|
+
}
|
|
2763
|
+
return `<a href="${options.prefix}${form}">${form}</a>`;
|
|
2764
|
+
}
|
|
2765
|
+
/**
|
|
2766
|
+
* Groups an address
|
|
2767
|
+
* @returns {String}
|
|
2768
|
+
*/
|
|
2769
|
+
group() {
|
|
2770
|
+
if (this.elidedGroups === 0) {
|
|
2771
|
+
return helpers.simpleGroup(this.address).join(":");
|
|
2772
|
+
}
|
|
2773
|
+
assert(typeof this.elidedGroups === "number");
|
|
2774
|
+
assert(typeof this.elisionBegin === "number");
|
|
2775
|
+
const output = [];
|
|
2776
|
+
const [left, right] = this.address.split("::");
|
|
2777
|
+
if (left.length) {
|
|
2778
|
+
output.push(...helpers.simpleGroup(left));
|
|
2779
|
+
} else {
|
|
2780
|
+
output.push("");
|
|
2781
|
+
}
|
|
2782
|
+
const classes = ["hover-group"];
|
|
2783
|
+
for (let i = this.elisionBegin; i < this.elisionBegin + this.elidedGroups; i++) {
|
|
2784
|
+
classes.push(`group-${i}`);
|
|
2785
|
+
}
|
|
2786
|
+
output.push(`<span class="${classes.join(" ")}"></span>`);
|
|
2787
|
+
if (right.length) {
|
|
2788
|
+
output.push(...helpers.simpleGroup(right, this.elisionEnd));
|
|
2789
|
+
} else {
|
|
2790
|
+
output.push("");
|
|
2791
|
+
}
|
|
2792
|
+
if (this.is4()) {
|
|
2793
|
+
assert(this.address4 instanceof ipv4_1.Address4);
|
|
2794
|
+
output.pop();
|
|
2795
|
+
output.push(this.address4.groupForV6());
|
|
2796
|
+
}
|
|
2797
|
+
return output.join(":");
|
|
2798
|
+
}
|
|
2799
|
+
// #endregion
|
|
2800
|
+
// #region Regular expressions
|
|
2801
|
+
/**
|
|
2802
|
+
* Generate a regular expression string that can be used to find or validate
|
|
2803
|
+
* all variations of this address
|
|
2804
|
+
* @memberof Address6
|
|
2805
|
+
* @instance
|
|
2806
|
+
* @param {boolean} substringSearch
|
|
2807
|
+
* @returns {string}
|
|
2808
|
+
*/
|
|
2809
|
+
regularExpressionString(substringSearch = false) {
|
|
2810
|
+
let output = [];
|
|
2811
|
+
const address6 = new _Address6(this.correctForm());
|
|
2812
|
+
if (address6.elidedGroups === 0) {
|
|
2813
|
+
output.push((0, regular_expressions_1.simpleRegularExpression)(address6.parsedAddress));
|
|
2814
|
+
} else if (address6.elidedGroups === constants6.GROUPS) {
|
|
2815
|
+
output.push((0, regular_expressions_1.possibleElisions)(constants6.GROUPS));
|
|
2816
|
+
} else {
|
|
2817
|
+
const halves = address6.address.split("::");
|
|
2818
|
+
if (halves[0].length) {
|
|
2819
|
+
output.push((0, regular_expressions_1.simpleRegularExpression)(halves[0].split(":")));
|
|
2820
|
+
}
|
|
2821
|
+
assert(typeof address6.elidedGroups === "number");
|
|
2822
|
+
output.push((0, regular_expressions_1.possibleElisions)(address6.elidedGroups, halves[0].length !== 0, halves[1].length !== 0));
|
|
2823
|
+
if (halves[1].length) {
|
|
2824
|
+
output.push((0, regular_expressions_1.simpleRegularExpression)(halves[1].split(":")));
|
|
2825
|
+
}
|
|
2826
|
+
output = [output.join(":")];
|
|
2827
|
+
}
|
|
2828
|
+
if (!substringSearch) {
|
|
2829
|
+
output = [
|
|
2830
|
+
"(?=^|",
|
|
2831
|
+
regular_expressions_1.ADDRESS_BOUNDARY,
|
|
2832
|
+
"|[^\\w\\:])(",
|
|
2833
|
+
...output,
|
|
2834
|
+
")(?=[^\\w\\:]|",
|
|
2835
|
+
regular_expressions_1.ADDRESS_BOUNDARY,
|
|
2836
|
+
"|$)"
|
|
2837
|
+
];
|
|
2838
|
+
}
|
|
2839
|
+
return output.join("");
|
|
2840
|
+
}
|
|
2841
|
+
/**
|
|
2842
|
+
* Generate a regular expression that can be used to find or validate all
|
|
2843
|
+
* variations of this address.
|
|
2844
|
+
* @memberof Address6
|
|
2845
|
+
* @instance
|
|
2846
|
+
* @param {boolean} substringSearch
|
|
2847
|
+
* @returns {RegExp}
|
|
2848
|
+
*/
|
|
2849
|
+
regularExpression(substringSearch = false) {
|
|
2850
|
+
return new RegExp(this.regularExpressionString(substringSearch), "i");
|
|
2851
|
+
}
|
|
2852
|
+
};
|
|
2853
|
+
exports.Address6 = Address6;
|
|
2854
|
+
}
|
|
2855
|
+
});
|
|
2856
|
+
|
|
2857
|
+
// ../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ip-address.js
|
|
2858
|
+
var require_ip_address = __commonJS({
|
|
2859
|
+
"../../node_modules/.pnpm/ip-address@10.0.1/node_modules/ip-address/dist/ip-address.js"(exports) {
|
|
2860
|
+
"use strict";
|
|
2861
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
2862
|
+
if (k2 === void 0) k2 = k;
|
|
2863
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
2864
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
2865
|
+
desc = { enumerable: true, get: function() {
|
|
2866
|
+
return m[k];
|
|
2867
|
+
} };
|
|
2868
|
+
}
|
|
2869
|
+
Object.defineProperty(o, k2, desc);
|
|
2870
|
+
}) : (function(o, m, k, k2) {
|
|
2871
|
+
if (k2 === void 0) k2 = k;
|
|
2872
|
+
o[k2] = m[k];
|
|
2873
|
+
}));
|
|
2874
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
2875
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
2876
|
+
}) : function(o, v) {
|
|
2877
|
+
o["default"] = v;
|
|
2878
|
+
});
|
|
2879
|
+
var __importStar = exports && exports.__importStar || function(mod) {
|
|
2880
|
+
if (mod && mod.__esModule) return mod;
|
|
2881
|
+
var result = {};
|
|
2882
|
+
if (mod != null) {
|
|
2883
|
+
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
2884
|
+
}
|
|
2885
|
+
__setModuleDefault(result, mod);
|
|
2886
|
+
return result;
|
|
2887
|
+
};
|
|
2888
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2889
|
+
exports.v6 = exports.AddressError = exports.Address6 = exports.Address4 = void 0;
|
|
2890
|
+
var ipv4_1 = require_ipv4();
|
|
2891
|
+
Object.defineProperty(exports, "Address4", { enumerable: true, get: function() {
|
|
2892
|
+
return ipv4_1.Address4;
|
|
2893
|
+
} });
|
|
2894
|
+
var ipv6_1 = require_ipv6();
|
|
2895
|
+
Object.defineProperty(exports, "Address6", { enumerable: true, get: function() {
|
|
2896
|
+
return ipv6_1.Address6;
|
|
2897
|
+
} });
|
|
2898
|
+
var address_error_1 = require_address_error();
|
|
2899
|
+
Object.defineProperty(exports, "AddressError", { enumerable: true, get: function() {
|
|
2900
|
+
return address_error_1.AddressError;
|
|
2901
|
+
} });
|
|
2902
|
+
var helpers = __importStar(require_helpers());
|
|
2903
|
+
exports.v6 = { helpers };
|
|
2904
|
+
}
|
|
2905
|
+
});
|
|
2906
|
+
|
|
2907
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/helpers.js
|
|
2908
|
+
var require_helpers2 = __commonJS({
|
|
2909
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/helpers.js"(exports) {
|
|
2910
|
+
"use strict";
|
|
2911
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2912
|
+
exports.ipToBuffer = exports.int32ToIpv4 = exports.ipv4ToInt32 = exports.validateSocksClientChainOptions = exports.validateSocksClientOptions = void 0;
|
|
2913
|
+
var util_1 = require_util();
|
|
2914
|
+
var constants_1 = require_constants();
|
|
2915
|
+
var stream = __require("stream");
|
|
2916
|
+
var ip_address_1 = require_ip_address();
|
|
2917
|
+
var net = __require("net");
|
|
2918
|
+
function validateSocksClientOptions(options, acceptedCommands = ["connect", "bind", "associate"]) {
|
|
2919
|
+
if (!constants_1.SocksCommand[options.command]) {
|
|
2920
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommand, options);
|
|
2921
|
+
}
|
|
2922
|
+
if (acceptedCommands.indexOf(options.command) === -1) {
|
|
2923
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandForOperation, options);
|
|
2924
|
+
}
|
|
2925
|
+
if (!isValidSocksRemoteHost(options.destination)) {
|
|
2926
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
|
|
2927
|
+
}
|
|
2928
|
+
if (!isValidSocksProxy(options.proxy)) {
|
|
2929
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
|
|
2930
|
+
}
|
|
2931
|
+
validateCustomProxyAuth(options.proxy, options);
|
|
2932
|
+
if (options.timeout && !isValidTimeoutValue(options.timeout)) {
|
|
2933
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
|
|
2934
|
+
}
|
|
2935
|
+
if (options.existing_socket && !(options.existing_socket instanceof stream.Duplex)) {
|
|
2936
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsExistingSocket, options);
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
exports.validateSocksClientOptions = validateSocksClientOptions;
|
|
2940
|
+
function validateSocksClientChainOptions(options) {
|
|
2941
|
+
if (options.command !== "connect") {
|
|
2942
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandChain, options);
|
|
2943
|
+
}
|
|
2944
|
+
if (!isValidSocksRemoteHost(options.destination)) {
|
|
2945
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
|
|
2946
|
+
}
|
|
2947
|
+
if (!(options.proxies && Array.isArray(options.proxies) && options.proxies.length >= 2)) {
|
|
2948
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxiesLength, options);
|
|
2949
|
+
}
|
|
2950
|
+
options.proxies.forEach((proxy) => {
|
|
2951
|
+
if (!isValidSocksProxy(proxy)) {
|
|
2952
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
|
|
2953
|
+
}
|
|
2954
|
+
validateCustomProxyAuth(proxy, options);
|
|
2955
|
+
});
|
|
2956
|
+
if (options.timeout && !isValidTimeoutValue(options.timeout)) {
|
|
2957
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
exports.validateSocksClientChainOptions = validateSocksClientChainOptions;
|
|
2961
|
+
function validateCustomProxyAuth(proxy, options) {
|
|
2962
|
+
if (proxy.custom_auth_method !== void 0) {
|
|
2963
|
+
if (proxy.custom_auth_method < constants_1.SOCKS5_CUSTOM_AUTH_START || proxy.custom_auth_method > constants_1.SOCKS5_CUSTOM_AUTH_END) {
|
|
2964
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthRange, options);
|
|
2965
|
+
}
|
|
2966
|
+
if (proxy.custom_auth_request_handler === void 0 || typeof proxy.custom_auth_request_handler !== "function") {
|
|
2967
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
|
2968
|
+
}
|
|
2969
|
+
if (proxy.custom_auth_response_size === void 0) {
|
|
2970
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
|
2971
|
+
}
|
|
2972
|
+
if (proxy.custom_auth_response_handler === void 0 || typeof proxy.custom_auth_response_handler !== "function") {
|
|
2973
|
+
throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
function isValidSocksRemoteHost(remoteHost) {
|
|
2978
|
+
return remoteHost && typeof remoteHost.host === "string" && Buffer.byteLength(remoteHost.host) < 256 && typeof remoteHost.port === "number" && remoteHost.port >= 0 && remoteHost.port <= 65535;
|
|
2979
|
+
}
|
|
2980
|
+
function isValidSocksProxy(proxy) {
|
|
2981
|
+
return proxy && (typeof proxy.host === "string" || typeof proxy.ipaddress === "string") && typeof proxy.port === "number" && proxy.port >= 0 && proxy.port <= 65535 && (proxy.type === 4 || proxy.type === 5);
|
|
2982
|
+
}
|
|
2983
|
+
function isValidTimeoutValue(value) {
|
|
2984
|
+
return typeof value === "number" && value > 0;
|
|
2985
|
+
}
|
|
2986
|
+
function ipv4ToInt32(ip) {
|
|
2987
|
+
const address = new ip_address_1.Address4(ip);
|
|
2988
|
+
return address.toArray().reduce((acc, part) => (acc << 8) + part, 0) >>> 0;
|
|
2989
|
+
}
|
|
2990
|
+
exports.ipv4ToInt32 = ipv4ToInt32;
|
|
2991
|
+
function int32ToIpv4(int32) {
|
|
2992
|
+
const octet1 = int32 >>> 24 & 255;
|
|
2993
|
+
const octet2 = int32 >>> 16 & 255;
|
|
2994
|
+
const octet3 = int32 >>> 8 & 255;
|
|
2995
|
+
const octet4 = int32 & 255;
|
|
2996
|
+
return [octet1, octet2, octet3, octet4].join(".");
|
|
2997
|
+
}
|
|
2998
|
+
exports.int32ToIpv4 = int32ToIpv4;
|
|
2999
|
+
function ipToBuffer(ip) {
|
|
3000
|
+
if (net.isIPv4(ip)) {
|
|
3001
|
+
const address = new ip_address_1.Address4(ip);
|
|
3002
|
+
return Buffer.from(address.toArray());
|
|
3003
|
+
} else if (net.isIPv6(ip)) {
|
|
3004
|
+
const address = new ip_address_1.Address6(ip);
|
|
3005
|
+
return Buffer.from(address.canonicalForm().split(":").map((segment) => segment.padStart(4, "0")).join(""), "hex");
|
|
3006
|
+
} else {
|
|
3007
|
+
throw new Error("Invalid IP address format");
|
|
3008
|
+
}
|
|
3009
|
+
}
|
|
3010
|
+
exports.ipToBuffer = ipToBuffer;
|
|
3011
|
+
}
|
|
3012
|
+
});
|
|
3013
|
+
|
|
3014
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/receivebuffer.js
|
|
3015
|
+
var require_receivebuffer = __commonJS({
|
|
3016
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/common/receivebuffer.js"(exports) {
|
|
3017
|
+
"use strict";
|
|
3018
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3019
|
+
exports.ReceiveBuffer = void 0;
|
|
3020
|
+
var ReceiveBuffer = class {
|
|
3021
|
+
constructor(size = 4096) {
|
|
3022
|
+
this.buffer = Buffer.allocUnsafe(size);
|
|
3023
|
+
this.offset = 0;
|
|
3024
|
+
this.originalSize = size;
|
|
3025
|
+
}
|
|
3026
|
+
get length() {
|
|
3027
|
+
return this.offset;
|
|
3028
|
+
}
|
|
3029
|
+
append(data) {
|
|
3030
|
+
if (!Buffer.isBuffer(data)) {
|
|
3031
|
+
throw new Error("Attempted to append a non-buffer instance to ReceiveBuffer.");
|
|
3032
|
+
}
|
|
3033
|
+
if (this.offset + data.length >= this.buffer.length) {
|
|
3034
|
+
const tmp = this.buffer;
|
|
3035
|
+
this.buffer = Buffer.allocUnsafe(Math.max(this.buffer.length + this.originalSize, this.buffer.length + data.length));
|
|
3036
|
+
tmp.copy(this.buffer);
|
|
3037
|
+
}
|
|
3038
|
+
data.copy(this.buffer, this.offset);
|
|
3039
|
+
return this.offset += data.length;
|
|
3040
|
+
}
|
|
3041
|
+
peek(length) {
|
|
3042
|
+
if (length > this.offset) {
|
|
3043
|
+
throw new Error("Attempted to read beyond the bounds of the managed internal data.");
|
|
3044
|
+
}
|
|
3045
|
+
return this.buffer.slice(0, length);
|
|
3046
|
+
}
|
|
3047
|
+
get(length) {
|
|
3048
|
+
if (length > this.offset) {
|
|
3049
|
+
throw new Error("Attempted to read beyond the bounds of the managed internal data.");
|
|
3050
|
+
}
|
|
3051
|
+
const value = Buffer.allocUnsafe(length);
|
|
3052
|
+
this.buffer.slice(0, length).copy(value);
|
|
3053
|
+
this.buffer.copyWithin(0, length, length + this.offset - length);
|
|
3054
|
+
this.offset -= length;
|
|
3055
|
+
return value;
|
|
3056
|
+
}
|
|
3057
|
+
};
|
|
3058
|
+
exports.ReceiveBuffer = ReceiveBuffer;
|
|
3059
|
+
}
|
|
3060
|
+
});
|
|
3061
|
+
|
|
3062
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/client/socksclient.js
|
|
3063
|
+
var require_socksclient = __commonJS({
|
|
3064
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/client/socksclient.js"(exports) {
|
|
3065
|
+
"use strict";
|
|
3066
|
+
var __awaiter = exports && exports.__awaiter || function(thisArg, _arguments, P, generator) {
|
|
3067
|
+
function adopt(value) {
|
|
3068
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
3069
|
+
resolve(value);
|
|
3070
|
+
});
|
|
3071
|
+
}
|
|
3072
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
3073
|
+
function fulfilled(value) {
|
|
3074
|
+
try {
|
|
3075
|
+
step(generator.next(value));
|
|
3076
|
+
} catch (e) {
|
|
3077
|
+
reject(e);
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
function rejected(value) {
|
|
3081
|
+
try {
|
|
3082
|
+
step(generator["throw"](value));
|
|
3083
|
+
} catch (e) {
|
|
3084
|
+
reject(e);
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
function step(result) {
|
|
3088
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
3089
|
+
}
|
|
3090
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
3091
|
+
});
|
|
3092
|
+
};
|
|
3093
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3094
|
+
exports.SocksClientError = exports.SocksClient = void 0;
|
|
3095
|
+
var events_1 = __require("events");
|
|
3096
|
+
var net = __require("net");
|
|
3097
|
+
var smart_buffer_1 = require_smartbuffer();
|
|
3098
|
+
var constants_1 = require_constants();
|
|
3099
|
+
var helpers_1 = require_helpers2();
|
|
3100
|
+
var receivebuffer_1 = require_receivebuffer();
|
|
3101
|
+
var util_1 = require_util();
|
|
3102
|
+
Object.defineProperty(exports, "SocksClientError", { enumerable: true, get: function() {
|
|
3103
|
+
return util_1.SocksClientError;
|
|
3104
|
+
} });
|
|
3105
|
+
var ip_address_1 = require_ip_address();
|
|
3106
|
+
var SocksClient = class _SocksClient extends events_1.EventEmitter {
|
|
3107
|
+
constructor(options) {
|
|
3108
|
+
super();
|
|
3109
|
+
this.options = Object.assign({}, options);
|
|
3110
|
+
(0, helpers_1.validateSocksClientOptions)(options);
|
|
3111
|
+
this.setState(constants_1.SocksClientState.Created);
|
|
3112
|
+
}
|
|
3113
|
+
/**
|
|
3114
|
+
* Creates a new SOCKS connection.
|
|
3115
|
+
*
|
|
3116
|
+
* Note: Supports callbacks and promises. Only supports the connect command.
|
|
3117
|
+
* @param options { SocksClientOptions } Options.
|
|
3118
|
+
* @param callback { Function } An optional callback function.
|
|
3119
|
+
* @returns { Promise }
|
|
3120
|
+
*/
|
|
3121
|
+
static createConnection(options, callback) {
|
|
3122
|
+
return new Promise((resolve, reject) => {
|
|
3123
|
+
try {
|
|
3124
|
+
(0, helpers_1.validateSocksClientOptions)(options, ["connect"]);
|
|
3125
|
+
} catch (err) {
|
|
3126
|
+
if (typeof callback === "function") {
|
|
3127
|
+
callback(err);
|
|
3128
|
+
return resolve(err);
|
|
3129
|
+
} else {
|
|
3130
|
+
return reject(err);
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
const client = new _SocksClient(options);
|
|
3134
|
+
client.connect(options.existing_socket);
|
|
3135
|
+
client.once("established", (info) => {
|
|
3136
|
+
client.removeAllListeners();
|
|
3137
|
+
if (typeof callback === "function") {
|
|
3138
|
+
callback(null, info);
|
|
3139
|
+
resolve(info);
|
|
3140
|
+
} else {
|
|
3141
|
+
resolve(info);
|
|
3142
|
+
}
|
|
3143
|
+
});
|
|
3144
|
+
client.once("error", (err) => {
|
|
3145
|
+
client.removeAllListeners();
|
|
3146
|
+
if (typeof callback === "function") {
|
|
3147
|
+
callback(err);
|
|
3148
|
+
resolve(err);
|
|
3149
|
+
} else {
|
|
3150
|
+
reject(err);
|
|
3151
|
+
}
|
|
3152
|
+
});
|
|
3153
|
+
});
|
|
3154
|
+
}
|
|
3155
|
+
/**
|
|
3156
|
+
* Creates a new SOCKS connection chain to a destination host through 2 or more SOCKS proxies.
|
|
3157
|
+
*
|
|
3158
|
+
* Note: Supports callbacks and promises. Only supports the connect method.
|
|
3159
|
+
* Note: Implemented via createConnection() factory function.
|
|
3160
|
+
* @param options { SocksClientChainOptions } Options
|
|
3161
|
+
* @param callback { Function } An optional callback function.
|
|
3162
|
+
* @returns { Promise }
|
|
3163
|
+
*/
|
|
3164
|
+
static createConnectionChain(options, callback) {
|
|
3165
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
3166
|
+
try {
|
|
3167
|
+
(0, helpers_1.validateSocksClientChainOptions)(options);
|
|
3168
|
+
} catch (err) {
|
|
3169
|
+
if (typeof callback === "function") {
|
|
3170
|
+
callback(err);
|
|
3171
|
+
return resolve(err);
|
|
3172
|
+
} else {
|
|
3173
|
+
return reject(err);
|
|
3174
|
+
}
|
|
3175
|
+
}
|
|
3176
|
+
if (options.randomizeChain) {
|
|
3177
|
+
(0, util_1.shuffleArray)(options.proxies);
|
|
3178
|
+
}
|
|
3179
|
+
try {
|
|
3180
|
+
let sock;
|
|
3181
|
+
for (let i = 0; i < options.proxies.length; i++) {
|
|
3182
|
+
const nextProxy = options.proxies[i];
|
|
3183
|
+
const nextDestination = i === options.proxies.length - 1 ? options.destination : {
|
|
3184
|
+
host: options.proxies[i + 1].host || options.proxies[i + 1].ipaddress,
|
|
3185
|
+
port: options.proxies[i + 1].port
|
|
3186
|
+
};
|
|
3187
|
+
const result = yield _SocksClient.createConnection({
|
|
3188
|
+
command: "connect",
|
|
3189
|
+
proxy: nextProxy,
|
|
3190
|
+
destination: nextDestination,
|
|
3191
|
+
existing_socket: sock
|
|
3192
|
+
});
|
|
3193
|
+
sock = sock || result.socket;
|
|
3194
|
+
}
|
|
3195
|
+
if (typeof callback === "function") {
|
|
3196
|
+
callback(null, { socket: sock });
|
|
3197
|
+
resolve({ socket: sock });
|
|
3198
|
+
} else {
|
|
3199
|
+
resolve({ socket: sock });
|
|
3200
|
+
}
|
|
3201
|
+
} catch (err) {
|
|
3202
|
+
if (typeof callback === "function") {
|
|
3203
|
+
callback(err);
|
|
3204
|
+
resolve(err);
|
|
3205
|
+
} else {
|
|
3206
|
+
reject(err);
|
|
3207
|
+
}
|
|
3208
|
+
}
|
|
3209
|
+
}));
|
|
3210
|
+
}
|
|
3211
|
+
/**
|
|
3212
|
+
* Creates a SOCKS UDP Frame.
|
|
3213
|
+
* @param options
|
|
3214
|
+
*/
|
|
3215
|
+
static createUDPFrame(options) {
|
|
3216
|
+
const buff = new smart_buffer_1.SmartBuffer();
|
|
3217
|
+
buff.writeUInt16BE(0);
|
|
3218
|
+
buff.writeUInt8(options.frameNumber || 0);
|
|
3219
|
+
if (net.isIPv4(options.remoteHost.host)) {
|
|
3220
|
+
buff.writeUInt8(constants_1.Socks5HostType.IPv4);
|
|
3221
|
+
buff.writeUInt32BE((0, helpers_1.ipv4ToInt32)(options.remoteHost.host));
|
|
3222
|
+
} else if (net.isIPv6(options.remoteHost.host)) {
|
|
3223
|
+
buff.writeUInt8(constants_1.Socks5HostType.IPv6);
|
|
3224
|
+
buff.writeBuffer((0, helpers_1.ipToBuffer)(options.remoteHost.host));
|
|
3225
|
+
} else {
|
|
3226
|
+
buff.writeUInt8(constants_1.Socks5HostType.Hostname);
|
|
3227
|
+
buff.writeUInt8(Buffer.byteLength(options.remoteHost.host));
|
|
3228
|
+
buff.writeString(options.remoteHost.host);
|
|
3229
|
+
}
|
|
3230
|
+
buff.writeUInt16BE(options.remoteHost.port);
|
|
3231
|
+
buff.writeBuffer(options.data);
|
|
3232
|
+
return buff.toBuffer();
|
|
3233
|
+
}
|
|
3234
|
+
/**
|
|
3235
|
+
* Parses a SOCKS UDP frame.
|
|
3236
|
+
* @param data
|
|
3237
|
+
*/
|
|
3238
|
+
static parseUDPFrame(data) {
|
|
3239
|
+
const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
|
|
3240
|
+
buff.readOffset = 2;
|
|
3241
|
+
const frameNumber = buff.readUInt8();
|
|
3242
|
+
const hostType = buff.readUInt8();
|
|
3243
|
+
let remoteHost;
|
|
3244
|
+
if (hostType === constants_1.Socks5HostType.IPv4) {
|
|
3245
|
+
remoteHost = (0, helpers_1.int32ToIpv4)(buff.readUInt32BE());
|
|
3246
|
+
} else if (hostType === constants_1.Socks5HostType.IPv6) {
|
|
3247
|
+
remoteHost = ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm();
|
|
3248
|
+
} else {
|
|
3249
|
+
remoteHost = buff.readString(buff.readUInt8());
|
|
3250
|
+
}
|
|
3251
|
+
const remotePort = buff.readUInt16BE();
|
|
3252
|
+
return {
|
|
3253
|
+
frameNumber,
|
|
3254
|
+
remoteHost: {
|
|
3255
|
+
host: remoteHost,
|
|
3256
|
+
port: remotePort
|
|
3257
|
+
},
|
|
3258
|
+
data: buff.readBuffer()
|
|
3259
|
+
};
|
|
3260
|
+
}
|
|
3261
|
+
/**
|
|
3262
|
+
* Internal state setter. If the SocksClient is in an error state, it cannot be changed to a non error state.
|
|
3263
|
+
*/
|
|
3264
|
+
setState(newState) {
|
|
3265
|
+
if (this.state !== constants_1.SocksClientState.Error) {
|
|
3266
|
+
this.state = newState;
|
|
3267
|
+
}
|
|
3268
|
+
}
|
|
3269
|
+
/**
|
|
3270
|
+
* Starts the connection establishment to the proxy and destination.
|
|
3271
|
+
* @param existingSocket Connected socket to use instead of creating a new one (internal use).
|
|
3272
|
+
*/
|
|
3273
|
+
connect(existingSocket) {
|
|
3274
|
+
this.onDataReceived = (data) => this.onDataReceivedHandler(data);
|
|
3275
|
+
this.onClose = () => this.onCloseHandler();
|
|
3276
|
+
this.onError = (err) => this.onErrorHandler(err);
|
|
3277
|
+
this.onConnect = () => this.onConnectHandler();
|
|
3278
|
+
const timer = setTimeout(() => this.onEstablishedTimeout(), this.options.timeout || constants_1.DEFAULT_TIMEOUT);
|
|
3279
|
+
if (timer.unref && typeof timer.unref === "function") {
|
|
3280
|
+
timer.unref();
|
|
3281
|
+
}
|
|
3282
|
+
if (existingSocket) {
|
|
3283
|
+
this.socket = existingSocket;
|
|
3284
|
+
} else {
|
|
3285
|
+
this.socket = new net.Socket();
|
|
3286
|
+
}
|
|
3287
|
+
this.socket.once("close", this.onClose);
|
|
3288
|
+
this.socket.once("error", this.onError);
|
|
3289
|
+
this.socket.once("connect", this.onConnect);
|
|
3290
|
+
this.socket.on("data", this.onDataReceived);
|
|
3291
|
+
this.setState(constants_1.SocksClientState.Connecting);
|
|
3292
|
+
this.receiveBuffer = new receivebuffer_1.ReceiveBuffer();
|
|
3293
|
+
if (existingSocket) {
|
|
3294
|
+
this.socket.emit("connect");
|
|
3295
|
+
} else {
|
|
3296
|
+
this.socket.connect(this.getSocketOptions());
|
|
3297
|
+
if (this.options.set_tcp_nodelay !== void 0 && this.options.set_tcp_nodelay !== null) {
|
|
3298
|
+
this.socket.setNoDelay(!!this.options.set_tcp_nodelay);
|
|
3299
|
+
}
|
|
3300
|
+
}
|
|
3301
|
+
this.prependOnceListener("established", (info) => {
|
|
3302
|
+
setImmediate(() => {
|
|
3303
|
+
if (this.receiveBuffer.length > 0) {
|
|
3304
|
+
const excessData = this.receiveBuffer.get(this.receiveBuffer.length);
|
|
3305
|
+
info.socket.emit("data", excessData);
|
|
3306
|
+
}
|
|
3307
|
+
info.socket.resume();
|
|
3308
|
+
});
|
|
3309
|
+
});
|
|
3310
|
+
}
|
|
3311
|
+
// Socket options (defaults host/port to options.proxy.host/options.proxy.port)
|
|
3312
|
+
getSocketOptions() {
|
|
3313
|
+
return Object.assign(Object.assign({}, this.options.socket_options), { host: this.options.proxy.host || this.options.proxy.ipaddress, port: this.options.proxy.port });
|
|
3314
|
+
}
|
|
3315
|
+
/**
|
|
3316
|
+
* Handles internal Socks timeout callback.
|
|
3317
|
+
* Note: If the Socks client is not BoundWaitingForConnection or Established, the connection will be closed.
|
|
3318
|
+
*/
|
|
3319
|
+
onEstablishedTimeout() {
|
|
3320
|
+
if (this.state !== constants_1.SocksClientState.Established && this.state !== constants_1.SocksClientState.BoundWaitingForConnection) {
|
|
3321
|
+
this.closeSocket(constants_1.ERRORS.ProxyConnectionTimedOut);
|
|
3322
|
+
}
|
|
3323
|
+
}
|
|
3324
|
+
/**
|
|
3325
|
+
* Handles Socket connect event.
|
|
3326
|
+
*/
|
|
3327
|
+
onConnectHandler() {
|
|
3328
|
+
this.setState(constants_1.SocksClientState.Connected);
|
|
3329
|
+
if (this.options.proxy.type === 4) {
|
|
3330
|
+
this.sendSocks4InitialHandshake();
|
|
3331
|
+
} else {
|
|
3332
|
+
this.sendSocks5InitialHandshake();
|
|
3333
|
+
}
|
|
3334
|
+
this.setState(constants_1.SocksClientState.SentInitialHandshake);
|
|
3335
|
+
}
|
|
3336
|
+
/**
|
|
3337
|
+
* Handles Socket data event.
|
|
3338
|
+
* @param data
|
|
3339
|
+
*/
|
|
3340
|
+
onDataReceivedHandler(data) {
|
|
3341
|
+
this.receiveBuffer.append(data);
|
|
3342
|
+
this.processData();
|
|
3343
|
+
}
|
|
3344
|
+
/**
|
|
3345
|
+
* Handles processing of the data we have received.
|
|
3346
|
+
*/
|
|
3347
|
+
processData() {
|
|
3348
|
+
while (this.state !== constants_1.SocksClientState.Established && this.state !== constants_1.SocksClientState.Error && this.receiveBuffer.length >= this.nextRequiredPacketBufferSize) {
|
|
3349
|
+
if (this.state === constants_1.SocksClientState.SentInitialHandshake) {
|
|
3350
|
+
if (this.options.proxy.type === 4) {
|
|
3351
|
+
this.handleSocks4FinalHandshakeResponse();
|
|
3352
|
+
} else {
|
|
3353
|
+
this.handleInitialSocks5HandshakeResponse();
|
|
3354
|
+
}
|
|
3355
|
+
} else if (this.state === constants_1.SocksClientState.SentAuthentication) {
|
|
3356
|
+
this.handleInitialSocks5AuthenticationHandshakeResponse();
|
|
3357
|
+
} else if (this.state === constants_1.SocksClientState.SentFinalHandshake) {
|
|
3358
|
+
this.handleSocks5FinalHandshakeResponse();
|
|
3359
|
+
} else if (this.state === constants_1.SocksClientState.BoundWaitingForConnection) {
|
|
3360
|
+
if (this.options.proxy.type === 4) {
|
|
3361
|
+
this.handleSocks4IncomingConnectionResponse();
|
|
3362
|
+
} else {
|
|
3363
|
+
this.handleSocks5IncomingConnectionResponse();
|
|
3364
|
+
}
|
|
3365
|
+
} else {
|
|
3366
|
+
this.closeSocket(constants_1.ERRORS.InternalError);
|
|
3367
|
+
break;
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
}
|
|
3371
|
+
/**
|
|
3372
|
+
* Handles Socket close event.
|
|
3373
|
+
* @param had_error
|
|
3374
|
+
*/
|
|
3375
|
+
onCloseHandler() {
|
|
3376
|
+
this.closeSocket(constants_1.ERRORS.SocketClosed);
|
|
3377
|
+
}
|
|
3378
|
+
/**
|
|
3379
|
+
* Handles Socket error event.
|
|
3380
|
+
* @param err
|
|
3381
|
+
*/
|
|
3382
|
+
onErrorHandler(err) {
|
|
3383
|
+
this.closeSocket(err.message);
|
|
3384
|
+
}
|
|
3385
|
+
/**
|
|
3386
|
+
* Removes internal event listeners on the underlying Socket.
|
|
3387
|
+
*/
|
|
3388
|
+
removeInternalSocketHandlers() {
|
|
3389
|
+
this.socket.pause();
|
|
3390
|
+
this.socket.removeListener("data", this.onDataReceived);
|
|
3391
|
+
this.socket.removeListener("close", this.onClose);
|
|
3392
|
+
this.socket.removeListener("error", this.onError);
|
|
3393
|
+
this.socket.removeListener("connect", this.onConnect);
|
|
3394
|
+
}
|
|
3395
|
+
/**
|
|
3396
|
+
* Closes and destroys the underlying Socket. Emits an error event.
|
|
3397
|
+
* @param err { String } An error string to include in error event.
|
|
3398
|
+
*/
|
|
3399
|
+
closeSocket(err) {
|
|
3400
|
+
if (this.state !== constants_1.SocksClientState.Error) {
|
|
3401
|
+
this.setState(constants_1.SocksClientState.Error);
|
|
3402
|
+
this.socket.destroy();
|
|
3403
|
+
this.removeInternalSocketHandlers();
|
|
3404
|
+
this.emit("error", new util_1.SocksClientError(err, this.options));
|
|
3405
|
+
}
|
|
3406
|
+
}
|
|
3407
|
+
/**
|
|
3408
|
+
* Sends initial Socks v4 handshake request.
|
|
3409
|
+
*/
|
|
3410
|
+
sendSocks4InitialHandshake() {
|
|
3411
|
+
const userId = this.options.proxy.userId || "";
|
|
3412
|
+
const buff = new smart_buffer_1.SmartBuffer();
|
|
3413
|
+
buff.writeUInt8(4);
|
|
3414
|
+
buff.writeUInt8(constants_1.SocksCommand[this.options.command]);
|
|
3415
|
+
buff.writeUInt16BE(this.options.destination.port);
|
|
3416
|
+
if (net.isIPv4(this.options.destination.host)) {
|
|
3417
|
+
buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
|
|
3418
|
+
buff.writeStringNT(userId);
|
|
3419
|
+
} else {
|
|
3420
|
+
buff.writeUInt8(0);
|
|
3421
|
+
buff.writeUInt8(0);
|
|
3422
|
+
buff.writeUInt8(0);
|
|
3423
|
+
buff.writeUInt8(1);
|
|
3424
|
+
buff.writeStringNT(userId);
|
|
3425
|
+
buff.writeStringNT(this.options.destination.host);
|
|
3426
|
+
}
|
|
3427
|
+
this.nextRequiredPacketBufferSize = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks4Response;
|
|
3428
|
+
this.socket.write(buff.toBuffer());
|
|
3429
|
+
}
|
|
3430
|
+
/**
|
|
3431
|
+
* Handles Socks v4 handshake response.
|
|
3432
|
+
* @param data
|
|
3433
|
+
*/
|
|
3434
|
+
handleSocks4FinalHandshakeResponse() {
|
|
3435
|
+
const data = this.receiveBuffer.get(8);
|
|
3436
|
+
if (data[1] !== constants_1.Socks4Response.Granted) {
|
|
3437
|
+
this.closeSocket(`${constants_1.ERRORS.Socks4ProxyRejectedConnection} - (${constants_1.Socks4Response[data[1]]})`);
|
|
3438
|
+
} else {
|
|
3439
|
+
if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.bind) {
|
|
3440
|
+
const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
|
|
3441
|
+
buff.readOffset = 2;
|
|
3442
|
+
const remoteHost = {
|
|
3443
|
+
port: buff.readUInt16BE(),
|
|
3444
|
+
host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE())
|
|
3445
|
+
};
|
|
3446
|
+
if (remoteHost.host === "0.0.0.0") {
|
|
3447
|
+
remoteHost.host = this.options.proxy.ipaddress;
|
|
3448
|
+
}
|
|
3449
|
+
this.setState(constants_1.SocksClientState.BoundWaitingForConnection);
|
|
3450
|
+
this.emit("bound", { remoteHost, socket: this.socket });
|
|
3451
|
+
} else {
|
|
3452
|
+
this.setState(constants_1.SocksClientState.Established);
|
|
3453
|
+
this.removeInternalSocketHandlers();
|
|
3454
|
+
this.emit("established", { socket: this.socket });
|
|
3455
|
+
}
|
|
3456
|
+
}
|
|
3457
|
+
}
|
|
3458
|
+
/**
|
|
3459
|
+
* Handles Socks v4 incoming connection request (BIND)
|
|
3460
|
+
* @param data
|
|
3461
|
+
*/
|
|
3462
|
+
handleSocks4IncomingConnectionResponse() {
|
|
3463
|
+
const data = this.receiveBuffer.get(8);
|
|
3464
|
+
if (data[1] !== constants_1.Socks4Response.Granted) {
|
|
3465
|
+
this.closeSocket(`${constants_1.ERRORS.Socks4ProxyRejectedIncomingBoundConnection} - (${constants_1.Socks4Response[data[1]]})`);
|
|
3466
|
+
} else {
|
|
3467
|
+
const buff = smart_buffer_1.SmartBuffer.fromBuffer(data);
|
|
3468
|
+
buff.readOffset = 2;
|
|
3469
|
+
const remoteHost = {
|
|
3470
|
+
port: buff.readUInt16BE(),
|
|
3471
|
+
host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE())
|
|
3472
|
+
};
|
|
3473
|
+
this.setState(constants_1.SocksClientState.Established);
|
|
3474
|
+
this.removeInternalSocketHandlers();
|
|
3475
|
+
this.emit("established", { remoteHost, socket: this.socket });
|
|
3476
|
+
}
|
|
3477
|
+
}
|
|
3478
|
+
/**
|
|
3479
|
+
* Sends initial Socks v5 handshake request.
|
|
3480
|
+
*/
|
|
3481
|
+
sendSocks5InitialHandshake() {
|
|
3482
|
+
const buff = new smart_buffer_1.SmartBuffer();
|
|
3483
|
+
const supportedAuthMethods = [constants_1.Socks5Auth.NoAuth];
|
|
3484
|
+
if (this.options.proxy.userId || this.options.proxy.password) {
|
|
3485
|
+
supportedAuthMethods.push(constants_1.Socks5Auth.UserPass);
|
|
3486
|
+
}
|
|
3487
|
+
if (this.options.proxy.custom_auth_method !== void 0) {
|
|
3488
|
+
supportedAuthMethods.push(this.options.proxy.custom_auth_method);
|
|
3489
|
+
}
|
|
3490
|
+
buff.writeUInt8(5);
|
|
3491
|
+
buff.writeUInt8(supportedAuthMethods.length);
|
|
3492
|
+
for (const authMethod of supportedAuthMethods) {
|
|
3493
|
+
buff.writeUInt8(authMethod);
|
|
3494
|
+
}
|
|
3495
|
+
this.nextRequiredPacketBufferSize = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5InitialHandshakeResponse;
|
|
3496
|
+
this.socket.write(buff.toBuffer());
|
|
3497
|
+
this.setState(constants_1.SocksClientState.SentInitialHandshake);
|
|
3498
|
+
}
|
|
3499
|
+
/**
|
|
3500
|
+
* Handles initial Socks v5 handshake response.
|
|
3501
|
+
* @param data
|
|
3502
|
+
*/
|
|
3503
|
+
handleInitialSocks5HandshakeResponse() {
|
|
3504
|
+
const data = this.receiveBuffer.get(2);
|
|
3505
|
+
if (data[0] !== 5) {
|
|
3506
|
+
this.closeSocket(constants_1.ERRORS.InvalidSocks5IntiailHandshakeSocksVersion);
|
|
3507
|
+
} else if (data[1] === constants_1.SOCKS5_NO_ACCEPTABLE_AUTH) {
|
|
3508
|
+
this.closeSocket(constants_1.ERRORS.InvalidSocks5InitialHandshakeNoAcceptedAuthType);
|
|
3509
|
+
} else {
|
|
3510
|
+
if (data[1] === constants_1.Socks5Auth.NoAuth) {
|
|
3511
|
+
this.socks5ChosenAuthType = constants_1.Socks5Auth.NoAuth;
|
|
3512
|
+
this.sendSocks5CommandRequest();
|
|
3513
|
+
} else if (data[1] === constants_1.Socks5Auth.UserPass) {
|
|
3514
|
+
this.socks5ChosenAuthType = constants_1.Socks5Auth.UserPass;
|
|
3515
|
+
this.sendSocks5UserPassAuthentication();
|
|
3516
|
+
} else if (data[1] === this.options.proxy.custom_auth_method) {
|
|
3517
|
+
this.socks5ChosenAuthType = this.options.proxy.custom_auth_method;
|
|
3518
|
+
this.sendSocks5CustomAuthentication();
|
|
3519
|
+
} else {
|
|
3520
|
+
this.closeSocket(constants_1.ERRORS.InvalidSocks5InitialHandshakeUnknownAuthType);
|
|
3521
|
+
}
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
/**
|
|
3525
|
+
* Sends Socks v5 user & password auth handshake.
|
|
3526
|
+
*
|
|
3527
|
+
* Note: No auth and user/pass are currently supported.
|
|
3528
|
+
*/
|
|
3529
|
+
sendSocks5UserPassAuthentication() {
|
|
3530
|
+
const userId = this.options.proxy.userId || "";
|
|
3531
|
+
const password = this.options.proxy.password || "";
|
|
3532
|
+
const buff = new smart_buffer_1.SmartBuffer();
|
|
3533
|
+
buff.writeUInt8(1);
|
|
3534
|
+
buff.writeUInt8(Buffer.byteLength(userId));
|
|
3535
|
+
buff.writeString(userId);
|
|
3536
|
+
buff.writeUInt8(Buffer.byteLength(password));
|
|
3537
|
+
buff.writeString(password);
|
|
3538
|
+
this.nextRequiredPacketBufferSize = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5UserPassAuthenticationResponse;
|
|
3539
|
+
this.socket.write(buff.toBuffer());
|
|
3540
|
+
this.setState(constants_1.SocksClientState.SentAuthentication);
|
|
3541
|
+
}
|
|
3542
|
+
sendSocks5CustomAuthentication() {
|
|
3543
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3544
|
+
this.nextRequiredPacketBufferSize = this.options.proxy.custom_auth_response_size;
|
|
3545
|
+
this.socket.write(yield this.options.proxy.custom_auth_request_handler());
|
|
3546
|
+
this.setState(constants_1.SocksClientState.SentAuthentication);
|
|
3547
|
+
});
|
|
3548
|
+
}
|
|
3549
|
+
handleSocks5CustomAuthHandshakeResponse(data) {
|
|
3550
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3551
|
+
return yield this.options.proxy.custom_auth_response_handler(data);
|
|
3552
|
+
});
|
|
3553
|
+
}
|
|
3554
|
+
handleSocks5AuthenticationNoAuthHandshakeResponse(data) {
|
|
3555
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3556
|
+
return data[1] === 0;
|
|
3557
|
+
});
|
|
3558
|
+
}
|
|
3559
|
+
handleSocks5AuthenticationUserPassHandshakeResponse(data) {
|
|
3560
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3561
|
+
return data[1] === 0;
|
|
3562
|
+
});
|
|
3563
|
+
}
|
|
3564
|
+
/**
|
|
3565
|
+
* Handles Socks v5 auth handshake response.
|
|
3566
|
+
* @param data
|
|
3567
|
+
*/
|
|
3568
|
+
handleInitialSocks5AuthenticationHandshakeResponse() {
|
|
3569
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3570
|
+
this.setState(constants_1.SocksClientState.ReceivedAuthenticationResponse);
|
|
3571
|
+
let authResult = false;
|
|
3572
|
+
if (this.socks5ChosenAuthType === constants_1.Socks5Auth.NoAuth) {
|
|
3573
|
+
authResult = yield this.handleSocks5AuthenticationNoAuthHandshakeResponse(this.receiveBuffer.get(2));
|
|
3574
|
+
} else if (this.socks5ChosenAuthType === constants_1.Socks5Auth.UserPass) {
|
|
3575
|
+
authResult = yield this.handleSocks5AuthenticationUserPassHandshakeResponse(this.receiveBuffer.get(2));
|
|
3576
|
+
} else if (this.socks5ChosenAuthType === this.options.proxy.custom_auth_method) {
|
|
3577
|
+
authResult = yield this.handleSocks5CustomAuthHandshakeResponse(this.receiveBuffer.get(this.options.proxy.custom_auth_response_size));
|
|
3578
|
+
}
|
|
3579
|
+
if (!authResult) {
|
|
3580
|
+
this.closeSocket(constants_1.ERRORS.Socks5AuthenticationFailed);
|
|
3581
|
+
} else {
|
|
3582
|
+
this.sendSocks5CommandRequest();
|
|
3583
|
+
}
|
|
3584
|
+
});
|
|
3585
|
+
}
|
|
3586
|
+
/**
|
|
3587
|
+
* Sends Socks v5 final handshake request.
|
|
3588
|
+
*/
|
|
3589
|
+
sendSocks5CommandRequest() {
|
|
3590
|
+
const buff = new smart_buffer_1.SmartBuffer();
|
|
3591
|
+
buff.writeUInt8(5);
|
|
3592
|
+
buff.writeUInt8(constants_1.SocksCommand[this.options.command]);
|
|
3593
|
+
buff.writeUInt8(0);
|
|
3594
|
+
if (net.isIPv4(this.options.destination.host)) {
|
|
3595
|
+
buff.writeUInt8(constants_1.Socks5HostType.IPv4);
|
|
3596
|
+
buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
|
|
3597
|
+
} else if (net.isIPv6(this.options.destination.host)) {
|
|
3598
|
+
buff.writeUInt8(constants_1.Socks5HostType.IPv6);
|
|
3599
|
+
buff.writeBuffer((0, helpers_1.ipToBuffer)(this.options.destination.host));
|
|
3600
|
+
} else {
|
|
3601
|
+
buff.writeUInt8(constants_1.Socks5HostType.Hostname);
|
|
3602
|
+
buff.writeUInt8(this.options.destination.host.length);
|
|
3603
|
+
buff.writeString(this.options.destination.host);
|
|
3604
|
+
}
|
|
3605
|
+
buff.writeUInt16BE(this.options.destination.port);
|
|
3606
|
+
this.nextRequiredPacketBufferSize = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
|
|
3607
|
+
this.socket.write(buff.toBuffer());
|
|
3608
|
+
this.setState(constants_1.SocksClientState.SentFinalHandshake);
|
|
3609
|
+
}
|
|
3610
|
+
/**
|
|
3611
|
+
* Handles Socks v5 final handshake response.
|
|
3612
|
+
* @param data
|
|
3613
|
+
*/
|
|
3614
|
+
handleSocks5FinalHandshakeResponse() {
|
|
3615
|
+
const header = this.receiveBuffer.peek(5);
|
|
3616
|
+
if (header[0] !== 5 || header[1] !== constants_1.Socks5Response.Granted) {
|
|
3617
|
+
this.closeSocket(`${constants_1.ERRORS.InvalidSocks5FinalHandshakeRejected} - ${constants_1.Socks5Response[header[1]]}`);
|
|
3618
|
+
} else {
|
|
3619
|
+
const addressType = header[3];
|
|
3620
|
+
let remoteHost;
|
|
3621
|
+
let buff;
|
|
3622
|
+
if (addressType === constants_1.Socks5HostType.IPv4) {
|
|
3623
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
|
|
3624
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3625
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3626
|
+
return;
|
|
3627
|
+
}
|
|
3628
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
|
|
3629
|
+
remoteHost = {
|
|
3630
|
+
host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
|
|
3631
|
+
port: buff.readUInt16BE()
|
|
3632
|
+
};
|
|
3633
|
+
if (remoteHost.host === "0.0.0.0") {
|
|
3634
|
+
remoteHost.host = this.options.proxy.ipaddress;
|
|
3635
|
+
}
|
|
3636
|
+
} else if (addressType === constants_1.Socks5HostType.Hostname) {
|
|
3637
|
+
const hostLength = header[4];
|
|
3638
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(hostLength);
|
|
3639
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3640
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3641
|
+
return;
|
|
3642
|
+
}
|
|
3643
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(5));
|
|
3644
|
+
remoteHost = {
|
|
3645
|
+
host: buff.readString(hostLength),
|
|
3646
|
+
port: buff.readUInt16BE()
|
|
3647
|
+
};
|
|
3648
|
+
} else if (addressType === constants_1.Socks5HostType.IPv6) {
|
|
3649
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
|
|
3650
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3651
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3652
|
+
return;
|
|
3653
|
+
}
|
|
3654
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
|
|
3655
|
+
remoteHost = {
|
|
3656
|
+
host: ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm(),
|
|
3657
|
+
port: buff.readUInt16BE()
|
|
3658
|
+
};
|
|
3659
|
+
}
|
|
3660
|
+
this.setState(constants_1.SocksClientState.ReceivedFinalResponse);
|
|
3661
|
+
if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.connect) {
|
|
3662
|
+
this.setState(constants_1.SocksClientState.Established);
|
|
3663
|
+
this.removeInternalSocketHandlers();
|
|
3664
|
+
this.emit("established", { remoteHost, socket: this.socket });
|
|
3665
|
+
} else if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.bind) {
|
|
3666
|
+
this.setState(constants_1.SocksClientState.BoundWaitingForConnection);
|
|
3667
|
+
this.nextRequiredPacketBufferSize = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHeader;
|
|
3668
|
+
this.emit("bound", { remoteHost, socket: this.socket });
|
|
3669
|
+
} else if (constants_1.SocksCommand[this.options.command] === constants_1.SocksCommand.associate) {
|
|
3670
|
+
this.setState(constants_1.SocksClientState.Established);
|
|
3671
|
+
this.removeInternalSocketHandlers();
|
|
3672
|
+
this.emit("established", {
|
|
3673
|
+
remoteHost,
|
|
3674
|
+
socket: this.socket
|
|
3675
|
+
});
|
|
3676
|
+
}
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
/**
|
|
3680
|
+
* Handles Socks v5 incoming connection request (BIND).
|
|
3681
|
+
*/
|
|
3682
|
+
handleSocks5IncomingConnectionResponse() {
|
|
3683
|
+
const header = this.receiveBuffer.peek(5);
|
|
3684
|
+
if (header[0] !== 5 || header[1] !== constants_1.Socks5Response.Granted) {
|
|
3685
|
+
this.closeSocket(`${constants_1.ERRORS.Socks5ProxyRejectedIncomingBoundConnection} - ${constants_1.Socks5Response[header[1]]}`);
|
|
3686
|
+
} else {
|
|
3687
|
+
const addressType = header[3];
|
|
3688
|
+
let remoteHost;
|
|
3689
|
+
let buff;
|
|
3690
|
+
if (addressType === constants_1.Socks5HostType.IPv4) {
|
|
3691
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv4;
|
|
3692
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3693
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3694
|
+
return;
|
|
3695
|
+
}
|
|
3696
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
|
|
3697
|
+
remoteHost = {
|
|
3698
|
+
host: (0, helpers_1.int32ToIpv4)(buff.readUInt32BE()),
|
|
3699
|
+
port: buff.readUInt16BE()
|
|
3700
|
+
};
|
|
3701
|
+
if (remoteHost.host === "0.0.0.0") {
|
|
3702
|
+
remoteHost.host = this.options.proxy.ipaddress;
|
|
3703
|
+
}
|
|
3704
|
+
} else if (addressType === constants_1.Socks5HostType.Hostname) {
|
|
3705
|
+
const hostLength = header[4];
|
|
3706
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseHostname(hostLength);
|
|
3707
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3708
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3709
|
+
return;
|
|
3710
|
+
}
|
|
3711
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(5));
|
|
3712
|
+
remoteHost = {
|
|
3713
|
+
host: buff.readString(hostLength),
|
|
3714
|
+
port: buff.readUInt16BE()
|
|
3715
|
+
};
|
|
3716
|
+
} else if (addressType === constants_1.Socks5HostType.IPv6) {
|
|
3717
|
+
const dataNeeded = constants_1.SOCKS_INCOMING_PACKET_SIZES.Socks5ResponseIPv6;
|
|
3718
|
+
if (this.receiveBuffer.length < dataNeeded) {
|
|
3719
|
+
this.nextRequiredPacketBufferSize = dataNeeded;
|
|
3720
|
+
return;
|
|
3721
|
+
}
|
|
3722
|
+
buff = smart_buffer_1.SmartBuffer.fromBuffer(this.receiveBuffer.get(dataNeeded).slice(4));
|
|
3723
|
+
remoteHost = {
|
|
3724
|
+
host: ip_address_1.Address6.fromByteArray(Array.from(buff.readBuffer(16))).canonicalForm(),
|
|
3725
|
+
port: buff.readUInt16BE()
|
|
3726
|
+
};
|
|
3727
|
+
}
|
|
3728
|
+
this.setState(constants_1.SocksClientState.Established);
|
|
3729
|
+
this.removeInternalSocketHandlers();
|
|
3730
|
+
this.emit("established", { remoteHost, socket: this.socket });
|
|
3731
|
+
}
|
|
3732
|
+
}
|
|
3733
|
+
get socksClientOptions() {
|
|
3734
|
+
return Object.assign({}, this.options);
|
|
3735
|
+
}
|
|
3736
|
+
};
|
|
3737
|
+
exports.SocksClient = SocksClient;
|
|
3738
|
+
}
|
|
3739
|
+
});
|
|
3740
|
+
|
|
3741
|
+
// ../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/index.js
|
|
3742
|
+
var require_build = __commonJS({
|
|
3743
|
+
"../../node_modules/.pnpm/socks@2.8.7/node_modules/socks/build/index.js"(exports) {
|
|
3744
|
+
"use strict";
|
|
3745
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
3746
|
+
if (k2 === void 0) k2 = k;
|
|
3747
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
3748
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
3749
|
+
desc = { enumerable: true, get: function() {
|
|
3750
|
+
return m[k];
|
|
3751
|
+
} };
|
|
3752
|
+
}
|
|
3753
|
+
Object.defineProperty(o, k2, desc);
|
|
3754
|
+
}) : (function(o, m, k, k2) {
|
|
3755
|
+
if (k2 === void 0) k2 = k;
|
|
3756
|
+
o[k2] = m[k];
|
|
3757
|
+
}));
|
|
3758
|
+
var __exportStar = exports && exports.__exportStar || function(m, exports2) {
|
|
3759
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports2, p)) __createBinding(exports2, m, p);
|
|
3760
|
+
};
|
|
3761
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3762
|
+
__exportStar(require_socksclient(), exports);
|
|
3763
|
+
}
|
|
3764
|
+
});
|
|
3765
|
+
|
|
3766
|
+
// ../../node_modules/.pnpm/socks-proxy-agent@8.0.5/node_modules/socks-proxy-agent/dist/index.js
|
|
3767
|
+
var require_dist2 = __commonJS({
|
|
3768
|
+
"../../node_modules/.pnpm/socks-proxy-agent@8.0.5/node_modules/socks-proxy-agent/dist/index.js"(exports) {
|
|
3769
|
+
var __createBinding = exports && exports.__createBinding || (Object.create ? (function(o, m, k, k2) {
|
|
3770
|
+
if (k2 === void 0) k2 = k;
|
|
3771
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
3772
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
3773
|
+
desc = { enumerable: true, get: function() {
|
|
3774
|
+
return m[k];
|
|
3775
|
+
} };
|
|
3776
|
+
}
|
|
3777
|
+
Object.defineProperty(o, k2, desc);
|
|
3778
|
+
}) : (function(o, m, k, k2) {
|
|
3779
|
+
if (k2 === void 0) k2 = k;
|
|
3780
|
+
o[k2] = m[k];
|
|
3781
|
+
}));
|
|
3782
|
+
var __setModuleDefault = exports && exports.__setModuleDefault || (Object.create ? (function(o, v) {
|
|
3783
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
3784
|
+
}) : function(o, v) {
|
|
3785
|
+
o["default"] = v;
|
|
3786
|
+
});
|
|
3787
|
+
var __importStar = exports && exports.__importStar || function(mod) {
|
|
3788
|
+
if (mod && mod.__esModule) return mod;
|
|
3789
|
+
var result = {};
|
|
3790
|
+
if (mod != null) {
|
|
3791
|
+
for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
3792
|
+
}
|
|
3793
|
+
__setModuleDefault(result, mod);
|
|
3794
|
+
return result;
|
|
3795
|
+
};
|
|
3796
|
+
var __importDefault = exports && exports.__importDefault || function(mod) {
|
|
3797
|
+
return mod && mod.__esModule ? mod : { "default": mod };
|
|
3798
|
+
};
|
|
3799
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3800
|
+
exports.SocksProxyAgent = void 0;
|
|
3801
|
+
var socks_1 = require_build();
|
|
3802
|
+
var agent_base_1 = require_dist();
|
|
3803
|
+
var debug_1 = __importDefault(require_src());
|
|
3804
|
+
var dns = __importStar(__require("dns"));
|
|
3805
|
+
var net = __importStar(__require("net"));
|
|
3806
|
+
var tls = __importStar(__require("tls"));
|
|
3807
|
+
var url_1 = __require("url");
|
|
3808
|
+
var debug = (0, debug_1.default)("socks-proxy-agent");
|
|
3809
|
+
var setServernameFromNonIpHost = (options) => {
|
|
3810
|
+
if (options.servername === void 0 && options.host && !net.isIP(options.host)) {
|
|
3811
|
+
return {
|
|
3812
|
+
...options,
|
|
3813
|
+
servername: options.host
|
|
3814
|
+
};
|
|
3815
|
+
}
|
|
3816
|
+
return options;
|
|
3817
|
+
};
|
|
3818
|
+
function parseSocksURL(url) {
|
|
3819
|
+
let lookup = false;
|
|
3820
|
+
let type = 5;
|
|
3821
|
+
const host = url.hostname;
|
|
3822
|
+
const port = parseInt(url.port, 10) || 1080;
|
|
3823
|
+
switch (url.protocol.replace(":", "")) {
|
|
3824
|
+
case "socks4":
|
|
3825
|
+
lookup = true;
|
|
3826
|
+
type = 4;
|
|
3827
|
+
break;
|
|
3828
|
+
// pass through
|
|
3829
|
+
case "socks4a":
|
|
3830
|
+
type = 4;
|
|
3831
|
+
break;
|
|
3832
|
+
case "socks5":
|
|
3833
|
+
lookup = true;
|
|
3834
|
+
type = 5;
|
|
3835
|
+
break;
|
|
3836
|
+
// pass through
|
|
3837
|
+
case "socks":
|
|
3838
|
+
type = 5;
|
|
3839
|
+
break;
|
|
3840
|
+
case "socks5h":
|
|
3841
|
+
type = 5;
|
|
3842
|
+
break;
|
|
3843
|
+
default:
|
|
3844
|
+
throw new TypeError(`A "socks" protocol must be specified! Got: ${String(url.protocol)}`);
|
|
3845
|
+
}
|
|
3846
|
+
const proxy = {
|
|
3847
|
+
host,
|
|
3848
|
+
port,
|
|
3849
|
+
type
|
|
3850
|
+
};
|
|
3851
|
+
if (url.username) {
|
|
3852
|
+
Object.defineProperty(proxy, "userId", {
|
|
3853
|
+
value: decodeURIComponent(url.username),
|
|
3854
|
+
enumerable: false
|
|
3855
|
+
});
|
|
3856
|
+
}
|
|
3857
|
+
if (url.password != null) {
|
|
3858
|
+
Object.defineProperty(proxy, "password", {
|
|
3859
|
+
value: decodeURIComponent(url.password),
|
|
3860
|
+
enumerable: false
|
|
3861
|
+
});
|
|
3862
|
+
}
|
|
3863
|
+
return { lookup, proxy };
|
|
3864
|
+
}
|
|
3865
|
+
var SocksProxyAgent = class extends agent_base_1.Agent {
|
|
3866
|
+
constructor(uri, opts) {
|
|
3867
|
+
super(opts);
|
|
3868
|
+
const url = typeof uri === "string" ? new url_1.URL(uri) : uri;
|
|
3869
|
+
const { proxy, lookup } = parseSocksURL(url);
|
|
3870
|
+
this.shouldLookup = lookup;
|
|
3871
|
+
this.proxy = proxy;
|
|
3872
|
+
this.timeout = opts?.timeout ?? null;
|
|
3873
|
+
this.socketOptions = opts?.socketOptions ?? null;
|
|
3874
|
+
}
|
|
3875
|
+
/**
|
|
3876
|
+
* Initiates a SOCKS connection to the specified SOCKS proxy server,
|
|
3877
|
+
* which in turn connects to the specified remote host and port.
|
|
3878
|
+
*/
|
|
3879
|
+
async connect(req, opts) {
|
|
3880
|
+
const { shouldLookup, proxy, timeout } = this;
|
|
3881
|
+
if (!opts.host) {
|
|
3882
|
+
throw new Error("No `host` defined!");
|
|
3883
|
+
}
|
|
3884
|
+
let { host } = opts;
|
|
3885
|
+
const { port, lookup: lookupFn = dns.lookup } = opts;
|
|
3886
|
+
if (shouldLookup) {
|
|
3887
|
+
host = await new Promise((resolve, reject) => {
|
|
3888
|
+
lookupFn(host, {}, (err, res) => {
|
|
3889
|
+
if (err) {
|
|
3890
|
+
reject(err);
|
|
3891
|
+
} else {
|
|
3892
|
+
resolve(res);
|
|
3893
|
+
}
|
|
3894
|
+
});
|
|
3895
|
+
});
|
|
3896
|
+
}
|
|
3897
|
+
const socksOpts = {
|
|
3898
|
+
proxy,
|
|
3899
|
+
destination: {
|
|
3900
|
+
host,
|
|
3901
|
+
port: typeof port === "number" ? port : parseInt(port, 10)
|
|
3902
|
+
},
|
|
3903
|
+
command: "connect",
|
|
3904
|
+
timeout: timeout ?? void 0,
|
|
3905
|
+
// @ts-expect-error the type supplied by socks for socket_options is wider
|
|
3906
|
+
// than necessary since socks will always override the host and port
|
|
3907
|
+
socket_options: this.socketOptions ?? void 0
|
|
3908
|
+
};
|
|
3909
|
+
const cleanup = (tlsSocket) => {
|
|
3910
|
+
req.destroy();
|
|
3911
|
+
socket.destroy();
|
|
3912
|
+
if (tlsSocket)
|
|
3913
|
+
tlsSocket.destroy();
|
|
3914
|
+
};
|
|
3915
|
+
debug("Creating socks proxy connection: %o", socksOpts);
|
|
3916
|
+
const { socket } = await socks_1.SocksClient.createConnection(socksOpts);
|
|
3917
|
+
debug("Successfully created socks proxy connection");
|
|
3918
|
+
if (timeout !== null) {
|
|
3919
|
+
socket.setTimeout(timeout);
|
|
3920
|
+
socket.on("timeout", () => cleanup());
|
|
3921
|
+
}
|
|
3922
|
+
if (opts.secureEndpoint) {
|
|
3923
|
+
debug("Upgrading socket connection to TLS");
|
|
3924
|
+
const tlsSocket = tls.connect({
|
|
3925
|
+
...omit(setServernameFromNonIpHost(opts), "host", "path", "port"),
|
|
3926
|
+
socket
|
|
3927
|
+
});
|
|
3928
|
+
tlsSocket.once("error", (error) => {
|
|
3929
|
+
debug("Socket TLS error", error.message);
|
|
3930
|
+
cleanup(tlsSocket);
|
|
3931
|
+
});
|
|
3932
|
+
return tlsSocket;
|
|
3933
|
+
}
|
|
3934
|
+
return socket;
|
|
3935
|
+
}
|
|
3936
|
+
};
|
|
3937
|
+
SocksProxyAgent.protocols = [
|
|
3938
|
+
"socks",
|
|
3939
|
+
"socks4",
|
|
3940
|
+
"socks4a",
|
|
3941
|
+
"socks5",
|
|
3942
|
+
"socks5h"
|
|
3943
|
+
];
|
|
3944
|
+
exports.SocksProxyAgent = SocksProxyAgent;
|
|
3945
|
+
function omit(obj, ...keys) {
|
|
3946
|
+
const ret = {};
|
|
3947
|
+
let key;
|
|
3948
|
+
for (key in obj) {
|
|
3949
|
+
if (!keys.includes(key)) {
|
|
3950
|
+
ret[key] = obj[key];
|
|
3951
|
+
}
|
|
3952
|
+
}
|
|
3953
|
+
return ret;
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
});
|
|
3957
|
+
export default require_dist2();
|