Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- const fs = require('fs');
- const net = require('net');
- const crypto = require('crypto');
- const request = require('request');
- const child_process = require('child_process');
- const apiKey = 'redacted';
- const apiSecret= 'redacted';
- const apiBase = 'https://www.miningrigrentals.com/api/v2';
- let rigAlgos = [
- {name: 'v8', endpoint: '/rig/108229', status: '', algo: 'cryptonight', variant: '2'},
- {name: 'heavy', endpoint: '/rig/108803', status: '', algo: 'cryptonight-heavy', variant: '0'},
- {name: 'lite', endpoint: '/rig/108809', status: '', algo: 'cryptonight-lite', variant: '1'},
- {name: 'fast', endpoint: '/rig/108810', status: '', algo: 'cryptonight', variant: 'msr'},
- {name: 'v7', endpoint: '/rig/109065', status: '', algo: 'cryptonight', variant: '1'},
- {name: 'xtl', endpoint: '/rig/109702', status: '', algo: 'cryptonight', variant: 'xtl'},
- {name: 'xhv', endpoint: '/rig/109744', status: '', algo: 'cryptonight-heavy', variant: 'xhv'},
- {name: 'tube', endpoint: '/rig/109751', status: '', algo: 'cryptonight-heavy', variant: 'tube'},
- {name: 'xfh', endpoint: '/rig/110803', status: '', algo: 'cryptonight', variant: 'xfh'},
- {name: 'mox', endpoint: '/rig/111386', status: '', algo: 'cryptonight', variant: 'red'}
- ];
- let nonce = Math.floor(Date.now());
- let logFile = 'log-MRR.txt';
- let defaultAlgo = 'MO';
- let minerRunning = null;
- let currentMiner = null;
- let minerArg = '';
- if (process.argv[2]) {
- minerArg = process.argv[2];
- logFile = 'log-MRR-' + process.argv[2] + '.txt';
- if (minerArg === '8gb') {
- rigAlgos = [
- {name: 'v8', endpoint: '/rig/108767', status: '', algo: 'cryptonight', variant: '2'},
- {name: 'heavy', endpoint: '/rig/109674', status: '', algo: 'cryptonight-heavy', variant: '0'},
- {name: 'lite', endpoint: '/rig/111166', status: '', algo: 'cryptonight-lite', variant: '1'},
- {name: 'fast', endpoint: '/rig/111165', status: '', algo: 'cryptonight', variant: 'msr'},
- {name: 'v7', endpoint: '/rig/111170', status: '', algo: 'cryptonight', variant: '1'},
- {name: 'xtl', endpoint: '/rig/111172', status: '', algo: 'cryptonight', variant: 'xtl'},
- {name: 'xhv', endpoint: '/rig/111168', status: '', algo: 'cryptonight-heavy', variant: 'xhv'},
- {name: 'tube', endpoint: '/rig/111169', status: '', algo: 'cryptonight-heavy', variant: 'tube'}
- ];
- }
- }
- getStatus();
- dummyLogin();
- function timeStamp() {
- let now = new Date(Date.now());
- let date = now.toISOString().slice(0, 10);
- let time = now.toTimeString().slice(0, 8);
- return '[' + date + ' ' + time + ']';
- }
- async function getStatus() {
- console.log(timeStamp(), 'Performing status check.');
- for (let index in rigAlgos) {
- let url = apiBase + rigAlgos[index].endpoint
- let status = await sendRequest(url);
- if (status !== '') {
- rigAlgos[index].status = status;
- // console.log(rigAlgos[index].name, rigAlgos[index].status);
- } else {
- console.log(timeStamp(), 'Error getting status:', rigAlgos[index].name + '.')
- fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status: ' + rigAlgos[index].name + '.' + '\r')
- }
- }
- checkRented();
- }
- function sendRequest(options) {
- // console.log('Performing send request.')
- return new Promise (status => {
- request(options, function(error, response, body) {
- if (error || (response && response.statusCode !== 200)) {
- console.error(timeStamp(), 'Could not check status.', error);
- fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status: ' + error + '\r');
- status('');
- } else if (response && response.statusCode === 200 && body) {
- // console.log(body);
- let parsed = JSON.parse(body);
- // console.log(parsed);
- if (parsed.success && parsed.success == true && parsed.data) {
- // console.log('Status update success.')
- status(parsed.data.status.status);
- } else {
- console.log(timeStamp(), 'Could not check status. Check 2');
- fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error getting status2: ' + error + '\r');
- status('');
- }
- }
- });
- });
- }
- function checkRented() {
- let rented = rigAlgos.some(name => name.status === 'rented');
- if (rented) {
- let algosToDisable = rigAlgos.filter(name => name.status !== 'rented' && name.status !== 'disabled');
- let newStatus = 'disabled';
- algosToDisable.forEach(name => updateStatus(name, newStatus));
- let rentedAlgo = rigAlgos.find(name => name.status === 'rented');
- console.log(timeStamp(), '*****', rentedAlgo.name, 'rented! *****');
- return switchMiner(rentedAlgo.name);
- } else {
- console.log(timeStamp(), 'No algos rented.');
- let newStatus = 'enabled';
- let algosToEnable = rigAlgos.filter(name => name.status === 'disabled');
- algosToEnable.forEach(name => updateStatus(name, newStatus));
- return switchMiner(defaultAlgo);
- }
- }
- async function updateStatus(name, status) {
- let sign_string = apiKey + nonce + name.endpoint;
- function signString(stringToSign, sharedSecret) {
- let hmac = crypto.createHmac('sha1', sharedSecret);
- hmac.update(stringToSign);
- hmac.end();
- return hmac.read();
- }
- let signature = signString(sign_string, apiSecret);
- let encodeSignature = new Buffer(signature).toString('hex');
- let postData = {'status': status};
- let options = {
- url: apiBase + name.endpoint,
- method: 'PUT',
- headers: {
- 'Content-Type': 'application/json',
- 'x-api-sign' : encodeSignature,
- 'x-api-key' : apiKey,
- 'x-api-nonce' : nonce
- },
- body: JSON.stringify(postData)
- };
- let newStatus = await sendRequest(options);
- console.log(timeStamp(), 'Setting', name.name, 'to', status, '=>', newStatus);
- }
- function switchMiner(nextMiner) {
- if (minerRunning) {
- if (currentMiner !== nextMiner) {
- console.log(timeStamp(), 'Miner running:', currentMiner)
- minerRunning.on('close', function() {
- minerRunning = startMiner(nextMiner);
- });
- tree_kill(minerRunning.pid);
- }
- } else {
- minerRunning = startMiner(nextMiner);
- }
- }
- function startMiner(newMiner) {
- if (currentMiner !== newMiner) {
- currentMiner = newMiner;
- console.log(timeStamp(), 'Starting', newMiner, 'miner.');
- if (newMiner === 'MO') {
- let runMiner = child_process.spawn('cmd.exe', ['/c', minerArg+newMiner+'.bat'], {stdio: 'inherit'});
- // runMiner.stdout.on('data', (data) => {
- // console.log(data.toString());
- // });
- // runMiner.stderr.on('data', (data) => {
- // console.log(data.toString());
- // });
- runMiner.on('close', (code) => {
- console.log(timeStamp(), `Miner closed with code ${code}`);
- });
- return runMiner;
- } else {
- let find = rigAlgos.find(name => name.name === newMiner);
- let name = find.name;
- let runMiner = child_process.spawn('cmd.exe', ['/c', minerArg+name+'.bat'], {stdio: 'inherit'});
- // runMiner.stdout.on('data', (data) => {
- // console.log(data.toString());
- // });
- // runMiner.stderr.on('data', (data) => {
- // console.log(data.toString());
- // });
- runMiner.on('close', (code) => {
- console.log(timeStamp(), `Miner closed with code ${code}`);
- });
- return runMiner;
- }
- }
- }
- function tree_kill(pid, signal, callback) {
- let tree = {};
- let pidsToProcess = {};
- tree[pid] = [];
- pidsToProcess[pid] = 1;
- if (typeof signal === 'function' && callback === undefined) {
- callback = signal;
- signal = undefined;
- }
- console.log(timeStamp(), 'Stopping', currentMiner, pid)
- child_process.exec('taskkill /pid ' + pid + ' /T /F', {}, callback);
- };
- async function dummyLogin() {
- console.log(timeStamp(), 'Performing dummy login.');
- let hosts = ['us-east01.miningrigrentals.com', 'us-central01.miningrigrentals.com', 'us-west01.miningrigrentals.com'];
- let currHostIndex = 0;
- let port = '3333';
- let pass = minerArg+'Dummy';
- for (let index in rigAlgos) {
- let user = 'Bathmat.'+rigAlgos[index].endpoint.slice(5);
- let login = {"id": 1, "jsonrpc": "2.0", "method": "login", "params": {"login": user, "pass": pass, "agent": "Miner/v1"}};
- let success = false;
- while (!success) {
- success = await attemptConnection(hosts[currHostIndex], port, login);
- if (!success) {
- if (currHostIndex < hosts.length - 1) {
- currHostIndex++;
- } else {
- setTimeout(dummyLogin, 5*60*1000);
- }
- }
- }
- }
- }
- function attemptConnection(host, port, login) {
- return new Promise (success => {
- let poolSocket = net.connect(port, host).on('connect', function() {
- poolSocket.write(JSON.stringify(login));
- success(true);
- }).on('error', function() {
- console.log(timeStamp(), 'Error performing dummy login to:', host+'.', 'Trying backup server.');
- fs.appendFileSync(logFile, timeStamp() + ' ' + 'Error performing dummy login to: ' + host+'. ' + 'Trying backup server.' + '\r');
- success(false);
- });
- });
- }
- setInterval(dummyLogin, 9*60*1000);
- setInterval(getStatus, 3*60*1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement