From 068675716086fe8cd24922dddc6c7d2db5c07a95 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 4 Oct 2022 16:19:56 +0800 Subject: [PATCH] [Docker Monitor] Change `tcp://` to `http://` --- server/docker.js | 14 +++++++++++++- server/model/monitor.js | 3 ++- server/socket-handlers/docker-socket-handler.js | 2 +- src/components/DockerHostDialog.vue | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/server/docker.js b/server/docker.js index 177fa6cb6..ff2315027 100644 --- a/server/docker.js +++ b/server/docker.js @@ -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 = { diff --git a/server/model/monitor.js b/server/model/monitor.js index f96b4df06..ac8925608 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -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`); diff --git a/server/socket-handlers/docker-socket-handler.js b/server/socket-handlers/docker-socket-handler.js index 5a53494db..542f18cef 100644 --- a/server/socket-handlers/docker-socket-handler.js +++ b/server/socket-handlers/docker-socket-handler.js @@ -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?"; diff --git a/src/components/DockerHostDialog.vue b/src/components/DockerHostDialog.vue index 92a8ce455..50ffa49c1 100644 --- a/src/components/DockerHostDialog.vue +++ b/src/components/DockerHostDialog.vue @@ -30,7 +30,8 @@ {{ $t("Examples") }}: