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