2021-09-22 12:57:51 +00:00
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
|
|
import * as https from 'https';
|
2021-07-22 23:04:56 +00:00
|
|
|
import { setDefaultProtocol } from 'insomnia-url';
|
|
|
|
import { parse as urlParse } from 'url';
|
|
|
|
|
2020-04-26 20:33:39 +00:00
|
|
|
import { isDevelopment } from '../common/constants';
|
2021-07-22 23:04:56 +00:00
|
|
|
import * as models from '../models';
|
2022-01-12 14:22:56 +00:00
|
|
|
import { isUrlMatchedInNoProxyRule } from './is-url-matched-in-no-proxy-rule';
|
2020-04-26 20:33:39 +00:00
|
|
|
|
2021-09-22 12:57:51 +00:00
|
|
|
export async function axiosRequest(config: AxiosRequestConfig) {
|
2020-04-26 20:33:39 +00:00
|
|
|
const settings = await models.settings.getOrCreate();
|
2021-09-22 12:57:51 +00:00
|
|
|
const isHttps = config.url?.indexOf('https:') === 0;
|
2021-05-12 06:35:00 +00:00
|
|
|
let proxyUrl: string | null = null;
|
2020-04-26 20:33:39 +00:00
|
|
|
|
|
|
|
if (isHttps && settings.httpsProxy) {
|
|
|
|
proxyUrl = settings.httpsProxy;
|
|
|
|
} else if (settings.httpProxy) {
|
|
|
|
proxyUrl = settings.httpProxy;
|
|
|
|
}
|
|
|
|
|
2021-09-22 12:57:51 +00:00
|
|
|
const finalConfig: AxiosRequestConfig = {
|
|
|
|
...config,
|
|
|
|
adapter: global.require('axios/lib/adapters/http'),
|
|
|
|
httpsAgent: new https.Agent({
|
|
|
|
rejectUnauthorized: settings.validateSSL,
|
|
|
|
}),
|
|
|
|
};
|
2020-04-26 20:33:39 +00:00
|
|
|
|
2022-01-12 14:22:56 +00:00
|
|
|
// ignore HTTP_PROXY, HTTPS_PROXY, NO_PROXY environment variables
|
|
|
|
finalConfig.proxy = false;
|
|
|
|
if (settings.proxyEnabled && proxyUrl && !isUrlMatchedInNoProxyRule(finalConfig.url, settings.noProxy)) {
|
2020-04-26 20:33:39 +00:00
|
|
|
const { hostname, port } = urlParse(setDefaultProtocol(proxyUrl));
|
2021-09-22 12:57:51 +00:00
|
|
|
|
|
|
|
if (hostname && port) {
|
|
|
|
finalConfig.proxy = {
|
|
|
|
host: hostname,
|
|
|
|
port: parseInt(port, 10),
|
|
|
|
};
|
|
|
|
}
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const response = await axios(finalConfig);
|
|
|
|
|
|
|
|
if (isDevelopment()) {
|
2021-05-12 06:35:00 +00:00
|
|
|
console.log('[axios] Response', {
|
|
|
|
config,
|
|
|
|
response,
|
|
|
|
});
|
2020-04-26 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|