Adding setup files

This commit is contained in:
sairajzero 2022-11-29 04:41:11 +05:30
parent 2dc338f90d
commit fd2aa002c0
10 changed files with 285 additions and 23 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
/package-lock.json
/args/config.json
/args/param.json
/args/keys.json
/log.txt
/bash_start*
*test*

View File

@ -1,9 +0,0 @@
{
"privateKey": "FLO_Private_Key_of_SuperNode",
"port": "8080",
"sql_user": "mySQL_user",
"sql_pwd": "mySQL_password",
"sql_db": "supernode",
"sql_host": "localhost"
}

View File

@ -1,11 +0,0 @@
let message =
`SupernodeStorage is installed.
To complete the setup:
1. Copy args/config-sample.json to args/config.json and Edit the values as required
2. (Optional) Open args/gen-param.html and Download param.json to args/ directory
To start the node, Run:
npm start
`;
console.log(message);

View File

@ -10,7 +10,11 @@
},
"devDependencies": {},
"scripts": {
"postinstall": "node args/post-install.js",
"postinstall": "node setup/post-install.js",
"help": "node setup/help.js",
"setup": "node setup/post-install.js",
"configure": "node setup/configure-settings.js",
"reset-password": "node setup/reset-password.js",
"start": "node start.js"
},
"repository": {
@ -29,4 +33,4 @@
"url": "https://github.com/ranchimall/SuperNodeStorage/issues"
},
"homepage": "https://github.com/ranchimall/SuperNodeStorage#readme"
}
}

View File

@ -0,0 +1,85 @@
const fs = require('fs');
const path = require('path');
const getInput = require('./getInput');
var config, flag_new;
try {
config = require(`../args/config.json`);
flag_new = false;
} catch (error) {
config = {
"port": "8080",
"sql_user": "mySQL_user",
"sql_pwd": "mySQL_password",
"sql_db": "supernode",
"sql_host": "localhost"
};
flag_new = true;
}
function flaggedYesOrNo(text) {
return new Promise((resolve) => {
if (flag_new)
resolve(true);
else
getInput.YesOrNo(text)
.then(result => resolve(result))
.catch(error => reject(error))
})
}
function configurePort() {
return new Promise(resolve => {
getInput.Text('Enter port', config["port"]).then(port => {
config["port"] = port;
resolve(true);
})
})
}
function configureSQL() {
return new Promise(resolve => {
flaggedYesOrNo('Do you want to re-configure mySQL connection').then(value => {
if (value) {
console.log('Enter mySQL connection values: ')
getInput.Text('Host', config['sql_host']).then(host => {
config['sql_host'] = host;
getInput.Text('Database name', config['sql_db']).then(dbname => {
config['sql_db'] = dbname;
getInput.Text('MySQL username', config['sql_user']).then(sql_user => {
config['sql_user'] = sql_user;
getInput.Text('Mysql password', config['sql_pwd']).then(sql_pwd => {
config['sql_pwd'] = sql_pwd;
resolve(true);
})
})
})
})
} else
resolve(false);
})
})
}
function configure() {
return new Promise((resolve, reject) => {
configurePort().then(port_result => {
configureSQL().then(sql_result => {
fs.writeFile(path.resolve(__dirname, '..', 'args', `config.json`), JSON.stringify(config), 'utf8', (err) => {
if (err) {
console.error(err);
return reject(false);
}
console.log('Configuration successful!');
resolve(true);
})
})
});
})
}
if (!module.parent)
configure().then(_ => null).catch(error => console.error(error)).finally(_ => process.exit(0));
else
module.exports = configure;

42
setup/getInput.js Normal file
View File

@ -0,0 +1,42 @@
const readline = require('readline');
const getInput = {
Text: function(text, current = null) {
return new Promise((resolve) => {
let r = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r.question(`${text} :` + (current ? `(${current})` : ''), value => {
r.close();
value = value || current;
if (value === null) {
console.log("Please enter a value!");
this.Text(text, current).then(result => resolve(result));
} else
resolve(value);
});
})
},
YesOrNo: function(text, def_value = "YES") {
return new Promise((resolve) => {
let r = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r.question(`${text}? [YES/NO] : (${def_value})`, value => {
r.close();
value = (value || def_value).toLowerCase();
value = ['yes', 'y'].includes(value) ? true : ['no', 'n'].includes(value) ? false : null;
if (value === null) {
console.log("Please enter a valid value!");
this.YesOrNo(text, def_value).then(result => resolve(result));
} else
resolve(value);
});
})
}
}
module.exports = getInput;

22
setup/help.js Normal file
View File

@ -0,0 +1,22 @@
let message = `
SuperNode Storage
-----------------
npm install - Install the app and node modules.
npm run help - List all commands.
npm run setup - Finish the setup (configure and reset password).
npm run configure - Configure the app.
npm run reset-password - Reset the password (for private-key).
npm start - Start the application (main).
NOTE: argument 'PASSWORD' required for 'npm start'
npm start -- -PASSWORD=<password>
(Optional) 'console.debug' is now turned off by default. pass argument '--debug' to turn it on
npm start -- -PASSWORD=<password> --debug
(Optional) Open args/gen-param.html and Download param.json to args/ directory
`;
console.log(message);

38
setup/post-install.js Normal file
View File

@ -0,0 +1,38 @@
const getInput = require("./getInput");
let message = `
SuperNode Storage is installed
To list all commands, use:
npm run help
`;
console.log(message);
getInput.YesOrNo('Do you want to finish the setup now').then(value => {
if (value) {
let configureSettings = require('./configure-settings');
configureSettings()
.then(_ => console.log('To Re-configure, use:'))
.catch(_ => console.log('Finish the configuration later using: '))
.finally(_ => {
console.log('npm run configure');
getInput.YesOrNo('Do you want to Reset password for private key now').then(value => {
if (value) {
let resetPassword = require('./reset-password');
resetPassword()
.then(_ => console.log('To reset the password again, use: '))
.catch(_ => console.log('Reset the password later using: '))
.finally(_ => {
console.log('npm run reset-password');
process.exit(0);
})
} else {
console.log('Reset the password later using:\n' + 'npm run reset-password');
process.exit(0);
}
})
})
} else {
console.log('Finish the setup later using:\n' + 'npm run setup');
process.exit(0);
}
})

87
setup/reset-password.js Normal file
View File

@ -0,0 +1,87 @@
const fs = require('fs');
const getInput = require('./getInput');
global.floGlobals = require('../src/floGlobals');
require('../src/set_globals');
require('../src/lib');
const floCrypto = require('../src/floCrypto');
const path = require('path');
function validateKey(privKey) {
return new Promise((resolve, reject) => {
try {
if (!privKey || privKey === "")
throw 'Private Key cannot be empty!';
let floID = floCrypto.getFloID(privKey);
if (!floID || !floCrypto.verifyPrivKey(privKey, floID))
throw 'Invalid Private Key!';
return resolve(privKey);
} catch (error) {
getInput.Text(error + ' Re-Enter: (Cancel)', 'Cancel').then(value => {
if (value === 'Cancel')
return reject(true);
validateKey(value)
.then(result => resolve(result))
.catch(error => reject(error))
});
}
})
}
function getPassword() {
return new Promise((resolve, reject) => {
getInput.Text(`Enter a password [Minimum 8 characters]`, 'Cancel').then(value1 => {
if (value1 === 'Cancel')
return reject(true);
else if (value1.length < 8) {
console.log('Password length must be minimum of 8 characters');
getPassword()
.then(result => resolve(result))
.catch(error => reject(error))
} else {
getInput.Text(`Re-enter password`).then(value2 => {
if (value1 !== value2) {
console.log('Passwords doesnot match! Try again.');
getPassword()
.then(result => resolve(result))
.catch(error => reject(error))
} else
resolve(value1);
})
}
});
})
}
function resetPassword() {
return new Promise((resolve, reject) => {
getInput.Text(`Enter private key`).then(value => {
validateKey(value).then(privKey => {
getPassword().then(password => {
let encrypted = Crypto.AES.encrypt(privKey, password);
let randNum = floCrypto.randInt(10, 15);
let splitShares = floCrypto.createShamirsSecretShares(encrypted, randNum, randNum);
fs.writeFile(path.resolve(__dirname, '..', 'args', `keys.json`), JSON.stringify(splitShares), 'utf8', (err) => {
if (err) {
console.error(err);
return reject(false);
}
console.log('Password reset successful!');
resolve(true);
})
}).catch(error => {
console.log('Password reset cancelled!');
reject(true);
})
}).catch(error => {
console.log('Password reset cancelled!');
reject(true);
})
})
})
}
if (!module.parent)
resetPassword().then(_ => null).catch(_ => null).finally(_ => process.exit(0));
else
module.exports = resetPassword;

View File

@ -11,4 +11,7 @@ try {
} finally {
for (let p in param)
global[p] = param[p];
}
}
if (!process.argv.includes("--debug"))
global.console.debug = () => null;