From ac7330432853ac7ec47a7a42db7106edaeff0dea Mon Sep 17 00:00:00 2001 From: Alicia Lopez Date: Fri, 5 Jun 2020 23:30:09 +0800 Subject: [PATCH] feat: support system proxy (#408) --- lib/proxy.js | 21 +++++++++++++++++++++ lib/request.js | 5 +++++ package.json | 1 + 3 files changed, 27 insertions(+) create mode 100644 lib/proxy.js diff --git a/lib/proxy.js b/lib/proxy.js new file mode 100644 index 0000000..deae20a --- /dev/null +++ b/lib/proxy.js @@ -0,0 +1,21 @@ +const ProxyAgent = require('proxy-agent'); +const { globalAgent } = require('https'); +const { spawnSync } = require('child_process'); +const { getMergedConfig } = require('./config'); + +function proxy() { + let proxyUrl = getMergedConfig().proxy; + if (proxyUrl == null || proxyUrl === '') { + proxyUrl = spawnSync( + 'git', + ['config', '--get', '--path', 'https.proxy'] + ).stdout.toString(); + } + if (proxyUrl == null || proxyUrl === '') { + return globalAgent; + } else { + return new ProxyAgent(proxyUrl); + } +} + +module.exports = proxy; diff --git a/lib/request.js b/lib/request.js index 3aff3e9..bcb1adc 100644 --- a/lib/request.js +++ b/lib/request.js @@ -4,10 +4,12 @@ const fetch = require('node-fetch'); const fs = require('fs'); const path = require('path'); const { CI_DOMAIN } = require('./ci/ci_type_parser'); +const proxy = require('./proxy'); class Request { constructor(credentials) { this.credentials = credentials; + this.proxyAgent = proxy(); } loadQuery(file) { @@ -16,6 +18,7 @@ class Request { } async text(url, options = {}) { + options.agent = this.proxyAgent; if (url.startsWith(`https://${CI_DOMAIN}`)) { options.headers = options.headers || {}; Object.assign(options.headers, this.getJenkinsHeaders()); @@ -24,6 +27,7 @@ class Request { } async json(url, options = {}) { + options.agent = this.proxyAgent; options.headers = options.headers || {}; options.headers.Accept = 'application/json'; if (url.startsWith(`https://${CI_DOMAIN}`)) { @@ -65,6 +69,7 @@ class Request { } const url = 'https://api.github.com/graphql'; const options = { + agent: this.proxyAgent, method: 'POST', headers: { Authorization: `Basic ${githubCredentials}`, diff --git a/package.json b/package.json index 6534c46..e2d2cc8 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "lodash": "^4.17.15", "node-fetch": "^2.6.0", "ora": "^4.0.4", + "proxy-agent": "^3.1.1", "replace-in-file": "^6.0.0", "rimraf": "^3.0.2", "yargs": "^15.3.1"