From ce3cbabd7f8587c048c1dc082b46d1d9f1723cab Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Sun, 21 Nov 2021 13:12:42 -0500 Subject: [PATCH 1/8] Add HTTPS option We're currently testing out DuckyAPI on Kubernetes and we have a practice of using HTTPS even in internal cluster communications. Others might find this helpful for different reasons. --- src/main.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 95f9667..eb4141c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,8 +15,18 @@ const writeFile = promisify(fs.writeFile) declare const module: any async function bootstrap(): Promise { - const app = await NestFactory.create(AppModule) const config: ConfigService = app.get('ConfigService') + let nestConfig = {} + + if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { + const fs = require('fs'); + nestConfig.httpsOptions = { + key: fs.readFileSync(config.TLS_KEY_PATH), + cert: fs.readFileSync(config.TLS_CERT_PATH) + }; + } + + const app = await NestFactory.create(AppModule, nestConfig); if (config.SERVE_DUCKYPANEL) { // Write baseurl to file for DuckyPanel to find From 178629909c3ee1e139522fc97cb9c43bd0c41cdc Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 09:37:38 -0500 Subject: [PATCH 2/8] Update ducky-api-config.class.ts --- src/config/ducky-api-config.class.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/config/ducky-api-config.class.ts b/src/config/ducky-api-config.class.ts index 58e93bc..846ea48 100644 --- a/src/config/ducky-api-config.class.ts +++ b/src/config/ducky-api-config.class.ts @@ -148,4 +148,12 @@ export class DuckyApiConfig { @IsNumber() @Min(0) DELAY: number + + @IsNotEmpty() + @IsString() + TLS_KEY_PATH: string + + @IsNotEmpty() + @IsString() + TLS_CERT_PATH: string } From 700cc62b72cb5e4a18db779b896fa821a5297df6 Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 11:18:38 -0500 Subject: [PATCH 3/8] Update main.ts --- src/main.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index eb4141c..81193c6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,12 @@ declare const module: any async function bootstrap(): Promise { const config: ConfigService = app.get('ConfigService') - let nestConfig = {} + let nestConfig: { + httpsOptions: { + key: string, + cert: string + } + } if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { const fs = require('fs'); From 4aaea883a8307892c19316027f6ec908b4266a99 Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 20:17:20 -0500 Subject: [PATCH 4/8] Update main.ts --- src/main.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 81193c6..3afadb1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,16 +23,16 @@ async function bootstrap(): Promise { } } + const app = await NestFactory.create(AppModule, nestConfig); + if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { const fs = require('fs'); - nestConfig.httpsOptions = { + app.appOptions.httpsOptions = { key: fs.readFileSync(config.TLS_KEY_PATH), cert: fs.readFileSync(config.TLS_CERT_PATH) }; } - const app = await NestFactory.create(AppModule, nestConfig); - if (config.SERVE_DUCKYPANEL) { // Write baseurl to file for DuckyPanel to find await writeFile( From 8b119f4694adf2a85cdcf7ea146df02abf3a1db1 Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 21:55:12 -0500 Subject: [PATCH 5/8] Update main.ts --- src/main.ts | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/main.ts b/src/main.ts index 3afadb1..8f129d5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,8 @@ import fs from 'fs' import Helmet from 'helmet' import { resolve } from 'path' import { promisify } from 'util' +import { ExpressAdapter } from '@nestjs/platform-express' +import * as express from 'express'; import { AppModule } from './app.module' import { UnauthorizedExceptionFilter } from './common/filters/unauthorized-exception.filter' @@ -15,23 +17,13 @@ const writeFile = promisify(fs.writeFile) declare const module: any async function bootstrap(): Promise { - const config: ConfigService = app.get('ConfigService') - let nestConfig: { - httpsOptions: { - key: string, - cert: string - } - } - - const app = await NestFactory.create(AppModule, nestConfig); + const server = express() + const app = await NestFactory.create( + AppModule, + new ExpressAdapter(server) + ) - if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { - const fs = require('fs'); - app.appOptions.httpsOptions = { - key: fs.readFileSync(config.TLS_KEY_PATH), - cert: fs.readFileSync(config.TLS_CERT_PATH) - }; - } + const config: ConfigService = app.get('ConfigService') if (config.SERVE_DUCKYPANEL) { // Write baseurl to file for DuckyPanel to find @@ -70,8 +62,19 @@ async function bootstrap(): Promise { displayOperationId: true, }, }) + + await app.init() - await app.listen(config.PORT) + if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { + const fs = require('fs') + await https.createServer({ + key: fs.readFileSync(config.TLS_KEY_PATH), + cert: fs.readFileSync(config.TLS_CERT_PATH) + }, server).listen(config.PORT) + } + else { + await http.createServer(server).listen(config.PORT) + } if (module.hot) { module.hot.accept() From 75341e8a205d66a357e5ddda6b67c081f76aa0f6 Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 22:30:55 -0500 Subject: [PATCH 6/8] Update main.ts --- src/main.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.ts b/src/main.ts index 8f129d5..362f60a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -66,6 +66,7 @@ async function bootstrap(): Promise { await app.init() if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) { + const https = require('https') const fs = require('fs') await https.createServer({ key: fs.readFileSync(config.TLS_KEY_PATH), @@ -73,6 +74,7 @@ async function bootstrap(): Promise { }, server).listen(config.PORT) } else { + const http = require('http') await http.createServer(server).listen(config.PORT) } From 7ffcaecafed00ae5ebb16bd36dae722d50cd6958 Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 26 Nov 2021 22:54:54 -0500 Subject: [PATCH 7/8] Update main.ts --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 362f60a..dd580ad 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,7 +17,7 @@ const writeFile = promisify(fs.writeFile) declare const module: any async function bootstrap(): Promise { - const server = express() + const server = express.default() const app = await NestFactory.create( AppModule, new ExpressAdapter(server) From 6c8c7149798648082773b83e897d18d3783e3ccf Mon Sep 17 00:00:00 2001 From: CheyenneForbes Date: Fri, 14 Jan 2022 00:40:38 -0500 Subject: [PATCH 8/8] Update ducky-api-config.class.ts --- src/config/ducky-api-config.class.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/config/ducky-api-config.class.ts b/src/config/ducky-api-config.class.ts index 58e93bc..0330d64 100644 --- a/src/config/ducky-api-config.class.ts +++ b/src/config/ducky-api-config.class.ts @@ -148,4 +148,14 @@ export class DuckyApiConfig { @IsNumber() @Min(0) DELAY: number + + @IsOptional() + @IsNotEmpty() + @IsString() + TLS_KEY_PATH: string + + @IsOptional() + @IsNotEmpty() + @IsString() + TLS_CERT_PATH: string }