diff --git a/.env.example b/.env.example index 50b3453..c5950e4 100644 --- a/.env.example +++ b/.env.example @@ -18,4 +18,6 @@ PORT=3333 #optional DEV_FEE_ADDRESS= # mainnet | testnet -NETWORK=mainnet \ No newline at end of file +NETWORK=mainnet + +API_SECURE=false \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index e30bd32..2c3745c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { NestFactory } from '@nestjs/core'; import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'; import * as bitcoinjs from 'bitcoinjs-lib'; import { useContainer } from 'class-validator'; +import { readFileSync } from 'fs'; import * as ecc from 'tiny-secp256k1'; import { AppModule } from './app.module'; @@ -14,20 +15,22 @@ async function bootstrap() { return; } - // const httpsOptions = { - // key: readFileSync('./secrets/key.pem'), - // cert: readFileSync('./secrets/cert.pem'), - // }; + let options = {}; + const secure = process.env.API_SECURE?.toLowerCase() == 'true'; + if (secure) { - //const app = await NestFactory.create(AppModule, new FastifyAdapter({ https: httpsOptions })); + const httpsOptions = { + key: readFileSync('./secrets/key.pem'), + cert: readFileSync('./secrets/cert.pem'), + }; - // stagger startup - // if (process.env.NODE_APP_INSTANCE != null) { - // await setTimeout(parseInt(process.env.NODE_APP_INSTANCE) * 5000); - // } + options = { https: httpsOptions }; + } else { - const app = await NestFactory.create(AppModule, new FastifyAdapter()); + } + + const app = await NestFactory.create(AppModule, new FastifyAdapter(options)); app.setGlobalPrefix('api') app.useGlobalPipes( new ValidationPipe({ @@ -45,7 +48,7 @@ async function bootstrap() { bitcoinjs.initEccLib(ecc); await app.listen(process.env.PORT, '0.0.0.0', () => { - console.log(`https listening on port ${process.env.PORT}`); + console.log(`${secure ? 'https' : 'http'} listening on port ${process.env.PORT}`); }); } diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index c655e66..e9d54d9 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -318,6 +318,11 @@ export class StratumV1Client { } break; } + default: { + console.log("Invalid message"); + await this.socket.end(); + return; + } } diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index b7320e0..c6303b9 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -42,7 +42,10 @@ export class StratumV1Service implements OnModuleInit { private startSocketServer() { const server = new Server(async (socket: Socket) => { - + socket.setTimeout(5000, () => { + console.log(`Client ${client.extraNonceAndSessionId} timeout`); + socket.destroy(); + }); const client = new StratumV1Client( socket,