@@ -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" ;
1413import { MonitorType , MonitorStatus } from "@/db/models/monitors/Monitor.js" ;
1514import ApiError from "@/utils/ApiError.js" ;
1615import { 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