codekingpro/portable-devtools
115k
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29exports.SocksProxyAgent = void 0;
30const socks_1 = require("socks");
31const agent_base_1 = require("agent-base");
32const debug_1 = __importDefault(require("debug"));
33const dns = __importStar(require("dns"));
34const net = __importStar(require("net"));
35const tls = __importStar(require("tls"));
36const url_1 = require("url");
37const debug = (0, debug_1.default)('socks-proxy-agent');
38const setServernameFromNonIpHost = (options) => {
39 if (options.servername === undefined &&
40 options.host &&
41 !net.isIP(options.host)) {
42 return {
43 ...options,
44 servername: options.host,
45 };
46 }
47 return options;
48};
49function parseSocksURL(url) {
50 let lookup = false;
51 let type = 5;
52 const host = url.hostname;
53 // From RFC 1928, Section 3: https://tools.ietf.org/html/rfc1928#section-3
54 // "The SOCKS service is conventionally located on TCP port 1080"
55 const port = parseInt(url.port, 10) || 1080;
56 // figure out if we want socks v4 or v5, based on the "protocol" used.
57 // Defaults to 5.
58 switch (url.protocol.replace(':', '')) {
59 case 'socks4':
60 lookup = true;
61 type = 4;
62 break;
63 // pass through
64 case 'socks4a':
65 type = 4;
66 break;
67 case 'socks5':
68 lookup = true;
69 type = 5;
70 break;
71 // pass through
72 case 'socks': // no version specified, default to 5h
73 type = 5;
74 break;
75 case 'socks5h':
76 type = 5;
77 break;
78 default:
79 throw new TypeError(`A "socks" protocol must be specified! Got: ${String(url.protocol)}`);
80 }
81 const proxy = {
82 host,
83 port,
84 type,
85 };
86 if (url.username) {
87 Object.defineProperty(proxy, 'userId', {
88 value: decodeURIComponent(url.username),
89 enumerable: false,
90 });
91 }
92 if (url.password != null) {
93 Object.defineProperty(proxy, 'password', {
94 value: decodeURIComponent(url.password),
95 enumerable: false,
96 });
97 }
98 return { lookup, proxy };
99}
100class SocksProxyAgent extends agent_base_1.Agent {
101 constructor(uri, opts) {
102 super(opts);
103 const url = typeof uri === 'string' ? new url_1.URL(uri) : uri;
104 const { proxy, lookup } = parseSocksURL(url);
105 this.shouldLookup = lookup;
106 this.proxy = proxy;
107 this.timeout = opts?.timeout ?? null;
108 this.socketOptions = opts?.socketOptions ?? null;
109 }
110 /**
111 * Initiates a SOCKS connection to the specified SOCKS proxy server,
112 * which in turn connects to the specified remote host and port.
113 */
114 async connect(req, opts) {
115 const { shouldLookup, proxy, timeout } = this;
116 if (!opts.host) {
117 throw new Error('No `host` defined!');
118 }
119 let { host } = opts;
120 const { port, lookup: lookupFn = dns.lookup } = opts;
121 if (shouldLookup) {
122 // Client-side DNS resolution for "4" and "5" socks proxy versions.
123 host = await new Promise((resolve, reject) => {
124 // Use the request's custom lookup, if one was configured:
125 lookupFn(host, {}, (err, res) => {
126 if (err) {
127 reject(err);
128 }
129 else {
130 resolve(res);
131 }
132 });
133 });
134 }
135 const socksOpts = {
136 proxy,
137 destination: {
138 host,
139 port: typeof port === 'number' ? port : parseInt(port, 10),
140 },
141 command: 'connect',
142 timeout: timeout ?? undefined,
143 // @ts-expect-error the type supplied by socks for socket_options is wider
144 // than necessary since socks will always override the host and port
145 socket_options: this.socketOptions ?? undefined,
146 };
147 const cleanup = (tlsSocket) => {
148 req.destroy();
149 socket.destroy();
150 if (tlsSocket)
151 tlsSocket.destroy();
152 };
153 debug('Creating socks proxy connection: %o', socksOpts);
154 const { socket } = await socks_1.SocksClient.createConnection(socksOpts);
155 debug('Successfully created socks proxy connection');
156 if (timeout !== null) {
157 socket.setTimeout(timeout);
158 socket.on('timeout', () => cleanup());
159 }
160 if (opts.secureEndpoint) {
161 // The proxy is connecting to a TLS server, so upgrade
162 // this socket connection to a TLS connection.
163 debug('Upgrading socket connection to TLS');
164 const tlsSocket = tls.connect({
165 ...omit(setServernameFromNonIpHost(opts), 'host', 'path', 'port'),
166 socket,
167 });
168 tlsSocket.once('error', (error) => {
169 debug('Socket TLS error', error.message);
170 cleanup(tlsSocket);
171 });
172 return tlsSocket;
173 }
174 return socket;
175 }
176}
177SocksProxyAgent.protocols = [
178 'socks',
179 'socks4',
180 'socks4a',
181 'socks5',
182 'socks5h',
183];
184exports.SocksProxyAgent = SocksProxyAgent;
185function omit(obj, ...keys) {
186 const ret = {};
187 let key;
188 for (key in obj) {
189 if (!keys.includes(key)) {
190 ret[key] = obj[key];
191 }
192 }
193 return ret;
194}
195//# sourceMappingURL=index.js.map