source: node_modules/socks/build/common/helpers.js@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 6.8 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ipToBuffer = exports.int32ToIpv4 = exports.ipv4ToInt32 = exports.validateSocksClientChainOptions = exports.validateSocksClientOptions = void 0;
4const util_1 = require("./util");
5const constants_1 = require("./constants");
6const stream = require("stream");
7const ip_address_1 = require("ip-address");
8const net = require("net");
9/**
10 * Validates the provided SocksClientOptions
11 * @param options { SocksClientOptions }
12 * @param acceptedCommands { string[] } A list of accepted SocksProxy commands.
13 */
14function validateSocksClientOptions(options, acceptedCommands = ['connect', 'bind', 'associate']) {
15 // Check SOCKs command option.
16 if (!constants_1.SocksCommand[options.command]) {
17 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommand, options);
18 }
19 // Check SocksCommand for acceptable command.
20 if (acceptedCommands.indexOf(options.command) === -1) {
21 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandForOperation, options);
22 }
23 // Check destination
24 if (!isValidSocksRemoteHost(options.destination)) {
25 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
26 }
27 // Check SOCKS proxy to use
28 if (!isValidSocksProxy(options.proxy)) {
29 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
30 }
31 // Validate custom auth (if set)
32 validateCustomProxyAuth(options.proxy, options);
33 // Check timeout
34 if (options.timeout && !isValidTimeoutValue(options.timeout)) {
35 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
36 }
37 // Check existing_socket (if provided)
38 if (options.existing_socket &&
39 !(options.existing_socket instanceof stream.Duplex)) {
40 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsExistingSocket, options);
41 }
42}
43exports.validateSocksClientOptions = validateSocksClientOptions;
44/**
45 * Validates the SocksClientChainOptions
46 * @param options { SocksClientChainOptions }
47 */
48function validateSocksClientChainOptions(options) {
49 // Only connect is supported when chaining.
50 if (options.command !== 'connect') {
51 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandChain, options);
52 }
53 // Check destination
54 if (!isValidSocksRemoteHost(options.destination)) {
55 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
56 }
57 // Validate proxies (length)
58 if (!(options.proxies &&
59 Array.isArray(options.proxies) &&
60 options.proxies.length >= 2)) {
61 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxiesLength, options);
62 }
63 // Validate proxies
64 options.proxies.forEach((proxy) => {
65 if (!isValidSocksProxy(proxy)) {
66 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
67 }
68 // Validate custom auth (if set)
69 validateCustomProxyAuth(proxy, options);
70 });
71 // Check timeout
72 if (options.timeout && !isValidTimeoutValue(options.timeout)) {
73 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
74 }
75}
76exports.validateSocksClientChainOptions = validateSocksClientChainOptions;
77function validateCustomProxyAuth(proxy, options) {
78 if (proxy.custom_auth_method !== undefined) {
79 // Invalid auth method range
80 if (proxy.custom_auth_method < constants_1.SOCKS5_CUSTOM_AUTH_START ||
81 proxy.custom_auth_method > constants_1.SOCKS5_CUSTOM_AUTH_END) {
82 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthRange, options);
83 }
84 // Missing custom_auth_request_handler
85 if (proxy.custom_auth_request_handler === undefined ||
86 typeof proxy.custom_auth_request_handler !== 'function') {
87 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
88 }
89 // Missing custom_auth_response_size
90 if (proxy.custom_auth_response_size === undefined) {
91 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
92 }
93 // Missing/invalid custom_auth_response_handler
94 if (proxy.custom_auth_response_handler === undefined ||
95 typeof proxy.custom_auth_response_handler !== 'function') {
96 throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
97 }
98 }
99}
100/**
101 * Validates a SocksRemoteHost
102 * @param remoteHost { SocksRemoteHost }
103 */
104function isValidSocksRemoteHost(remoteHost) {
105 return (remoteHost &&
106 typeof remoteHost.host === 'string' &&
107 Buffer.byteLength(remoteHost.host) < 256 &&
108 typeof remoteHost.port === 'number' &&
109 remoteHost.port >= 0 &&
110 remoteHost.port <= 65535);
111}
112/**
113 * Validates a SocksProxy
114 * @param proxy { SocksProxy }
115 */
116function isValidSocksProxy(proxy) {
117 return (proxy &&
118 (typeof proxy.host === 'string' || typeof proxy.ipaddress === 'string') &&
119 typeof proxy.port === 'number' &&
120 proxy.port >= 0 &&
121 proxy.port <= 65535 &&
122 (proxy.type === 4 || proxy.type === 5));
123}
124/**
125 * Validates a timeout value.
126 * @param value { Number }
127 */
128function isValidTimeoutValue(value) {
129 return typeof value === 'number' && value > 0;
130}
131function ipv4ToInt32(ip) {
132 const address = new ip_address_1.Address4(ip);
133 // Convert the IPv4 address parts to an integer
134 return address.toArray().reduce((acc, part) => (acc << 8) + part, 0) >>> 0;
135}
136exports.ipv4ToInt32 = ipv4ToInt32;
137function int32ToIpv4(int32) {
138 // Extract each byte (octet) from the 32-bit integer
139 const octet1 = (int32 >>> 24) & 0xff;
140 const octet2 = (int32 >>> 16) & 0xff;
141 const octet3 = (int32 >>> 8) & 0xff;
142 const octet4 = int32 & 0xff;
143 // Combine the octets into a string in IPv4 format
144 return [octet1, octet2, octet3, octet4].join('.');
145}
146exports.int32ToIpv4 = int32ToIpv4;
147function ipToBuffer(ip) {
148 if (net.isIPv4(ip)) {
149 // Handle IPv4 addresses
150 const address = new ip_address_1.Address4(ip);
151 return Buffer.from(address.toArray());
152 }
153 else if (net.isIPv6(ip)) {
154 // Handle IPv6 addresses
155 const address = new ip_address_1.Address6(ip);
156 return Buffer.from(address
157 .canonicalForm()
158 .split(':')
159 .map((segment) => segment.padStart(4, '0'))
160 .join(''), 'hex');
161 }
162 else {
163 throw new Error('Invalid IP address format');
164 }
165}
166exports.ipToBuffer = ipToBuffer;
167//# sourceMappingURL=helpers.js.map
Note: See TracBrowser for help on using the repository browser.