From 9c653c3d05d990a4d80244dd07b86363014ccb31 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Thu, 1 Jul 2021 14:03:06 +0800 Subject: [PATCH] implement tcping --- package-lock.json | 5 +++++ package.json | 1 + server/model/monitor.js | 11 +++++++++-- server/server.js | 2 ++ server/util-server.js | 22 ++++++++++++++++++++++ src/components/CountUp.vue | 2 +- src/pages/Details.vue | 5 ++++- src/pages/EditMonitor.vue | 15 ++++++++++++++- 8 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 server/util-server.js diff --git a/package-lock.json b/package-lock.json index 513303619..b8a7b92a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3208,6 +3208,11 @@ "resolved": "https://registry.npmjs.org/tarn/-/tarn-3.0.1.tgz", "integrity": "sha512-6usSlV9KyHsspvwu2duKH+FMUhqJnAh6J5J/4MITl8s94iSUQTLkJggdiewKv4RyARQccnigV48Z+khiuVZDJw==" }, + "tcp-ping": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/tcp-ping/-/tcp-ping-0.1.1.tgz", + "integrity": "sha1-At1/QrW/fXy3jVt6rO+hVf2PfAw=" + }, "tildify": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/tildify/-/tildify-2.0.0.tgz", diff --git a/package.json b/package.json index 867af2b1b..f37949fb3 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "redbean-node": "0.0.20", "socket.io": "^4.0.2", "socket.io-client": "^4.1.2", + "tcp-ping": "^0.1.1", "vue": "^3.0.5", "vue-confirm-dialog": "^1.0.2", "vue-router": "^4.0.10", diff --git a/server/model/monitor.js b/server/model/monitor.js index a96633598..8346040bc 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1,9 +1,11 @@ + const dayjs = require("dayjs"); const utc = require('dayjs/plugin/utc') var timezone = require('dayjs/plugin/timezone') dayjs.extend(utc) dayjs.extend(timezone) const axios = require("axios"); +const {tcping} = require("../util-server"); const {R} = require("redbean-node"); const {BeanModel} = require("redbean-node/dist/bean-model"); @@ -20,6 +22,8 @@ class Monitor extends BeanModel { id: this.id, name: this.name, url: this.url, + hostname: this.hostname, + port: this.port, weight: this.weight, active: this.active, type: this.type, @@ -59,6 +63,10 @@ class Monitor extends BeanModel { bean.msg = `${res.status} - ${res.statusText}` bean.ping = dayjs().valueOf() - startTime; bean.status = 1; + + } else if (this.type === "port") { + bean.ping = await tcping(this.hostname, this.port); + bean.status = 1; } } catch (error) { @@ -74,9 +82,8 @@ class Monitor extends BeanModel { io.to(this.user_id).emit("heartbeat", bean.toJSON()); - Monitor.sendStats(io, this.id, this.user_id) - await R.store(bean) + Monitor.sendStats(io, this.id, this.user_id) previousBeat = bean; } diff --git a/server/server.js b/server/server.js index 7775e646c..dd053ed25 100644 --- a/server/server.js +++ b/server/server.js @@ -145,6 +145,8 @@ let monitorList = {}; bean.type = monitor.type bean.url = monitor.url bean.interval = monitor.interval + bean.hostname = monitor.hostname; + bean.port = monitor.port; await R.store(bean) diff --git a/server/util-server.js b/server/util-server.js new file mode 100644 index 000000000..8e03284a1 --- /dev/null +++ b/server/util-server.js @@ -0,0 +1,22 @@ +const tcpp = require('tcp-ping'); + +exports.tcping = function (hostname, port) { + return new Promise((resolve, reject) => { + tcpp.ping({ + address: hostname, + port: port, + attempts: 1, + }, function(err, data) { + + if (err) { + reject(err); + } + + if (data.results.length >= 1 && data.results[0].err) { + reject(data.results[0].err); + } + + resolve(Math.round(data.max)); + }); + }); +} diff --git a/src/components/CountUp.vue b/src/components/CountUp.vue index 113c3cbe6..5cfc8d4c6 100644 --- a/src/components/CountUp.vue +++ b/src/components/CountUp.vue @@ -48,7 +48,7 @@ export default { let frames = 12; let step = Math.floor(diff / frames); - if ((diff > 0 && step < 1) || (diff < 0 && step > 1) || diff === 0) { + if (! this.isNum || (diff > 0 && step < 1) || (diff < 0 && step > 1) || diff === 0) { // Lazy to NOT this condition, hahaha. } else { for (let i = 1; i < frames; i++) { diff --git a/src/pages/Details.vue b/src/pages/Details.vue index 7fef349c5..21bdc611a 100644 --- a/src/pages/Details.vue +++ b/src/pages/Details.vue @@ -1,6 +1,9 @@