source: node_modules/socks-proxy-agent/dist/index.js@ 62b2964

finki-main main
Last change on this file since 62b2964 was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

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