-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
72 lines (60 loc) · 2.44 KB
/
main.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
const net = require('net');
const mainServer = net.createServer((receiverSocket) => {
const miningPoolSockets = {};
const receiverSockets = {};
const minerIdentifier = `${receiverSocket.remoteAddress}:${receiverSocket.remotePort}`;
console.log(`Connect from:${minerIdentifier}`);
receiverSockets[minerIdentifier] = receiverSocket;
receiverSocket.on('data', (data) => {
try {
console.log(`Received data: ${data.toString('utf-8')}`);
const message = JSON.parse(data.toString('utf-8'));
forwardToMiningPool(minerIdentifier, message.data, message.port, receiverSockets, miningPoolSockets);
} catch (err) {
console.error(`Received data is not valid JSON: ${data.toString('utf-8')}`);
}
});
receiverSocket.on('error', (err) => {
console.error(`Receiver socket error: ${err}`);
});
receiverSocket.on('close', () => {
delete receiverSockets[minerIdentifier];
if (miningPoolSockets[minerIdentifier]) {
miningPoolSockets[minerIdentifier].end();
delete miningPoolSockets[minerIdentifier];
}
});
});
function forwardToMiningPool(minerIdentifier, data, port, receiverSockets, miningPoolSockets) {
if (!miningPoolSockets[minerIdentifier]) {
console.log('Connecting to mining pool');
miningPoolSockets[minerIdentifier] = new net.Socket();
miningPoolSockets[minerIdentifier].connect(port, '127.0.0.1', () => {
miningPoolSockets[minerIdentifier].write(data);
});
miningPoolSockets[minerIdentifier].on('data', (response) => {
console.log(`Received data: ${response.toString('utf-8')}`);
if (receiverSockets[minerIdentifier]) {
receiverSockets[minerIdentifier].write(JSON.stringify({ minerIdentifier, data: response.toString('utf-8') }));
}
});
miningPoolSockets[minerIdentifier].on('close', () => {
delete miningPoolSockets[minerIdentifier];
});
miningPoolSockets[minerIdentifier].on('error', (err) => {
console.error(`Mining pool socket error: ${err}`);
if (receiverSockets[minerIdentifier]) {
receiverSockets[minerIdentifier].write(JSON.stringify({ minerIdentifier, error: err.message }));
}
delete miningPoolSockets[minerIdentifier];
});
} else {
miningPoolSockets[minerIdentifier].write(data);
}
}
mainServer.listen(15299, '0.0.0.0', () => {
console.log('Main server is listening on port 15299');
});
mainServer.on('error', (err) => {
console.error(`Main server error: ${err}`);
});