fix: proxy changes

pull/3757/head
Ryan 5 months ago
parent 4659bf312a
commit 67e6f05a00

@ -60,6 +60,7 @@
"express-session": "1.17.3",
"formik": "2.2.9",
"gravatar-url": "3.1.0",
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.2",
"intl": "1.2.5",
"lodash": "4.17.21",

@ -1,4 +1,3 @@
import logger from '@server/logger';
import type {
AxiosInstance,
AxiosProxyConfig,
@ -7,9 +6,8 @@ import type {
} from 'axios';
import axios from 'axios';
import rateLimit from 'axios-rate-limit';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import type { NeedleOptions } from 'needle';
import needle from 'needle';
import type NodeCache from 'node-cache';
// 5 minute default TTL (in seconds)
@ -58,18 +56,10 @@ class ExternalAPI {
};
if (process.env.HTTPS_PROXY) {
const parsedUrl = new URL(process.env.HTTPS_PROXY);
const port = parseInt(parsedUrl.port);
this.proxy = {
host: parsedUrl.hostname,
port: port,
auth: {
username: parsedUrl.username,
password: parsedUrl.password,
},
protocol: port == 443 ? 'https' : 'http',
};
this.config.proxy = this.proxy;
this.config.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
if (process.env.HTTP_PROXY) {
this.config.httpAgent = new HttpProxyAgent(process.env.HTTP_PROXY);
}
}
this.axios = axios.create(this.config);
@ -96,41 +86,13 @@ class ExternalAPI {
return cachedItem;
}
const params = {
...config?.params,
...this.params,
};
const args = '?' + new URLSearchParams(params).toString();
const path = new URL(endpoint + args, this.baseUrl);
const request: NeedleOptions = {
json: true,
headers: this.headers,
};
const response = await this.axios.get<T>(endpoint, config);
if (process.env.HTTPS_PROXY) {
request.agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
if (this.cache) {
this.cache.set(cacheKey, response.data, ttl ?? DEFAULT_TTL);
}
return needle('get', path.toString(), request)
.then((res) => {
if (res.statusCode && res.statusCode >= 200 && res.statusCode <= 400) {
return res.body;
} else {
logger.error(
'Failed to get %s, reason %s',
path.toString(),
res.statusMessage
);
return res.body;
}
})
.then((data) => {
if (this.cache) {
this.cache.set(cacheKey, data, ttl ?? DEFAULT_TTL);
}
return data;
});
return response.data;
}
protected async post<T>(

@ -8176,7 +8176,7 @@ http-proxy-agent@^5.0.0:
http-proxy-agent@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.0.tgz"
resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-7.0.0.tgz#e9096c5afd071a3fce56e6252bb321583c124673"
integrity sha512-+ZT+iBxVUQ1asugqnD6oWoRiS25AkjNfG085dKJGtGxkdwLQrMKU5wJr2bOOFAXzKcTuqq+7fZlTMgG3SRfIYQ==
dependencies:
agent-base "^7.1.0"
@ -8215,15 +8215,7 @@ https-proxy-agent@^5.0.0:
agent-base "6"
debug "4"
https-proxy-agent@^7.0.0, https-proxy-agent@^7.0.1:
version "7.0.2"
resolved "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.2.tgz"
integrity sha512-NmLNjm6ucYwtcUmL7JQC1ZQ57LmHP4lT15FQ8D61nak1rO6DH+fz5qNK2Ap5UN4ZapYICE3/0KodcLYSPsPbaA==
dependencies:
agent-base "^7.0.2"
debug "4"
https-proxy-agent@^7.0.2:
https-proxy-agent@^7.0.0, https-proxy-agent@^7.0.1, https-proxy-agent@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.2.tgz#e2645b846b90e96c6e6f347fb5b2e41f1590b09b"
integrity sha512-NmLNjm6ucYwtcUmL7JQC1ZQ57LmHP4lT15FQ8D61nak1rO6DH+fz5qNK2Ap5UN4ZapYICE3/0KodcLYSPsPbaA==

Loading…
Cancel
Save