diff options
| author | MeiMei <30769358+mei23@users.noreply.github.com> | 2020-04-12 20:32:34 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-12 20:32:34 +0900 |
| commit | 36b9a0d42fa4bd7036d5d4a11203006d0ceb59f0 (patch) | |
| tree | 6fb00ab7bdf952ebb7e0c6e705fc5837a8022689 /src/misc | |
| parent | Resolve #6242 (diff) | |
| download | sharkey-36b9a0d42fa4bd7036d5d4a11203006d0ceb59f0.tar.gz sharkey-36b9a0d42fa4bd7036d5d4a11203006d0ceb59f0.tar.bz2 sharkey-36b9a0d42fa4bd7036d5d4a11203006d0ceb59f0.zip | |
プロキシの除外ホスト (#6244)
* プロキシの除外ホスト
* オブジェクトストレージとの通信にProxyを使うかを選択できるように
* fix lint
* コメント
Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
Diffstat (limited to 'src/misc')
| -rw-r--r-- | src/misc/download-url.ts | 4 | ||||
| -rw-r--r-- | src/misc/fetch.ts | 49 |
2 files changed, 41 insertions, 12 deletions
diff --git a/src/misc/download-url.ts b/src/misc/download-url.ts index 3f42fb3bef..9c8439f2c0 100644 --- a/src/misc/download-url.ts +++ b/src/misc/download-url.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as stream from 'stream'; import * as util from 'util'; import fetch from 'node-fetch'; -import { httpAgent, httpsAgent } from './fetch'; +import { getAgentByUrl } from './fetch'; import { AbortController } from 'abort-controller'; import config from '../config'; import * as chalk from 'chalk'; @@ -25,7 +25,7 @@ export async function downloadUrl(url: string, path: string) { }, timeout: 10 * 1000, signal: controller.signal, - agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent, + agent: getAgentByUrl, }); if (!response.ok) { diff --git a/src/misc/fetch.ts b/src/misc/fetch.ts index 887aae1659..358bc25030 100644 --- a/src/misc/fetch.ts +++ b/src/misc/fetch.ts @@ -13,7 +13,7 @@ export async function getJson(url: string, accept = 'application/json, */*', tim Accept: accept }, headers || {}), timeout, - agent: u => u.protocol == 'http:' ? httpAgent : httpsAgent, + agent: getAgentByUrl, }); if (!res.ok) { @@ -27,17 +27,46 @@ export async function getJson(url: string, accept = 'application/json, */*', tim return await res.json(); } +/** + * Get http non-proxy agent + */ +const _http = new http.Agent({ + keepAlive: true, + keepAliveMsecs: 30 * 1000, +}); + +/** + * Get https non-proxy agent + */ +const _https = new https.Agent({ + keepAlive: true, + keepAliveMsecs: 30 * 1000, + lookup: cache.lookup, +}); + +/** + * Get http proxy or non-proxy agent + */ export const httpAgent = config.proxy ? new HttpProxyAgent(config.proxy) - : new http.Agent({ - keepAlive: true, - keepAliveMsecs: 30 * 1000, - }); + : _http; +/** + * Get https proxy or non-proxy agent + */ export const httpsAgent = config.proxy ? new HttpsProxyAgent(config.proxy) - : new https.Agent({ - keepAlive: true, - keepAliveMsecs: 30 * 1000, - lookup: cache.lookup, - }); + : _https; + +/** + * Get agent by URL + * @param url URL + * @param bypassProxy Allways bypass proxy + */ +export function getAgentByUrl(url: URL, bypassProxy = false) { + if (bypassProxy || (config.proxyBypassHosts || []).includes(url.hostname)) { + return url.protocol == 'http:' ? _http : _https; + } else { + return url.protocol == 'http:' ? httpAgent : httpsAgent; + } +} |