source: trip-planner-front/node_modules/socks-proxy-agent/dist/agent.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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