1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.validateSocksClientChainOptions = exports.validateSocksClientOptions = void 0;
|
---|
4 | const util_1 = require("./util");
|
---|
5 | const constants_1 = require("./constants");
|
---|
6 | const stream = require("stream");
|
---|
7 | /**
|
---|
8 | * Validates the provided SocksClientOptions
|
---|
9 | * @param options { SocksClientOptions }
|
---|
10 | * @param acceptedCommands { string[] } A list of accepted SocksProxy commands.
|
---|
11 | */
|
---|
12 | function validateSocksClientOptions(options, acceptedCommands = ['connect', 'bind', 'associate']) {
|
---|
13 | // Check SOCKs command option.
|
---|
14 | if (!constants_1.SocksCommand[options.command]) {
|
---|
15 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommand, options);
|
---|
16 | }
|
---|
17 | // Check SocksCommand for acceptable command.
|
---|
18 | if (acceptedCommands.indexOf(options.command) === -1) {
|
---|
19 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandForOperation, options);
|
---|
20 | }
|
---|
21 | // Check destination
|
---|
22 | if (!isValidSocksRemoteHost(options.destination)) {
|
---|
23 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
|
---|
24 | }
|
---|
25 | // Check SOCKS proxy to use
|
---|
26 | if (!isValidSocksProxy(options.proxy)) {
|
---|
27 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
|
---|
28 | }
|
---|
29 | // Validate custom auth (if set)
|
---|
30 | validateCustomProxyAuth(options.proxy, options);
|
---|
31 | // Check timeout
|
---|
32 | if (options.timeout && !isValidTimeoutValue(options.timeout)) {
|
---|
33 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
|
---|
34 | }
|
---|
35 | // Check existing_socket (if provided)
|
---|
36 | if (options.existing_socket &&
|
---|
37 | !(options.existing_socket instanceof stream.Duplex)) {
|
---|
38 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsExistingSocket, options);
|
---|
39 | }
|
---|
40 | }
|
---|
41 | exports.validateSocksClientOptions = validateSocksClientOptions;
|
---|
42 | /**
|
---|
43 | * Validates the SocksClientChainOptions
|
---|
44 | * @param options { SocksClientChainOptions }
|
---|
45 | */
|
---|
46 | function validateSocksClientChainOptions(options) {
|
---|
47 | // Only connect is supported when chaining.
|
---|
48 | if (options.command !== 'connect') {
|
---|
49 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandChain, options);
|
---|
50 | }
|
---|
51 | // Check destination
|
---|
52 | if (!isValidSocksRemoteHost(options.destination)) {
|
---|
53 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
|
---|
54 | }
|
---|
55 | // Validate proxies (length)
|
---|
56 | if (!(options.proxies &&
|
---|
57 | Array.isArray(options.proxies) &&
|
---|
58 | options.proxies.length >= 2)) {
|
---|
59 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxiesLength, options);
|
---|
60 | }
|
---|
61 | // Validate proxies
|
---|
62 | options.proxies.forEach((proxy) => {
|
---|
63 | if (!isValidSocksProxy(proxy)) {
|
---|
64 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
|
---|
65 | }
|
---|
66 | // Validate custom auth (if set)
|
---|
67 | validateCustomProxyAuth(proxy, options);
|
---|
68 | });
|
---|
69 | // Check timeout
|
---|
70 | if (options.timeout && !isValidTimeoutValue(options.timeout)) {
|
---|
71 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | exports.validateSocksClientChainOptions = validateSocksClientChainOptions;
|
---|
75 | function validateCustomProxyAuth(proxy, options) {
|
---|
76 | if (proxy.custom_auth_method !== undefined) {
|
---|
77 | // Invalid auth method range
|
---|
78 | if (proxy.custom_auth_method < constants_1.SOCKS5_CUSTOM_AUTH_START ||
|
---|
79 | proxy.custom_auth_method > constants_1.SOCKS5_CUSTOM_AUTH_END) {
|
---|
80 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthRange, options);
|
---|
81 | }
|
---|
82 | // Missing custom_auth_request_handler
|
---|
83 | if (proxy.custom_auth_request_handler === undefined ||
|
---|
84 | typeof proxy.custom_auth_request_handler !== 'function') {
|
---|
85 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
---|
86 | }
|
---|
87 | // Missing custom_auth_response_size
|
---|
88 | if (proxy.custom_auth_response_size === undefined) {
|
---|
89 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
---|
90 | }
|
---|
91 | // Missing/invalid custom_auth_response_handler
|
---|
92 | if (proxy.custom_auth_response_handler === undefined ||
|
---|
93 | typeof proxy.custom_auth_response_handler !== 'function') {
|
---|
94 | throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | /**
|
---|
99 | * Validates a SocksRemoteHost
|
---|
100 | * @param remoteHost { SocksRemoteHost }
|
---|
101 | */
|
---|
102 | function isValidSocksRemoteHost(remoteHost) {
|
---|
103 | return (remoteHost &&
|
---|
104 | typeof remoteHost.host === 'string' &&
|
---|
105 | typeof remoteHost.port === 'number' &&
|
---|
106 | remoteHost.port >= 0 &&
|
---|
107 | remoteHost.port <= 65535);
|
---|
108 | }
|
---|
109 | /**
|
---|
110 | * Validates a SocksProxy
|
---|
111 | * @param proxy { SocksProxy }
|
---|
112 | */
|
---|
113 | function isValidSocksProxy(proxy) {
|
---|
114 | return (proxy &&
|
---|
115 | (typeof proxy.host === 'string' || typeof proxy.ipaddress === 'string') &&
|
---|
116 | typeof proxy.port === 'number' &&
|
---|
117 | proxy.port >= 0 &&
|
---|
118 | proxy.port <= 65535 &&
|
---|
119 | (proxy.type === 4 || proxy.type === 5));
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * Validates a timeout value.
|
---|
123 | * @param value { Number }
|
---|
124 | */
|
---|
125 | function isValidTimeoutValue(value) {
|
---|
126 | return typeof value === 'number' && value > 0;
|
---|
127 | }
|
---|
128 | //# sourceMappingURL=helpers.js.map |
---|