From 0d8250a3acc68ea7ffb3928f9db66eea369a45ea Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 16 Feb 2016 14:24:45 +0100 Subject: [PATCH] HTTP proxy support (fix vatesfr/xo-web#737). --- package.json | 1 + sample.config.yaml | 6 ++++++ src/http-proxy.js | 12 ++++++++++++ src/index.js | 7 +++++++ src/xapi.js | 13 +++++++++---- 5 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/http-proxy.js diff --git a/package.json b/package.json index 7cde3019d..29525ba97 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "passport": "^0.3.0", "passport-local": "^1.0.0", "promise-toolbox": "^0.1.0", + "proxy-agent": "^2.0.0", "proxy-http-request": "0.1.0", "redis": "^2.0.1", "schema-inspector": "^1.5.1", diff --git a/sample.config.yaml b/sample.config.yaml index 655061ad2..0b8454092 100644 --- a/sample.config.yaml +++ b/sample.config.yaml @@ -107,6 +107,12 @@ http: proxies: # '/any/url': 'http://localhost:54722' +# HTTP proxy configuration used by xo-server to fetch resources on the +# Internet. +# +# See: https://github.com/TooTallNate/node-proxy-agent#maps-proxy-protocols-to-httpagent-implementations +#httpProxy: 'http://jsmith:qwerty@proxy.lan:3128' + #===================================================================== # Connection to the Redis server. diff --git a/src/http-proxy.js b/src/http-proxy.js new file mode 100644 index 000000000..1a6b1427e --- /dev/null +++ b/src/http-proxy.js @@ -0,0 +1,12 @@ +import ProxyAgent from 'proxy-agent' + +export let agent + +export function setup (uri) { + agent = uri != null + ? new ProxyAgent(uri) + : undefined +} + +const { env } = process +setup(env.http_proxy || env.HTTP_PROXY) diff --git a/src/index.js b/src/index.js index 98f999ab1..25d337b8f 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,9 @@ import Api from './api' import WebServer from 'http-server-plus' import wsProxy from './ws-proxy' import Xo from './xo' +import { + setup as setupHttpProxy +} from './http-proxy' import { createRawObject, forEach, @@ -600,6 +603,10 @@ export default async function main (args) { warn('Failed to change user/group:', error) } + if (config.httpProxy) { + setupHttpProxy(config.httpProxy) + } + // Creates main object. const xo = new Xo(config) diff --git a/src/xapi.js b/src/xapi.js index cf1dea4c9..ec44c4442 100644 --- a/src/xapi.js +++ b/src/xapi.js @@ -17,14 +17,18 @@ import { wrapError as wrapXapiError, Xapi as XapiBase } from 'xen-api' +import { + satisfies as versionSatisfies +} from 'semver' import httpRequest from './http-request' import { debounce, deferrable } from './decorators' -import { satisfies as versionSatisfies } from 'semver' - +import { + agent as httpProxy +} from './http-proxy' import { bufferToStream, camelToSnakeCase, @@ -447,7 +451,8 @@ export default class Xapi extends XapiBase { @debounce(24 * 60 * 60 * 1000) async _getXenUpdates () { const { readAll, statusCode } = await httpRequest( - 'http://updates.xensource.com/XenServer/updates.xml' + 'http://updates.xensource.com/XenServer/updates.xml', + { agent: httpProxy } ) if (statusCode !== 200) { @@ -637,7 +642,7 @@ export default class Xapi extends XapiBase { throw new Error('no such patch ' + uuid) } - let stream = await httpRequest(patchInfo.url) + let stream = await httpRequest(patchInfo.url, { agent: httpProxy }) stream = await new Promise((resolve, reject) => { const PATCH_RE = /\.xsupdate$/ stream.pipe(unzip.Parse()).on('entry', entry => {