-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
97 lines (88 loc) · 2.97 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
const config = require('./config.js');
const logger = require('./logger.js').default;
logger.debug('Booting');
const path = require('path');
const express = require('express');
const http = require('http');
const url = require('url');
const ws = require('ws');
const formatter = require('./miner/formatter.js');
const Claymore = require('./miner/claymore.js');
const Ewbf = require('./miner/ewbf.js');
const Xmrig = require('./miner/xmrig.js');
// static server
const app = express();
var router = express.Router();
app.use(express.static(path.join(__dirname, 'web')));
const server = http.createServer(app);
// web socket
const wss = new ws.Server({ server });
wss.on('connection', function connection(ws, req) {
const location = url.parse(req.url, true);
logger.trace("location: " + JSON.stringify(location));
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send(formatter.ws("config", config));
});
var miners = [];
if (config.rigs) {
for(var r in config.rigs) {
if(config.rigs.hasOwnProperty(r)) {
let rig = config.rigs[r];
if (rig.miners && Array.isArray(rig.miners)) {
for(var i=rig.miners.length-1; i>=0; i--) {
var minerConf = rig.miners[i];
logger.info(`rig ${r}-${minerConf.no} ${minerConf.type} - is enabled ${minerConf.enabled}`);
if (minerConf.enabled) {
if (minerConf.type === "claymore") {
var claymoreMiner = new Claymore(r, minerConf, wss);
miners.push(claymoreMiner);
} else if (minerConf.type === "ewbf") {
var ewbfMiner = new Ewbf(r, minerConf, wss);
miners.push(ewbfMiner);
} else if (minerConf.type === "xmrig") {
var xmrig = new Xmrig(r, minerConf, wss);
miners.push(xmrig);
}
} else {
rig.miners.splice(i, 1);
}
}
}
}
}
}
// api
router.get('/', (req, res) => {
res.json({ message: 'This is the api' });
});
router.get('/config', (req, res) => {
res.json(config);
});
router.get('/status', (req, res) => {
var minersData = [];
miners.forEach(function(miner) {
var data = miner.latestData;
minersData.push(data);
});
res.json(minersData);
});
router.get('/status/:id', (req, res) => {
let minerData = null;
let id = req.params.id;
miners.forEach((miner) => {
if (miner.rigUniqueId === id) {
minerData = miner.latestData;
}
});
if (minerData) {
res.json(minerData);
} else {
res.status(404).send(`Rig id ${id} not found`);
}
});
app.use('/api', router);
server.listen(config.port, () => {
logger.info('Listening on %d', server.address().port);
});