/ cloudformation-templates / node_modules / aws-cdk / node_modules / socks-proxy-agent / dist / agent.js
agent.js
1 "use strict"; 2 var __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 }; 11 var __importDefault = (this && this.__importDefault) || function (mod) { 12 return (mod && mod.__esModule) ? mod : { "default": mod }; 13 }; 14 Object.defineProperty(exports, "__esModule", { value: true }); 15 const dns_1 = __importDefault(require("dns")); 16 const tls_1 = __importDefault(require("tls")); 17 const url_1 = __importDefault(require("url")); 18 const debug_1 = __importDefault(require("debug")); 19 const agent_base_1 = require("agent-base"); 20 const socks_1 = require("socks"); 21 const debug = debug_1.default('socks-proxy-agent'); 22 function 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 } 34 function 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 */ 114 class 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 } 131 /** 132 * Initiates a SOCKS connection to the specified SOCKS proxy server, 133 * which in turn connects to the specified remote host and port. 134 * 135 * @api protected 136 */ 137 callback(req, opts) { 138 return __awaiter(this, void 0, void 0, function* () { 139 const { lookup, proxy } = this; 140 let { host, port, timeout } = opts; 141 if (!host) { 142 throw new Error('No `host` defined!'); 143 } 144 if (lookup) { 145 // Client-side DNS resolution for "4" and "5" socks proxy versions. 146 host = yield dnsLookup(host); 147 } 148 const socksOpts = { 149 proxy, 150 destination: { host, port }, 151 command: 'connect', 152 timeout 153 }; 154 debug('Creating socks proxy connection: %o', socksOpts); 155 const { socket } = yield socks_1.SocksClient.createConnection(socksOpts); 156 debug('Successfully created socks proxy connection'); 157 if (opts.secureEndpoint) { 158 // The proxy is connecting to a TLS server, so upgrade 159 // this socket connection to a TLS connection. 160 debug('Upgrading socket connection to TLS'); 161 const servername = opts.servername || host; 162 return tls_1.default.connect(Object.assign(Object.assign({}, omit(opts, 'host', 'hostname', 'path', 'port')), { socket, 163 servername })); 164 } 165 return socket; 166 }); 167 } 168 } 169 exports.default = SocksProxyAgent; 170 function omit(obj, ...keys) { 171 const ret = {}; 172 let key; 173 for (key in obj) { 174 if (!keys.includes(key)) { 175 ret[key] = obj[key]; 176 } 177 } 178 return ret; 179 } 180 //# sourceMappingURL=agent.js.map