From c79be19ec348129b6734aaf4ee2131880c2e01e4 Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Mon, 23 Aug 2021 00:05:48 +0200 Subject: [PATCH 01/12] Added DNS Monitor Type --- db/patch7.sql | 80 +++++++++++++++++++++++++++++++++++++++ server/database.js | 2 +- server/model/monitor.js | 42 +++++++++++++++++++- server/server.js | 2 + server/util-server.js | 25 ++++++++++++ src/pages/EditMonitor.vue | 55 +++++++++++++++++++++++++++ 6 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 db/patch7.sql diff --git a/db/patch7.sql b/db/patch7.sql new file mode 100644 index 000000000..9e4ab13fa --- /dev/null +++ b/db/patch7.sql @@ -0,0 +1,80 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +PRAGMA foreign_keys = off; + +BEGIN TRANSACTION; + +create table monitor_dg_tmp ( + id INTEGER not null primary key autoincrement, + name VARCHAR(150), + active BOOLEAN default 1 not null, + user_id INTEGER references user on update cascade on delete + set + null, + interval INTEGER default 20 not null, + url TEXT, + type VARCHAR(20), + weight INTEGER default 2000, + hostname VARCHAR(255), + port INTEGER, + created_date DATETIME default (DATETIME('now')) not null, + keyword VARCHAR(255), + maxretries INTEGER NOT NULL DEFAULT 0, + ignore_tls BOOLEAN default 0 not null, + upside_down BOOLEAN default 0 not null, + maxredirects INTEGER default 10 not null, + accepted_statuscodes_json TEXT default '["200-299"]' not null, + dns_resolve_type VARCHAR(5), + dns_resolve_server VARCHAR(255) +); + +insert into + monitor_dg_tmp( + id, + name, + active, + user_id, + interval, + url, + type, + weight, + hostname, + port, + created_date, + keyword, + maxretries, + ignore_tls, + upside_down, + maxredirects, + accepted_statuscodes_json + ) +select + id, + name, + active, + user_id, + interval, + url, + type, + weight, + hostname, + port, + created_date, + keyword, + maxretries, + ignore_tls, + upside_down, + maxredirects, + accepted_statuscodes_json +from + monitor; + +drop table monitor; + +alter table + monitor_dg_tmp rename to monitor; + +create index user_id on monitor (user_id); + +COMMIT; + +PRAGMA foreign_keys = on; diff --git a/server/database.js b/server/database.js index ff0304f1f..802e24197 100644 --- a/server/database.js +++ b/server/database.js @@ -9,7 +9,7 @@ class Database { static templatePath = "./db/kuma.db" static path = "./data/kuma.db"; - static latestVersion = 6; + static latestVersion = 7; static noReject = true; static sqliteInstance = null; diff --git a/server/model/monitor.js b/server/model/monitor.js index 2b4839824..9420312f1 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -7,7 +7,7 @@ dayjs.extend(timezone) const axios = require("axios"); const { Prometheus } = require("../prometheus"); const { debug, UP, DOWN, PENDING, flipStatus, TimeLogger } = require("../../src/util"); -const { tcping, ping, checkCertificate, checkStatusCode } = require("../util-server"); +const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode } = require("../util-server"); const { R } = require("redbean-node"); const { BeanModel } = require("redbean-node/dist/bean-model"); const { Notification } = require("../notification") @@ -48,6 +48,8 @@ class Monitor extends BeanModel { upsideDown: this.isUpsideDown(), maxredirects: this.maxredirects, accepted_statuscodes: this.getAcceptedStatuscodes(), + dns_resolve_type: this.dns_resolve_type, + dns_resolve_server: this.dns_resolve_server, notificationIDList, }; } @@ -175,6 +177,44 @@ class Monitor extends BeanModel { bean.ping = await ping(this.hostname); bean.msg = "" bean.status = UP; + } else if (this.type === "dns") { + let startTime = dayjs().valueOf(); + + var dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); + + var dnsMessage = ""; + + if (this.dns_resolve_type == 'A' || this.dns_resolve_type == 'AAAA' || this.dns_resolve_type == 'CNAME' || this.dns_resolve_type == 'PTR') { + var dnsMessage = dnsRes[0]; + } else if (this.dns_resolve_type == 'CAA') { + var dnsMessage = dnsRes[0].issue; + } else if (this.dns_resolve_type == 'MX') { + dnsRes.forEach(record => { + dnsMessage += `Server: ${record.exchange} - Priority: ${record.priority} | `; + }); + var dnsMessage = dnsMessage.slice(0, -2) + } else if (this.dns_resolve_type == 'NS') { + dnsRes.forEach(record => { + dnsMessage += `Server: ${record} | `; + }); + var dnsMessage = dnsMessage.slice(0, -2) + } else if (this.dns_resolve_type == 'SOA') { + dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; + } else if (this.dns_resolve_type == 'SRV') { + dnsRes.forEach(record => { + dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; + }); + var dnsMessage = dnsMessage.slice(0, -2) + } else if (this.dns_resolve_type == 'TXT') { + dnsRes.forEach(record => { + dnsMessage += `Record: ${record} | `; + }); + var dnsMessage = dnsMessage.slice(0, -2) + } + + bean.msg = dnsMessage; + bean.ping = dayjs().valueOf() - startTime; + bean.status = UP; } if (this.isUpsideDown()) { diff --git a/server/server.js b/server/server.js index 7c4441187..6fd15f73e 100644 --- a/server/server.js +++ b/server/server.js @@ -293,6 +293,8 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); bean.upsideDown = monitor.upsideDown; bean.maxredirects = monitor.maxredirects; bean.accepted_statuscodes_json = JSON.stringify(monitor.accepted_statuscodes); + bean.dns_resolve_type = monitor.dns_resolve_type; + bean.dns_resolve_server = monitor.dns_resolve_server; await R.store(bean) diff --git a/server/util-server.js b/server/util-server.js index 8a2f03879..2628a4fe5 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -4,6 +4,7 @@ const { R } = require("redbean-node"); const { debug } = require("../src/util"); const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); +const { Resolver } = require('dns'); /** * Init or reset JWT secret @@ -76,6 +77,30 @@ exports.pingAsync = function (hostname, ipv6 = false) { }); } +exports.dnsResolve = function (hostname, resolver_server, rrtype) { + const resolver = new Resolver(); + resolver.setServers([resolver_server]); + return new Promise((resolve, reject) => { + if (rrtype == 'PTR') { + resolver.reverse(hostname, (err, records) => { + if (err) { + reject(err); + } else { + resolve(records); + } + }); + } else { + resolver.resolve(hostname, rrtype, (err, records) => { + if (err) { + reject(err); + } else { + resolve(records); + } + }); + } + }) +} + exports.setting = async function (key) { let value = await R.getCell("SELECT `value` FROM setting WHERE `key` = ? ", [ key, diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 749921ea1..f55b8d4a6 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -23,6 +23,9 @@ + @@ -54,6 +57,41 @@ +
+ + +
+ +
+ + +
+ Cloudflare is the default server, you can change the resolver server anytime. +
+
+ +
+ + + + +
+ Select the RR-Type you want to monitor +
+
+
@@ -170,6 +208,8 @@ export default { notificationIDList: {}, }, acceptedStatusCodeOptions: [], + dnsresolvetypeOptions: [], + ipRegex: "((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))", } }, @@ -200,11 +240,25 @@ export default { "500-599", ]; + let dnsresolvetypeOptions = [ + "A", + "AAAA", + "CAA", + "CNAME", + "MX", + "NS", + "PTR", + "SOA", + "SRV", + "TXT", + ]; + for (let i = 100; i <= 999; i++) { acceptedStatusCodeOptions.push(i.toString()); } this.acceptedStatusCodeOptions = acceptedStatusCodeOptions; + this.dnsresolvetypeOptions = dnsresolvetypeOptions; }, methods: { init() { @@ -221,6 +275,7 @@ export default { upsideDown: false, maxredirects: 10, accepted_statuscodes: ["200-299"], + dns_resolve_server: "1.1.1.1", } } else if (this.isEdit) { this.$root.getSocket().emit("getMonitor", this.$route.params.id, (res) => { From e5981b10ce6c84485efcf07912bbda33756b9d7b Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Mon, 23 Aug 2021 00:05:48 +0200 Subject: [PATCH 02/12] Replaced var with let and removed re-declaration --- server/model/monitor.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 9420312f1..4fa8e2979 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -179,41 +179,40 @@ class Monitor extends BeanModel { bean.status = UP; } else if (this.type === "dns") { let startTime = dayjs().valueOf(); + let dnsMessage = ""; - var dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); - - var dnsMessage = ""; + let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); + bean.ping = dayjs().valueOf() - startTime; if (this.dns_resolve_type == 'A' || this.dns_resolve_type == 'AAAA' || this.dns_resolve_type == 'CNAME' || this.dns_resolve_type == 'PTR') { - var dnsMessage = dnsRes[0]; + dnsMessage = dnsRes[0]; } else if (this.dns_resolve_type == 'CAA') { - var dnsMessage = dnsRes[0].issue; + dnsMessage = dnsRes[0].issue; } else if (this.dns_resolve_type == 'MX') { dnsRes.forEach(record => { dnsMessage += `Server: ${record.exchange} - Priority: ${record.priority} | `; }); - var dnsMessage = dnsMessage.slice(0, -2) + dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == 'NS') { dnsRes.forEach(record => { dnsMessage += `Server: ${record} | `; }); - var dnsMessage = dnsMessage.slice(0, -2) + dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == 'SOA') { dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; } else if (this.dns_resolve_type == 'SRV') { dnsRes.forEach(record => { dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; }); - var dnsMessage = dnsMessage.slice(0, -2) + dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == 'TXT') { dnsRes.forEach(record => { dnsMessage += `Record: ${record} | `; }); - var dnsMessage = dnsMessage.slice(0, -2) + dnsMessage = dnsMessage.slice(0, -2) } bean.msg = dnsMessage; - bean.ping = dayjs().valueOf() - startTime; bean.status = UP; } From b2041cb36b5824eac89eacf1763591264c1f732e Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Mon, 23 Aug 2021 16:30:11 +0200 Subject: [PATCH 03/12] Fixed ESLint Errors --- server/model/monitor.js | 14 +++++++------- server/util-server.js | 4 ++-- src/pages/EditMonitor.vue | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 4fa8e2979..bdfa59555 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -184,28 +184,28 @@ class Monitor extends BeanModel { let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); bean.ping = dayjs().valueOf() - startTime; - if (this.dns_resolve_type == 'A' || this.dns_resolve_type == 'AAAA' || this.dns_resolve_type == 'CNAME' || this.dns_resolve_type == 'PTR') { + if (this.dns_resolve_type == "A" || this.dns_resolve_type == "AAAA" || this.dns_resolve_type == "CNAME" || this.dns_resolve_type == "PTR") { dnsMessage = dnsRes[0]; - } else if (this.dns_resolve_type == 'CAA') { + } else if (this.dns_resolve_type == "CAA") { dnsMessage = dnsRes[0].issue; - } else if (this.dns_resolve_type == 'MX') { + } else if (this.dns_resolve_type == "MX") { dnsRes.forEach(record => { dnsMessage += `Server: ${record.exchange} - Priority: ${record.priority} | `; }); dnsMessage = dnsMessage.slice(0, -2) - } else if (this.dns_resolve_type == 'NS') { + } else if (this.dns_resolve_type == "NS") { dnsRes.forEach(record => { dnsMessage += `Server: ${record} | `; }); dnsMessage = dnsMessage.slice(0, -2) - } else if (this.dns_resolve_type == 'SOA') { + } else if (this.dns_resolve_type == "SOA") { dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; - } else if (this.dns_resolve_type == 'SRV') { + } else if (this.dns_resolve_type == "SRV") { dnsRes.forEach(record => { dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; }); dnsMessage = dnsMessage.slice(0, -2) - } else if (this.dns_resolve_type == 'TXT') { + } else if (this.dns_resolve_type == "TXT") { dnsRes.forEach(record => { dnsMessage += `Record: ${record} | `; }); diff --git a/server/util-server.js b/server/util-server.js index 2628a4fe5..a30bcfec0 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -4,7 +4,7 @@ const { R } = require("redbean-node"); const { debug } = require("../src/util"); const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); -const { Resolver } = require('dns'); +const { Resolver } = require("dns"); /** * Init or reset JWT secret @@ -81,7 +81,7 @@ exports.dnsResolve = function (hostname, resolver_server, rrtype) { const resolver = new Resolver(); resolver.setServers([resolver_server]); return new Promise((resolve, reject) => { - if (rrtype == 'PTR') { + if (rrtype == "PTR") { resolver.reverse(hostname, (err, records) => { if (err) { reject(err); diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index f55b8d4a6..17754c694 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -64,7 +64,7 @@
- +
Cloudflare is the default server, you can change the resolver server anytime.
From 054186370e210b7d3c0f77bdf0233c639c7453c4 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 24 Aug 2021 15:00:30 +0800 Subject: [PATCH 04/12] add source of ipRegex --- src/pages/EditMonitor.vue | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 17754c694..6b75805cd 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -209,6 +209,8 @@ export default { }, acceptedStatusCodeOptions: [], dnsresolvetypeOptions: [], + + // Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ ipRegex: "((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4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\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))", } }, From 44d9fa63f06ec97f72bc258404a48a87c651954c Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Tue, 24 Aug 2021 11:47:12 +0200 Subject: [PATCH 05/12] Adjusted the output for A and AAAA records --- server/model/monitor.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index bdfa59555..40abf1f1d 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -184,18 +184,25 @@ class Monitor extends BeanModel { let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); bean.ping = dayjs().valueOf() - startTime; - if (this.dns_resolve_type == "A" || this.dns_resolve_type == "AAAA" || this.dns_resolve_type == "CNAME" || this.dns_resolve_type == "PTR") { + if (this.dns_resolve_type == "A" || this.dns_resolve_type == "AAAA") { + dnsMessage += "Records: "; + dnsRes.forEach(record => { + dnsMessage += `${record} | `; + }); + dnsMessage = dnsMessage.slice(0, -2) + } else if (this.dns_resolve_type == "CNAME" || this.dns_resolve_type == "PTR") { dnsMessage = dnsRes[0]; } else if (this.dns_resolve_type == "CAA") { dnsMessage = dnsRes[0].issue; } else if (this.dns_resolve_type == "MX") { dnsRes.forEach(record => { - dnsMessage += `Server: ${record.exchange} - Priority: ${record.priority} | `; + dnsMessage += `Hostname: ${record.exchange} - Priority: ${record.priority} | `; }); dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == "NS") { + dnsMessage += "Servers: "; dnsRes.forEach(record => { - dnsMessage += `Server: ${record} | `; + dnsMessage += `${record} | `; }); dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == "SOA") { From d313a06d5c21f90376a88280324e74de57d10bb6 Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Wed, 25 Aug 2021 09:31:42 +0200 Subject: [PATCH 06/12] Optimizations for output handling --- server/model/monitor.js | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 40abf1f1d..69fc2c917 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -184,12 +184,9 @@ class Monitor extends BeanModel { let dnsRes = await dnsResolve(this.hostname, this.dns_resolve_server, this.dns_resolve_type); bean.ping = dayjs().valueOf() - startTime; - if (this.dns_resolve_type == "A" || this.dns_resolve_type == "AAAA") { + if (this.dns_resolve_type == "A" || this.dns_resolve_type == "AAAA" || this.dns_resolve_type == "TXT") { dnsMessage += "Records: "; - dnsRes.forEach(record => { - dnsMessage += `${record} | `; - }); - dnsMessage = dnsMessage.slice(0, -2) + dnsMessage += dnsRes.join(" | "); } else if (this.dns_resolve_type == "CNAME" || this.dns_resolve_type == "PTR") { dnsMessage = dnsRes[0]; } else if (this.dns_resolve_type == "CAA") { @@ -201,10 +198,7 @@ class Monitor extends BeanModel { dnsMessage = dnsMessage.slice(0, -2) } else if (this.dns_resolve_type == "NS") { dnsMessage += "Servers: "; - dnsRes.forEach(record => { - dnsMessage += `${record} | `; - }); - dnsMessage = dnsMessage.slice(0, -2) + dnsMessage += dnsRes.join(" | "); } else if (this.dns_resolve_type == "SOA") { dnsMessage += `NS-Name: ${dnsRes.nsname} | Hostmaster: ${dnsRes.hostmaster} | Serial: ${dnsRes.serial} | Refresh: ${dnsRes.refresh} | Retry: ${dnsRes.retry} | Expire: ${dnsRes.expire} | MinTTL: ${dnsRes.minttl}`; } else if (this.dns_resolve_type == "SRV") { @@ -212,11 +206,6 @@ class Monitor extends BeanModel { dnsMessage += `Name: ${record.name} | Port: ${record.port} | Priority: ${record.priority} | Weight: ${record.weight} | `; }); dnsMessage = dnsMessage.slice(0, -2) - } else if (this.dns_resolve_type == "TXT") { - dnsRes.forEach(record => { - dnsMessage += `Record: ${record} | `; - }); - dnsMessage = dnsMessage.slice(0, -2) } bean.msg = dnsMessage; From 02a12e68b81ce51e8e67c2d3fdbf1733a420bb81 Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Wed, 25 Aug 2021 12:45:47 +0200 Subject: [PATCH 07/12] Used ALTER TABLE instead of rebuilding the table --- db/patch7.sql | 78 +++------------------------------------------------ 1 file changed, 4 insertions(+), 74 deletions(-) diff --git a/db/patch7.sql b/db/patch7.sql index 9e4ab13fa..2e8eba15c 100644 --- a/db/patch7.sql +++ b/db/patch7.sql @@ -1,80 +1,10 @@ -- You should not modify if this have pushed to Github, unless it does serious wrong with the db. -PRAGMA foreign_keys = off; - BEGIN TRANSACTION; -create table monitor_dg_tmp ( - id INTEGER not null primary key autoincrement, - name VARCHAR(150), - active BOOLEAN default 1 not null, - user_id INTEGER references user on update cascade on delete - set - null, - interval INTEGER default 20 not null, - url TEXT, - type VARCHAR(20), - weight INTEGER default 2000, - hostname VARCHAR(255), - port INTEGER, - created_date DATETIME default (DATETIME('now')) not null, - keyword VARCHAR(255), - maxretries INTEGER NOT NULL DEFAULT 0, - ignore_tls BOOLEAN default 0 not null, - upside_down BOOLEAN default 0 not null, - maxredirects INTEGER default 10 not null, - accepted_statuscodes_json TEXT default '["200-299"]' not null, - dns_resolve_type VARCHAR(5), - dns_resolve_server VARCHAR(255) -); - -insert into - monitor_dg_tmp( - id, - name, - active, - user_id, - interval, - url, - type, - weight, - hostname, - port, - created_date, - keyword, - maxretries, - ignore_tls, - upside_down, - maxredirects, - accepted_statuscodes_json - ) -select - id, - name, - active, - user_id, - interval, - url, - type, - weight, - hostname, - port, - created_date, - keyword, - maxretries, - ignore_tls, - upside_down, - maxredirects, - accepted_statuscodes_json -from - monitor; - -drop table monitor; +ALTER TABLE monitor + ADD dns_resolve_type VARCHAR(5); -alter table - monitor_dg_tmp rename to monitor; - -create index user_id on monitor (user_id); +ALTER TABLE monitor + ADD dns_resolve_server VARCHAR(255); COMMIT; - -PRAGMA foreign_keys = on; From 8318c2e8ffb375573cd9b36ac495806dacdd079d Mon Sep 17 00:00:00 2001 From: LouisLam Date: Thu, 26 Aug 2021 01:50:27 +0800 Subject: [PATCH 08/12] add a simple dns server for testing, and disable ipRegex for dev only (need to input port) --- extra/simple-dns-server.js | 58 ++++++++++++++++++++++++++++++++++++++ package-lock.json | 13 +++++++++ package.json | 4 ++- src/pages/EditMonitor.vue | 15 ++++++++-- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 extra/simple-dns-server.js diff --git a/extra/simple-dns-server.js b/extra/simple-dns-server.js new file mode 100644 index 000000000..bf50aed03 --- /dev/null +++ b/extra/simple-dns-server.js @@ -0,0 +1,58 @@ +/* + * Simple DNS Server + * For testing DNS monitoring type, dev only + */ +const dns2 = require("dns2"); + +const { Packet } = dns2; + +const server = dns2.createServer({ + udp: true +}); + +server.on("request", (request, send, rinfo) => { + for (let question of request.questions) { + console.log(question.name, type(question.type), question.class); + + const response = Packet.createResponseFromRequest(request); + + if (question.name === "existing.com") { + + if (question.type === Packet.TYPE.A) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + address: "1.2.3.4" + }); + } + + // TODO: all other types + + } + + send(response); + } +}); + +server.on("listening", () => { + console.log("Listening"); + console.log(server.addresses()); +}); + +server.on("close", () => { + console.log("server closed"); +}); + +server.listen({ + udp: 5300 +}); + +function type(code) { + for (let name in Packet.TYPE) { + if (Packet.TYPE[name] === code) { + return name; + } + } +} diff --git a/package-lock.json b/package-lock.json index cc1feebc5..61bab41eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,6 +53,7 @@ "@vitejs/plugin-vue": "^1.4.0", "@vue/compiler-sfc": "^3.2.2", "core-js": "^3.16.1", + "dns2": "^2.0.1", "eslint": "^7.32.0", "eslint-plugin-vue": "^7.16.0", "sass": "^1.37.5", @@ -2457,6 +2458,12 @@ "node": ">=8" } }, + "node_modules/dns2": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/dns2/-/dns2-2.0.1.tgz", + "integrity": "sha512-jHRTCcS2h/MEQjhcCnOWGENtz5A4RrLoK1YFqlHCejGfK5zYu99C8cxVwTsIY7JUqolhDN8zuGlyqnbEe6azqg==", + "dev": true + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -9441,6 +9448,12 @@ "path-type": "^4.0.0" } }, + "dns2": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/dns2/-/dns2-2.0.1.tgz", + "integrity": "sha512-jHRTCcS2h/MEQjhcCnOWGENtz5A4RrLoK1YFqlHCejGfK5zYu99C8cxVwTsIY7JUqolhDN8zuGlyqnbEe6azqg==", + "dev": true + }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", diff --git a/package.json b/package.json index 2d20c4094..24e67373f 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,8 @@ "test-install-script-alpine3": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/alpine3.dockerfile .", "test-install-script-ubuntu": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu.dockerfile .", "test-install-script-ubuntu1604": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu1604.dockerfile .", - "test-install-script-debian": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/debian.dockerfile ." + "test-install-script-debian": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/debian.dockerfile .", + "simple-dns-server": "node extra/simple-dns-server.js" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.36", @@ -79,6 +80,7 @@ "@vitejs/plugin-vue": "^1.4.0", "@vue/compiler-sfc": "^3.2.2", "core-js": "^3.16.1", + "dns2": "^2.0.1", "eslint": "^7.32.0", "eslint-plugin-vue": "^7.16.0", "sass": "^1.37.5", diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index f597f0742..4c0675252 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -193,6 +193,7 @@ import NotificationDialog from "../components/NotificationDialog.vue"; import { useToast } from "vue-toastification" import VueMultiselect from "vue-multiselect" +import { isDev } from "../util.ts"; const toast = useToast() export default { @@ -209,13 +210,23 @@ export default { }, acceptedStatusCodeOptions: [], dnsresolvetypeOptions: [], - + // Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ - ipRegex: "((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A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1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))", } }, computed: { + + ipRegex() { + + // Allow to test with simple dns server with port (127.0.0.1:5300) + if (! isDev) { + return this.ipRegexPattern; + } + return null; + }, + pageName() { return this.$t((this.isAdd) ? "Add New Monitor" : "Edit"); }, From 508586fcfdbab19a8ae87f63e59a25faa3289bee Mon Sep 17 00:00:00 2001 From: LouisLam Date: Thu, 26 Aug 2021 20:36:58 +0800 Subject: [PATCH 09/12] add codesandbox config for demo --- package.json | 3 ++- sandbox.config.json | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 sandbox.config.json diff --git a/package.json b/package.json index 24e67373f..b4075b474 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "test-install-script-ubuntu": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu.dockerfile .", "test-install-script-ubuntu1604": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu1604.dockerfile .", "test-install-script-debian": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/debian.dockerfile .", - "simple-dns-server": "node extra/simple-dns-server.js" + "simple-dns-server": "node extra/simple-dns-server.js", + "codesandbox": "npm run build && npm run start-server" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.36", diff --git a/sandbox.config.json b/sandbox.config.json new file mode 100644 index 000000000..d51303a5d --- /dev/null +++ b/sandbox.config.json @@ -0,0 +1,10 @@ +{ + "infiniteLoopProtection": true, + "hardReloadOnChange": false, + "view": "browser", + "container": { + "node": "14", + "port": 3001, + "startScript": "codesandbox" + } +} From 177af2d58807fa922cdc10d80f970f4963f465af Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Fri, 27 Aug 2021 17:32:50 +0200 Subject: [PATCH 10/12] Added more dns types to simple-dns-server --- extra/simple-dns-server.js | 87 +++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/extra/simple-dns-server.js b/extra/simple-dns-server.js index bf50aed03..e84e57426 100644 --- a/extra/simple-dns-server.js +++ b/extra/simple-dns-server.js @@ -26,10 +26,95 @@ server.on("request", (request, send, rinfo) => { ttl: 300, address: "1.2.3.4" }); + } if (question.type === Packet.TYPE.AAAA) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + address: "fe80::::1234:5678:abcd:ef00", + }); + } else if (question.type === Packet.TYPE.CNAME) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + domain: "cname1.existing.com", + }); + } else if (question.type === Packet.TYPE.MX) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + exchange: "mx1.existing.com", + priority: 5 + }); + } else if (question.type === Packet.TYPE.NS) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + ns: "ns1.existing.com", + }); + } else if (question.type === Packet.TYPE.PTR) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + domain: "ptr1.existing.com", + }); + } else if (question.type === Packet.TYPE.SOA) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + primary: "existing.com", + admin: "admin@existing.com", + serial: 2021082701, + refresh: 300, + retry: 3, + expiration: 10, + minimum: 10, + }); + } else if (question.type === Packet.TYPE.SRV) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + priority: 5, + weight: 5, + port: 8080, + target: "srv1.existing.com", + }); + } else if (question.type === Packet.TYPE.TXT) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + data: "#v=spf1 include:_spf.existing.com ~all", + }); } - // TODO: all other types + // TODO: CAA type + } + if (question.name === "4.3.2.1.in-addr.arpa") { + if (question.type === Packet.TYPE.PTR) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + domain: "ptr1.existing.com", + }); + } } send(response); From acd5cf63fd29a348b77b302228b56dff1e788e33 Mon Sep 17 00:00:00 2001 From: Ponkhy Date: Fri, 27 Aug 2021 17:41:22 +0200 Subject: [PATCH 11/12] Removed wrong PTR answer --- extra/simple-dns-server.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/extra/simple-dns-server.js b/extra/simple-dns-server.js index e84e57426..4be3f68e8 100644 --- a/extra/simple-dns-server.js +++ b/extra/simple-dns-server.js @@ -59,14 +59,6 @@ server.on("request", (request, send, rinfo) => { ttl: 300, ns: "ns1.existing.com", }); - } else if (question.type === Packet.TYPE.PTR) { - response.answers.push({ - name: question.name, - type: question.type, - class: question.class, - ttl: 300, - domain: "ptr1.existing.com", - }); } else if (question.type === Packet.TYPE.SOA) { response.answers.push({ name: question.name, From 03b4086372d1be7dfde9062cd1aa0c1bb0c5dc69 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Fri, 27 Aug 2021 23:57:42 +0800 Subject: [PATCH 12/12] add CAA test and remove some files added by mistake --- extra/simple-dns-server.js | 11 ++++++++++- package.json | 3 +-- sandbox.config.json | 10 ---------- 3 files changed, 11 insertions(+), 13 deletions(-) delete mode 100644 sandbox.config.json diff --git a/extra/simple-dns-server.js b/extra/simple-dns-server.js index 4be3f68e8..5e5745f04 100644 --- a/extra/simple-dns-server.js +++ b/extra/simple-dns-server.js @@ -92,9 +92,18 @@ server.on("request", (request, send, rinfo) => { ttl: 300, data: "#v=spf1 include:_spf.existing.com ~all", }); + } else if (question.type === Packet.TYPE.CAA) { + response.answers.push({ + name: question.name, + type: question.type, + class: question.class, + ttl: 300, + flags: 0, + tag: "issue", + value: "ca.existing.com", + }); } - // TODO: CAA type } if (question.name === "4.3.2.1.in-addr.arpa") { diff --git a/package.json b/package.json index b4075b474..24e67373f 100644 --- a/package.json +++ b/package.json @@ -33,8 +33,7 @@ "test-install-script-ubuntu": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu.dockerfile .", "test-install-script-ubuntu1604": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/ubuntu1604.dockerfile .", "test-install-script-debian": "npm run compile-install-script && docker build --progress plain -f test/test_install_script/debian.dockerfile .", - "simple-dns-server": "node extra/simple-dns-server.js", - "codesandbox": "npm run build && npm run start-server" + "simple-dns-server": "node extra/simple-dns-server.js" }, "dependencies": { "@fortawesome/fontawesome-svg-core": "^1.2.36", diff --git a/sandbox.config.json b/sandbox.config.json deleted file mode 100644 index d51303a5d..000000000 --- a/sandbox.config.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "infiniteLoopProtection": true, - "hardReloadOnChange": false, - "view": "browser", - "container": { - "node": "14", - "port": 3001, - "startScript": "codesandbox" - } -}