Advertisement
Guest User

Untitled

a guest
Dec 5th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. "use strict";
  2. const fs = require('fs');
  3. const net = require('net');
  4. const crypto = require('crypto');
  5. const request = require('request');
  6. const child_process = require('child_process');
  7.  
  8. const apiKey = 'redacted';
  9. const apiSecret= 'redacted';
  10. const apiBase = 'https://www.miningrigrentals.com/api/v2';
  11.  
  12. let rigAlgos = [
  13. {name: 'v8', endpoint: '/rig/108229', status: '', algo: 'cryptonight', variant: '2'},
  14. {name: 'heavy', endpoint: '/rig/108803', status: '', algo: 'cryptonight-heavy', variant: '0'},
  15. {name: 'lite', endpoint: '/rig/108809', status: '', algo: 'cryptonight-lite', variant: '1'},
  16. {name: 'fast', endpoint: '/rig/108810', status: '', algo: 'cryptonight', variant: 'msr'},
  17. {name: 'v7', endpoint: '/rig/109065', status: '', algo: 'cryptonight', variant: '1'},
  18. {name: 'xtl', endpoint: '/rig/109702', status: '', algo: 'cryptonight', variant: 'xtl'},
  19. {name: 'xhv', endpoint: '/rig/109744', status: '', algo: 'cryptonight-heavy', variant: 'xhv'},
  20. {name: 'tube', endpoint: '/rig/109751', status: '', algo: 'cryptonight-heavy', variant: 'tube'},
  21. {name: 'xfh', endpoint: '/rig/110803', status: '', algo: 'cryptonight', variant: 'xfh'},
  22. {name: 'mox', endpoint: '/rig/111386', status: '', algo: 'cryptonight', variant: 'red'}
  23. ];
  24.  
  25. let nonce = Math.floor(Date.now());
  26. let logFile = 'log-MRR.txt';
  27. let defaultAlgo = 'MO';
  28. let minerRunning = null;
  29. let currentMiner = null;
  30. let minerArg = '';
  31. if (process.argv[2]) {
  32. minerArg = process.argv[2];
  33. logFile = 'log-MRR-' + process.argv[2] + '.txt';
  34. if (minerArg === '8gb') {
  35. rigAlgos = [
  36. {name: 'v8', endpoint: '/rig/108767', status: '', algo: 'cryptonight', variant: '2'},
  37. {name: 'heavy', endpoint: '/rig/109674', status: '', algo: 'cryptonight-heavy', variant: '0'},
  38. {name: 'lite', endpoint: '/rig/111166', status: '', algo: 'cryptonight-lite', variant: '1'},
  39. {name: 'fast', endpoint: '/rig/111165', status: '', algo: 'cryptonight', variant: 'msr'},
  40. {name: 'v7', endpoint: '/rig/111170', status: '', algo: 'cryptonight', variant: '1'},
  41. {name: 'xtl', endpoint: '/rig/111172', status: '', algo: 'cryptonight', variant: 'xtl'},
  42. {name: 'xhv', endpoint: '/rig/111168', status: '', algo: 'cryptonight-heavy', variant: 'xhv'},
  43. {name: 'tube', endpoint: '/rig/111169', status: '', algo: 'cryptonight-heavy', variant: 'tube'}
  44. ];
  45. }
  46. }
  47.  
  48. getStatus();
  49. dummyLogin();
  50.  
  51. function timeStamp() {
  52. let now = new Date(Date.now());
  53. let date = now.toISOString().slice(0, 10);
  54. let time = now.toTimeString().slice(0, 8);
  55. return '[' + date + ' ' + time + ']';
  56. }
  57.  
  58. async function getStatus() {
  59. console.log(timeStamp(), 'Performing status check.');
  60. for (let index in rigAlgos) {
  61. let url = apiBase + rigAlgos[index].endpoint
  62. let status = await sendRequest(url);
  63. if (status !== '') {
  64. rigAlgos[index].status = status;
  65. // console.log(rigAlgos[index].name, rigAlgos[index].status);
  66. } else {
  67. console.log(timeStamp(), 'Error getting status:', rigAlgos[index].name + '.')
  68. fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status: ' + rigAlgos[index].name + '.' + '\r')
  69. }
  70. }
  71. checkRented();
  72. }
  73.  
  74. function sendRequest(options) {
  75. // console.log('Performing send request.')
  76. return new Promise (status => {
  77. request(options, function(error, response, body) {
  78. if (error || (response && response.statusCode !== 200)) {
  79. console.error(timeStamp(), 'Could not check status.', error);
  80. fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status: ' + error + '\r');
  81. status('');
  82. } else if (response && response.statusCode === 200 && body) {
  83. // console.log(body);
  84. let parsed = JSON.parse(body);
  85. // console.log(parsed);
  86. if (parsed.success && parsed.success == true && parsed.data) {
  87. // console.log('Status update success.')
  88. status(parsed.data.status.status);
  89. } else {
  90. console.log(timeStamp(), 'Could not check status. Check 2');
  91. fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status2: ' + error + '\r');
  92. status('');
  93. }
  94. }
  95. });
  96. });
  97. }
  98.  
  99. function checkRented() {
  100. let rented = rigAlgos.some(name => name.status === 'rented');
  101. if (rented) {
  102. let algosToDisable = rigAlgos.filter(name => name.status !== 'rented' && name.status !== 'disabled');
  103. let newStatus = 'disabled';
  104. algosToDisable.forEach(name => updateStatus(name, newStatus));
  105. let rentedAlgo = rigAlgos.find(name => name.status === 'rented');
  106. console.log(timeStamp(), '*****', rentedAlgo.name, 'rented! *****');
  107. return switchMiner(rentedAlgo.name);
  108. } else {
  109. console.log(timeStamp(), 'No algos rented.');
  110. let newStatus = 'enabled';
  111. let algosToEnable = rigAlgos.filter(name => name.status === 'disabled');
  112. algosToEnable.forEach(name => updateStatus(name, newStatus));
  113. return switchMiner(defaultAlgo);
  114. }
  115. }
  116.  
  117. async function updateStatus(name, status) {
  118. let sign_string = apiKey + nonce + name.endpoint;
  119. function signString(stringToSign, sharedSecret) {
  120. let hmac = crypto.createHmac('sha1', sharedSecret);
  121. hmac.update(stringToSign);
  122. hmac.end();
  123. return hmac.read();
  124. }
  125. let signature = signString(sign_string, apiSecret);
  126. let encodeSignature = new Buffer(signature).toString('hex');
  127. let postData = {'status': status};
  128. let options = {
  129. url: apiBase + name.endpoint,
  130. method: 'PUT',
  131. headers: {
  132. 'Content-Type': 'application/json',
  133. 'x-api-sign' : encodeSignature,
  134. 'x-api-key' : apiKey,
  135. 'x-api-nonce' : nonce
  136. },
  137. body: JSON.stringify(postData)
  138. };
  139. let newStatus = await sendRequest(options);
  140. console.log(timeStamp(), 'Setting', name.name, 'to', status, '=>', newStatus);
  141. }
  142.  
  143. function switchMiner(nextMiner) {
  144. if (minerRunning) {
  145. if (currentMiner !== nextMiner) {
  146. console.log(timeStamp(), 'Miner running:', currentMiner)
  147. minerRunning.on('close', function() {
  148. minerRunning = startMiner(nextMiner);
  149. });
  150. tree_kill(minerRunning.pid);
  151. }
  152. } else {
  153. minerRunning = startMiner(nextMiner);
  154. }
  155. }
  156.  
  157. function startMiner(newMiner) {
  158. if (currentMiner !== newMiner) {
  159. currentMiner = newMiner;
  160. console.log(timeStamp(), 'Starting', newMiner, 'miner.');
  161. if (newMiner === 'MO') {
  162. let runMiner = child_process.spawn('cmd.exe', ['/c', minerArg+newMiner+'.bat'], {stdio: 'inherit'});
  163. // runMiner.stdout.on('data', (data) => {
  164. // console.log(data.toString());
  165. // });
  166. // runMiner.stderr.on('data', (data) => {
  167. // console.log(data.toString());
  168. // });
  169. runMiner.on('close', (code) => {
  170. console.log(timeStamp(), `Miner closed with code ${code}`);
  171. });
  172. return runMiner;
  173. } else {
  174. let find = rigAlgos.find(name => name.name === newMiner);
  175. let name = find.name;
  176. let runMiner = child_process.spawn('cmd.exe', ['/c', minerArg+name+'.bat'], {stdio: 'inherit'});
  177. // runMiner.stdout.on('data', (data) => {
  178. // console.log(data.toString());
  179. // });
  180. // runMiner.stderr.on('data', (data) => {
  181. // console.log(data.toString());
  182. // });
  183. runMiner.on('close', (code) => {
  184. console.log(timeStamp(), `Miner closed with code ${code}`);
  185. });
  186. return runMiner;
  187. }
  188. }
  189. }
  190.  
  191. function tree_kill(pid, signal, callback) {
  192. let tree = {};
  193. let pidsToProcess = {};
  194. tree[pid] = [];
  195. pidsToProcess[pid] = 1;
  196. if (typeof signal === 'function' && callback === undefined) {
  197. callback = signal;
  198. signal = undefined;
  199. }
  200. console.log(timeStamp(), 'Stopping', currentMiner, pid)
  201. child_process.exec('taskkill /pid ' + pid + ' /T /F', {}, callback);
  202. };
  203.  
  204. async function dummyLogin() {
  205. console.log(timeStamp(), 'Performing dummy login.');
  206. let hosts = ['us-east01.miningrigrentals.com', 'us-central01.miningrigrentals.com', 'us-west01.miningrigrentals.com'];
  207. let currHostIndex = 0;
  208. let port = '3333';
  209. let pass = minerArg+'Dummy';
  210. for (let index in rigAlgos) {
  211. let user = 'Bathmat.'+rigAlgos[index].endpoint.slice(5);
  212. let login = {"id": 1, "jsonrpc": "2.0", "method": "login", "params": {"login": user, "pass": pass, "agent": "Miner/v1"}};
  213. let success = false;
  214. while (!success) {
  215. success = await attemptConnection(hosts[currHostIndex], port, login);
  216. if (!success) {
  217. if (currHostIndex < hosts.length - 1) {
  218. currHostIndex++;
  219. } else {
  220. setTimeout(dummyLogin, 5*60*1000);
  221. }
  222. }
  223. }
  224. }
  225. }
  226.  
  227. function attemptConnection(host, port, login) {
  228. return new Promise (success => {
  229. let poolSocket = net.connect(port, host).on('connect', function() {
  230. poolSocket.write(JSON.stringify(login));
  231. success(true);
  232. }).on('error', function() {
  233. console.log(timeStamp(), 'Error performing dummy login to:', host+'.', 'Trying backup server.');
  234. fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error performing dummy login to: ' + host+'. ' + 'Trying backup server.' + '\r');
  235. success(false);
  236. });
  237. });
  238. }
  239.  
  240. setInterval(dummyLogin, 9*60*1000);
  241. setInterval(getStatus, 3*60*1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement