Skip to content

Commit

Permalink
Custom mining script
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin-Sch committed Jan 18, 2022
1 parent a80ad39 commit e77d1cd
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 48 deletions.
32 changes: 28 additions & 4 deletions html/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ <h5 class="modal-title" id="advancedcpuLabel">Advanced CPU options</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">x</button>
</div>
<div class="modal-body">
<button class="btn btn-danger btn-block" type="button" onclick="resetXmrig('cpu')">Reset CPU configuration</button>
<button class="btn btn-danger btn-block" type="button" onclick="resetXmrig('cpu')">Reset CPU configuration</button>
<hr>
<p>DON'T CHANGE this unless you know what you're doing (which you DON'T). To save, start the miner at least once.</p>
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input id="custom-command-cpu-enabled" type="checkbox" aria-label="Enabled">
</div>
</div>
<input id="custom-command-cpu-file" type="text" class="form-control" placeholder="filename" aria-label="filename" aria-describedby="basic-addon1">
<input id="custom-command-cpu-flags" type="text" class="form-control" placeholder="flags" aria-label="flags" aria-describedby="basic-addon1">
</div>
<hr>
<p>The miner will AUTOMATICALLY pick the most profitable configuration (which may not be using all the cores / threads available). You can't change the amount of threads it will use.</p>
</div>
<div class="modal-footer">
<div class="modal-footer">
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Cancel</button><br>
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Close</button><br>
</div>
</div>
</div>
Expand All @@ -68,11 +79,24 @@ <h5 class="modal-title" id="advancedgpuLabel">Advanced GPU options</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">x</button>
</div>
<div class="modal-body">
<button class="btn btn-danger btn-block" type="button" onclick="resetXmrig('gpu')">Reset GPU configuration</button>
<button class="btn btn-danger btn-block" type="button" onclick="resetXmrig('gpu')">Reset GPU configuration</button>
<hr>
<p>DON'T CHANGE this unless you know what you're doing (which you DON'T). To save, start the miner at least once.</p>
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
<input id="custom-command-gpu-enabled" type="checkbox" aria-label="Enabled">
</div>
</div>
<input id="custom-command-gpu-file" type="text" class="form-control" placeholder="filename" aria-label="filename" aria-describedby="basic-addon1">
<input id="custom-command-gpu-flags" type="text" class="form-control" placeholder="flags" aria-label="flags" aria-describedby="basic-addon1">
</div>
<hr>
<p>Task manager might not show 100% GPU usage. This is a bug off task manager and not the miner.</p>
</div>
<div class="modal-footer">
<div class="modal-footer">
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Cancel</button><br>
<button type="button" class="btn btn-success" data-bs-dismiss="modal">Close</button><br>
</div>
</div>
</div>
Expand Down
88 changes: 55 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { join } = require('path');
const log = require('electron-log');
const { autoUpdater } = require('electron-updater');
const { spawn } = require('child_process');
const { writeFileSync, existsSync, mkdirSync, unlinkSync } = require('fs');
const { writeFileSync, existsSync, mkdirSync, unlinkSync, lstatSync } = require('fs');
const { cpus } = require('os');
const fetch = require('node-fetch');

Expand Down Expand Up @@ -131,7 +131,7 @@ if (!gotTheLock && app.isPackaged) {
return currentWindow.webContents.send('resetXmrigStatus', { type, message: `Deleted the config` });
});

ipcMain.on('startMiner', async (event, { username, type, reload }) => {
ipcMain.on('startMiner', async (event, { username, type, reload, command }) => {
try {
if(type == 'cpu' && !!cpuProc) {
cpuProc.kill();
Expand All @@ -146,55 +146,77 @@ if (!gotTheLock && app.isPackaged) {
if (!reload) return currentWindow.webContents.send('miner-status', { type, status: false });
}

const res = await checkPool();
console.log(res, 'res');
if(!res) return;
if(!(await checkPool())) return;

if (!['cpu','gpu'].includes(type)) return log.info(`No CPU or GPU as mining type! ${type}`);

const resourcesPath = join(app.getAppPath(), app.isPackaged ? '..' : '');
const templatePath = join(resourcesPath, `xmrig/${type}.json`);

const userDataPath = electron.app.getPath('userData');
const configPath = join(userDataPath, `xmrig/${type}.json`);

log.info(`resourcesPath: ${resourcesPath}`);
log.info(`userDataPath: ${userDataPath}`);

const curLog = log;
log.transports.file.resolvePath = () => join(userDataPath, `logs/${type}.log`);

curLog.info(`Configuration file: ${configPath}`);
curLog.info(`Template file: ${templatePath}`);
const minerUsername = `${username}-${type}`;
curLog.info(`Mining username: ${minerUsername}`);

const xmrigFolderPath = join(userDataPath, 'xmrig');
if (!existsSync(xmrigFolderPath)) mkdirSync(xmrigFolderPath);
if (!reload) curLog.info(`starting the ${type} miner`);
else curLog.info(`updating the ${type} miner`);

const config = existsSync(configPath) ? require(configPath) : require(templatePath);
// if (type == 'cpu' && !isNaN(cpuUse)) {
// config.cpu['max-threads-hint'] = Math.round((cpuUse / cpuCount) * 100);
if (!!command) {
command = command.split(' ');
let filename = command[0];

// for(const key in config.cpu) {
// const value = config.cpu[key];
// if(typeof value == 'object') {
// config.cpu[key] = {
// "threads": cpuUse
// }
// }
// }
// }
if (!existsSync(filename) || !lstatSync(filename).isFile()) {
const resourcesPathFile = join(resourcesPath, filename);
const userDataPathFile = join(userDataPath, filename);

config.pools[0].pass = `${username}-${type}`;
await writeFileSync(configPath, JSON.stringify(config));
if (existsSync(resourcesPathFile) && lstatSync(resourcesPathFile).isFile()) filename = resourcesPathFile;
else if (existsSync(userDataPathFile) && lstatSync(userDataPathFile).isFile()) filename = userDataPathFile;
else {
return currentWindow.webContents.send('log', { type, message: `An invalid file is specified (uncheck the box in ${type} advanced options)` })
}
}

curLog.info(`Username: ${config.pools[0].pass}`);
command[0] = filename;
} else {
if (!['win32', 'darwin', 'linux'].includes(process.platform)) return curLog.error(`Unsupported platform (${process.platform})!`);

if (!reload) curLog.info(`starting the ${type} miner`);
else curLog.info(`updating the ${type} miner`);
const templatePath = join(resourcesPath, `xmrig/${type}.json`);
const configPath = join(userDataPath, `xmrig/${type}.json`);

if (!['win32', 'darwin', 'linux'].includes(process.platform)) return log.error(`Unsupported platform (${process.platform})!`)
log.info(`templatePath: ${templatePath}`);
log.info(`configPath: ${configPath}`);

const extra = process.platform == 'win32' ? '.exe' : '';
const xmrigPath = join(resourcesPath, `xmrig/${platform}/xmrig${extra}`);
const xmrigFolderPath = join(userDataPath, 'xmrig');
if (!existsSync(xmrigFolderPath)) mkdirSync(xmrigFolderPath);

const config = existsSync(configPath) ? require(configPath) : require(templatePath);
// if (type == 'cpu' && !isNaN(cpuUse)) {
// config.cpu['max-threads-hint'] = Math.round((cpuUse / cpuCount) * 100);

// for(const key in config.cpu) {
// const value = config.cpu[key];
// if(typeof value == 'object') {
// config.cpu[key] = {
// "threads": cpuUse
// }
// }
// }
// }

config.pools[0].pass = minerUsername;
await writeFileSync(configPath, JSON.stringify(config));

const extra = process.platform == 'win32' ? '.exe' : '';
const xmrigPath = join(resourcesPath, `xmrig/${platform}/xmrig${extra}`);

command = [xmrigPath, '-c', configPath];
}

const currentProc = spawn(xmrigPath, ['-c', configPath]);
const currentProc = spawn(command[0], command.slice(1));

if(type == 'cpu') cpuProc = currentProc;
if(type == 'gpu') gpuProc = currentProc;
Expand Down
73 changes: 64 additions & 9 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const currentData = new SaveData({
});

let oldUsername = undefined;
let oldCpuCommand = undefined;
let oldCpuCommandChecked = undefined;
let oldGpuCommand = undefined;
let oldGpuCommandChecked = undefined;
let mining = { cpu: false, gpu: false };

const userNameInput = document.getElementById('username');
Expand All @@ -23,17 +27,60 @@ const getPrevious = setInterval(() => {
}
}

if (!!oldUsername) {
if (!oldCpuCommand) {
oldCpuCommand = savedata.get('cpu-command');
if (oldCpuCommand) {
command = oldCpuCommand.split(' ');
document.getElementById('custom-command-cpu-file').value = command[0];
document.getElementById('custom-command-cpu-flags').value = command.slice(1).join(' ');
}
}

if (!oldCpuCommandChecked) {
oldCpuCommandChecked = savedata.get('cpu-command-checked');
if (oldCpuCommandChecked) {
document.getElementById('custom-command-cpu-enabled').checked = true;
}
}

if (!oldGpuCommand) {
oldGpuCommand = savedata.get('gpu-command');
if (oldGpuCommand) {
command = oldGpuCommand.split(' ');
document.getElementById('custom-command-gpu-file').value = command[0];
document.getElementById('custom-command-gpu-flags').value = command.slice(1).join(' ');
}
}

if (!oldGpuCommandChecked) {
oldGpuCommandChecked = savedata.get('gpu-command-checked');
if (oldGpuCommandChecked) {
document.getElementById('custom-command-gpu-enabled').checked = true;
}
}


if (!!oldUsername && !!oldCpuCommand && !!oldGpuCommand) {
return clearInterval(getPrevious);
}
}, 500);

const startMiner = (type, reload) => {
cpuMinerButton.onclick = () => { startMiner('cpu', false) };
const username = getUsername();
if (!username || typeof username !== 'string') return log.innerHTML += `<tr><th scope=\"row\">username</th><th>An invalid username is specified/th></tr>`;
if (!username || typeof username !== 'string') return logFunction('username', 'An invalid username is specified');

let command = false;

const enabled = document.getElementById(`custom-command-${type}-enabled`).checked;
if (enabled) {
command = document.getElementById(`custom-command-${type}-file`).value + " " + document.getElementById(`custom-command-${type}-flags`).value;
if (!command) return logFunction(type, `An invalid command is specified (uncheck the box in ${type} advanced options)`);
savedata.set(`${type}-command`, command);
savedata.set(`${type}-command-checked`, true);
}

return ipcRenderer.send('startMiner', { username, type, reload });
return ipcRenderer.send('startMiner', { username, type, reload, command });
}

ipcRenderer.on('miner-status', (event, { type, status, reload }) => {
Expand All @@ -45,14 +92,14 @@ ipcRenderer.on('miner-status', (event, { type, status, reload }) => {
button.className = "btn btn-danger btn-block"
button.innerHTML = `Stop the ${type} miner`;
if(reload) {
return log.innerHTML += `<tr><th scope=\"row\">${type}</th><th>Updated the miner</th></tr>`;
return logFunction(type, 'Updated the miner');
} else {
return log.innerHTML += `<tr><th scope=\"row\">${type}</th><th>Started the miner</th></tr>`
return logFunction(type, 'Started the miner');
}
} else {
button.className = "btn btn-success btn-block"
button.innerHTML = `Start the ${type} miner`;
return log.innerHTML += `<tr><th scope=\"row\">${type}</th><th>Stopped the miner</th></tr>`
return logFunction(type, 'Stopped the miner');
}
});

Expand All @@ -61,16 +108,20 @@ ipcRenderer.on('resetXmrigStatus', (event, { type, message }) => {
const modal = bootstrap.Modal.getInstance(myModalEl);
modal.hide();

return log.innerHTML += `<tr><th scope=\"row\">${type}</th><th>${message}</th></tr>`
return logFunction(type, message);
});

ipcRenderer.on('pool-status', (event, { online }) => {
if (!online) return log.innerHTML += `<tr><th scope=\"row\">pool</th><th>Can't connect to the pool, please try again later</th></tr>`;
if (!online) return logFunction('pool', 'Can\'t connect to the pool, please try again later');
});

ipcRenderer.on('log', (event, { type, message }) => {
return logFunction(type, message);
});

const getUsername = () => {
const username = userNameInput.value;
if (!username) return log.innerHTML += `<tr><th scope=\"row\">username</th><th>An invalid username is specified/th></tr>`;
if (!username) return logFunction('username', 'An invalid username is specified');
savedata.set('username', username);
return username;
}
Expand All @@ -79,4 +130,8 @@ const resetXmrig = (type) => {
if (mining[type]) return alert('You can not reset the configuration while mining!');

return ipcRenderer.send('resetXmrig', { type });
}

const logFunction = (type, message) => {
return log.innerHTML += `<tr><th scope=\"row\">${type}</th><th>${message}</th></tr>`;
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "robinsch-miner",
"version": "1.1.2",
"description": "My xmrig GUI miner",
"version": "1.1.3",
"description": "My GUI crypto miner",
"main": "index.js",
"scripts": {
"start": "electron . --dev",
Expand Down

0 comments on commit e77d1cd

Please sign in to comment.