Skip to content

Commit

Permalink
feat: 支持使用自定义证书
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Nov 27, 2024
1 parent f903821 commit b03dc2f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ export async function bootstrap(version: string): Promise<void> {
cluster.connect()
const proto = config.byoc ? 'http' : 'https'
if (proto === 'https') {
logger.info('请求证书')
await cluster.requestCert()
if (config.sslCert && config.sslKey) {
logger.debug('使用自定义证书')
await cluster.useSelfCert()
} else {
logger.info('请求证书')
await cluster.requestCert()
}
}

if (config.enableNginx) {
if (typeof cluster.port === 'number') {
await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto)
Expand Down
20 changes: 20 additions & 0 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,26 @@ export class Cluster {
await fse.outputFile(join(this.tmpDir, 'key.pem'), cert.key)
}

public async useSelfCert(): Promise<void> {
if (!config.sslCert) {
throw new Error('缺少ssl证书')
}
if (!config.sslKey) {
throw new Error('缺少ssl私钥')
}

if (await fse.pathExists(config.sslCert)) {
await fse.copyFile(config.sslCert, join(this.tmpDir, 'cert.pem'))
} else {
await fse.outputFile(join(this.tmpDir, 'cert.pem'), config.sslCert)
}
if (await fse.pathExists(config.sslKey)) {
await fse.copyFile(config.sslKey, join(this.tmpDir, 'key.pem'))
} else {
await fse.outputFile(join(this.tmpDir, 'key.pem'), config.sslKey)
}
}

public exit(code: number = 0): void {
if (this.nginxProcess) {
this.nginxProcess.kill()
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export class Config {
public readonly enableUpnp = env.get('ENABLE_UPNP').asBool()
public readonly storage = env.get('CLUSTER_STORAGE').default('file').asString()
public readonly storageOpts = env.get('CLUSTER_STORAGE_OPTIONS').asJsonObject()

public readonly sslKey = env.get('SSL_KEY').asString()
public readonly sslCert = env.get('SSL_CERT').asString()

public readonly flavor: IConfigFlavor

private constructor() {
Expand Down

0 comments on commit b03dc2f

Please sign in to comment.