Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

agent.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 net_1 = __importDefault(require("net"));
  16. const tls_1 = __importDefault(require("tls"));
  17. const url_1 = __importDefault(require("url"));
  18. const debug_1 = __importDefault(require("debug"));
  19. const once_1 = __importDefault(require("@tootallnate/once"));
  20. const agent_base_1 = require("agent-base");
  21. const debug = debug_1.default('http-proxy-agent');
  22. function isHTTPS(protocol) {
  23. return typeof protocol === 'string' ? /^https:?$/i.test(protocol) : false;
  24. }
  25. /**
  26. * The `HttpProxyAgent` implements an HTTP Agent subclass that connects
  27. * to the specified "HTTP proxy server" in order to proxy HTTP requests.
  28. *
  29. * @api public
  30. */
  31. class HttpProxyAgent extends agent_base_1.Agent {
  32. constructor(_opts) {
  33. let opts;
  34. if (typeof _opts === 'string') {
  35. opts = url_1.default.parse(_opts);
  36. }
  37. else {
  38. opts = _opts;
  39. }
  40. if (!opts) {
  41. throw new Error('an HTTP(S) proxy server `host` and `port` must be specified!');
  42. }
  43. debug('Creating new HttpProxyAgent instance: %o', opts);
  44. super(opts);
  45. const proxy = Object.assign({}, opts);
  46. // If `true`, then connect to the proxy server over TLS.
  47. // Defaults to `false`.
  48. this.secureProxy = opts.secureProxy || isHTTPS(proxy.protocol);
  49. // Prefer `hostname` over `host`, and set the `port` if needed.
  50. proxy.host = proxy.hostname || proxy.host;
  51. if (typeof proxy.port === 'string') {
  52. proxy.port = parseInt(proxy.port, 10);
  53. }
  54. if (!proxy.port && proxy.host) {
  55. proxy.port = this.secureProxy ? 443 : 80;
  56. }
  57. if (proxy.host && proxy.path) {
  58. // If both a `host` and `path` are specified then it's most likely
  59. // the result of a `url.parse()` call... we need to remove the
  60. // `path` portion so that `net.connect()` doesn't attempt to open
  61. // that as a Unix socket file.
  62. delete proxy.path;
  63. delete proxy.pathname;
  64. }
  65. this.proxy = proxy;
  66. }
  67. /**
  68. * Called when the node-core HTTP client library is creating a
  69. * new HTTP request.
  70. *
  71. * @api protected
  72. */
  73. callback(req, opts) {
  74. return __awaiter(this, void 0, void 0, function* () {
  75. const { proxy, secureProxy } = this;
  76. const parsed = url_1.default.parse(req.path);
  77. if (!parsed.protocol) {
  78. parsed.protocol = 'http:';
  79. }
  80. if (!parsed.hostname) {
  81. parsed.hostname = opts.hostname || opts.host || null;
  82. }
  83. if (parsed.port == null && typeof opts.port) {
  84. parsed.port = String(opts.port);
  85. }
  86. if (parsed.port === '80') {
  87. // if port is 80, then we can remove the port so that the
  88. // ":80" portion is not on the produced URL
  89. delete parsed.port;
  90. }
  91. // Change the `http.ClientRequest` instance's "path" field
  92. // to the absolute path of the URL that will be requested.
  93. req.path = url_1.default.format(parsed);
  94. // Inject the `Proxy-Authorization` header if necessary.
  95. if (proxy.auth) {
  96. req.setHeader('Proxy-Authorization', `Basic ${Buffer.from(proxy.auth).toString('base64')}`);
  97. }
  98. // Create a socket connection to the proxy server.
  99. let socket;
  100. if (secureProxy) {
  101. debug('Creating `tls.Socket`: %o', proxy);
  102. socket = tls_1.default.connect(proxy);
  103. }
  104. else {
  105. debug('Creating `net.Socket`: %o', proxy);
  106. socket = net_1.default.connect(proxy);
  107. }
  108. // At this point, the http ClientRequest's internal `_header` field
  109. // might have already been set. If this is the case then we'll need
  110. // to re-generate the string since we just changed the `req.path`.
  111. if (req._header) {
  112. let first;
  113. let endOfHeaders;
  114. debug('Regenerating stored HTTP header string for request');
  115. req._header = null;
  116. req._implicitHeader();
  117. if (req.output && req.output.length > 0) {
  118. // Node < 12
  119. debug('Patching connection write() output buffer with updated header');
  120. first = req.output[0];
  121. endOfHeaders = first.indexOf('\r\n\r\n') + 4;
  122. req.output[0] = req._header + first.substring(endOfHeaders);
  123. debug('Output buffer: %o', req.output);
  124. }
  125. else if (req.outputData && req.outputData.length > 0) {
  126. // Node >= 12
  127. debug('Patching connection write() output buffer with updated header');
  128. first = req.outputData[0].data;
  129. endOfHeaders = first.indexOf('\r\n\r\n') + 4;
  130. req.outputData[0].data =
  131. req._header + first.substring(endOfHeaders);
  132. debug('Output buffer: %o', req.outputData[0].data);
  133. }
  134. }
  135. // Wait for the socket's `connect` event, so that this `callback()`
  136. // function throws instead of the `http` request machinery. This is
  137. // important for i.e. `PacProxyAgent` which determines a failed proxy
  138. // connection via the `callback()` function throwing.
  139. yield once_1.default(socket, 'connect');
  140. return socket;
  141. });
  142. }
  143. }
  144. exports.default = HttpProxyAgent;
  145. //# sourceMappingURL=agent.js.map