11cb0ef41Sopenharmony_ci"use strict";
21cb0ef41Sopenharmony_civar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
31cb0ef41Sopenharmony_ci    if (k2 === undefined) k2 = k;
41cb0ef41Sopenharmony_ci    var desc = Object.getOwnPropertyDescriptor(m, k);
51cb0ef41Sopenharmony_ci    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
61cb0ef41Sopenharmony_ci      desc = { enumerable: true, get: function() { return m[k]; } };
71cb0ef41Sopenharmony_ci    }
81cb0ef41Sopenharmony_ci    Object.defineProperty(o, k2, desc);
91cb0ef41Sopenharmony_ci}) : (function(o, m, k, k2) {
101cb0ef41Sopenharmony_ci    if (k2 === undefined) k2 = k;
111cb0ef41Sopenharmony_ci    o[k2] = m[k];
121cb0ef41Sopenharmony_ci}));
131cb0ef41Sopenharmony_civar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
141cb0ef41Sopenharmony_ci    Object.defineProperty(o, "default", { enumerable: true, value: v });
151cb0ef41Sopenharmony_ci}) : function(o, v) {
161cb0ef41Sopenharmony_ci    o["default"] = v;
171cb0ef41Sopenharmony_ci});
181cb0ef41Sopenharmony_civar __importStar = (this && this.__importStar) || function (mod) {
191cb0ef41Sopenharmony_ci    if (mod && mod.__esModule) return mod;
201cb0ef41Sopenharmony_ci    var result = {};
211cb0ef41Sopenharmony_ci    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
221cb0ef41Sopenharmony_ci    __setModuleDefault(result, mod);
231cb0ef41Sopenharmony_ci    return result;
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_civar __importDefault = (this && this.__importDefault) || function (mod) {
261cb0ef41Sopenharmony_ci    return (mod && mod.__esModule) ? mod : { "default": mod };
271cb0ef41Sopenharmony_ci};
281cb0ef41Sopenharmony_ciObject.defineProperty(exports, "__esModule", { value: true });
291cb0ef41Sopenharmony_ciexports.SocksProxyAgent = void 0;
301cb0ef41Sopenharmony_ciconst socks_1 = require("socks");
311cb0ef41Sopenharmony_ciconst agent_base_1 = require("agent-base");
321cb0ef41Sopenharmony_ciconst debug_1 = __importDefault(require("debug"));
331cb0ef41Sopenharmony_ciconst dns = __importStar(require("dns"));
341cb0ef41Sopenharmony_ciconst net = __importStar(require("net"));
351cb0ef41Sopenharmony_ciconst tls = __importStar(require("tls"));
361cb0ef41Sopenharmony_ciconst url_1 = require("url");
371cb0ef41Sopenharmony_ciconst debug = (0, debug_1.default)('socks-proxy-agent');
381cb0ef41Sopenharmony_cifunction parseSocksURL(url) {
391cb0ef41Sopenharmony_ci    let lookup = false;
401cb0ef41Sopenharmony_ci    let type = 5;
411cb0ef41Sopenharmony_ci    const host = url.hostname;
421cb0ef41Sopenharmony_ci    // From RFC 1928, Section 3: https://tools.ietf.org/html/rfc1928#section-3
431cb0ef41Sopenharmony_ci    // "The SOCKS service is conventionally located on TCP port 1080"
441cb0ef41Sopenharmony_ci    const port = parseInt(url.port, 10) || 1080;
451cb0ef41Sopenharmony_ci    // figure out if we want socks v4 or v5, based on the "protocol" used.
461cb0ef41Sopenharmony_ci    // Defaults to 5.
471cb0ef41Sopenharmony_ci    switch (url.protocol.replace(':', '')) {
481cb0ef41Sopenharmony_ci        case 'socks4':
491cb0ef41Sopenharmony_ci            lookup = true;
501cb0ef41Sopenharmony_ci            type = 4;
511cb0ef41Sopenharmony_ci            break;
521cb0ef41Sopenharmony_ci        // pass through
531cb0ef41Sopenharmony_ci        case 'socks4a':
541cb0ef41Sopenharmony_ci            type = 4;
551cb0ef41Sopenharmony_ci            break;
561cb0ef41Sopenharmony_ci        case 'socks5':
571cb0ef41Sopenharmony_ci            lookup = true;
581cb0ef41Sopenharmony_ci            type = 5;
591cb0ef41Sopenharmony_ci            break;
601cb0ef41Sopenharmony_ci        // pass through
611cb0ef41Sopenharmony_ci        case 'socks': // no version specified, default to 5h
621cb0ef41Sopenharmony_ci            type = 5;
631cb0ef41Sopenharmony_ci            break;
641cb0ef41Sopenharmony_ci        case 'socks5h':
651cb0ef41Sopenharmony_ci            type = 5;
661cb0ef41Sopenharmony_ci            break;
671cb0ef41Sopenharmony_ci        default:
681cb0ef41Sopenharmony_ci            throw new TypeError(`A "socks" protocol must be specified! Got: ${String(url.protocol)}`);
691cb0ef41Sopenharmony_ci    }
701cb0ef41Sopenharmony_ci    const proxy = {
711cb0ef41Sopenharmony_ci        host,
721cb0ef41Sopenharmony_ci        port,
731cb0ef41Sopenharmony_ci        type,
741cb0ef41Sopenharmony_ci    };
751cb0ef41Sopenharmony_ci    if (url.username) {
761cb0ef41Sopenharmony_ci        Object.defineProperty(proxy, 'userId', {
771cb0ef41Sopenharmony_ci            value: decodeURIComponent(url.username),
781cb0ef41Sopenharmony_ci            enumerable: false,
791cb0ef41Sopenharmony_ci        });
801cb0ef41Sopenharmony_ci    }
811cb0ef41Sopenharmony_ci    if (url.password != null) {
821cb0ef41Sopenharmony_ci        Object.defineProperty(proxy, 'password', {
831cb0ef41Sopenharmony_ci            value: decodeURIComponent(url.password),
841cb0ef41Sopenharmony_ci            enumerable: false,
851cb0ef41Sopenharmony_ci        });
861cb0ef41Sopenharmony_ci    }
871cb0ef41Sopenharmony_ci    return { lookup, proxy };
881cb0ef41Sopenharmony_ci}
891cb0ef41Sopenharmony_ciclass SocksProxyAgent extends agent_base_1.Agent {
901cb0ef41Sopenharmony_ci    constructor(uri, opts) {
911cb0ef41Sopenharmony_ci        super(opts);
921cb0ef41Sopenharmony_ci        const url = typeof uri === 'string' ? new url_1.URL(uri) : uri;
931cb0ef41Sopenharmony_ci        const { proxy, lookup } = parseSocksURL(url);
941cb0ef41Sopenharmony_ci        this.shouldLookup = lookup;
951cb0ef41Sopenharmony_ci        this.proxy = proxy;
961cb0ef41Sopenharmony_ci        this.timeout = opts?.timeout ?? null;
971cb0ef41Sopenharmony_ci    }
981cb0ef41Sopenharmony_ci    /**
991cb0ef41Sopenharmony_ci     * Initiates a SOCKS connection to the specified SOCKS proxy server,
1001cb0ef41Sopenharmony_ci     * which in turn connects to the specified remote host and port.
1011cb0ef41Sopenharmony_ci     */
1021cb0ef41Sopenharmony_ci    async connect(req, opts) {
1031cb0ef41Sopenharmony_ci        const { shouldLookup, proxy, timeout } = this;
1041cb0ef41Sopenharmony_ci        if (!opts.host) {
1051cb0ef41Sopenharmony_ci            throw new Error('No `host` defined!');
1061cb0ef41Sopenharmony_ci        }
1071cb0ef41Sopenharmony_ci        let { host } = opts;
1081cb0ef41Sopenharmony_ci        const { port, lookup: lookupFn = dns.lookup } = opts;
1091cb0ef41Sopenharmony_ci        if (shouldLookup) {
1101cb0ef41Sopenharmony_ci            // Client-side DNS resolution for "4" and "5" socks proxy versions.
1111cb0ef41Sopenharmony_ci            host = await new Promise((resolve, reject) => {
1121cb0ef41Sopenharmony_ci                // Use the request's custom lookup, if one was configured:
1131cb0ef41Sopenharmony_ci                lookupFn(host, {}, (err, res) => {
1141cb0ef41Sopenharmony_ci                    if (err) {
1151cb0ef41Sopenharmony_ci                        reject(err);
1161cb0ef41Sopenharmony_ci                    }
1171cb0ef41Sopenharmony_ci                    else {
1181cb0ef41Sopenharmony_ci                        resolve(res);
1191cb0ef41Sopenharmony_ci                    }
1201cb0ef41Sopenharmony_ci                });
1211cb0ef41Sopenharmony_ci            });
1221cb0ef41Sopenharmony_ci        }
1231cb0ef41Sopenharmony_ci        const socksOpts = {
1241cb0ef41Sopenharmony_ci            proxy,
1251cb0ef41Sopenharmony_ci            destination: {
1261cb0ef41Sopenharmony_ci                host,
1271cb0ef41Sopenharmony_ci                port: typeof port === 'number' ? port : parseInt(port, 10),
1281cb0ef41Sopenharmony_ci            },
1291cb0ef41Sopenharmony_ci            command: 'connect',
1301cb0ef41Sopenharmony_ci            timeout: timeout ?? undefined,
1311cb0ef41Sopenharmony_ci        };
1321cb0ef41Sopenharmony_ci        const cleanup = (tlsSocket) => {
1331cb0ef41Sopenharmony_ci            req.destroy();
1341cb0ef41Sopenharmony_ci            socket.destroy();
1351cb0ef41Sopenharmony_ci            if (tlsSocket)
1361cb0ef41Sopenharmony_ci                tlsSocket.destroy();
1371cb0ef41Sopenharmony_ci        };
1381cb0ef41Sopenharmony_ci        debug('Creating socks proxy connection: %o', socksOpts);
1391cb0ef41Sopenharmony_ci        const { socket } = await socks_1.SocksClient.createConnection(socksOpts);
1401cb0ef41Sopenharmony_ci        debug('Successfully created socks proxy connection');
1411cb0ef41Sopenharmony_ci        if (timeout !== null) {
1421cb0ef41Sopenharmony_ci            socket.setTimeout(timeout);
1431cb0ef41Sopenharmony_ci            socket.on('timeout', () => cleanup());
1441cb0ef41Sopenharmony_ci        }
1451cb0ef41Sopenharmony_ci        if (opts.secureEndpoint) {
1461cb0ef41Sopenharmony_ci            // The proxy is connecting to a TLS server, so upgrade
1471cb0ef41Sopenharmony_ci            // this socket connection to a TLS connection.
1481cb0ef41Sopenharmony_ci            debug('Upgrading socket connection to TLS');
1491cb0ef41Sopenharmony_ci            const servername = opts.servername || opts.host;
1501cb0ef41Sopenharmony_ci            const tlsSocket = tls.connect({
1511cb0ef41Sopenharmony_ci                ...omit(opts, 'host', 'path', 'port'),
1521cb0ef41Sopenharmony_ci                socket,
1531cb0ef41Sopenharmony_ci                servername: net.isIP(servername) ? undefined : servername,
1541cb0ef41Sopenharmony_ci            });
1551cb0ef41Sopenharmony_ci            tlsSocket.once('error', (error) => {
1561cb0ef41Sopenharmony_ci                debug('Socket TLS error', error.message);
1571cb0ef41Sopenharmony_ci                cleanup(tlsSocket);
1581cb0ef41Sopenharmony_ci            });
1591cb0ef41Sopenharmony_ci            return tlsSocket;
1601cb0ef41Sopenharmony_ci        }
1611cb0ef41Sopenharmony_ci        return socket;
1621cb0ef41Sopenharmony_ci    }
1631cb0ef41Sopenharmony_ci}
1641cb0ef41Sopenharmony_ciSocksProxyAgent.protocols = [
1651cb0ef41Sopenharmony_ci    'socks',
1661cb0ef41Sopenharmony_ci    'socks4',
1671cb0ef41Sopenharmony_ci    'socks4a',
1681cb0ef41Sopenharmony_ci    'socks5',
1691cb0ef41Sopenharmony_ci    'socks5h',
1701cb0ef41Sopenharmony_ci];
1711cb0ef41Sopenharmony_ciexports.SocksProxyAgent = SocksProxyAgent;
1721cb0ef41Sopenharmony_cifunction omit(obj, ...keys) {
1731cb0ef41Sopenharmony_ci    const ret = {};
1741cb0ef41Sopenharmony_ci    let key;
1751cb0ef41Sopenharmony_ci    for (key in obj) {
1761cb0ef41Sopenharmony_ci        if (!keys.includes(key)) {
1771cb0ef41Sopenharmony_ci            ret[key] = obj[key];
1781cb0ef41Sopenharmony_ci        }
1791cb0ef41Sopenharmony_ci    }
1801cb0ef41Sopenharmony_ci    return ret;
1811cb0ef41Sopenharmony_ci}
1821cb0ef41Sopenharmony_ci//# sourceMappingURL=index.js.map