Add configurable dns cache

pull/2391/head
Louis Lam 1 year ago
parent e6dc0a0293
commit 3b87209e26

@ -1,6 +1,8 @@
const https = require("https"); const https = require("https");
const http = require("http"); const http = require("http");
const CacheableLookup = require("cacheable-lookup"); const CacheableLookup = require("cacheable-lookup");
const { Settings } = require("./settings");
const { log } = require("../src/util");
class CacheableDnsHttpAgent { class CacheableDnsHttpAgent {
@ -9,12 +11,30 @@ class CacheableDnsHttpAgent {
static httpAgentList = {}; static httpAgentList = {};
static httpsAgentList = {}; static httpsAgentList = {};
static enable = false;
/** /**
* Register cacheable to global agents * Register/Disable cacheable to global agents
*/ */
static registerGlobalAgent() { static async update() {
this.cacheable.install(http.globalAgent); log.debug("CacheableDnsHttpAgent", "update");
this.cacheable.install(https.globalAgent); 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;
} }
static install(agent) { static install(agent) {
@ -26,6 +46,10 @@ class CacheableDnsHttpAgent {
* @return {https.Agent} * @return {https.Agent}
*/ */
static getHttpsAgent(agentOptions) { static getHttpsAgent(agentOptions) {
if (!this.enable) {
return new https.Agent(agentOptions);
}
let key = JSON.stringify(agentOptions); let key = JSON.stringify(agentOptions);
if (!(key in this.httpsAgentList)) { if (!(key in this.httpsAgentList)) {
this.httpsAgentList[key] = new https.Agent(agentOptions); this.httpsAgentList[key] = new https.Agent(agentOptions);
@ -39,6 +63,10 @@ class CacheableDnsHttpAgent {
* @return {https.Agents} * @return {https.Agents}
*/ */
static getHttpAgent(agentOptions) { static getHttpAgent(agentOptions) {
if (!this.enable) {
return new http.Agent(agentOptions);
}
let key = JSON.stringify(agentOptions); let key = JSON.stringify(agentOptions);
if (!(key in this.httpAgentList)) { if (!(key in this.httpAgentList)) {
this.httpAgentList[key] = new http.Agent(agentOptions); this.httpAgentList[key] = new http.Agent(agentOptions);

@ -136,6 +136,7 @@ const { proxySocketHandler } = require("./socket-handlers/proxy-socket-handler")
const { dockerSocketHandler } = require("./socket-handlers/docker-socket-handler"); const { dockerSocketHandler } = require("./socket-handlers/docker-socket-handler");
const { maintenanceSocketHandler } = require("./socket-handlers/maintenance-socket-handler"); const { maintenanceSocketHandler } = require("./socket-handlers/maintenance-socket-handler");
const { Settings } = require("./settings"); const { Settings } = require("./settings");
const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent");
app.use(express.json()); app.use(express.json());
@ -1114,6 +1115,8 @@ let needSetup = false;
await setSettings("general", data); await setSettings("general", data);
server.entryPage = data.entryPage; server.entryPage = data.entryPage;
await CacheableDnsHttpAgent.update();
// Also need to apply timezone globally // Also need to apply timezone globally
if (data.serverTimezone) { if (data.serverTimezone) {
await server.setTimezone(data.serverTimezone); await server.setTimezone(data.serverTimezone);

@ -83,12 +83,12 @@ class UptimeKumaServer {
} }
} }
CacheableDnsHttpAgent.registerGlobalAgent();
this.io = new Server(this.httpServer); this.io = new Server(this.httpServer);
} }
async initAfterDatabaseReady() { async initAfterDatabaseReady() {
await CacheableDnsHttpAgent.update();
process.env.TZ = await this.getTimezone(); process.env.TZ = await this.getTimezone();
dayjs.tz.setDefault(process.env.TZ); dayjs.tz.setDefault(process.env.TZ);
log.debug("DEBUG", "Timezone: " + process.env.TZ); log.debug("DEBUG", "Timezone: " + process.env.TZ);

@ -150,6 +150,46 @@
</div> </div>
</div> </div>
<!-- Search Engine -->
<div class="mb-4">
<label class="form-label">
{{ $t("Enable DNS Cache") }}
<div class="form-text">
{{ $t("dnsCacheDescription") }}
</div>
</label>
<div class="form-check">
<input
id="dnsCacheEnable"
v-model="settings.dnsCache"
class="form-check-input"
type="radio"
name="flexRadioDefault"
:value="true"
required
/>
<label class="form-check-label" for="dnsCacheEnable">
{{ $t("Enable") }}
</label>
</div>
<div class="form-check">
<input
id="dnsCacheDisable"
v-model="settings.dnsCache"
class="form-check-input"
type="radio"
name="flexRadioDefault"
:value="false"
required
/>
<label class="form-check-label" for="dnsCacheDisable">
{{ $t("Disable") }}
</label>
</div>
</div>
<!-- Save Button --> <!-- Save Button -->
<div> <div>
<button class="btn btn-primary" type="submit"> <button class="btn btn-primary" type="submit">

@ -646,4 +646,8 @@ export default {
"Server Timezone": "Server Timezone", "Server Timezone": "Server Timezone",
statusPageMaintenanceEndDate: "End", statusPageMaintenanceEndDate: "End",
IconUrl: "Icon URL", IconUrl: "Icon URL",
"Enable DNS Cache": "Enable DNS Cache",
"Enable": "Enable",
"Disable": "Disable",
dnsCacheDescription: "It may be not working in some IPv6 environments, disable it if you encounter any issues.",
}; };

@ -382,4 +382,7 @@ export default {
setAsDefaultProxyDescription: "預設情況下,新監測器將啟用此代理伺服器。您仍可分別停用各監測器的代理伺服器。", setAsDefaultProxyDescription: "預設情況下,新監測器將啟用此代理伺服器。您仍可分別停用各監測器的代理伺服器。",
Maintenance: "維護", Maintenance: "維護",
statusMaintenance: "維護中", statusMaintenance: "維護中",
"Enable DNS Cache": "啟用 DNS 快取",
"Enable": "啟用",
"Disable": "停用",
}; };

@ -157,6 +157,10 @@ export default {
this.settings.entryPage = "dashboard"; this.settings.entryPage = "dashboard";
} }
if (this.settings.dnsCache === undefined) {
this.settings.dnsCache = false;
}
if (this.settings.keepDataPeriodDays === undefined) { if (this.settings.keepDataPeriodDays === undefined) {
this.settings.keepDataPeriodDays = 180; this.settings.keepDataPeriodDays = 180;
} }

@ -101,7 +101,7 @@ class Logger {
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized. * @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
*/ */
log(module, msg, level) { log(module, msg, level) {
if (this.hideLog[level] && this.hideLog[level].includes(module)) { if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) {
return; return;
} }
module = module.toUpperCase(); module = module.toUpperCase();

@ -115,7 +115,7 @@ class Logger {
* @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized. * @param level Log level. One of INFO, WARN, ERROR, DEBUG or can be customized.
*/ */
log(module: string, msg: any, level: string) { log(module: string, msg: any, level: string) {
if (this.hideLog[level] && this.hideLog[level].includes(module)) { if (this.hideLog[level] && this.hideLog[level].includes(module.toLowerCase())) {
return; return;
} }

Loading…
Cancel
Save