feat(xo-server): inject proxy in env (#6322)

Fixes zammad#8073

Related to #6320

- brings `no_proxy` supports
- implicit supports for other libs
This commit is contained in:
Julien Fontanet 2022-07-20 15:27:57 +02:00 committed by GitHub
parent af87d6a0ea
commit 9ccb5f8aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 11 deletions

View File

@ -15,6 +15,11 @@
# See: https://github.com/TooTallNate/node-proxy-agent#maps-proxy-protocols-to-httpagent-implementations
# httpProxy = 'http://jsmith:qwerty@proxy.lan:3128'
# List of host names (optionally with a port), separated by commas, for which
# the proxy above will not be used.
#
# noProxy = 'example.net, example.com:443'
#=====================================================================
# It may be necessary to run XO-Server as a privileged user (e.g. `root`) for

View File

@ -1,8 +1,12 @@
import hrp from 'http-request-plus'
import ProxyAgent from 'proxy-agent'
import firstDefined from '@xen-orchestra/defined'
const { env } = process
export default class Http {
// automatically fetches the proxy to use for the requested URL from the environnement
_agent = new ProxyAgent()
// whether XO has a proxy set from its own config/environment
get hasOwnHttpProxy() {
return this._hasOwnHttpProxy
@ -12,16 +16,14 @@ export default class Http {
return this._agent
}
constructor(_, { config: { httpProxy = firstDefined(process.env.http_proxy, process.env.HTTP_PROXY) } }) {
this._hasOwnHttpProxy = httpProxy != null
constructor(_, { config: { httpProxy, noProxy } }) {
if (httpProxy !== undefined) {
this._hasOwnHttpProxy = true
this.setHttpProxy(httpProxy)
this.setHttpProxy(httpProxy, noProxy)
}
}
// TODO: find a way to decide for which addresses the proxy should be used
//
// For the moment, use this method only to access external resources (e.g.
// patches, upgrades, support tunnel)
httpRequest(...args) {
return hrp(
{
@ -31,11 +33,24 @@ export default class Http {
)
}
setHttpProxy(proxy) {
// Inject the proxy into the environnement, it will be automatically used by `_agent` and by most libs (e.g `axios`)
setHttpProxy(proxy, noProxy) {
if (proxy == null) {
this._agent = undefined
delete env.http_proxy
delete env.HTTP_PROXY
delete env.https_proxy
delete env.HTTPS_PROXY
} else {
this._agent = new ProxyAgent(proxy)
env.http_proxy = env.HTTP_PROXY = env.https_proxy = env.HTTPS_PROXY = proxy
}
if (noProxy !== undefined) {
if (proxy === null) {
delete env.no_proxy
delete env.NO_PROXY
} else {
env.no_proxy = env.NO_PROXY = noProxy
}
}
}
}