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

Introduce new check method ("SSL") #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions src/helpers/check-tls.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { checkTls } from "./check-tls";

test("checkTls", async () => {
const tcpResult = await checkTls({
address: "smtp.gmail.com",
port: 465,
});
expect(tcpResult.results.every(
(result) => Object.prototype.toString.call((result as any).err) === "[object Error]"
)).toBe(false);
expect(tcpResult.avg || -1).toBeGreaterThan(0);
expect(tcpResult.avg || 0).toBeLessThan(60000);
});

test("checkTls2", async () => {
const tcpResult = await checkTls({
address: "wrong.host.badssl.com",
});
expect(tcpResult.results.every(
(result) => Object.prototype.toString.call((result as any).err) === "[object Error]"
)).toBe(true);
expect(tcpResult.avg).toBe(0);
});

test("checkTls3", async () => {
const tcpResult = await checkTls({
address: "expired.host.badssl.com",
});
expect(tcpResult.results.every(
(result) => Object.prototype.toString.call((result as any).err) === "[object Error]"
)).toBe(true);
expect(tcpResult.avg).toBe(0);
});

test("checkTls4", async () => {
const tcpResult = await checkTls({
address: "self-signed.badssl.com",
});
expect(tcpResult.results.every(
(result) => Object.prototype.toString.call((result as any).err) === "[object Error]"
)).toBe(true);
expect(tcpResult.avg).toBe(0);
});
65 changes: 65 additions & 0 deletions src/helpers/check-tls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { connect } from "tls";
import { Options, Results, Result } from "tcp-ping";

export const checkTls = (options: Options) => new Promise<Result>((resolve, reject) => {
let i = 0;
const results: Results[] = [];
const check = () => {
if (i < (options.attempts || 10)) {
doConnect();
} else {
resolve({
address: options.address || "localhost",
port: options.port || 443,
attempts: options.attempts || 10,
avg: results.reduce((acc, curr) => acc + (curr.time || 0), 0) / results.length,
max: results.reduce((acc, curr) => Math.max(acc, curr.time || 0), 0),
min: results.reduce((acc, curr) => Math.min(acc, curr.time || 0), Infinity),
results: results,
})
}
}
const doConnect = () => {
const start = process.hrtime();
const socket = connect(options.port || 443, options.address, {
servername: options.address,
rejectUnauthorized: true,
//checkServerIdentity: () => undefined,
}, () => {
const timeArr = process.hrtime(start);
results.push({
time: (timeArr[0] * 1e9 + timeArr[1]) / 1e6,
seq: i,
});
socket.end();
socket.destroy();
i++;
check();
});

socket.setTimeout(options.timeout || 5000, () => {
results.push({
time: undefined,
seq: i,
err: Error("Request timeout"),
})
socket.end();
socket.destroy();
i++;
check();
})

socket.on("error", (error) => {
results.push({
time: undefined,
seq: i,
err: error,
});
socket.end();
socket.destroy();
i++;
check();
});
}
doConnect();
})
2 changes: 1 addition & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface UpptimeConfig {
repo: string;
"user-agent"?: string;
sites: {
check?: "http" | "tcp-ping" | "ws";
check?: "http" | "tcp-ping" | "ws" | "ssl";
method?: string;
name: string;
url: string;
Expand Down
36 changes: 36 additions & 0 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getOctokit } from "./helpers/github";
import { shouldContinue } from "./helpers/init-check";
import { sendNotification } from "./helpers/notifme";
import { ping } from "./helpers/ping";
import { checkTls } from "./helpers/check-tls";
import { curl } from "./helpers/request";
import { getOwnerRepo, getSecret } from "./helpers/secrets";
import { SiteHistory } from "./interfaces";
Expand Down Expand Up @@ -194,6 +195,41 @@ export const update = async (shouldCommit = false) => {
console.log("ERROR Got pinging error", error);
return { result: { httpCode: 0 }, responseTime: (0).toFixed(0), status: "down" };
}
} else if (site.check === "ssl") {
console.log("Using check-tls instead of curl");
try {
let status: "up" | "down" | "degraded" = "up";
// https://github.com/upptime/upptime/discussions/888
const url = replaceEnvironmentVariables(site.url);
let address = url;
if (isIP(url)) {
if (site.ipv6 && !isIPv6(url))
throw new Error("Site URL must be IPv6 for ipv6 check");
}

const tcpResult = await checkTls({
address,
attempts: 5,
port: Number(replaceEnvironmentVariables(site.port ? String(site.port) : "")),
});
if (
tcpResult.results.every(
(result) => Object.prototype.toString.call((result as any).err) === "[object Error]"
)
)
throw Error("all attempts failed");
console.log("Got result", tcpResult);
let responseTime = (tcpResult.avg || 0).toFixed(0);
if (parseInt(responseTime) > (site.maxResponseTime || 60000)) status = "degraded";
return {
result: { httpCode: 200 },
responseTime,
status,
};
} catch (error) {
console.log("ERROR Got pinging error", error);
return { result: { httpCode: 0 }, responseTime: (0).toFixed(0), status: "down" };
}
} else if (site.check === "ws") {
console.log("Using websocket check instead of curl");
let success = false;
Expand Down