Skip to content

Commit d8007a9

Browse files
committed
fix peer cert
1 parent df95ee7 commit d8007a9

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

client/src/pages/uptime/UptimeDetails.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ const UptimeDetailsPage = () => {
129129
stats?.lastResponseTime ? `${stats?.lastResponseTime} ms` : "N/A"
130130
}
131131
/>
132-
{/**
133132
<StatBox
134133
title={t("monitors.common.stats.certificate")}
135134
subtitle={
@@ -138,7 +137,6 @@ const UptimeDetailsPage = () => {
138137
: "N/A"
139138
}
140139
/>
141-
*/}
142140
</Stack>
143141
<HeaderRange loading={loading} range={range} setRange={setRange} />
144142
<Stack direction={isSmall ? "column" : "row"} gap={theme.spacing(8)}>

server/src/services/infrastructure/NetworkService.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import type {
99
ICaptureInfo,
1010
ILighthouseResult,
1111
} from "@/db/models/index.js";
12-
import type { TLSSocket, DetailedPeerCertificate } from "node:tls";
13-
import type { ClientRequest } from "node:http";
12+
import type { TLSSocket } from "node:tls";
1413
import { MonitorType, MonitorStatus } from "@/db/models/monitors/Monitor.js";
1514
import ApiError from "@/utils/ApiError.js";
1615
import { config } from "@/config/index.js";
@@ -69,15 +68,28 @@ class NetworkService implements INetworkService {
6968
});
7069
}
7170

71+
private getCertificateExpiryFromSocket(
72+
socket?: TLSSocket | null
73+
): Date | null {
74+
if (!socket || typeof socket.getPeerCertificate !== "function") {
75+
return null;
76+
}
77+
try {
78+
const cert = socket.getPeerCertificate(true);
79+
if (!cert?.valid_to) return null;
80+
const parsed = new Date(cert.valid_to);
81+
return Number.isNaN(parsed.getTime()) ? null : parsed;
82+
} catch {
83+
return null;
84+
}
85+
}
86+
7287
private buildStatusResponse = <T>(
7388
monitor: IMonitor,
7489
response: Response<T> | null,
7590
certificateExpiryOrError?: Date | null | any,
7691
maybeError?: any | null
7792
): StatusResponse<T> => {
78-
// Support both call styles:
79-
// - buildStatusResponse(monitor, response, error)
80-
// - buildStatusResponse(monitor, response, certificateExpiry, error)
8193
let certificateExpiry: Date | null = null;
8294
let error: any | null = null;
8395
if (
@@ -137,15 +149,21 @@ class NetworkService implements INetworkService {
137149
const isHttps = url.startsWith("https://");
138150
const haveRejectFlag =
139151
isHttps && typeof (monitor as any).rejectUnauthorized !== "undefined";
140-
const req: any = haveRejectFlag
152+
const req = haveRejectFlag
141153
? this.client(url, {
142154
https: {
143155
rejectUnauthorized: (monitor as any).rejectUnauthorized,
144156
},
145157
})
146-
: this.client(url);
158+
: this.client(url, {});
147159

148160
const response: Response = await req;
161+
162+
// Attempt to get SSL certificate expiry date
163+
certificateExpiry = this.getCertificateExpiryFromSocket(
164+
(response.request?.socket as TLSSocket | undefined) ?? null
165+
);
166+
149167
return this.buildStatusResponse(
150168
monitor,
151169
response,

0 commit comments

Comments
 (0)