mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
enable stratum TLS
This commit is contained in:
@@ -17,6 +17,9 @@ BITCOIN_RPC_TIMEOUT=10000
|
|||||||
API_PORT=3334
|
API_PORT=3334
|
||||||
STRATUM_PORTS=3333,3332,3331,3330
|
STRATUM_PORTS=3333,3332,3331,3330
|
||||||
|
|
||||||
|
STRATUM_SECURE=true
|
||||||
|
SECURE_STRATUM_PORTS=4333,4332,4331,4330
|
||||||
|
|
||||||
#optional telegram bot
|
#optional telegram bot
|
||||||
#TELEGRAM_BOT_TOKEN=
|
#TELEGRAM_BOT_TOKEN=
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,11 @@ import { BitcoinRpcService } from './bitcoin-rpc.service';
|
|||||||
import { NotificationService } from './notification.service';
|
import { NotificationService } from './notification.service';
|
||||||
import { StratumV1JobsService } from './stratum-v1-jobs.service';
|
import { StratumV1JobsService } from './stratum-v1-jobs.service';
|
||||||
|
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { TlsOptions, TLSSocket, createServer } from 'tls';
|
||||||
|
import * as path from 'path';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class StratumV1Service implements OnModuleInit {
|
export class StratumV1Service implements OnModuleInit {
|
||||||
@@ -41,9 +46,14 @@ export class StratumV1Service implements OnModuleInit {
|
|||||||
|
|
||||||
// wait for all the other processes to init for an even connection distribution
|
// wait for all the other processes to init for an even connection distribution
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
process.env.STRATUM_PORTS.split(',').forEach(port =>{
|
process.env.STRATUM_PORTS.split(',').forEach(port => {
|
||||||
this.startSocketServer(parseInt(port));
|
this.startSocketServer(parseInt(port));
|
||||||
});
|
});
|
||||||
|
if (process.env.STRATUM_SECURE?.toLowerCase() === 'true') {
|
||||||
|
process.env.SECURE_STRATUM_PORTS.split(',').forEach(port => {
|
||||||
|
this.startSecureSocketServer(parseInt(port));
|
||||||
|
});
|
||||||
|
}
|
||||||
}, (10000));
|
}, (10000));
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
@@ -77,9 +87,9 @@ export class StratumV1Service implements OnModuleInit {
|
|||||||
const cleanup = async (reason: string) => {
|
const cleanup = async (reason: string) => {
|
||||||
if (client.extraNonceAndSessionId != null) {
|
if (client.extraNonceAndSessionId != null) {
|
||||||
await client.destroy();
|
await client.destroy();
|
||||||
if(reason == 'Error'){
|
if (reason == 'Error') {
|
||||||
this.errorClosure++;
|
this.errorClosure++;
|
||||||
}else{
|
} else {
|
||||||
this.normalClosure++;
|
this.normalClosure++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,9 +106,9 @@ export class StratumV1Service implements OnModuleInit {
|
|||||||
|
|
||||||
// Handle socket timeouts
|
// Handle socket timeouts
|
||||||
socket.on('timeout', async () => {
|
socket.on('timeout', async () => {
|
||||||
if(socket.bytesRead == 0 || socket.bytesWritten == 0){
|
if (socket.bytesRead == 0 || socket.bytesWritten == 0) {
|
||||||
this.emptySocket++;
|
this.emptySocket++;
|
||||||
}else{
|
} else {
|
||||||
this.socketTimeout++;
|
this.socketTimeout++;
|
||||||
}
|
}
|
||||||
await cleanup("Timeout");
|
await cleanup("Timeout");
|
||||||
@@ -123,8 +133,79 @@ export class StratumV1Service implements OnModuleInit {
|
|||||||
console.log(`Stratum server is listening on port ${port}`);
|
console.log(`Stratum server is listening on port ${port}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
private startSecureSocketServer(port: number) {
|
||||||
|
|
||||||
|
const currentDirectory = process.cwd();
|
||||||
|
const keyPath = path.join(currentDirectory, 'secrets', 'key.pem');
|
||||||
|
const certPath = path.join(currentDirectory, 'secrets', 'cert.pem');
|
||||||
|
|
||||||
|
const tlsOptions: TlsOptions = {
|
||||||
|
key: readFileSync(keyPath),
|
||||||
|
cert: readFileSync(certPath),
|
||||||
|
};
|
||||||
|
|
||||||
|
const server = createServer(tlsOptions, async (socket: TLSSocket) => {
|
||||||
|
// Set 15-minute timeout
|
||||||
|
socket.setTimeout(1000 * 60 * 15);
|
||||||
|
|
||||||
|
const client = new StratumV1Client(
|
||||||
|
socket,
|
||||||
|
this.stratumV1JobsService,
|
||||||
|
this.bitcoinRpcService,
|
||||||
|
this.clientService,
|
||||||
|
this.clientStatisticsService,
|
||||||
|
this.notificationService,
|
||||||
|
this.blocksService,
|
||||||
|
this.configService,
|
||||||
|
this.addressSettingsService
|
||||||
|
);
|
||||||
|
|
||||||
|
const cleanup = async (reason: string) => {
|
||||||
|
if (client.extraNonceAndSessionId != null) {
|
||||||
|
await client.destroy();
|
||||||
|
if (reason === 'Error') {
|
||||||
|
this.errorClosure++;
|
||||||
|
} else {
|
||||||
|
this.normalClosure++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!socket.destroyed) {
|
||||||
|
socket.end();
|
||||||
|
socket.destroy();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.on('close', async (hadError: boolean) => {
|
||||||
|
await cleanup(hadError ? 'Error' : 'Normal Closure');
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('timeout', async () => {
|
||||||
|
if (socket.bytesRead === 0 || socket.bytesWritten === 0) {
|
||||||
|
this.emptySocket++;
|
||||||
|
} else {
|
||||||
|
this.socketTimeout++;
|
||||||
|
}
|
||||||
|
await cleanup('Timeout');
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('error', async (error: Error) => {
|
||||||
|
await cleanup('Error');
|
||||||
|
});
|
||||||
|
|
||||||
|
// your protocol handling stays the same
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('error', (err) => {
|
||||||
|
console.error(`Server error: ${err.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(port, () => {
|
||||||
|
console.log(`Stratum TLS server is listening on port ${port}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user