Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display certificate Info (#1) #91

Merged
merged 15 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions db/patch2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN TRANSACTION;

CREATE TABLE monitor_tls_info (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
monitor_id INTEGER NOT NULL,
info_json TEXT
);

COMMIT;
2 changes: 1 addition & 1 deletion server/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Database {

static templatePath = "./db/kuma.db"
static path = './data/kuma.db';
static latestVersion = 1;
static latestVersion = 2;
static noReject = true;

static async patch() {
Expand Down
44 changes: 36 additions & 8 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ class Monitor extends BeanModel {
})
bean.msg = `${res.status} - ${res.statusText}`
bean.ping = dayjs().valueOf() - startTime;
if (this.url.startsWith("https")) {
Monitor.sendCertInfo(checkCertificate(res), io, this.id, this.user_id);

// Check certificate if https is used
if (this.getUrl().protocol === "https:") {
await this.updateTlsInfo(checkCertificate(res));
}

if (this.type === "http") {
Expand Down Expand Up @@ -168,10 +170,35 @@ class Monitor extends BeanModel {
clearInterval(this.heartbeatInterval)
}

// Helper Method:
// returns URL object for further usage
// returns null if url is invalid
getUrl() {
try {
return new URL(this.url);
} catch (_) {
return null;
}
}

// Store TLS info to database
async updateTlsInfo(checkCertificateResult) {
let tls_info_bean = await R.findOne("monitor_tls_info", "monitor_id = ?", [
this.id
]);
if (tls_info_bean == null) {
tls_info_bean = R.dispense("monitor_tls_info");
tls_info_bean.monitor_id = this.id;
}
tls_info_bean.info_json = JSON.stringify(checkCertificateResult);
R.store(tls_info_bean);
}

static async sendStats(io, monitorID, userID) {
Monitor.sendAvgPing(24, io, monitorID, userID);
Monitor.sendUptime(24, io, monitorID, userID);
Monitor.sendUptime(24 * 30, io, monitorID, userID);
Monitor.sendCertInfo(io, monitorID, userID);
}

/**
Expand All @@ -192,12 +219,13 @@ class Monitor extends BeanModel {
io.to(userID).emit("avgPing", monitorID, avgPing);
}

/**
*
* @param checkCertificateResult : Object return result of checkCertificate
*/
static async sendCertInfo(checkCertificateResult, io, monitorID, userID) {
io.to(userID).emit("certInfo", monitorID, checkCertificateResult);
static async sendCertInfo(io, monitorID, userID) {
let tls_info = await R.findOne("monitor_tls_info", "monitor_id = ?", [
monitorID
]);
if (tls_info != null) {
io.to(userID).emit("certInfo", monitorID, tls_info.info_json);
}
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/mixins/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ export default {
this.$router.push("/setup")
});

socket.on('monitorList', (data) => {
socket.on("monitorList", (data) => {
// Add Helper function
Object.entries(data).forEach(([monitorID, monitor]) => {
monitor.getUrl = () => {
try {
return new URL(monitor.url);
} catch (_) {
return null;
}
};
});
this.monitorList = data;
});

Expand Down Expand Up @@ -116,7 +126,7 @@ export default {
});

socket.on('certInfo', (monitorID, data) => {
this.certInfoList[monitorID] = data
this.certInfoList[monitorID] = JSON.parse(data)
});

socket.on('importantHeartbeatList', (monitorID, data) => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Details.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</div>
</div>

<div class="shadow-box big-padding text-center stats" v-if="monitor.type === 'http' && monitor.url.startsWith('https') && certInfo != null">
<div class="shadow-box big-padding text-center stats" v-if="monitor.type === 'http' && monitor.getUrl().protocol === 'https:' && certInfo != null">
<div class="row">
<div class="col">
<h4>Certificate Info</h4>
Expand Down