Support cert and key as string env variable
This commit is contained in:
1 parent
052706877a
commit
24cd3f5670
1 file changed
+38
-7
+38
-7
@@ -1,8 +1,9 @@
|
|||||||
import * as fs from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
import * as http from "node:http";
|
import * as http from "node:http";
|
||||||
import * as https from "node:https";
|
import * as https from "node:https";
|
||||||
import { Socket } from "node:net";
|
import { Socket } from "node:net";
|
||||||
import * as path from "node:path";
|
import * as path from "node:path";
|
||||||
|
import { X509Certificate, createPrivateKey } from "node:crypto";
|
||||||
import { ROOT_DIR } from "./config.ts";
|
import { ROOT_DIR } from "./config.ts";
|
||||||
|
|
||||||
let server: http.Server | https.Server;
|
let server: http.Server | https.Server;
|
||||||
@@ -59,6 +60,40 @@ type Promisify<T extends (...args: any) => any> = (
|
|||||||
...args: Parameters<T>
|
...args: Parameters<T>
|
||||||
) => Promise<ReturnType<T>>;
|
) => Promise<ReturnType<T>>;
|
||||||
|
|
||||||
|
function getValidPrivKeys(value: string): Buffer[] {
|
||||||
|
return value.split(":").map((str) => {
|
||||||
|
str = str.trim();
|
||||||
|
const buf = str.startsWith("-----BEGIN ")
|
||||||
|
? Buffer.from(str)
|
||||||
|
: readFileSync(path.resolve(ROOT_DIR, str));
|
||||||
|
|
||||||
|
try {
|
||||||
|
createPrivateKey(buf);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Invalid private key: ${err.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getValidCerts(value: string): Buffer[] {
|
||||||
|
return value.split(":").map((str) => {
|
||||||
|
str = str.trim();
|
||||||
|
const buf = str.startsWith("-----BEGIN ")
|
||||||
|
? Buffer.from(str)
|
||||||
|
: readFileSync(path.resolve(ROOT_DIR, str));
|
||||||
|
|
||||||
|
try {
|
||||||
|
new X509Certificate(buf);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Invalid certificate: ${err.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function start(
|
export function start(
|
||||||
options: ServerOptions,
|
options: ServerOptions,
|
||||||
_listener: Promisify<http.RequestListener>,
|
_listener: Promisify<http.RequestListener>,
|
||||||
@@ -81,12 +116,8 @@ export function start(
|
|||||||
|
|
||||||
if (options.ssl) {
|
if (options.ssl) {
|
||||||
const opts = {
|
const opts = {
|
||||||
key: options.ssl.key
|
key: getValidPrivKeys(options.ssl.key),
|
||||||
.split(":")
|
cert: getValidCerts(options.ssl.cert),
|
||||||
.map((f) => fs.readFileSync(path.resolve(ROOT_DIR, f.trim()))),
|
|
||||||
cert: options.ssl.cert
|
|
||||||
.split(":")
|
|
||||||
.map((f) => fs.readFileSync(path.resolve(ROOT_DIR, f.trim()))),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
server = https.createServer(opts, listener);
|
server = https.createServer(opts, listener);
|
||||||
|
|||||||
Reference in new issue
Block a user