Merge pull request #1882 from automatisch/no-proxy
feat: add no_proxy support in http(s) agents
This commit is contained in:
@@ -6,14 +6,16 @@ const config = axios.defaults;
|
|||||||
const httpProxyUrl = process.env.http_proxy;
|
const httpProxyUrl = process.env.http_proxy;
|
||||||
const httpsProxyUrl = process.env.https_proxy;
|
const httpsProxyUrl = process.env.https_proxy;
|
||||||
const supportsProxy = httpProxyUrl || httpsProxyUrl;
|
const supportsProxy = httpProxyUrl || httpsProxyUrl;
|
||||||
|
const noProxyEnv = process.env.no_proxy;
|
||||||
|
const noProxyHosts = noProxyEnv ? noProxyEnv.split(',').map(host => host.trim()) : [];
|
||||||
|
|
||||||
if (supportsProxy) {
|
if (supportsProxy) {
|
||||||
if (httpProxyUrl) {
|
if (httpProxyUrl) {
|
||||||
config.httpAgent = new HttpProxyAgent(process.env.http_proxy);
|
config.httpAgent = new HttpProxyAgent(httpProxyUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (httpsProxyUrl) {
|
if (httpsProxyUrl) {
|
||||||
config.httpsAgent = new HttpsProxyAgent(process.env.https_proxy);
|
config.httpsAgent = new HttpsProxyAgent(httpsProxyUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
config.proxy = false;
|
config.proxy = false;
|
||||||
@@ -21,4 +23,21 @@ if (supportsProxy) {
|
|||||||
|
|
||||||
const axiosWithProxyInstance = axios.create(config);
|
const axiosWithProxyInstance = axios.create(config);
|
||||||
|
|
||||||
|
function shouldSkipProxy(hostname) {
|
||||||
|
return noProxyHosts.some(noProxyHost => {
|
||||||
|
return hostname.endsWith(noProxyHost) || hostname === noProxyHost;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
axiosWithProxyInstance.interceptors.request.use(function skipProxyIfInNoProxy(requestConfig) {
|
||||||
|
const hostname = new URL(requestConfig.url).hostname;
|
||||||
|
|
||||||
|
if (supportsProxy && shouldSkipProxy(hostname)) {
|
||||||
|
requestConfig.httpAgent = undefined;
|
||||||
|
requestConfig.httpsAgent = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return requestConfig;
|
||||||
|
});
|
||||||
|
|
||||||
export default axiosWithProxyInstance;
|
export default axiosWithProxyInstance;
|
||||||
|
Reference in New Issue
Block a user