@@ -34,6 +34,7 @@ export interface WebRTCDirectListenerInit {
34
34
rtcConfiguration ?: RTCConfiguration | ( ( ) => RTCConfiguration | Promise < RTCConfiguration > )
35
35
useLibjuice ?: boolean
36
36
certificateDuration ?: number
37
+ certificateExpiryThreshold ?: number
37
38
}
38
39
39
40
export interface WebRTCListenerMetrics {
@@ -86,10 +87,10 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
86
87
87
88
private isCertificateExpiring ( ) : boolean {
88
89
if ( this . certificate == null ) return true
89
- const expiryDate = new Date ( this . certificate . notAfter )
90
+ const expiryDate = this . certificate . notAfter
90
91
const now = new Date ( )
91
- const timeToExpiry = expiryDate . getTime ( ) - now . getTime ( )
92
- const threshold = 30 * 24 * 60 * 60 * 1000
92
+ const timeToExpiry = expiryDate - now . getTime ( )
93
+ const threshold = this . init . certificateExpiryThreshold ?? 7 * 86400000
93
94
return timeToExpiry < threshold
94
95
}
95
96
@@ -166,17 +167,7 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
166
167
// ensure we have a certificate
167
168
if ( this . certificate == null || this . isCertificateExpiring ( ) ) {
168
169
this . log . trace ( 'creating TLS certificate' )
169
- const keyPair = await crypto . subtle . generateKey ( {
170
- name : 'ECDSA' ,
171
- namedCurve : 'P-256'
172
- } , true , [ 'sign' , 'verify' ] )
173
-
174
- const certificate = await generateTransportCertificate ( keyPair , {
175
- days : this . init . certificateDuration ?? 365 * 10
176
- } )
177
- this . safeDispatchEvent ( 'listening' )
178
-
179
- this . certificate = certificate
170
+ await this . createAndSetCertificate ( )
180
171
}
181
172
182
173
if ( port === 0 ) {
@@ -196,6 +187,37 @@ export class WebRTCDirectListener extends TypedEventEmitter<ListenerEvents> impl
196
187
}
197
188
}
198
189
190
+ private async createAndSetCertificate ( ) : Promise < void > {
191
+ const keyPair = await crypto . subtle . generateKey ( {
192
+ name : 'ECDSA' ,
193
+ namedCurve : 'P-256'
194
+ } , true , [ 'sign' , 'verify' ] )
195
+
196
+ const certificate = await generateTransportCertificate ( keyPair , {
197
+ days : this . init . certificateDuration ?? 365 * 10
198
+ } )
199
+
200
+ this . certificate = certificate
201
+ this . setCertificateExpiryTimeout ( )
202
+ }
203
+
204
+ private setCertificateExpiryTimeout ( ) : void {
205
+ if ( this . certificate == null ) {
206
+ return
207
+ }
208
+
209
+ const expiryDate = new Date ( this . certificate . notAfter )
210
+ const now = new Date ( )
211
+ const timeToExpiry = expiryDate . getTime ( ) - now . getTime ( )
212
+ const timeoutDuration = timeToExpiry - 7 * 86400000
213
+
214
+ setTimeout ( async ( ) => {
215
+ this . log . trace ( 'renewing TLS certificate' )
216
+ await this . createAndSetCertificate ( )
217
+ this . safeDispatchEvent ( 'listening' )
218
+ } , timeoutDuration )
219
+ }
220
+
199
221
private async incomingConnection ( ufrag : string , remoteHost : string , remotePort : number ) : Promise < void > {
200
222
const key = `${ remoteHost } :${ remotePort } :${ ufrag } `
201
223
let peerConnection = this . connections . get ( key )
0 commit comments