*refactor ssl support
This commit is contained in:
parent
3b1b6425ae
commit
714da70209
|
@ -20,7 +20,7 @@ export async function start() {
|
||||||
const dht = getSwarm();
|
const dht = getSwarm();
|
||||||
let sslOptions: boolean | http2.SecureServerOptions = false;
|
let sslOptions: boolean | http2.SecureServerOptions = false;
|
||||||
|
|
||||||
if (getSslManager().enabled) {
|
if (getSslManager().ready) {
|
||||||
sslOptions = {
|
sslOptions = {
|
||||||
SNICallback: () => getSslManager().context,
|
SNICallback: () => getSslManager().context,
|
||||||
} as http2.SecureServerOptions;
|
} as http2.SecureServerOptions;
|
||||||
|
|
|
@ -6,37 +6,47 @@ import config from "../config.js";
|
||||||
export type SSLManagerRenewHandler = (domain: string) => Promise<boolean>;
|
export type SSLManagerRenewHandler = (domain: string) => Promise<boolean>;
|
||||||
|
|
||||||
export class SSLManager {
|
export class SSLManager {
|
||||||
private _context?: tls.SecureContext;
|
|
||||||
private _key?: Buffer;
|
private _key?: Buffer;
|
||||||
private _cert?: Buffer;
|
|
||||||
private _domain: string;
|
private _domain: string;
|
||||||
private _renewHandler?: SSLManagerRenewHandler;
|
|
||||||
|
|
||||||
constructor(domain: string) {
|
constructor(domain: string) {
|
||||||
this._domain = domain;
|
this._domain = domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _context?: tls.SecureContext;
|
||||||
|
|
||||||
get context(): tls.SecureContext {
|
get context(): tls.SecureContext {
|
||||||
return this._context as tls.SecureContext;
|
return this._context as tls.SecureContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _cert?: Buffer;
|
||||||
|
|
||||||
|
set cert(cert: Buffer) {
|
||||||
|
this._cert = cert;
|
||||||
|
this._maybeUpdateContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _renewHandler?: SSLManagerRenewHandler;
|
||||||
|
|
||||||
|
get renewHandler(): SSLManagerRenewHandler {
|
||||||
|
return this._renewHandler as any;
|
||||||
|
}
|
||||||
|
|
||||||
|
set renewHandler(value: SSLManagerRenewHandler) {
|
||||||
|
this._renewHandler = value;
|
||||||
|
}
|
||||||
|
|
||||||
set privateKey(key: Buffer) {
|
set privateKey(key: Buffer) {
|
||||||
this._key = key;
|
this._key = key;
|
||||||
this._maybeUpdateContext();
|
this._maybeUpdateContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
set cert(cert: Buffer) {
|
get enabled() {
|
||||||
this._cert = cert;
|
return config.bool("core.ssl");
|
||||||
this._maybeUpdateContext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _maybeUpdateContext() {
|
get ready() {
|
||||||
if (b4a.isBuffer(this._cert) && b4a.isBuffer(this._key)) {
|
return this.enabled && this.renewHandler;
|
||||||
this._context = tls.createSecureContext({
|
|
||||||
cert: this._cert,
|
|
||||||
key: this._key,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async renew(): Promise<boolean> {
|
public async renew(): Promise<boolean> {
|
||||||
|
@ -50,8 +60,13 @@ export class SSLManager {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
get enabled() {
|
private _maybeUpdateContext() {
|
||||||
return config.bool("core.ssl") && this._renewHandler;
|
if (b4a.isBuffer(this._cert) && b4a.isBuffer(this._key)) {
|
||||||
|
this._context = tls.createSecureContext({
|
||||||
|
cert: this._cert,
|
||||||
|
key: this._key,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue