-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
341 lines (306 loc) · 10.2 KB
/
server.js
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const net = require('net');
const WebSocket = require('ws');
const config = require('./config');
const stratum = require('./stratum');
const express = require('express');
const path = require('path');
const sqlite = require('./sqlite');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const utils = require('./utils');
const jwt = require('jsonwebtoken');
const winston = require('winston');
const rateLimit = require('express-rate-limit');
// Initialize Express for the web UI
const app = express();
app.use(express.static(path.join(__dirname, 'public'))); // Serve static files
app.use(cookieParser());
app.use(express.urlencoded({ extended: true })); //Parse URL encoded bodies
// Setup session
app.use(session({
secret: config.WEB_UI_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
httpOnly: true,
secure: false, // Set to true in production with HTTPS
},
}));
// WebSocket server for the web UI
const wss = new WebSocket.Server({ port: config.WEB_UI_PORT + 1 });
//Rate limiter for web UI
const limiter = rateLimit({
windowMs: config.WEB_UI_RATE_LIMIT_WINDOW,
max: config.WEB_UI_RATE_LIMIT_MAX,
message: "Too many requests, please try again later",
standardHeaders: true,
legacyHeaders: false,
});
// Middleware to verify the jwt token and authenticate web socket connections.
function authenticateWebsocket(ws, req) {
try {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
ws.close(1008, 'Authentication failed');
return false;
}
jwt.verify(token, config.WEB_UI_SECRET);
return true;
} catch (error) {
logger.error("Invalid token", error);
ws.close(1008, 'Authentication failed');
return false;
}
}
// Setup logger
const logger = winston.createLogger({
level: config.LOG_LEVEL,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({ level, message, timestamp }) => {
return `${timestamp} [${level.toUpperCase()}] ${message}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: config.LOG_FILE }),
],
});
// Function to send data through websocket
function sendWsMessage(data) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
try {
client.send(JSON.stringify(data));
} catch (e) {
logger.error("Error sending message to websocket",e);
}
}
});
}
async function handleConnection(socket) {
logger.info(`Client connected from: ${socket.remoteAddress}:${socket.remotePort}`);
socket.on('data', (data) => {
stratum.handleMessage(socket,data);
});
socket.on('close', () => {
logger.info(`Client connection closed: ${socket.remoteAddress}:${socket.remotePort}`);
if (stratum.MINERS[socket]) {
delete stratum.MINERS[socket];
}
});
socket.on('error', (err) => {
logger.error(`TCP Socket error ${err}`);
});
}
async function handleWsConnection(ws, req) {
if(!authenticateWebsocket(ws,req)){
return;
}
logger.info(`Client connected from: WS`);
ws.on('message', (message) => {
stratum.handleMessage(ws, message);
});
ws.on('close', () => {
logger.info(`Client WS connection closed`);
if (stratum.MINERS[ws]) {
delete stratum.MINERS[ws];
}
});
ws.on('error', (err) => {
logger.error(`Websocket error ${err}`);
});
}
// Start TCP Server
const tcpServer = net.createServer(handleConnection);
tcpServer.listen(config.SERVER_TCP_PORT, config.SERVER_HOST, () => {
logger.info(`TCP Server listening on ${config.SERVER_HOST}:${config.SERVER_TCP_PORT}`);
});
// Start WebSocket Server for miners
const wss_miners = new WebSocket.Server({ port: config.SERVER_WS_PORT });
wss_miners.on('connection', handleWsConnection);
logger.info(`WebSocket Server listening on ${config.SERVER_HOST}:${config.SERVER_WS_PORT}`);
// Broadcast new block to all clients.
async function broadcastNewBlock(){
logger.info("New Block! sending mining.notify to all miners.")
for (const socket of Object.keys(stratum.MINERS)) {
try{
await stratum.sendMiningNotify(socket, true);
} catch (e) {
logger.error(`Error broadcasting block: ${e}`);
}
}
}
// Notify clients of new blocks every X seconds.
setInterval(broadcastNewBlock, config.BLOCK_TIME_TARGET * 1000)
// Authentication middleware
function authenticate(req, res, next) {
if (config.WEB_UI_ENABLE_AUTH) {
if (!req.session.token) {
return res.redirect('/login');
}
try {
jwt.verify(req.session.token, config.WEB_UI_SECRET);
next();
} catch (e) {
logger.error("Invalid session token", e);
return res.redirect('/login');
}
} else {
next();
}
}
//Login route
app.get('/login', (req, res) => {
if (config.WEB_UI_ENABLE_AUTH){
res.sendFile(path.join(__dirname, 'public', 'login.html'));
} else {
res.redirect('/');
}
});
// Handle login form submission
app.post('/login', (req, res) => {
if (config.WEB_UI_ENABLE_AUTH){
const { username, password } = req.body;
if (username === config.WEB_UI_USERNAME && password === config.WEB_UI_PASSWORD) {
const token = utils.generateJWT({ user: username });
req.session.token = token
res.redirect('/');
} else {
res.status(401).send('Invalid username or password');
}
} else {
res.redirect('/');
}
});
app.get('/logout', (req, res)=>{
if (config.WEB_UI_ENABLE_AUTH){
req.session.destroy((err) => {
if (err) {
logger.error("Error destroying session", err);
return res.status(500).send('Error destroying session');
}
res.redirect('/login');
});
} else {
res.redirect('/')
}
})
// Apply authentication middleware to all other routes
app.use(authenticate);
app.use(limiter); //Apply rate limiting to all routes
// Set up Web UI
app.get('/api/miners', (req, res) => {
try {
const minerData = Object.values(stratum.MINERS).map(miner => ({
user: miner.user,
shares: miner.shares,
authenticated: miner.authenticated,
hashrate: miner.hashrate.toFixed(2)
}));
res.json(minerData);
sendWsMessage({ type: 'miners', data: minerData});
} catch (error){
logger.error("Error getting miner data", error);
res.status(500).send('Internal Server Error');
}
});
app.get('/api/shares', async (req, res) => {
try {
const shareData = await sqlite.getAllShares();
res.json(shareData);
sendWsMessage({type: 'shares', data: shareData})
} catch (error){
logger.error("Error getting share data", error);
res.status(500).send('Internal Server Error');
}
});
let blockCount = 0;
app.get('/api/pool', (req, res) => {
try{
blockCount++;
res.json({
poolName: config.POOL_NAME,
serverHost: config.SERVER_HOST,
serverPort: config.SERVER_TCP_PORT,
webUI_Port: config.WEB_UI_PORT,
difficulty: CURRENT_DIFFICULTY.toFixed(2),
blocksFound: blockCount
});
sendWsMessage({type: 'pool', data: {
poolName: config.POOL_NAME,
serverHost: config.SERVER_HOST,
serverPort: config.SERVER_TCP_PORT,
webUI_Port: config.WEB_UI_PORT,
difficulty: CURRENT_DIFFICULTY.toFixed(2),
blocksFound: blockCount
}
})
} catch (error){
logger.error("Error getting pool data", error);
res.status(500).send('Internal Server Error');
}
});
app.get('/', (req,res)=>{
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
//Start web UI server
const server = app.listen(config.WEB_UI_PORT, () => {
logger.info(`Web UI started on port: ${config.WEB_UI_PORT}`);
});
//Handle websocket connection, and verify the authorization token.
wss.on('connection', (ws, req) => {
handleWsConnection(ws,req);
});
sqlite.init()
//Initial data fetch for the web UI on startup.
async function init_webUI_data() {
try{
const minerData = Object.values(stratum.MINERS).map(miner => ({
user: miner.user,
shares: miner.shares,
authenticated: miner.authenticated,
hashrate: miner.hashrate.toFixed(2)
}));
sendWsMessage({ type: 'miners', data: minerData});
const shareData = await sqlite.getAllShares();
sendWsMessage({type: 'shares', data: shareData});
sendWsMessage({type: 'pool', data: {
poolName: config.POOL_NAME,
serverHost: config.SERVER_HOST,
serverPort: config.SERVER_TCP_PORT,
webUI_Port: config.WEB_UI_PORT,
difficulty: CURRENT_DIFFICULTY.toFixed(2),
blocksFound: blockCount
}
})
} catch (error) {
logger.error("Error initializing webUI data", error);
}
}
init_webUI_data();
//Send pool data to client every x seconds.
setInterval(async () => {
try {
const minerData = Object.values(stratum.MINERS).map(miner => ({
user: miner.user,
shares: miner.shares,
authenticated: miner.authenticated,
hashrate: miner.hashrate.toFixed(2)
}));
sendWsMessage({ type: 'miners', data: minerData});
const shareData = await sqlite.getAllShares();
sendWsMessage({type: 'shares', data: shareData})
sendWsMessage({type: 'pool', data: {
poolName: config.POOL_NAME,
serverHost: config.SERVER_HOST,
serverPort: config.SERVER_TCP_PORT,
webUI_Port: config.WEB_UI_PORT,
difficulty: CURRENT_DIFFICULTY.toFixed(2),
blocksFound: blockCount
}
})
} catch (error) {
logger.error("Error updating the webUI", error);
}
}, 5000);