-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
166 lines (136 loc) · 4.4 KB
/
server.js
File metadata and controls
166 lines (136 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env node
/**
* BBS Firewall (bbsfw)
* TCP proxy server for telnet connections
*/
const net = require('net');
const { config, validateConfig } = require('./config');
const logger = require('./logger');
const { handleConnection } = require('./proxy');
const { initializeGeoIP } = require('./geoip');
const { initializeIPFilter } = require('./ipfilter');
const { startSSHServer } = require('./ssh');
class BBSFirewall {
constructor() {
this.server = null;
this.sshServer = null;
this.activeConnections = 0;
}
async start() {
try {
validateConfig();
} catch (err) {
logger.error('Configuration error:', err.message);
process.exit(1);
}
logger.info('Starting BBS Firewall...');
// Initialize GeoIP database
await initializeGeoIP();
// Initialize IP filter
initializeIPFilter(config);
const configLog = {
listenPort: config.listenPort,
backendHost: config.backendHost,
backendPort: config.backendPort,
maxConnections: config.maxConnections,
blockedCountries: config.blockedCountries.length > 0
? config.blockedCountries.join(', ')
: 'none',
rateLimitEnabled: config.rateLimitEnabled,
maxConnectionsPerWindow: config.maxConnectionsPerWindow,
rateLimitWindowMs: `${config.rateLimitWindowMs}ms`,
blocklistPath: config.blocklistPath || 'none',
sshEnabled: config.sshEnabled,
};
if (config.sshEnabled) {
configLog.sshListenPort = config.sshListenPort;
configLog.sshCiphers = config.sshCiphers.join(', ');
}
logger.info(`Configuration:`, configLog);
this.server = net.createServer((clientSocket) => {
this.handleNewConnection(clientSocket);
});
this.server.on('error', (err) => {
logger.error('Server error:', err.message);
if (err.code === 'EADDRINUSE') {
logger.error(`Port ${config.listenPort} is already in use`);
process.exit(1);
}
});
this.server.listen(config.listenPort, () => {
logger.info(`BBS Firewall listening on port ${config.listenPort}`);
logger.info(`Forwarding connections to ${config.backendHost}:${config.backendPort}`);
});
// Start SSH server if enabled
this.sshServer = startSSHServer(config, this);
this.setupGracefulShutdown();
}
handleNewConnection(clientSocket) {
// Check max connections limit
if (this.activeConnections >= config.maxConnections) {
logger.warn(`Connection rejected: max connections (${config.maxConnections}) reached`);
clientSocket.end();
return;
}
this.activeConnections++;
logger.debug(`Active connections: ${this.activeConnections}`);
// Set connection timeout
if (config.connectionTimeout > 0) {
clientSocket.setTimeout(config.connectionTimeout);
clientSocket.on('timeout', () => {
logger.info(`Connection timeout for ${clientSocket.remoteAddress}`);
clientSocket.destroy();
});
}
// Handle the proxy connection
handleConnection(clientSocket, config.backendHost, config.backendPort);
// Track connection close
clientSocket.on('close', () => {
this.activeConnections--;
logger.debug(`Active connections: ${this.activeConnections}`);
});
}
setupGracefulShutdown() {
const shutdown = () => {
logger.info('Shutting down gracefully...');
let serversToClose = 0;
let serversClosed = 0;
if (this.server) {
serversToClose++;
this.server.close(() => {
logger.info('Telnet server closed');
serversClosed++;
if (serversClosed === serversToClose) {
process.exit(0);
}
});
}
if (this.sshServer) {
serversToClose++;
this.sshServer.close(() => {
logger.info('SSH server closed');
serversClosed++;
if (serversClosed === serversToClose) {
process.exit(0);
}
});
}
if (serversToClose === 0) {
process.exit(0);
}
// Force shutdown after 10 seconds
setTimeout(() => {
logger.warn('Forcing shutdown');
process.exit(1);
}, 10000);
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
}
}
// Start the firewall
if (require.main === module) {
const firewall = new BBSFirewall();
firewall.start();
}
module.exports = BBSFirewall;