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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _serializeError = require("serialize-error");
  7. var _boolean = require("boolean");
  8. var _Logger = _interopRequireDefault(require("../Logger"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. const log = _Logger.default.child({
  11. namespace: 'Agent'
  12. });
  13. let requestId = 0;
  14. class Agent {
  15. constructor(isProxyConfigured, mustUrlUseProxy, getUrlProxy, fallbackAgent, socketConnectionTimeout) {
  16. this.fallbackAgent = fallbackAgent;
  17. this.isProxyConfigured = isProxyConfigured;
  18. this.mustUrlUseProxy = mustUrlUseProxy;
  19. this.getUrlProxy = getUrlProxy;
  20. this.socketConnectionTimeout = socketConnectionTimeout;
  21. }
  22. addRequest(request, configuration) {
  23. let requestUrl; // It is possible that addRequest was constructed for a proxied request already, e.g.
  24. // "request" package does this when it detects that a proxy should be used
  25. // https://github.com/request/request/blob/212570b6971a732b8dd9f3c73354bcdda158a737/request.js#L402
  26. // https://gist.github.com/gajus/e2074cd3b747864ffeaabbd530d30218
  27. if (request.path.startsWith('http://') || request.path.startsWith('https://')) {
  28. requestUrl = request.path;
  29. } else {
  30. requestUrl = this.protocol + '//' + (configuration.hostname || configuration.host) + (configuration.port === 80 || configuration.port === 443 ? '' : ':' + configuration.port) + request.path;
  31. }
  32. if (!this.isProxyConfigured()) {
  33. log.trace({
  34. destination: requestUrl
  35. }, 'not proxying request; GLOBAL_AGENT.HTTP_PROXY is not configured'); // $FlowFixMe It appears that Flow is missing the method description.
  36. this.fallbackAgent.addRequest(request, configuration);
  37. return;
  38. }
  39. if (!this.mustUrlUseProxy(requestUrl)) {
  40. log.trace({
  41. destination: requestUrl
  42. }, 'not proxying request; url matches GLOBAL_AGENT.NO_PROXY'); // $FlowFixMe It appears that Flow is missing the method description.
  43. this.fallbackAgent.addRequest(request, configuration);
  44. return;
  45. }
  46. const currentRequestId = requestId++;
  47. const proxy = this.getUrlProxy(requestUrl);
  48. if (this.protocol === 'http:') {
  49. request.path = requestUrl;
  50. if (proxy.authorization) {
  51. request.setHeader('proxy-authorization', 'Basic ' + Buffer.from(proxy.authorization).toString('base64'));
  52. }
  53. }
  54. log.trace({
  55. destination: requestUrl,
  56. proxy: 'http://' + proxy.hostname + ':' + proxy.port,
  57. requestId: currentRequestId
  58. }, 'proxying request');
  59. request.on('error', error => {
  60. log.error({
  61. error: (0, _serializeError.serializeError)(error)
  62. }, 'request error');
  63. });
  64. request.once('response', response => {
  65. log.trace({
  66. headers: response.headers,
  67. requestId: currentRequestId,
  68. statusCode: response.statusCode
  69. }, 'proxying response');
  70. });
  71. request.shouldKeepAlive = false;
  72. const connectionConfiguration = {
  73. host: configuration.hostname || configuration.host,
  74. port: configuration.port || 80,
  75. proxy,
  76. tls: {}
  77. }; // add optional tls options for https requests.
  78. // @see https://nodejs.org/docs/latest-v12.x/api/https.html#https_https_request_url_options_callback :
  79. // > The following additional options from tls.connect()
  80. // > - https://nodejs.org/docs/latest-v12.x/api/tls.html#tls_tls_connect_options_callback -
  81. // > are also accepted:
  82. // > ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, honorCipherOrder,
  83. // > key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, sessionIdContext.
  84. if (this.protocol === 'https:') {
  85. connectionConfiguration.tls = {
  86. ca: configuration.ca,
  87. cert: configuration.cert,
  88. ciphers: configuration.ciphers,
  89. clientCertEngine: configuration.clientCertEngine,
  90. crl: configuration.crl,
  91. dhparam: configuration.dhparam,
  92. ecdhCurve: configuration.ecdhCurve,
  93. honorCipherOrder: configuration.honorCipherOrder,
  94. key: configuration.key,
  95. passphrase: configuration.passphrase,
  96. pfx: configuration.pfx,
  97. rejectUnauthorized: configuration.rejectUnauthorized,
  98. secureOptions: configuration.secureOptions,
  99. secureProtocol: configuration.secureProtocol,
  100. servername: configuration.servername || connectionConfiguration.host,
  101. sessionIdContext: configuration.sessionIdContext
  102. }; // This is not ideal because there is no way to override this setting using `tls` configuration if `NODE_TLS_REJECT_UNAUTHORIZED=0`.
  103. // However, popular HTTP clients (such as https://github.com/sindresorhus/got) come with pre-configured value for `rejectUnauthorized`,
  104. // which makes it impossible to override that value globally and respect `rejectUnauthorized` for specific requests only.
  105. //
  106. // eslint-disable-next-line no-process-env
  107. if (typeof process.env.NODE_TLS_REJECT_UNAUTHORIZED === 'string' && (0, _boolean.boolean)(process.env.NODE_TLS_REJECT_UNAUTHORIZED) === false) {
  108. connectionConfiguration.tls.rejectUnauthorized = false;
  109. }
  110. } // $FlowFixMe It appears that Flow is missing the method description.
  111. this.createConnection(connectionConfiguration, (error, socket) => {
  112. log.trace({
  113. target: connectionConfiguration
  114. }, 'connecting'); // @see https://github.com/nodejs/node/issues/5757#issuecomment-305969057
  115. if (socket) {
  116. socket.setTimeout(this.socketConnectionTimeout, () => {
  117. socket.destroy();
  118. });
  119. socket.once('connect', () => {
  120. log.trace({
  121. target: connectionConfiguration
  122. }, 'connected');
  123. socket.setTimeout(0);
  124. });
  125. socket.once('secureConnect', () => {
  126. log.trace({
  127. target: connectionConfiguration
  128. }, 'connected (secure)');
  129. socket.setTimeout(0);
  130. });
  131. }
  132. if (error) {
  133. request.emit('error', error);
  134. } else {
  135. log.debug('created socket');
  136. socket.on('error', socketError => {
  137. log.error({
  138. error: (0, _serializeError.serializeError)(socketError)
  139. }, 'socket error');
  140. });
  141. request.onSocket(socket);
  142. }
  143. });
  144. }
  145. }
  146. var _default = Agent;
  147. exports.default = _default;
  148. //# sourceMappingURL=Agent.js.map