123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.validateSocksClientChainOptions = exports.validateSocksClientOptions = void 0;
- const util_1 = require("./util");
- const constants_1 = require("./constants");
- const stream = require("stream");
- function validateSocksClientOptions(options, acceptedCommands = ['connect', 'bind', 'associate']) {
-
- if (!constants_1.SocksCommand[options.command]) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommand, options);
- }
-
- if (acceptedCommands.indexOf(options.command) === -1) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandForOperation, options);
- }
-
- if (!isValidSocksRemoteHost(options.destination)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
- }
-
- if (!isValidSocksProxy(options.proxy)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
- }
-
- validateCustomProxyAuth(options.proxy, options);
-
- if (options.timeout && !isValidTimeoutValue(options.timeout)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
- }
-
- if (options.existing_socket &&
- !(options.existing_socket instanceof stream.Duplex)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsExistingSocket, options);
- }
- }
- exports.validateSocksClientOptions = validateSocksClientOptions;
- function validateSocksClientChainOptions(options) {
-
- if (options.command !== 'connect') {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksCommandChain, options);
- }
-
- if (!isValidSocksRemoteHost(options.destination)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsDestination, options);
- }
-
- if (!(options.proxies &&
- Array.isArray(options.proxies) &&
- options.proxies.length >= 2)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxiesLength, options);
- }
-
- options.proxies.forEach((proxy) => {
- if (!isValidSocksProxy(proxy)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsProxy, options);
- }
-
- validateCustomProxyAuth(proxy, options);
- });
-
- if (options.timeout && !isValidTimeoutValue(options.timeout)) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsTimeout, options);
- }
- }
- exports.validateSocksClientChainOptions = validateSocksClientChainOptions;
- function validateCustomProxyAuth(proxy, options) {
- if (proxy.custom_auth_method !== undefined) {
-
- if (proxy.custom_auth_method < constants_1.SOCKS5_CUSTOM_AUTH_START ||
- proxy.custom_auth_method > constants_1.SOCKS5_CUSTOM_AUTH_END) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthRange, options);
- }
-
- if (proxy.custom_auth_request_handler === undefined ||
- typeof proxy.custom_auth_request_handler !== 'function') {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
- }
-
- if (proxy.custom_auth_response_size === undefined) {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
- }
-
- if (proxy.custom_auth_response_handler === undefined ||
- typeof proxy.custom_auth_response_handler !== 'function') {
- throw new util_1.SocksClientError(constants_1.ERRORS.InvalidSocksClientOptionsCustomAuthOptions, options);
- }
- }
- }
- function isValidSocksRemoteHost(remoteHost) {
- return (remoteHost &&
- typeof remoteHost.host === 'string' &&
- typeof remoteHost.port === 'number' &&
- remoteHost.port >= 0 &&
- remoteHost.port <= 65535);
- }
- function isValidSocksProxy(proxy) {
- 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));
- }
- function isValidTimeoutValue(value) {
- return typeof value === 'number' && value > 0;
- }
|