-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
265 lines (222 loc) · 8.4 KB
/
Copy pathserver.js
File metadata and controls
265 lines (222 loc) · 8.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env node
/**
* BBSFirewall by Sysop Network
* https://github.com/SysopNetwork/BBSFirewall
*
* Copyright (c) 2026 Sysop Network
* Based on bbsfw by Ryan Fantus — https://github.com/ryanfantus/bbsfw
* Licensed under MIT
*/
const net = require('net');
const { config, validateConfig } = require('./config');
const logger = require('./logger');
const fileLogger = require('./file-logger');
const metrics = require('./metrics');
const { handleConnection } = require('./proxy');
const { initializeGeoIP } = require('./geoip');
const { initializeIPFilter } = require('./ipfilter');
const { startSSHServer } = require('./ssh');
const { startWebRedirectServer, stopWebRedirectServer } = require('./web-redirect');
const { startConfigEditorServer, stopConfigEditorServer } = require('./config-editor');
class BBSFirewall {
constructor() {
this.server = null;
this.sshServer = null;
this.sshPassthroughServer = null;
this.activeConnections = 0;
}
async start() {
try {
validateConfig();
} catch (err) {
logger.error(`Configuration error: ${err.message}`);
process.exit(1);
}
logger.info('================================================');
logger.info(' BBSFirewall by Sysop Network');
logger.info(' https://github.com/SysopNetwork/BBSFirewall');
logger.info(' Based on bbsfw by Ryan Fantus');
logger.info('================================================');
logger.info('Starting...');
await initializeGeoIP();
initializeIPFilter(config);
// Daily is plenty (retention granularity is whole days anyway) and this
// runs independently of the config editor, so pruning still happens even
// with CONFIG_EDITOR_ENABLED=false.
fileLogger.startPruning(24 * 60 * 60 * 1000);
// Server-side sampler so bandwidth avg/max stay meaningful even when no
// admin has the System Stats tab open (independent of its own 4s poll).
metrics.startBandwidthSampler(4000);
const configLog = {
listenPort: config.listenPort,
backendHost: config.backendHost,
backendPort: config.backendPort,
maxConnections: config.maxConnections,
maxConnectionsPerIP: config.maxConnectionsPerIP === 0 ? 'unlimited' : config.maxConnectionsPerIP,
blockedCountries: config.blockedCountries.length > 0
? config.blockedCountries.join(', ')
: 'none',
rateLimitEnabled: config.rateLimitEnabled,
maxConnectionsPerWindow: config.maxConnectionsPerWindow,
rateLimitWindowMs: `${config.rateLimitWindowMs}ms`,
blocklistPath: config.blocklistPath || 'none',
proxyProtocolEnabled: config.proxyProtocolEnabled,
webRedirectEnabled: config.webRedirectEnabled,
sshMode: config.sshMode,
fileLog: config.fileLog.enabled ? `on (default: ${config.fileLog.defaultLevel})` : 'off',
};
if (config.webRedirectEnabled) {
configLog.webRedirectUrl = config.webRedirectUrl;
}
if (config.httpsRedirectEnabled) {
configLog.httpsRedirectPort = config.httpsRedirectPort;
configLog.httpsCertPath = config.httpsCertPath;
}
configLog.configEditorEnabled = config.configEditor.enabled;
if (config.configEditor.enabled) {
configLog.configEditorPort = config.configEditor.port;
}
if (config.sshMode === 'terminate') {
configLog.sshListenPort = config.sshListenPort;
configLog.sshCiphers = config.sshCiphers.join(', ');
} else if (config.sshMode === 'passthrough') {
configLog.sshListenPort = config.sshListenPort;
configLog.sshBackend = `${config.sshBackendHost}:${config.sshBackendPort}`;
}
logger.info('Configuration:', configLog);
this.server = net.createServer((clientSocket) => {
this.handleNewConnection(clientSocket, config.backendHost, config.backendPort, {
proxyName: 'telnet',
});
});
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(`Telnet proxy listening on port ${config.listenPort}`);
logger.info(`Forwarding connections to ${config.backendHost}:${config.backendPort}`);
});
this.startSSH();
// Start web redirect server if enabled
startWebRedirectServer();
// Start the web config editor if enabled. Passes `this` so the /status
// endpoint can report real telnet/SSH listener state, not just config flags.
startConfigEditorServer(this);
this.setupGracefulShutdown();
}
// Bring up whichever SSH frontend the config selected. Both listen on
// SSH_LISTEN_PORT, so only one runs at a time.
startSSH() {
if (config.sshMode === 'terminate') {
this.sshServer = startSSHServer(config, this);
return;
}
if (config.sshMode === 'passthrough') {
const log = logger.getLogger('ssh-passthrough');
this.sshPassthroughServer = net.createServer((clientSocket) => {
this.handleNewConnection(clientSocket, config.sshBackendHost, config.sshBackendPort, {
proxyName: 'ssh-passthrough',
proxyProtocol: config.sshProxyProtocol,
encodingDetection: false, // encrypted stream — nothing to sniff
});
});
this.sshPassthroughServer.on('error', (err) => {
log.error(`SSH passthrough server error: ${err.message}`);
if (err.code === 'EADDRINUSE') {
log.error(`SSH port ${config.sshListenPort} is already in use`);
process.exit(1);
}
});
this.sshPassthroughServer.listen(config.sshListenPort, () => {
log.info(`SSH passthrough listening on port ${config.sshListenPort}`);
log.info(`Forwarding encrypted SSH to ${config.sshBackendHost}:${config.sshBackendPort}`);
});
return;
}
logger.info('SSH is disabled (SSH_MODE=off)');
}
handleNewConnection(clientSocket, backendHost, backendPort, options = {}) {
const proxyName = options.proxyName || 'telnet';
if (this.activeConnections >= config.maxConnections) {
const log = logger.getLogger(proxyName);
log.blocked(`Connection rejected: max connections (${config.maxConnections}) reached`);
metrics.incRejected();
clientSocket.end();
return;
}
this.activeConnections++;
metrics.incActive(proxyName);
logger.debug(`Active connections: ${this.activeConnections}`);
if (config.connectionTimeout > 0) {
clientSocket.setTimeout(config.connectionTimeout);
clientSocket.on('timeout', () => {
logger.info(`Connection timeout for ${clientSocket.remoteAddress}`);
clientSocket.destroy();
});
}
handleConnection(clientSocket, backendHost, backendPort, options);
clientSocket.on('close', () => {
this.activeConnections--;
metrics.decActive(proxyName);
logger.debug(`Active connections: ${this.activeConnections}`);
});
}
setupGracefulShutdown() {
const shutdown = async () => {
logger.info('Shutting down gracefully...');
await stopWebRedirectServer();
await stopConfigEditorServer();
let serversToClose = 0;
let serversClosed = 0;
const onClose = () => {
serversClosed++;
if (serversClosed === serversToClose) {
fileLogger.closeAll();
process.exit(0);
}
};
if (this.server) {
serversToClose++;
this.server.close(() => {
logger.info('Telnet server closed');
onClose();
});
}
if (this.sshServer) {
serversToClose++;
this.sshServer.close(() => {
logger.info('SSH server closed');
onClose();
});
}
if (this.sshPassthroughServer) {
serversToClose++;
this.sshPassthroughServer.close(() => {
logger.info('SSH passthrough server closed');
onClose();
});
}
if (serversToClose === 0) {
fileLogger.closeAll();
process.exit(0);
}
// Force shutdown after 10 seconds if servers don't close cleanly
setTimeout(() => {
logger.warn('Forcing shutdown after timeout');
fileLogger.closeAll();
process.exit(1);
}, 10000);
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
}
}
if (require.main === module) {
const firewall = new BBSFirewall();
firewall.start();
}
module.exports = BBSFirewall;