プロキシの除外ホスト (#6244)

* プロキシの除外ホスト

* オブジェクトストレージとの通信にProxyを使うかを選択できるように

* fix lint

* コメント

Co-authored-by: rinsuki <428rinsuki+git@gmail.com>
This commit is contained in:
MeiMei
2020-04-12 20:32:34 +09:00
committed by GitHub
parent e7da10ae58
commit 36b9a0d42f
12 changed files with 89 additions and 16 deletions

View File

@ -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;
}
}