auto-pod
Version:
Automatically choose tunneling or direct connection for cocoapods. As easy as `pod` itself. Help you get rid of GFW of China.
83 lines • 2.72 kB
JavaScript
;
/*
Rewritten from: https://github.com/TooTallNate/node-socks-proxy-agent
*/
Object.defineProperty(exports, "__esModule", { value: true });
const dns = require("dns");
const tls = require("tls");
const agentBase = require("agent-base");
const socks_1 = require("socks");
/**
* The `SocksProxyAgent`.
*
* @api public
*/
class SocksProxyAgent extends agentBase {
constructor(options) {
super(options);
this.proxy = options;
}
destroy() { } // tslint:disable-line no-empty
/**
* Initiates a SOCKS connection to the specified SOCKS proxy server,
* which in turn connects to the specified remote host and port.
*
* @api public
*/
callback(req, opts, fn) {
const proxy = this.proxy;
// called once the SOCKS proxy has connected to the specified remote endpoint
function onhostconnect(err, result) {
if (err)
return fn(err);
const socket = result.socket;
let s = socket;
if (opts.secureEndpoint) {
// since the proxy is connecting to an SSL server, we have
// to upgrade this socket connection to an SSL connection
opts.socket = socket;
opts.servername = opts.host;
opts.host = null;
opts.hostname = null;
opts.port = null;
s = tls.connect(opts);
}
fn(null, s);
}
// called for the `dns.lookup()` callback
function onlookup(err, ip) {
if (err)
return fn(err);
options.destination.host = ip;
socks_1.SocksClient.createConnection(options, onhostconnect);
}
const options = {
command: 'connect',
destination: {
host: undefined,
port: +opts.port,
},
proxy: {
ipaddress: proxy.host,
password: undefined,
port: +proxy.port,
type: proxy.version,
userId: undefined,
},
};
if (proxy.authentication) {
options.proxy.userId = proxy.authentication.username;
options.proxy.password = proxy.authentication.password;
}
if (proxy.lookup) {
// client-side DNS resolution for "4" and "5" socks proxy versions
dns.lookup(opts.host, onlookup);
}
else {
// proxy hostname DNS resolution for "4a" and "5h" socks proxy servers
onlookup(null, opts.host);
}
}
}
exports.SocksProxyAgent = SocksProxyAgent;
//# sourceMappingURL=Agent.js.map