You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Module dependencies.
  3. */
  4. var net = require('net');
  5. var tls = require('tls');
  6. var url = require('url');
  7. var Agent = require('agent-base');
  8. var inherits = require('util').inherits;
  9. var debug = require('debug')('http-proxy-agent');
  10. /**
  11. * Module exports.
  12. */
  13. module.exports = HttpProxyAgent;
  14. /**
  15. * The `HttpProxyAgent` implements an HTTP Agent subclass that connects to the
  16. * specified "HTTP proxy server" in order to proxy HTTP requests.
  17. *
  18. * @api public
  19. */
  20. function HttpProxyAgent (opts) {
  21. if (!(this instanceof HttpProxyAgent)) return new HttpProxyAgent(opts);
  22. if ('string' == typeof opts) opts = url.parse(opts);
  23. if (!opts) throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!');
  24. debug('creating new HttpProxyAgent instance: %o', opts);
  25. Agent.call(this, opts);
  26. var proxy = Object.assign({}, opts);
  27. // if `true`, then connect to the proxy server over TLS. defaults to `false`.
  28. this.secureProxy = proxy.protocol ? /^https:?$/i.test(proxy.protocol) : false;
  29. // prefer `hostname` over `host`, and set the `port` if needed
  30. proxy.host = proxy.hostname || proxy.host;
  31. proxy.port = +proxy.port || (this.secureProxy ? 443 : 80);
  32. if (proxy.host && proxy.path) {
  33. // if both a `host` and `path` are specified then it's most likely the
  34. // result of a `url.parse()` call... we need to remove the `path` portion so
  35. // that `net.connect()` doesn't attempt to open that as a unix socket file.
  36. delete proxy.path;
  37. delete proxy.pathname;
  38. }
  39. this.proxy = proxy;
  40. }
  41. inherits(HttpProxyAgent, Agent);
  42. /**
  43. * Called when the node-core HTTP client library is creating a new HTTP request.
  44. *
  45. * @api public
  46. */
  47. HttpProxyAgent.prototype.callback = function connect (req, opts, fn) {
  48. var proxy = this.proxy;
  49. // change the `http.ClientRequest` instance's "path" field
  50. // to the absolute path of the URL that will be requested
  51. var parsed = url.parse(req.path);
  52. if (null == parsed.protocol) parsed.protocol = 'http:';
  53. if (null == parsed.hostname) parsed.hostname = opts.hostname || opts.host;
  54. if (null == parsed.port) parsed.port = opts.port;
  55. if (parsed.port == 80) {
  56. // if port is 80, then we can remove the port so that the
  57. // ":80" portion is not on the produced URL
  58. delete parsed.port;
  59. }
  60. var absolute = url.format(parsed);
  61. req.path = absolute;
  62. // inject the `Proxy-Authorization` header if necessary
  63. if (proxy.auth) {
  64. req.setHeader(
  65. 'Proxy-Authorization',
  66. 'Basic ' + Buffer.from(proxy.auth).toString('base64')
  67. );
  68. }
  69. // create a socket connection to the proxy server
  70. var socket;
  71. if (this.secureProxy) {
  72. socket = tls.connect(proxy);
  73. } else {
  74. socket = net.connect(proxy);
  75. }
  76. // at this point, the http ClientRequest's internal `_header` field might have
  77. // already been set. If this is the case then we'll need to re-generate the
  78. // string since we just changed the `req.path`
  79. if (req._header) {
  80. debug('regenerating stored HTTP header string for request');
  81. req._header = null;
  82. req._implicitHeader();
  83. if (req.output && req.output.length > 0) {
  84. debug('patching connection write() output buffer with updated header');
  85. // the _header has already been queued to be written to the socket
  86. var first = req.output[0];
  87. var endOfHeaders = first.indexOf('\r\n\r\n') + 4;
  88. req.output[0] = req._header + first.substring(endOfHeaders);
  89. debug('output buffer: %o', req.output);
  90. }
  91. }
  92. fn(null, socket);
  93. };