[Docker Monitor] Change tcp:// to http://

This commit is contained in:
Louis Lam 2022-10-04 16:19:56 +08:00
parent 1c4e97439c
commit 0686757160
4 changed files with 18 additions and 4 deletions

View file

@ -75,7 +75,7 @@ class DockerHost {
if (dockerHost.dockerType === "socket") {
options.socketPath = dockerHost.dockerDaemon;
} else if (dockerHost.dockerType === "tcp") {
options.baseURL = dockerHost.dockerDaemon;
options.baseURL = DockerHost.patchDockerURL(dockerHost.dockerDaemon);
}
let res = await axios.request(options);
@ -99,6 +99,18 @@ class DockerHost {
}
}
/**
* Since axios 0.27.X, it does not accept `tcp://` protocol.
* Change it to `http://` on the fly in order to fix it. (https://github.com/louislam/uptime-kuma/issues/2165)
*/
static patchDockerURL(url) {
if (typeof url === "string") {
// Replace the first occurrence only with g
return url.replace(/tcp:\/\//g, "http://");
}
return url;
}
}
module.exports = {

View file

@ -17,6 +17,7 @@ const version = require("../../package.json").version;
const apicache = require("../modules/apicache");
const { UptimeKumaServer } = require("../uptime-kuma-server");
const { CacheableDnsHttpAgent } = require("../cacheable-dns-http-agent");
const { DockerHost } = require("../docker");
/**
* status:
@ -498,7 +499,7 @@ class Monitor extends BeanModel {
if (dockerHost._dockerType === "socket") {
options.socketPath = dockerHost._dockerDaemon;
} else if (dockerHost._dockerType === "tcp") {
options.baseURL = dockerHost._dockerDaemon;
options.baseURL = DockerHost.patchDockerURL(dockerHost._dockerDaemon);
}
log.debug(`[${this.name}] Axios Request`);

View file

@ -56,7 +56,7 @@ module.exports.dockerSocketHandler = (socket) => {
let amount = await DockerHost.testDockerHost(dockerHost);
let msg;
if (amount > 1) {
if (amount >= 1) {
msg = "Connected Successfully. Amount of containers: " + amount;
} else {
msg = "Connected Successfully, but there are no containers?";

View file

@ -30,7 +30,8 @@
{{ $t("Examples") }}:
<ul>
<li>/var/run/docker.sock</li>
<li>tcp://localhost:2375</li>
<li>http://localhost:2375</li>
<li>https://localhost:2376 (TLS)</li>
</ul>
</div>
</div>