2022-07-18 14:33:35 +00:00
|
|
|
const https = require("https");
|
|
|
|
const http = require("http");
|
|
|
|
const CacheableLookup = require("cacheable-lookup");
|
2022-12-12 09:19:22 +00:00
|
|
|
const { Settings } = require("./settings");
|
|
|
|
const { log } = require("../src/util");
|
2022-07-18 14:33:35 +00:00
|
|
|
|
|
|
|
class CacheableDnsHttpAgent {
|
|
|
|
|
|
|
|
static cacheable = new CacheableLookup();
|
|
|
|
|
|
|
|
static httpAgentList = {};
|
|
|
|
static httpsAgentList = {};
|
|
|
|
|
2022-12-12 09:19:22 +00:00
|
|
|
static enable = false;
|
|
|
|
|
2022-07-18 14:33:35 +00:00
|
|
|
/**
|
2022-12-12 09:19:22 +00:00
|
|
|
* Register/Disable cacheable to global agents
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {void}
|
2022-07-18 14:33:35 +00:00
|
|
|
*/
|
2022-12-12 09:19:22 +00:00
|
|
|
static async update() {
|
|
|
|
log.debug("CacheableDnsHttpAgent", "update");
|
|
|
|
let isEnable = await Settings.get("dnsCache");
|
|
|
|
|
|
|
|
if (isEnable !== this.enable) {
|
|
|
|
log.debug("CacheableDnsHttpAgent", "value changed");
|
|
|
|
|
|
|
|
if (isEnable) {
|
|
|
|
log.debug("CacheableDnsHttpAgent", "enable");
|
|
|
|
this.cacheable.install(http.globalAgent);
|
|
|
|
this.cacheable.install(https.globalAgent);
|
|
|
|
} else {
|
|
|
|
log.debug("CacheableDnsHttpAgent", "disable");
|
|
|
|
this.cacheable.uninstall(http.globalAgent);
|
|
|
|
this.cacheable.uninstall(https.globalAgent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.enable = isEnable;
|
2022-07-18 14:33:35 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 22:19:05 +00:00
|
|
|
/**
|
|
|
|
* Attach cacheable to HTTP agent
|
|
|
|
* @param {http.Agent} agent Agent to install
|
2023-08-11 07:46:41 +00:00
|
|
|
* @returns {void}
|
2023-01-05 22:19:05 +00:00
|
|
|
*/
|
2022-07-18 15:32:45 +00:00
|
|
|
static install(agent) {
|
|
|
|
this.cacheable.install(agent);
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:33:35 +00:00
|
|
|
/**
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {https.AgentOptions} agentOptions Options to pass to HTTPS agent
|
|
|
|
* @returns {https.Agent} The new HTTPS agent
|
2022-07-18 14:33:35 +00:00
|
|
|
*/
|
|
|
|
static getHttpsAgent(agentOptions) {
|
2022-12-12 09:19:22 +00:00
|
|
|
if (!this.enable) {
|
|
|
|
return new https.Agent(agentOptions);
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:33:35 +00:00
|
|
|
let key = JSON.stringify(agentOptions);
|
|
|
|
if (!(key in this.httpsAgentList)) {
|
|
|
|
this.httpsAgentList[key] = new https.Agent(agentOptions);
|
|
|
|
this.cacheable.install(this.httpsAgentList[key]);
|
|
|
|
}
|
|
|
|
return this.httpsAgentList[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-11 07:46:41 +00:00
|
|
|
* @param {http.AgentOptions} agentOptions Options to pass to the HTTP agent
|
|
|
|
* @returns {https.Agents} The new HTTP agent
|
2022-07-18 14:33:35 +00:00
|
|
|
*/
|
|
|
|
static getHttpAgent(agentOptions) {
|
2022-12-12 09:19:22 +00:00
|
|
|
if (!this.enable) {
|
|
|
|
return new http.Agent(agentOptions);
|
|
|
|
}
|
|
|
|
|
2022-07-18 14:33:35 +00:00
|
|
|
let key = JSON.stringify(agentOptions);
|
|
|
|
if (!(key in this.httpAgentList)) {
|
|
|
|
this.httpAgentList[key] = new http.Agent(agentOptions);
|
|
|
|
this.cacheable.install(this.httpAgentList[key]);
|
|
|
|
}
|
|
|
|
return this.httpAgentList[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
CacheableDnsHttpAgent,
|
|
|
|
};
|