Set default ua for axios: Uptime-Kuma/version instead of axios/version

This commit is contained in:
Louis Lam 2023-10-18 21:54:22 +08:00
parent e11aad2d60
commit bbaba29222
3 changed files with 8 additions and 4 deletions

View file

@ -72,7 +72,6 @@ class DockerHost {
url: "/containers/json?all=true", url: "/containers/json?all=true",
headers: { headers: {
"Accept": "*/*", "Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version
}, },
}; };

View file

@ -447,7 +447,6 @@ class Monitor extends BeanModel {
timeout: this.timeout * 1000, timeout: this.timeout * 1000,
headers: { headers: {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"User-Agent": "Uptime-Kuma/" + version,
...(contentType ? { "Content-Type": contentType } : {}), ...(contentType ? { "Content-Type": contentType } : {}),
...(basicAuthHeader), ...(basicAuthHeader),
...(oauth2AuthHeader), ...(oauth2AuthHeader),
@ -627,7 +626,6 @@ class Monitor extends BeanModel {
timeout: this.timeout * 1000, timeout: this.timeout * 1000,
headers: { headers: {
"Accept": "*/*", "Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version,
}, },
httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({ httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)
@ -681,7 +679,6 @@ class Monitor extends BeanModel {
timeout: this.interval * 1000 * 0.8, timeout: this.interval * 1000 * 0.8,
headers: { headers: {
"Accept": "*/*", "Accept": "*/*",
"User-Agent": "Uptime-Kuma/" + version,
}, },
httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({ httpsAgent: CacheableDnsHttpAgent.getHttpsAgent({
maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940) maxCachedSessions: 0, // Use Custom agent to disable session reuse (https://github.com/nodejs/node/issues/3940)

View file

@ -12,6 +12,7 @@ const { Settings } = require("./settings");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const childProcess = require("child_process"); const childProcess = require("child_process");
const path = require("path"); const path = require("path");
const axios = require("axios");
// DO NOT IMPORT HERE IF THE MODULES USED `UptimeKumaServer.getInstance()`, put at the bottom of this file instead. // DO NOT IMPORT HERE IF THE MODULES USED `UptimeKumaServer.getInstance()`, put at the bottom of this file instead.
/** /**
@ -83,6 +84,9 @@ class UptimeKumaServer {
const sslCert = args["ssl-cert"] || process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || undefined; const sslCert = args["ssl-cert"] || process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || undefined;
const sslKeyPassphrase = args["ssl-key-passphrase"] || process.env.UPTIME_KUMA_SSL_KEY_PASSPHRASE || process.env.SSL_KEY_PASSPHRASE || undefined; const sslKeyPassphrase = args["ssl-key-passphrase"] || process.env.UPTIME_KUMA_SSL_KEY_PASSPHRASE || process.env.SSL_KEY_PASSPHRASE || undefined;
// Set axios default user-agent to Uptime-Kuma/version
axios.defaults.headers.common["User-Agent"] = this.getUserAgent();
log.debug("server", "Creating express and socket.io instance"); log.debug("server", "Creating express and socket.io instance");
this.app = express(); this.app = express();
if (sslKey && sslCert) { if (sslKey && sslCert) {
@ -411,6 +415,10 @@ class UptimeKumaServer {
} }
} }
} }
getUserAgent() {
return "Uptime-Kuma/" + require("../package.json").version
}
} }
module.exports = { module.exports = {