diff --git a/gui/init.js b/gui/init.js index 7008fa7..265e389 100644 --- a/gui/init.js +++ b/gui/init.js @@ -26,7 +26,7 @@ $(document).ready(function() { if (result !== 'error') { stopcheck(); - if (appConf && appConf.forks && appConf.forks.basilisk) { + if (appConf && appConf.useBasiliskInstance) { StartIguana_Cache(); } diff --git a/gui/loading.js b/gui/loading.js index b30fc51..8a178bc 100644 --- a/gui/loading.js +++ b/gui/loading.js @@ -123,6 +123,7 @@ function EDEX_DEXgetinfoAll(skip, minNotaries, appConf) { 'method': 'notarychains' }, tmp_index = 0, + tmp_index_failed = 0, get_dex_notarychains = IguanaAJAX('http://127.0.0.1:' + appConf.iguanaCorePort, 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 @@ -143,11 +144,11 @@ function EDEX_DEXgetinfoAll(skip, minNotaries, appConf) { console.log(ajax_data); if (coin_value !== 'MESH' || coin_value !== 'CEAL') { - var getinfo_each_chain = IguanaAJAX('http://127.0.0.1:' + appConf.iguanaCorePort, ajax_data).done(function(data) { + var getinfo_each_chain = IguanaAJAX('http://127.0.0.1:' + appConf.iguanaCorePort, ajax_data, 10000).done(function(data) { getinfo_each_chain = JSON.parse(getinfo_each_chain.responseText); console.log(getinfo_each_chain); - tmp_index = parseInt(coin_index) + 1; + tmp_index++; $('#loading_sub_status_text').text('Connection status... ' + tmp_index + '/' + get_dex_notarychains.length + ': ' + coin_value); if (getinfo_each_chain.error === 'less than required responses') { @@ -156,13 +157,21 @@ function EDEX_DEXgetinfoAll(skip, minNotaries, appConf) { $('#loading_sub_status_output_text').text('Output: Connected'); } - if ( tmp_index == minNotaries ) { + if ( tmp_index + tmp_index_failed === minNotaries ) { console.log('min notaries connected'); window.createWindow('open'); window.hide(); } }) .fail(function(xhr, textStatus, error) { + tmp_index_failed++; + + if ( tmp_index + tmp_index_failed === minNotaries ) { + console.log('min notaries connected'); + window.createWindow('open'); + window.hide(); + } + // handle request failures console.log(xhr.statusText); if ( xhr.readyState == 0 ) { diff --git a/main.js b/main.js index b95fcad..0a4f60d 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,7 @@ const electron = require('electron'), path = require('path'), url = require('url'), os = require('os'), + md5 = require('md5'), spawn = require('child_process').spawn, exec = require('child_process').exec, { Menu } = require('electron'), @@ -28,7 +29,7 @@ Promise = require('bluebird'); const appBasicInfo = { name: 'Agama', - version: '0.1.6.2e-beta' + version: '0.1.7.77a-beta' }; app.setName(appBasicInfo.name); @@ -41,9 +42,13 @@ if (os.platform() === 'linux') { // GUI APP settings and starting gui on address http://120.0.0.1:17777 var shepherd = require('./routes/shepherd'), - guiapp = express(); + guiapp = express(); -shepherd.writeLog('app init'); +shepherd.createIguanaDirs(); + +const appSessionHash = md5(new Date(Date.now()).toLocaleString); + +shepherd.writeLog('app init ' + appSessionHash); shepherd.writeLog('app info: ' + appBasicInfo.name + ' ' + appBasicInfo.version); shepherd.writeLog('sys info:'); shepherd.writeLog('totalmem_readable: ' + formatBytes(os.totalmem())); @@ -143,12 +148,12 @@ guiapp.use('/shepherd', shepherd); var server = require('http').createServer(guiapp), io = require('socket.io').listen(server); -server.listen(appConfig.iguanaAppPort, function() { - console.log('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!'); - shepherd.writeLog('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!'); +server.listen(appConfig.agamaPort, function() { + console.log('guiapp and sockets.io are listening on port ' + appConfig.agamaPort + '!'); + shepherd.writeLog('guiapp and sockets.io are listening on port ' + appConfig.agamaPort + '!'); }); -io.set('origins', appConfig.dev ? 'http://127.0.0.1:3000' : 'http://127.0.0.1:' + appConfig.iguanaAppPort); // set origin +io.set('origins', appConfig.dev ? 'http://127.0.0.1:3000' : 'http://127.0.0.1:' + appConfig.agamaPort); // set origin io.on('connection', function(client) { console.log('EDEX GUI is connected...'); @@ -168,6 +173,7 @@ io.on('connection', function(client) { shepherd.setIO(io); // pass sockets object to shepherd router shepherd.setVar('appBasicInfo', appBasicInfo); +shepherd.setVar('appSessionHash', appSessionHash); module.exports = guiapp; // END GUI App Settings @@ -246,7 +252,7 @@ function createLoadingWindow() { loadingWindow.createWindow = createWindow; // expose createWindow to front-end scripts // load our index.html (i.e. easyDEX GUI) - loadingWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/'); + loadingWindow.loadURL('http://' + appConfig.host + ':' + appConfig.agamaPort + '/gui/'); shepherd.writeLog('show loading window'); // DEVTOOLS - only for dev purposes - ca333 @@ -360,13 +366,15 @@ function createWindow (status) { if (appConfig.v2) { shepherd.writeLog('show edex gui'); mainWindow.loadURL('http://127.0.0.1:3000'); - //mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/react/build'); + mainWindow.appConfig = appConfig; + mainWindow.appSessionHash = appSessionHash; + //mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.agamaPort + '/gui/EasyDEX-GUI/react/build'); } else { shepherd.writeLog('show edex gui'); - mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/'); + mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.agamaPort + '/gui/EasyDEX-GUI/'); } } else { - mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/main.html'); + mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.agamaPort + '/gui/main.html'); } mainWindow.webContents.on('context-menu', (e, params) => { //context-menu returns params diff --git a/routes/cache.js b/routes/cache.js index 406414c..bcc37cb 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -429,7 +429,7 @@ cache.one = function(req, res, next) { 'listunspent': 'http://' + cache.appConfig.host + ':' + iguanaCorePort + '/api/dex/listunspent?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, 'listtransactions': 'http://' + cache.appConfig.host + ':' + iguanaCorePort + '/api/dex/listtransactions?userpass=' + sessionKey + '&count=100&skip=0&symbol=' + coin + '&address=' + address, 'getbalance': 'http://' + cache.appConfig.host + ':' + iguanaCorePort + '/api/dex/getbalance?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address, - 'refresh': 'http://' + cache.appConfig.host + ':' + iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&timeout=600000&symbol=' + coin + '&address=' + address + 'refresh': 'http://' + cache.appConfig.host + ':' + iguanaCorePort + '/api/basilisk/refresh?userpass=' + sessionKey + '&symbol=' + coin + '&address=' + address }, _dexUrls = {}; diff --git a/routes/shepherd.js b/routes/shepherd.js index 5a90adc..9d33d56 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -23,11 +23,12 @@ const fixPath = require('fix-path'); var ps = require('ps-node'), setconf = require('../private/setconf.js'), //coincli = require('../private/coincli.js'), - assetChainPorts = require('./ports.js') + assetChainPorts = require('./ports.js'), shepherd = express.Router(), iguanaInstanceRegistry = {}, syncOnlyIguanaInstanceInfo = {}, - syncOnlyInstanceInterval = -1; + syncOnlyInstanceInterval = -1, + guiLog = {}; // IGUANA FILES AND CONFIG SETTINGS var iguanaConfsDirSrc = path.join(__dirname, '../assets/deps/confs'), @@ -80,10 +81,10 @@ shepherd.appConfig = { "edexGuiOnly": true, "iguanaGuiOnly": false, "manualIguanaStart": false, - "skipBasiliskNetworkCheck": false, + "skipBasiliskNetworkCheck": true, "minNotaries": 8, "host": "127.0.0.1", - "iguanaAppPort": 17777, + "agamaPort": 17777, "iguanaCorePort": 7778, "maxDescriptors": { "darwin": 90000, @@ -91,16 +92,14 @@ shepherd.appConfig = { }, "killIguanaOnStart": true, "dev": false, - "v2": false, - "forks": { - "basilisk": false, - "all": false - } + "v2": true, + "useBasiliskInstance": true, + "debug": true, }; shepherd.writeLog = function(data) { const logLocation = iguanaDir + '/shepherd'; - const timeFormatted = new Date(Date.now()).toLocaleString().replace('AM', '').replace('PM', ''); + const timeFormatted = new Date(Date.now()).toLocaleString('en-US', { hour12: false }); if (fs.existsSync(logLocation + '/agamalog.txt')) { fs.appendFile(logLocation + '/agamalog.txt', timeFormatted + ' ' + data + '\r\n', function (err) { @@ -117,6 +116,28 @@ shepherd.writeLog = function(data) { } } +shepherd.createIguanaDirs = function() { + if (!fs.existsSync(iguanaDir)) { + fs.mkdirSync(iguanaDir); + if (fs.existsSync(iguanaDir)) { + console.log('created iguana folder at ' + iguanaDir); + shepherd.writeLog('created iguana folder at ' + iguanaDir); + } + } else { + console.log('iguana folder already exists'); + } + + if (!fs.existsSync(iguanaDir + '/shepherd')) { + fs.mkdirSync(iguanaDir + '/shepherd'); + if (fs.existsSync(iguanaDir)) { + console.log('created shepherd folder at ' + iguanaDir + '/shepherd'); + shepherd.writeLog('create shepherd folder at ' + iguanaDir + '/shepherd'); + } + } else { + console.log('shepherd folder already exists'); + } +} + shepherd.get('/coinslist', function(req, res, next) { if (fs.existsSync(iguanaDir + '/shepherd/coinslist.json')) { fs.readFile(iguanaDir + '/shepherd/coinslist.json', 'utf8', function (err, data) { @@ -146,6 +167,75 @@ shepherd.get('/coinslist', function(req, res, next) { } }); +/* + * type: POST + * params: payload + */ +shepherd.post('/guilog', function(req, res, next) { + const logLocation = iguanaDir + '/shepherd'; + + if (!guiLog[shepherd.appSessionHash]) { + guiLog[shepherd.appSessionHash] = {}; + } + + if (guiLog[shepherd.appSessionHash][req.body.timestamp]) { + guiLog[shepherd.appSessionHash][req.body.timestamp].status = req.body.status; + guiLog[shepherd.appSessionHash][req.body.timestamp].response = req.body.response; + } else { + guiLog[shepherd.appSessionHash][req.body.timestamp] = { + 'function': req.body.function, + 'type': req.body.type, + 'url': req.body.url, + 'payload': req.body.payload, + 'status': req.body.status, + }; + } + + fs.writeFile(logLocation + '/agamalog.json', JSON.stringify(guiLog), function (err) { + if (err) { + shepherd.writeLog('error writing gui log file'); + } + + const returnObj = { + 'msg': 'success', + 'result': 'gui log entry is added' + }; + + res.end(JSON.stringify(returnObj)); + }); +}); + +shepherd.get('/getlog', function(req, res, next) { + const logExt = req.query.type === 'txt' ? 'txt' : 'json'; + + if (fs.existsSync(iguanaDir + '/shepherd/agamalog.' + logExt)) { + fs.readFile(iguanaDir + '/shepherd/agamalog.' + logExt, 'utf8', function (err, data) { + if (err) { + const errorObj = { + 'msg': 'error', + 'result': err + }; + + res.end(JSON.stringify(errorObj)); + } else { + const successObj = { + 'msg': 'success', + 'result': data ? JSON.parse(data) : '' + }; + + res.end(JSON.stringify(successObj)); + } + }); + } else { + const errorObj = { + 'msg': 'error', + 'result': 'agama.' + logExt + ' doesn\'t exist' + }; + + res.end(JSON.stringify(errorObj)); + } +}); + shepherd.post('/coinslist', function(req, res, next) { const _payload = req.body.payload; @@ -184,6 +274,7 @@ shepherd.quitKomodod = function(chain) { exec(komodocliBin + (chain ? ' ac_name=' + chain : '') + ' stop', function(error, stdout, stderr) { console.log('stdout: ' + stdout) console.log('stderr: ' + stderr) + if (error !== null) { console.log('exec error: ' + error) } @@ -217,46 +308,51 @@ shepherd.post('/appconf', function(req, res, next) { 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'; + _fs.access(iguanaDir, fs.constants.R_OK, function(err) { + if (!err) { - fsnode.chmodSync(appConfFileName, '0666'); + var FixFilePermissions = function() { + return new Promise(function(resolve, reject) { + var result = 'config.json file permissions updated to Read/Write'; - setTimeout(function() { - console.log(result); - shepherd.writeLog(result); - resolve(result); - }, 1000); - }); - } + fsnode.chmodSync(appConfFileName, '0666'); - 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); - }); + setTimeout(function() { + console.log(result); + shepherd.writeLog(result); + resolve(result); + }, 1000); + }); + } - fsnode.chmodSync(appConfFileName, '0666'); - setTimeout(function() { - console.log(result); - console.log('app conf.json file is created successfully at: ' + iguanaConfsDir); - shepherd.writeLog('app conf.json file is created successfully at: ' + iguanaConfsDir); - resolve(result); - }, 2000); - }); - } + 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); + }); - FsWrite() - .then(FixFilePermissions()); + fsnode.chmodSync(appConfFileName, '0666'); + setTimeout(function() { + console.log(result); + console.log('app conf.json file is created successfully at: ' + iguanaConfsDir); + shepherd.writeLog('app conf.json file is created successfully at: ' + iguanaConfsDir); + resolve(result); + }, 2000); + }); + } + + FsWrite() + .then(FixFilePermissions()); + } + }); } shepherd.loadLocalConfig = function() { @@ -1070,8 +1166,8 @@ function herder(flock, data) { exec(komododBin + ' ' + data.ac_options.join(' '), { maxBuffer: 1024 * 10000 // 10 mb }, function(error, stdout, stderr) { - console.log('stdout: ' + stdout); - console.log('stderr: ' + stderr); + // console.log('stdout: ' + stdout); + // console.log('stderr: ' + stderr); shepherd.writeLog('stdout: ' + stdout); shepherd.writeLog('stderr: ' + stderr); @@ -1560,6 +1656,7 @@ shepherd.appInfo = function() { sysInfo, releaseInfo, dirs, + appSession: shepherd.appSessionHash }; }