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.

README.md 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # global-agent
  2. [![GitSpo Mentions](https://gitspo.com/badges/mentions/gajus/global-agent?style=flat-square)](https://gitspo.com/mentions/gajus/global-agent)
  3. [![Travis build status](http://img.shields.io/travis/gajus/global-agent/master.svg?style=flat-square)](https://travis-ci.org/gajus/global-agent)
  4. [![Coveralls](https://img.shields.io/coveralls/gajus/global-agent.svg?style=flat-square)](https://coveralls.io/github/gajus/global-agent)
  5. [![NPM version](http://img.shields.io/npm/v/global-agent.svg?style=flat-square)](https://www.npmjs.org/package/global-agent)
  6. [![Canonical Code Style](https://img.shields.io/badge/code%20style-canonical-blue.svg?style=flat-square)](https://github.com/gajus/canonical)
  7. [![Twitter Follow](https://img.shields.io/twitter/follow/kuizinas.svg?style=social&label=Follow)](https://twitter.com/kuizinas)
  8. Global HTTP/HTTPS proxy configurable using environment variables.
  9. * [Usage](#usage)
  10. * [Setup proxy using `global-agent/bootstrap`](#setup-proxy-using-global-agentbootstrap)
  11. * [Setup proxy using `bootstrap` routine](#setup-proxy-using-bootstrap-routine)
  12. * [Runtime configuration](#runtime-configuration)
  13. * [Exclude URLs](#exclude-urls)
  14. * [Enable logging](#enable-logging)
  15. * [API](#api)
  16. * [`createGlobalProxyAgent`](#createglobalproxyagent)
  17. * [Environment variables](#environment-variables)
  18. * [`global.GLOBAL_AGENT`](#globalglobal_agent)
  19. * [Supported libraries](#supported-libraries)
  20. * [FAQ](#faq)
  21. * [What is the reason `global-agent` overrides explicitly configured HTTP(S) agent?](#what-is-the-reason-global-agent-overrides-explicitly-configured-https-agent)
  22. * [What is the reason `global-agent/bootstrap` does not use `HTTP_PROXY`?](#what-is-the-reason-global-agentbootstrap-does-not-use-http_proxy)
  23. * [What is the difference from `global-tunnel` and `tunnel`?](#what-is-the-difference-from-global-tunnel-and-tunnel)
  24. ## Usage
  25. ### Setup proxy using `global-agent/bootstrap`
  26. To configure HTTP proxy:
  27. 1. Import `global-agent/bootstrap`.
  28. 1. Export HTTP proxy address as `GLOBAL_AGENT_HTTP_PROXY` environment variable.
  29. Code:
  30. ```js
  31. import 'global-agent/bootstrap';
  32. // or:
  33. // import {bootstrap} from 'global-agent';
  34. // bootstrap();
  35. ```
  36. Bash:
  37. ```bash
  38. $ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080
  39. ```
  40. Alternatively, you can preload module using Node.js `--require, -r` configuration, e.g.
  41. ```bash
  42. $ export GLOBAL_AGENT_HTTP_PROXY=http://127.0.0.1:8080
  43. $ node -r 'global-agent/bootstrap' your-script.js
  44. ```
  45. ### Setup proxy using `bootstrap` routine
  46. Instead of importing a self-initialising script with side-effects as demonstrated in the [setup proxy using `global-agent/bootstrap`](#setup-proxy-using-global-agentbootstrap) documentation, you can import `bootstrap` routine and explicitly evaluate the bootstrap logic, e.g.
  47. ```js
  48. import {
  49. bootstrap
  50. } from 'global-agent';
  51. bootstrap();
  52. ```
  53. This is useful if you need to conditionally bootstrap `global-agent`, e.g.
  54. ```js
  55. import {
  56. bootstrap
  57. } from 'global-agent';
  58. import globalTunnel from 'global-tunnel-ng';
  59. const MAJOR_NODEJS_VERSION = parseInt(process.version.slice(1).split('.')[0], 10);
  60. if (MAJOR_NODEJS_VERSION >= 10) {
  61. // `global-agent` works with Node.js v10 and above.
  62. bootstrap();
  63. } else {
  64. // `global-tunnel-ng` works only with Node.js v10 and below.
  65. globalTunnel.initialize();
  66. }
  67. ```
  68. ### Setup proxy using `createGlobalProxyAgent`
  69. If you do not want to use `global.GLOBAL_AGENT` variable, then you can use `createGlobalProxyAgent` to instantiate a controlled instance of `global-agent`, e.g.
  70. ```js
  71. import {
  72. createGlobalProxyAgent
  73. } from 'global-agent';
  74. const globalProxyAgent = createGlobalProxyAgent();
  75. ```
  76. Unlike `bootstrap` routine, `createGlobalProxyAgent` factory does not create `global.GLOBAL_AGENT` variable and does not guard against multiple initializations of `global-agent`. The result object of `createGlobalProxyAgent` is equivalent to `global.GLOBAL_AGENT`.
  77. ### Runtime configuration
  78. `global-agent/bootstrap` script copies `process.env.GLOBAL_AGENT_HTTP_PROXY` value to `global.GLOBAL_AGENT.HTTP_PROXY` and continues to use the latter variable.
  79. You can override the `global.GLOBAL_AGENT.HTTP_PROXY` value at runtime to change proxy behaviour, e.g.
  80. ```js
  81. http.get('http://127.0.0.1:8000');
  82. global.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8001';
  83. http.get('http://127.0.0.1:8000');
  84. global.GLOBAL_AGENT.HTTP_PROXY = 'http://127.0.0.1:8002';
  85. ```
  86. The first HTTP request is going to use http://127.0.0.1:8001 proxy and the secord request is going to use http://127.0.0.1:8002.
  87. All `global-agent` configuration is available under `global.GLOBAL_AGENT` namespace.
  88. ### Exclude URLs
  89. The `GLOBAL_AGENT_NO_PROXY` environment variable specifies a pattern of URLs that should be excluded from proxying. `GLOBAL_AGENT_NO_PROXY` value is a comma-separated list of domain names. Asterisks can be used as wildcards, e.g.
  90. ```bash
  91. export GLOBAL_AGENT_NO_PROXY='*.foo.com,baz.com'
  92. ```
  93. says to contact all machines with the 'foo.com' TLD and 'baz.com' domains directly.
  94. ### Separate proxy for HTTPS
  95. The environment variable `GLOBAL_AGENT_HTTPS_PROXY` can be set to specify a separate proxy for HTTPS requests. When this variable is not set `GLOBAL_AGENT_HTTP_PROXY` is used for both HTTP and HTTPS requests.
  96. ### Enable logging
  97. `global-agent` is using [`roarr`](https://www.npmjs.com/package/roarr) logger to log HTTP requests and response (HTTP status code and headers), e.g.
  98. ```json
  99. {"context":{"program":"global-agent","namespace":"Agent","logLevel":10,"destination":"http://gajus.com","proxy":"http://127.0.0.1:8076"},"message":"proxying request","sequence":1,"time":1556269669663,"version":"1.0.0"}
  100. {"context":{"program":"global-agent","namespace":"Agent","logLevel":10,"headers":{"content-type":"text/plain","content-length":"2","date":"Fri, 26 Apr 2019 12:07:50 GMT","connection":"close"},"requestId":6,"statusCode":200},"message":"proxying response","sequence":2,"time":1557133856955,"version":"1.0.0"}
  101. ```
  102. Export `ROARR_LOG=true` environment variable to enable log printing to stdout.
  103. Use [`roarr-cli`](https://github.com/gajus/roarr-cli) program to pretty-print the logs.
  104. ## API
  105. ### `createGlobalProxyAgent`
  106. ```js
  107. /**
  108. * @property environmentVariableNamespace Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables. (Default: `GLOBAL_AGENT_`)
  109. * @property forceGlobalAgent Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent. (Default: `true`)
  110. * @property socketConnectionTimeout Destroys socket if connection is not established within the timeout. (Default: `60000`)
  111. */
  112. type ProxyAgentConfigurationInputType = {|
  113. +environmentVariableNamespace?: string,
  114. +forceGlobalAgent?: boolean,
  115. +socketConnectionTimeout?: number,
  116. |};
  117. (configurationInput: ProxyAgentConfigurationInputType) => ProxyAgentConfigurationType;
  118. ```
  119. ### Environment variables
  120. |Name|Description|Default|
  121. |---|---|---|
  122. |`GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE`|Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables.|`GLOBAL_AGENT_`|
  123. |`GLOBAL_AGENT_FORCE_GLOBAL_AGENT`|Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent.|`true`|
  124. |`GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT`|Destroys socket if connection is not established within the timeout.|`60000`|
  125. |`${NAMESPACE}_HTTP_PROXY`|Sets the initial proxy controller HTTP_PROXY value.|N/A|
  126. |`${NAMESPACE}_HTTPS_PROXY`|Sets the initial proxy controller HTTPS_PROXY value.|N/A|
  127. |`${NAMESPACE}_NO_PROXY`|Sets the initial proxy controller NO_PROXY value.|N/A|
  128. ### `global.GLOBAL_AGENT`
  129. `global.GLOBAL_AGENT` is initialized by `bootstrap` routine.
  130. `global.GLOBAL_AGENT` has the following properties:
  131. |Name|Description|Configurable|
  132. |---|---|---|
  133. |`HTTP_PROXY`|Yes|Sets HTTP proxy to use.|
  134. |`HTTPS_PROXY`|Yes|Sets a distinct proxy to use for HTTPS requests.|
  135. |`NO_PROXY`|Yes|Specifies a pattern of URLs that should be excluded from proxying. See [Exclude URLs](#exclude-urls).|
  136. ## Supported libraries
  137. `global-agent` works with all libraries that internally use [`http.request`](https://nodejs.org/api/http.html#http_http_request_options_callback).
  138. `global-agent` has been tested to work with:
  139. * [`got`](https://www.npmjs.com/package/got)
  140. * [`axios`](https://www.npmjs.com/package/axios)
  141. * [`request`](https://www.npmjs.com/package/axios)
  142. ## FAQ
  143. ### What is the reason `global-agent` overrides explicitly configured HTTP(S) agent?
  144. By default, `global-agent` overrides [`agent` property](https://nodejs.org/api/http.html#http_http_request_options_callback) of any HTTP request, even if `agent` property was explicitly set when constructing a HTTP request. This behaviour allows to intercept requests of libraries that use a custom instance of an agent per default (e.g. Stripe SDK [uses an `http(s).globalAgent` instance pre-configured with `keepAlive: true`](https://github.com/stripe/stripe-node/blob/e542902dd8fbe591fe3c3ce07a7e89d1d60e4cf7/lib/StripeResource.js#L11-L12)).
  145. This behaviour can be disabled with `GLOBAL_AGENT_FORCE_GLOBAL_AGENT=false` environment variable. When disabled, then `global-agent` will only set `agent` property when it is not already defined or if `agent` is an instance of `http(s).globalAgent`.
  146. ### What is the reason `global-agent/bootstrap` does not use `HTTP_PROXY`?
  147. Some libraries (e.g. [`request`](https://npmjs.org/package/request)) change their behaviour when `HTTP_PROXY` environment variable is present. Using a namespaced environment variable prevents conflicting library behaviour.
  148. You can override this behaviour by configuring `GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE` variable, e.g.
  149. ```bash
  150. $ export GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE=
  151. ```
  152. Now script initialized using `global-agent/bootstrap` will use `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables.
  153. ### What is the difference from `global-tunnel` and `tunnel`?
  154. [`global-tunnel`](https://github.com/salesforce/global-tunnel) (including [`global-tunnel-ng`](https://github.com/np-maintain/global-tunnel) and [`tunnel`](https://npmjs.com/package/tunnel)) are designed to support legacy Node.js versions. They use various [workarounds](https://github.com/koichik/node-tunnel/blob/5fb2fb424788597146b7be6729006cad1cf9e9a8/lib/tunnel.js#L134-L144) and rely on [monkey-patching `http.request`, `http.get`, `https.request` and `https.get` methods](https://github.com/np-maintain/global-tunnel/blob/51413dcf0534252b5049ec213105c7063ccc6367/index.js#L302-L338).
  155. In contrast, `global-agent` supports Node.js v10 and above, and does not implements workarounds for the older Node.js versions.