From 5ae24e777c9c214f5ba921dd8c18d6cf1851d603 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Tue, 28 Mar 2017 08:00:16 +0300 Subject: [PATCH] fixed shepherd config bug --- gui/loading.js | 6 +- routes/shepherd.js | 987 +++++++++++++++++++++++---------------------- 2 files changed, 498 insertions(+), 495 deletions(-) diff --git a/gui/loading.js b/gui/loading.js index cf1927f..8815cfb 100644 --- a/gui/loading.js +++ b/gui/loading.js @@ -1,10 +1,10 @@ -function IguanaAJAX(url, ajax_data) { +function IguanaAJAX(url, ajax_data, timeout) { return $.ajax({ data: JSON.stringify(ajax_data), url: url, type: 'POST', dataType: 'json', - timeout: 120000 + timeout: timeout ? timeout : 120000 //beforeSend: showLoadingImgFn }) .fail(function(xhr, textStatus, error) { @@ -125,7 +125,7 @@ function EDEX_DEXgetinfoAll(skip, minNotaries) { 'method': 'notarychains' }, tmp_index = 0, - get_dex_notarychains = IguanaAJAX('http://127.0.0.1:' + config.iguanaPort, ajax_data).done(function(data) { + get_dex_notarychains = IguanaAJAX('http://127.0.0.1:' + config.iguanaPort, ajax_data, 10000).done(function(data) { get_dex_notarychains = JSON.parse(get_dex_notarychains.responseText); if (minNotaries > get_dex_notarychains.length) { // if config value exceeds total num of notaries minNotaries = get_dex_notarychains.length; diff --git a/routes/shepherd.js b/routes/shepherd.js index 2813504..8201481 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -6,7 +6,7 @@ const electron = require('electron'), os = require('os'), fsnode = require('fs'), fs = require('fs-extra'), - _fs = require('graceful-fs'), + _fs = require('graceful-fs'), mkdirp = require('mkdirp'), express = require('express'), exec = require('child_process').exec, @@ -31,45 +31,45 @@ var iguanaConfsDirSrc = path.join(__dirname, '../assets/deps/confs'), // SETTING OS DIR TO RUN IGUANA FROM // SETTING APP ICON FOR LINUX AND WINDOWS if (os.platform() === 'darwin') { - fixPath(); - var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'), - iguanaDir = process.env.HOME + '/Library/Application Support/iguana', - iguanaConfsDir = iguanaDir + '/confs', - komododBin = path.join(__dirname, '../assets/bin/osx/komodod'), - komodocliBin = path.join(__dirname, '../assets/bin/osx/komodo-cli'), - komodoDir = process.env.HOME + '/Library/Application Support/Komodo'; - - zcashdBin = '/Applications/ZCashSwingWalletUI.app/Contents/MacOS/zcashd', - zcashcliBin = '/Applications/ZCashSwingWalletUI.app/Contents/MacOS/zcash-cli', - zcashDir = process.env.HOME + '/Library/Application Support/Zcash'; + fixPath(); + var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'), + iguanaDir = process.env.HOME + '/Library/Application Support/iguana', + iguanaConfsDir = iguanaDir + '/confs', + komododBin = path.join(__dirname, '../assets/bin/osx/komodod'), + komodocliBin = path.join(__dirname, '../assets/bin/osx/komodo-cli'), + komodoDir = process.env.HOME + '/Library/Application Support/Komodo'; + + zcashdBin = '/Applications/ZCashSwingWalletUI.app/Contents/MacOS/zcashd', + zcashcliBin = '/Applications/ZCashSwingWalletUI.app/Contents/MacOS/zcash-cli', + zcashDir = process.env.HOME + '/Library/Application Support/Zcash'; } if (os.platform() === 'linux') { - var iguanaBin = path.join(__dirname, '../assets/bin/linux64/iguana'), - iguanaDir = process.env.HOME + '/.iguana', - iguanaConfsDir = iguanaDir + '/confs', - iguanaIcon = path.join(__dirname, '/assets/icons/agama_icons/128x128.png'), - komododBin = path.join(__dirname, '../assets/bin/linux64/komodod'), - komodocliBin = path.join(__dirname, '../assets/bin/linux64/komodo-cli'), - komodoDir = process.env.HOME + '/.komodo'; + var iguanaBin = path.join(__dirname, '../assets/bin/linux64/iguana'), + iguanaDir = process.env.HOME + '/.iguana', + iguanaConfsDir = iguanaDir + '/confs', + iguanaIcon = path.join(__dirname, '/assets/icons/agama_icons/128x128.png'), + komododBin = path.join(__dirname, '../assets/bin/linux64/komodod'), + komodocliBin = path.join(__dirname, '../assets/bin/linux64/komodo-cli'), + komodoDir = process.env.HOME + '/.komodo'; } if (os.platform() === 'win32') { - var iguanaBin = path.join(__dirname, '../assets/bin/win64/iguana.exe'); - iguanaBin = path.normalize(iguanaBin); - iguanaDir = process.env.APPDATA + '/iguana'; - iguanaDir = path.normalize(iguanaDir); - iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; - iguanaConfsDir = path.normalize(iguanaConfsDir); - iguanaIcon = path.join(__dirname, '/assets/icons/agama_icons/agama_app_icon.ico'), - iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); - - komododBin = path.join(__dirname, '../assets/bin/win64/komodod.exe'), - komododBin = path.normalize(komododBin), - komodocliBin = path.join(__dirname, '../assets/bin/win64/komodo-cli.exe'), - komodocliBin = path.normalize(komodocliBin), - komodoDir = process.env.APPDATA + '/Komodo', - komodoDir = path.normalize(komodoDir); + var iguanaBin = path.join(__dirname, '../assets/bin/win64/iguana.exe'); + iguanaBin = path.normalize(iguanaBin); + iguanaDir = process.env.APPDATA + '/iguana'; + iguanaDir = path.normalize(iguanaDir); + iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; + iguanaConfsDir = path.normalize(iguanaConfsDir); + iguanaIcon = path.join(__dirname, '/assets/icons/agama_icons/agama_app_icon.ico'), + iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); + + komododBin = path.join(__dirname, '../assets/bin/win64/komodod.exe'), + komododBin = path.normalize(komododBin), + komodocliBin = path.join(__dirname, '../assets/bin/win64/komodo-cli.exe'), + komodocliBin = path.normalize(komodocliBin), + komodoDir = process.env.APPDATA + '/Komodo', + komodoDir = path.normalize(komodoDir); } shepherd.appConfig = { @@ -87,6 +87,49 @@ shepherd.appConfig = { } }; +shepherd.saveLocalAppConf = function(appSettings) { + var appConfFileName = iguanaDir + '/config.json'; + + var FixFilePermissions = function() { + return new Promise(function(resolve, reject) { + var result = 'config.json file permissions updated to Read/Write'; + + fsnode.chmodSync(appConfFileName, '0666'); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 1000); + }); + } + + var FsWrite = function() { + return new Promise(function(resolve, reject) { + var result = 'config.json write file is done' + + fs.writeFile(appConfFileName, + JSON.stringify(appSettings) + .replace(/,/g, ',\n') // format json in human readable form + .replace(/:/g, ': ') + .replace(/{/g, '{\n') + .replace(/}/g, '\n}'), 'utf8', function(err) { + if (err) + return console.log(err); + }); + + fsnode.chmodSync(appConfFileName, '0666'); + setTimeout(function() { + console.log(result); + console.log('app conf.json file is created successfully at: ' + iguanaConfsDir); + resolve(result); + }, 2000); + }); + } + + FsWrite() + .then(FixFilePermissions()); +} + shepherd.loadLocalConfig = function() { if (fs.existsSync(iguanaDir + '/config.json')) { var localAppConfig = fs.readFileSync(iguanaDir + '/config.json', 'utf8'); @@ -106,20 +149,23 @@ shepherd.loadLocalConfig = function() { return result; }; - var compareConfigs = compareJSON(shepherd.appConfig, JSON.parse(localAppConfig)); - if (Object.keys(compareConfigs).length) { - var newConfig = Object.assign(JSON.parse(localAppConfig), compareConfigs); + if (localAppConfig) { + var compareConfigs = compareJSON(shepherd.appConfig, JSON.parse(localAppConfig)); + if (Object.keys(compareConfigs).length) { + var newConfig = Object.assign(JSON.parse(localAppConfig), compareConfigs); - console.log('config diff is found, updating local config'); - console.log('config diff:'); - console.log(compareConfigs); + console.log('config diff is found, updating local config'); + console.log('config diff:'); + console.log(compareConfigs); - shepherd.saveLocalAppConf(newConfig); - return newConfig; + shepherd.saveLocalAppConf(newConfig); + return newConfig; + } else { + return JSON.parse(localAppConfig); + } } else { - return JSON.parse(localAppConfig); + return shepherd.appConfig; } - } else { console.log('local config file is not found!'); shepherd.saveLocalAppConf(shepherd.appConfig); @@ -156,8 +202,8 @@ var mock = require('./mock'); // expose sockets obj shepherd.setIO = function(io) { - shepherd.io = io; - cache.setVar('io', io); + shepherd.io = io; + cache.setVar('io', io); }; cache.setVar('iguanaDir', iguanaDir); @@ -168,7 +214,7 @@ cache.setVar('appConfig', shepherd.appConfig); * params: pubkey */ shepherd.get('/cache', function(req, res, next) { - cache.get(req, res, next); + cache.get(req, res, next); }); /* @@ -176,7 +222,7 @@ shepherd.get('/cache', function(req, res, next) { * params: filename */ shepherd.get('/groom', function(req, res, next) { - cache.groomGet(req, res, next); + cache.groomGet(req, res, next); }) /* @@ -184,7 +230,7 @@ shepherd.get('/groom', function(req, res, next) { * params: filename */ shepherd.delete('/groom', function(req, res, next) { - cache.groomDelete(req, res, next); + cache.groomDelete(req, res, next); }); /* @@ -192,7 +238,7 @@ shepherd.delete('/groom', function(req, res, next) { * params: filename, payload */ shepherd.post('/groom', function(req, res) { - cache.groomPost(req, res, next); + cache.groomPost(req, res, next); }); /* @@ -200,7 +246,7 @@ shepherd.post('/groom', function(req, res) { * params: userpass, pubkey, skip */ shepherd.get('/cache-all', function(req, res, next) { - cache.all(req, res, next); + cache.all(req, res, next); }); /* @@ -208,19 +254,19 @@ shepherd.get('/cache-all', function(req, res, next) { * params: userpass, pubkey, coin, address, skip */ shepherd.get('/cache-one', function(req, res, next) { - cache.one(req, res, next); + cache.one(req, res, next); }); /* * type: GET */ shepherd.get('/mock', function(req, res, next) { - mock.get(req, res, next); + mock.get(req, res, next); }); /* - * type: GET - * params: herd, lastLines + * type: GET + * params: herd, lastLines */ shepherd.post('/debuglog', function(req, res) { var _herd = req.body.herdname, @@ -252,8 +298,8 @@ shepherd.post('/debuglog', function(req, res) { }); /* - * type: POST - * params: herd + * type: POST + * params: herd */ shepherd.post('/herd', function(req, res) { console.log('======= req.body ======='); @@ -273,8 +319,8 @@ shepherd.post('/herd', function(req, res) { }); /* - * type: POST - * params: herdname + * type: POST + * params: herdname */ shepherd.post('/herdlist', function(req, res) { //console.log('======= req.body ======='); @@ -306,7 +352,7 @@ shepherd.post('/herdlist', function(req, res) { }); /* - * type: POST + * type: POST */ shepherd.post('/slay', function(req, res) { console.log('======= req.body ======='); @@ -324,7 +370,7 @@ shepherd.post('/slay', function(req, res) { }); /* - * type: POST + * type: POST */ shepherd.post('/setconf', function(req, res) { console.log('======= req.body ======='); @@ -333,9 +379,9 @@ shepherd.post('/setconf', function(req, res) { //console.log(req.body.chain); if (os.platform() === 'win32' && req.body.chain == 'komodod') { - setkomodoconf = spawn(path.join(__dirname, '../assets/bin/win64/genkmdconf.bat')); + setkomodoconf = spawn(path.join(__dirname, '../assets/bin/win64/genkmdconf.bat')); } else { - setConf(req.body.chain); + setConf(req.body.chain); } @@ -348,7 +394,7 @@ shepherd.post('/setconf', function(req, res) { }); /* - * type: POST + * type: POST */ shepherd.post('/getconf', function(req, res) { console.log('======= req.body ======='); @@ -368,14 +414,14 @@ shepherd.post('/getconf', function(req, res) { }); /* - * type: GET - * params: coin, type + * type: GET + * params: coin, type */ shepherd.get('/kick', function(req, res, next) { - var _coin = req.query.coin, - _type = req.query.type; + var _coin = req.query.coin, + _type = req.query.type; - if (!_coin) { + if (!_coin) { var errorObj = { 'msg': 'error', 'result': 'no coin name provided' @@ -384,7 +430,7 @@ shepherd.get('/kick', function(req, res, next) { res.end(JSON.stringify(errorObj)); } - if (!_type) { + if (!_type) { var errorObj = { 'msg': 'error', 'result': 'no type provided' @@ -394,110 +440,110 @@ shepherd.get('/kick', function(req, res, next) { } var kickStartDirs = { - 'soft': [ - { - 'name': 'DB/[coin]', - 'type': 'pattern', - 'match': 'balancecrc.' - }, - { - 'name': 'DB/[coin]/utxoaddrs', - 'type': 'file' - }, - { - 'name': 'DB/[coin]/accounts', - 'type': 'folder' - }, - { - 'name': 'DB/[coin]/fastfind', - 'type': 'folder' - }, - { - 'name': 'tmp/[coin]', - 'type': 'folder' - } - ], - 'hard': [ - { - 'name': 'DB/[coin]', - 'type': 'pattern', - 'match': 'balancecrc.' - }, - { - 'name': 'DB/[coin]/utxoaddrs', - 'type': 'file' - }, - { - 'name': 'DB/[coin]', - 'type': 'pattern', - 'match': 'utxoaddrs.' - }, - { - 'name': 'DB/[coin]/accounts', - 'type': 'folder' - }, - { - 'name': 'DB/[coin]/fastfind', - 'type': 'folder' - }, - { - 'name': 'DB/[coin]/spends', - 'type': 'folder' - }, - { - 'name': 'tmp/[coin]', - 'type': 'folder' - } - ], - 'brutal': [ // delete coin related data - { - 'name': 'DB/[coin]', - 'type': 'folder' - }, - { - 'name': 'DB/purgeable/[coin]', - 'type': 'folder' - }, - { - 'name': 'DB/ro/[coin]', - 'type': 'folder' - }, - { - 'name': 'tmp/[coin]', - 'type': 'folder' - } - ] + 'soft': [ + { + 'name': 'DB/[coin]', + 'type': 'pattern', + 'match': 'balancecrc.' + }, + { + 'name': 'DB/[coin]/utxoaddrs', + 'type': 'file' + }, + { + 'name': 'DB/[coin]/accounts', + 'type': 'folder' + }, + { + 'name': 'DB/[coin]/fastfind', + 'type': 'folder' + }, + { + 'name': 'tmp/[coin]', + 'type': 'folder' + } + ], + 'hard': [ + { + 'name': 'DB/[coin]', + 'type': 'pattern', + 'match': 'balancecrc.' + }, + { + 'name': 'DB/[coin]/utxoaddrs', + 'type': 'file' + }, + { + 'name': 'DB/[coin]', + 'type': 'pattern', + 'match': 'utxoaddrs.' + }, + { + 'name': 'DB/[coin]/accounts', + 'type': 'folder' + }, + { + 'name': 'DB/[coin]/fastfind', + 'type': 'folder' + }, + { + 'name': 'DB/[coin]/spends', + 'type': 'folder' + }, + { + 'name': 'tmp/[coin]', + 'type': 'folder' + } + ], + 'brutal': [ // delete coin related data + { + 'name': 'DB/[coin]', + 'type': 'folder' + }, + { + 'name': 'DB/purgeable/[coin]', + 'type': 'folder' + }, + { + 'name': 'DB/ro/[coin]', + 'type': 'folder' + }, + { + 'name': 'tmp/[coin]', + 'type': 'folder' + } + ] }; if (_coin && _type) { - for (var i = 0; i < kickStartDirs[_type].length; i++) { - var currentKickItem = kickStartDirs[_type][i]; - - console.log('deleting ' + currentKickItem.type + (currentKickItem.match ? ' ' + currentKickItem.match : '') + ' ' + iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin)); - if (currentKickItem.type === 'folder' || currentKickItem.type === 'file') { - rimraf(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin), function(err) { - if (err) { - throw err; - } - }); - } else if (currentKickItem.type === 'pattern') { - var dirItems = fs.readdirSync(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin)); - - if (dirItems && dirItems.length) { - for (var j = 0; j < dirItems.length; j++) { - if (dirItems[j].indexOf(currentKickItem.match) > -1) { - rimraf(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin) + '/' + dirItems[j], function(err) { - if (err) { - throw err; - } - }); - - console.log('deleting ' + dirItems[j]); - } - } - } - } - } + for (var i = 0; i < kickStartDirs[_type].length; i++) { + var currentKickItem = kickStartDirs[_type][i]; + + console.log('deleting ' + currentKickItem.type + (currentKickItem.match ? ' ' + currentKickItem.match : '') + ' ' + iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin)); + if (currentKickItem.type === 'folder' || currentKickItem.type === 'file') { + rimraf(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin), function(err) { + if (err) { + throw err; + } + }); + } else if (currentKickItem.type === 'pattern') { + var dirItems = fs.readdirSync(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin)); + + if (dirItems && dirItems.length) { + for (var j = 0; j < dirItems.length; j++) { + if (dirItems[j].indexOf(currentKickItem.match) > -1) { + rimraf(iguanaDir + '/' + currentKickItem.name.replace('[coin]', _coin) + '/' + dirItems[j], function(err) { + if (err) { + throw err; + } + }); + + console.log('deleting ' + dirItems[j]); + } + } + } + } + } var successObj = { 'msg': 'success', @@ -513,19 +559,19 @@ shepherd.readDebugLog = function(fileLocation, lastNLines) { function(resolve, reject) { if (lastNLines) { _fs.access(fileLocation, fs.constants.R_OK, function(err) { - if (err) { - console.log('error reading ' + fileLocation); - reject('readDebugLog error: ' + err); - } else { - console.log('reading ' + fileLocation); - _fs.readFile(fileLocation, 'utf-8', function(err, data) { - if (err) throw err; - - var lines = data.trim().split('\n'), - lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n'); - resolve(lastLine); - }); - } + if (err) { + console.log('error reading ' + fileLocation); + reject('readDebugLog error: ' + err); + } else { + console.log('reading ' + fileLocation); + _fs.readFile(fileLocation, 'utf-8', function(err, data) { + if (err) throw err; + + var lines = data.trim().split('\n'), + lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n'); + resolve(lastLine); + }); + } }); } else { reject('readDebugLog error: lastNLines param is not provided!'); @@ -627,19 +673,19 @@ function herder(flock, data) { } if (flock === 'komodod') { - var kmdDebugLogLocation = komodoDir + '/debug.log'; + var kmdDebugLogLocation = komodoDir + '/debug.log'; console.log('komodod flock selected...'); console.log('selected data: ' + data); // truncate debug.log - _fs.access(kmdDebugLogLocation, fs.constants.R_OK, function(err) { + _fs.access(kmdDebugLogLocation, fs.constants.R_OK, function(err) { if (err) { console.log('error accessing ' + kmdDebugLogLocation); } else { - console.log('truncate ' + kmdDebugLogLocation); - fs.unlink(kmdDebugLogLocation); - } - }); + console.log('truncate ' + kmdDebugLogLocation); + fs.unlink(kmdDebugLogLocation); + } + }); pm2.connect(true, function(err) { // start up pm2 god if (err) { @@ -663,7 +709,7 @@ function herder(flock, data) { } if (flock === 'zcashd') { - var kmdDebugLogLocation = zcashDir + '/debug.log'; + var kmdDebugLogLocation = zcashDir + '/debug.log'; console.log('zcashd flock selected...'); console.log('selected data: ' + data); @@ -722,14 +768,66 @@ function slayer(flock) { }); } -shepherd.saveLocalAppConf = function(appSettings) { - var appConfFileName = iguanaDir + '/config.json'; +function setConf(flock) { + console.log(flock); + + if (os.platform() === 'darwin') { + var komodoDir = process.env.HOME + '/Library/Application Support/Komodo', + ZcashDir = process.env.HOME + '/Library/Application Support/Zcash'; + } + + if (os.platform() === 'linux') { + var komodoDir = process.env.HOME + '/.komodo', + ZcashDir = process.env.HOME + '/.zcash'; + } + + if (os.platform() === 'win32') { + var komodoDir = process.env.APPDATA + '/Komodo', + ZcashDir = process.env.APPDATA + '/Zcash'; + } + + switch (flock) { + case 'komodod': + var DaemonConfPath = komodoDir + '/komodo.conf'; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + } + break; + case 'zcashd': + var DaemonConfPath = ZcashDir + '/zcash.conf'; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + } + break; + default: + var DaemonConfPath = komodoDir + '/' + flock + '/' + flock + '.conf'; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + } + } + + console.log(DaemonConfPath); + + var CheckFileExists = function() { + return new Promise(function(resolve, reject) { + var result = 'Check Conf file exists is done' + + fs.ensureFile(DaemonConfPath, function(err) { + console.log(err); // => null + }); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 2000); + }); + } var FixFilePermissions = function() { return new Promise(function(resolve, reject) { - var result = 'config.json file permissions updated to Read/Write'; + var result = 'Conf file permissions updated to Read/Write'; - fsnode.chmodSync(appConfFileName, '0666'); + fsnode.chmodSync(DaemonConfPath, '0666'); setTimeout(function() { console.log(result); @@ -738,303 +836,208 @@ shepherd.saveLocalAppConf = function(appSettings) { }); } - var FsWrite = function() { + var RemoveLines = function() { return new Promise(function(resolve, reject) { - var result = 'config.json write file is done' + var result = 'RemoveLines is done' - fs.writeFile(appConfFileName, - JSON.stringify(appSettings) - .replace(/,/g, ',\n') // format json in human readable form - .replace(/:/g, ': ') - .replace(/{/g, '{\n') - .replace(/}/g, '\n}'), 'utf8', function(err) { - if (err) + fs.readFile(DaemonConfPath, 'utf8', function(err, data) { + if (err) { return console.log(err); + } + + var rmlines = data.replace(/(?:(?:\r\n|\r|\n)\s*){2}/gm, '\n'); + + fs.writeFile(DaemonConfPath, rmlines, 'utf8', function(err) { + if (err) + return console.log(err); + }); }); - fsnode.chmodSync(appConfFileName, '0666'); + fsnode.chmodSync(DaemonConfPath, '0666'); setTimeout(function() { console.log(result); - console.log('app conf.json file is created successfully at: ' + iguanaConfsDir); resolve(result); }, 2000); }); } - FsWrite() - .then(FixFilePermissions()); // not really required now -} + var CheckConf = function() { + return new Promise(function(resolve, reject) { + var result = 'CheckConf is done'; + + setconf.status(DaemonConfPath, function(err, status) { + //console.log(status[0]); + //console.log(status[0].rpcuser); + var rpcuser = function() { + return new Promise(function(resolve, reject) { + var result = 'checking rpcuser...'; + + if (status[0].hasOwnProperty('rpcuser')) { + console.log('rpcuser: OK'); + } else { + console.log('rpcuser: NOT FOUND'); + var randomstring = md5(Math.random() * Math.random() * 999); + + fs.appendFile(DaemonConfPath, '\nrpcuser=user' + randomstring.substring(0, 16), (err) => { + if (err) + throw err; + console.log('rpcuser: ADDED'); + }); + } -function setConf(flock) { - console.log(flock); - - if (os.platform() === 'darwin') { - var komodoDir = process.env.HOME + '/Library/Application Support/Komodo', - ZcashDir = process.env.HOME + '/Library/Application Support/Zcash'; - } - - if (os.platform() === 'linux') { - var komodoDir = process.env.HOME + '/.komodo', - ZcashDir = process.env.HOME + '/.zcash'; - } - - if (os.platform() === 'win32') { - var komodoDir = process.env.APPDATA + '/Komodo', - ZcashDir = process.env.APPDATA + '/Zcash'; - } - - switch (flock) { - case 'komodod': - var DaemonConfPath = komodoDir + '/komodo.conf'; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - } - break; - case 'zcashd': - var DaemonConfPath = ZcashDir + '/zcash.conf'; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - } - break; - default: - var DaemonConfPath = komodoDir + '/' + flock + '/' + flock + '.conf'; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - } - } - - console.log(DaemonConfPath); - - var CheckFileExists = function() { - return new Promise(function(resolve, reject) { - var result = 'Check Conf file exists is done' - - fs.ensureFile(DaemonConfPath, function(err) { - console.log(err); // => null - }); - - setTimeout(function() { - console.log(result); - resolve(result); - }, 2000); - }); - } - - var FixFilePermissions = function() { - return new Promise(function(resolve, reject) { - var result = 'Conf file permissions updated to Read/Write'; - - fsnode.chmodSync(DaemonConfPath, '0666'); - - setTimeout(function() { - console.log(result); - resolve(result); - }, 1000); - }); - } - - var RemoveLines = function() { - return new Promise(function(resolve, reject) { - var result = 'RemoveLines is done' - - fs.readFile(DaemonConfPath, 'utf8', function(err, data) { - if (err) { - return console.log(err); - } - - var rmlines = data.replace(/(?:(?:\r\n|\r|\n)\s*){2}/gm, '\n'); - - fs.writeFile(DaemonConfPath, rmlines, 'utf8', function(err) { - if (err) - return console.log(err); - }); - }); - - fsnode.chmodSync(DaemonConfPath, '0666'); - setTimeout(function() { - console.log(result); - resolve(result); - }, 2000); - }); - } - - var CheckConf = function() { - return new Promise(function(resolve, reject) { - var result = 'CheckConf is done'; - - setconf.status(DaemonConfPath, function(err, status) { - //console.log(status[0]); - //console.log(status[0].rpcuser); - var rpcuser = function() { - return new Promise(function(resolve, reject) { - var result = 'checking rpcuser...'; - - if (status[0].hasOwnProperty('rpcuser')) { - console.log('rpcuser: OK'); - } else { - console.log('rpcuser: NOT FOUND'); - var randomstring = md5(Math.random() * Math.random() * 999); - - fs.appendFile(DaemonConfPath, '\nrpcuser=user' + randomstring.substring(0, 16), (err) => { - if (err) - throw err; - console.log('rpcuser: ADDED'); - }); - } - - //console.log(result) - resolve(result); - }); - } - - var rpcpass = function() { - return new Promise(function(resolve, reject) { - var result = 'checking rpcpassword...'; - - if (status[0].hasOwnProperty('rpcpassword')) { - console.log('rpcpassword: OK'); - } else { - console.log('rpcpassword: NOT FOUND'); - var randomstring = md5(Math.random() * Math.random() * 999); - - fs.appendFile(DaemonConfPath, '\nrpcpassword=' + randomstring, (err) => { - if (err) - throw err; - console.log('rpcpassword: ADDED'); - }); - } - - //console.log(result) - resolve(result); - }); - } - - var server = function() { - return new Promise(function(resolve, reject) { - var result = 'checking server...'; - - if (status[0].hasOwnProperty('server')) { - console.log('server: OK'); - } else { - console.log('server: NOT FOUND'); - fs.appendFile(DaemonConfPath, '\nserver=1', (err) => { - if (err) - throw err; - console.log('server: ADDED'); - }); - } - - //console.log(result) - resolve(result); - }); - } - - var addnode = function() { - return new Promise(function(resolve, reject) { - var result = 'checking addnode...'; - - if(status[0].hasOwnProperty('addnode')) { - console.log('addnode: OK'); - } else { - console.log('addnode: NOT FOUND') - fs.appendFile(DaemonConfPath, - '\naddnode=78.47.196.146' + - '\naddnode=5.9.102.210' + - '\naddnode=178.63.69.164' + - '\naddnode=88.198.65.74' + - '\naddnode=5.9.122.241' + - '\naddnode=144.76.94.3', - (err) => { - if (err) - throw err; - console.log('addnode: ADDED'); - }); - } - - //console.log(result) - resolve(result); - }); - } - - rpcuser() - .then(function(result) { - return rpcpass(); - }) - .then(server) - .then(addnode) - }); - - setTimeout(function() { - console.log(result); - resolve(result); - }, 2000); - }); - } - - var MakeConfReadOnly = function() { - return new Promise(function(resolve, reject) { - var result = 'Conf file permissions updated to Read Only'; - - fsnode.chmodSync(DaemonConfPath, '0400'); - - setTimeout(function() { - console.log(result); - resolve(result); - }, 1000); - }); - } - - CheckFileExists() - .then(function(result) { - return FixFilePermissions(); - }) - .then(RemoveLines) - .then(CheckConf) - .then(MakeConfReadOnly); + //console.log(result) + resolve(result); + }); + } + + var rpcpass = function() { + return new Promise(function(resolve, reject) { + var result = 'checking rpcpassword...'; + + if (status[0].hasOwnProperty('rpcpassword')) { + console.log('rpcpassword: OK'); + } else { + console.log('rpcpassword: NOT FOUND'); + var randomstring = md5(Math.random() * Math.random() * 999); + + fs.appendFile(DaemonConfPath, '\nrpcpassword=' + randomstring, (err) => { + if (err) + throw err; + console.log('rpcpassword: ADDED'); + }); + } + + //console.log(result) + resolve(result); + }); + } + + var server = function() { + return new Promise(function(resolve, reject) { + var result = 'checking server...'; + + if (status[0].hasOwnProperty('server')) { + console.log('server: OK'); + } else { + console.log('server: NOT FOUND'); + fs.appendFile(DaemonConfPath, '\nserver=1', (err) => { + if (err) + throw err; + console.log('server: ADDED'); + }); + } + + //console.log(result) + resolve(result); + }); + } + + var addnode = function() { + return new Promise(function(resolve, reject) { + var result = 'checking addnode...'; + + if(status[0].hasOwnProperty('addnode')) { + console.log('addnode: OK'); + } else { + console.log('addnode: NOT FOUND') + fs.appendFile(DaemonConfPath, + '\naddnode=78.47.196.146' + + '\naddnode=5.9.102.210' + + '\naddnode=178.63.69.164' + + '\naddnode=88.198.65.74' + + '\naddnode=5.9.122.241' + + '\naddnode=144.76.94.3', + (err) => { + if (err) + throw err; + console.log('addnode: ADDED'); + }); + } + + //console.log(result) + resolve(result); + }); + } + + rpcuser() + .then(function(result) { + return rpcpass(); + }) + .then(server) + .then(addnode) + }); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 2000); + }); + } + + var MakeConfReadOnly = function() { + return new Promise(function(resolve, reject) { + var result = 'Conf file permissions updated to Read Only'; + + fsnode.chmodSync(DaemonConfPath, '0400'); + + setTimeout(function() { + console.log(result); + resolve(result); + }, 1000); + }); + } + + CheckFileExists() + .then(function(result) { + return FixFilePermissions(); + }) + .then(RemoveLines) + .then(CheckConf) + .then(MakeConfReadOnly); } function getConf(flock) { - var komodoDir = '', - ZcashDir = '', - DaemonConfPath = ''; + var komodoDir = '', + ZcashDir = '', + DaemonConfPath = ''; console.log(flock); - if (os.platform() === 'darwin') { - komodoDir = process.env.HOME + '/Library/Application Support/Komodo'; - ZcashDir = process.env.HOME + '/Library/Application Support/Zcash'; - } + if (os.platform() === 'darwin') { + komodoDir = process.env.HOME + '/Library/Application Support/Komodo'; + ZcashDir = process.env.HOME + '/Library/Application Support/Zcash'; + } - if (os.platform() === 'linux') { - komodoDir = process.env.HOME + '/.komodo'; - ZcashDir = process.env.HOME + '/.zcash'; - } + if (os.platform() === 'linux') { + komodoDir = process.env.HOME + '/.komodo'; + ZcashDir = process.env.HOME + '/.zcash'; + } if (os.platform() === 'win32') { - komodoDir = process.env.APPDATA + '/Komodo'; - ZcashDir = process.env.APPDATA + '/Zcash'; - } - - switch (flock) { - case 'komodod': - DaemonConfPath = komodoDir; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - console.log('===>>> SHEPHERD API OUTPUT ===>>>'); - } - break; - case 'zcashd': - DaemonConfPath = ZcashDir; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - } - break; - default: - DaemonConfPath = komodoDir + '/' + flock; - if (os.platform() === 'win32') { - DaemonConfPath = path.normalize(DaemonConfPath); - } - } + komodoDir = process.env.APPDATA + '/Komodo'; + ZcashDir = process.env.APPDATA + '/Zcash'; + } + + switch (flock) { + case 'komodod': + DaemonConfPath = komodoDir; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + console.log('===>>> SHEPHERD API OUTPUT ===>>>'); + } + break; + case 'zcashd': + DaemonConfPath = ZcashDir; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + } + break; + default: + DaemonConfPath = komodoDir + '/' + flock; + if (os.platform() === 'win32') { + DaemonConfPath = path.normalize(DaemonConfPath); + } + } console.log(DaemonConfPath); return DaemonConfPath; @@ -1042,20 +1045,20 @@ function getConf(flock) { function formatBytes(bytes, decimals) { if (bytes == 0) - return '0 Bytes'; + return '0 Bytes'; var k = 1000, dm = decimals + 1 || 3, sizes = [ - 'Bytes', - 'KB', - 'MB', - 'GB', - 'TB', - 'PB', - 'EB', - 'ZB', - 'YB' + 'Bytes', + 'KB', + 'MB', + 'GB', + 'TB', + 'PB', + 'EB', + 'ZB', + 'YB' ], i = Math.floor(Math.log(bytes) / Math.log(k)); @@ -1063,18 +1066,18 @@ function formatBytes(bytes, decimals) { } shepherd.SystemInfo = function() { - const os_data = { - 'totalmem_bytes': os.totalmem(), - 'totalmem_readble': formatBytes(os.totalmem()), - 'arch': os.arch(), - 'cpu': os.cpus()[0].model, - 'cpu_cores': os.cpus().length, - 'platform': os.platform(), - 'os_release': os.release(), - 'os_type': os.type() - }; - - return os_data; + const os_data = { + 'totalmem_bytes': os.totalmem(), + 'totalmem_readble': formatBytes(os.totalmem()), + 'arch': os.arch(), + 'cpu': os.cpus()[0].model, + 'cpu_cores': os.cpus().length, + 'platform': os.platform(), + 'os_release': os.release(), + 'os_type': os.type() + }; + + return os_data; } module.exports = shepherd; \ No newline at end of file