From 7bdce37fd3f18e2712e18c4e2c64cac69af0aca1 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sun, 19 Sep 2021 19:43:11 +0200 Subject: :boom: Introduce new UI based on svelte, and rewrite a lot of the node app and the NeoRuntime --- src/SSLCert/index.js | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 src/SSLCert/index.js (limited to 'src/SSLCert/index.js') diff --git a/src/SSLCert/index.js b/src/SSLCert/index.js new file mode 100644 index 0000000..6dad579 --- /dev/null +++ b/src/SSLCert/index.js @@ -0,0 +1,144 @@ +/** + * Module that exports an instance of CertMon + * see class definition to see what it does. + * + * Requires global var '__datadir' to be set. + * + * @author jakobst1n. + * @since 14.16.2019 + */ + let logger = require(__basedir + "/src/logger"); + const fs = require("fs"); + const { execSync } = require("child_process"); + +var neoModules; + + /** + * This checks if the server has a valid certificate, if not, + * it will generate one. + */ + class CertMon { + + constructor(configPath, certPath, httpsConfig) { + this.certPath = __datadir + "/config/certs/"; + + let valid = this.checkValidity(); + if (!valid) { + logger.notice("No valid certificate found, creating one now."); + this.generateCert(); + } + + let interval = setInterval(() => { + let certIsValid = this.checkValidity(); + if (!valid) { + logger.crit("Certificate no longer valid, server should reboot to make a new one."); + } + }, 1440000); // Run once every day + } + + checkValidity() { + let sslConfig = this.getConfig(); + if (!sslConfig["certMade"]) { + logger.debug("'certMade' in config is false, assuming no valid certificate"); + return false; + } + let expire = ((sslConfig["certExpire"] - Date.now()) / 86400000).toFixed(2); + if (expire > 0) { + logger.debug(`Certificate should be valid for ${expire} more days.`); + } else { + expire = Math.abs(expire); + logger.debug(`Certificate expired ${expire} days ago`); + return false; + } + return true; + } + + getConfig() { + return neoModules.userData.config.SSLCert; + } + + updateConfig(parameters) { + neoModules.userData.config.set(parameters); + } + + generateCert() { + let certPath = this.certPath; + let config = this.getConfig(); + + + // Create Root Certificate Autority + let res = openssl( + `genrsa ` + + `-out "${certPath}/root-CA.key.pem" ` + + `2048` + ); + + // Self sign the Root Certificate Autority + res = openssl( + `req ` + + `-x509 ` + + `-new ` + + `-nodes ` + + `-key "${certPath}/root-CA.key.pem" ` + + `-days 1024 ` + + `-out "${certPath}/root-CA.crt.pem" ` + + `-subj "/C=NO/ST=Oslo/L=Oslo/O=Luxcena Neo Self-Signing Authority/CN=${config.CN}"` + ); + + // Create a Device Certificate for each domain, + // such as example.com, *.example.com, awesome.example.com + // NOTE: You MUST match CN to the domain name or ip address you want to use + res = openssl( + `genrsa ` + + `-out "${certPath}/privkey.pem" ` + + `2048` + ); + + // Create a request from your Device, which your Root CA will sign + res = openssl( + `req ` + + `-new ` + + `-key "${certPath}/privkey.pem" ` + + `-out "${certPath}/csr.pem" ` + + `-subj "/C=NO/ST=Oslo/L=Oslo/O=Luxcena Neo Self-Signing Autohity/CN=${config.CN}"` + ); + + // Sign the request from Device with your Root CA + // -CAserial certs/ca/my-root-ca.srl + res = openssl( + `x509 ` + + `-req ` + + `-in "${certPath}/csr.pem" ` + + `-CA "${certPath}/root-CA.crt.pem" ` + + `-CAkey "${certPath}/root-CA.key.pem" ` + + `-CAcreateserial ` + + `-out "${certPath}/cert.pem" ` + + `-days 500` + ); + + let creationDate = Date.now(); + config.certMade = true; + config.certDate = creationDate; + config.certExpire = creationDate + (500*86400000); + config.certCN = config.CN; + + logger.info("Self-signed certificate created."); + + } + + } + +function openssl(command) { + try { + let stdout = execSync("openssl " + command); + return true + } catch (e) { + return false + } + } + +module.exports = (_neoModules) => { + neoModules = _neoModules; + return new CertMon(); +}; + \ No newline at end of file -- cgit v1.2.3 From 8fe3b246a12d409b0819b574a050b64ca96ce251 Mon Sep 17 00:00:00 2001 From: jakobst1n Date: Sun, 3 Oct 2021 18:36:12 +0200 Subject: :boom: Change paths to be in normal linux locations --- app.js | 27 +++--- bin/install.sh | 142 +++++++------------------------- bin/luxcena-neo-cli.sh | 41 ++++++++- bin/luxcena-neo.service | 6 +- bin/luxcena-neo.sh | 4 +- src/NeoRuntimeManager/RuntimeProcess.js | 4 +- src/NeoRuntimeManager/index.js | 8 +- src/SSLCert/index.js | 6 +- src/SelfUpdater/index.js | 4 +- src/SocketIO/index.js | 4 +- src/UserData/index.js | 48 +++++------ 11 files changed, 124 insertions(+), 170 deletions(-) (limited to 'src/SSLCert/index.js') diff --git a/app.js b/app.js index 0196645..30146ea 100644 --- a/app.js +++ b/app.js @@ -2,22 +2,27 @@ let fse = require("fs-extra"); let events = require('events'); // Firstly we set up all globals, check that the usrData dir exists, if not, we run the setup -let userDir = "/home/lux-neo"; -if (process.argv.length >= 3) { userDir = process.argv[2]; } -if (!fse.existsSync(userDir + "/userdata/")) { +global.__appdir = "/opt/luxcena-neo"; +global.__configdir = "/etc/luxcena-neo"; +global.__datadir = "/var/luxcena-neo"; +global.__logdir = "/var/log/luxcena-neo"; + +if ((process.argv.length >= 3) && (process.argv[2] == "dev")) { + global.__appdir = __dirname; + global.__configdir = __dirname + "/tmp/config"; + global.__datadir = __dirname + "/tmp/userdata"; + global.__logdir = __dirname + "/tmp/logs"; +} +if (!fse.existsSync(global.__datadir)) { console.log(`CRITICAL UserDir not found '${userDir}'! Exiting...`); process.exit(1); } -// Global path variables -global.__basedir = __dirname + ""; -global.__datadir = userDir + "/userdata"; -global.__logdir = userDir + "/logs"; // global eventEmitter global.__event = new events.EventEmitter(); // Secondly we setup the logger, -let logger = require("./src/logger"); +let logger = require("./src/Logger"); logger.info("Starting Luxcena-Neo..."); let neoModules = {}; @@ -33,14 +38,14 @@ let express = require("express"); let https = require("https"); let app = express(); let server = https.createServer({ - key: fse.readFileSync(__datadir + "/config/certs/privkey.pem"), - cert: fse.readFileSync(__datadir + "/config/certs/cert.pem") + key: fse.readFileSync(__configdir + "/certs/privkey.pem"), + cert: fse.readFileSync(__configdir + "/certs/cert.pem") }, app ); let io = require("socket.io")(server); require("./src/SocketIO")(neoModules, io); -app.use("/", express.static(__basedir + "/public")); +app.use("/", express.static(__appdir + "/public")); server.listen(neoModules.userData.config.HTTP.port, () => { let host = server.address().address; diff --git a/bin/install.sh b/bin/install.sh index cd4f07b..a7b811b 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -5,9 +5,6 @@ printf '%s\n' "Luxcena-neo Installer" tput sgr0 printf '\e[93m%s\e[0m\n\n' "---------------------" -LOG="/tmp/luxcena-neo.install.log" -echo "Starting Luxcena-neo installer..." > $LOG - if [ "$EUID" -ne 0 ]; then echo "You need to run this script as root." echo "Try running with 'sudo ./bin/install.sh'" @@ -16,139 +13,56 @@ fi function die() { tput setaf 1 - printf "\n\nInstall failed.\n" - printf "Check the logfile at '/tmp/lucxena-neo.install.log'.\n" - printf "Use this command to see the last 30 lines of the file;\n" - printf " tail -n 30 /tmp/luxcena-neo.install.log" + printf "\n\nInstall failed, successfull steps not reversed.\n" tput sgr0 exit 1 } -function dlgYN() { - tput sc - tput setaf 4 - printf "$1 (y/n)? " - while : - do - read -n 1 -p "" YNQuestionAnswer - if [[ $YNQuestionAnswer == "y" ]]; then - tput rc; tput el - printf ". $1?: \e[0;32mYes\e[0m\n" - tput sc - eval $2=1 # Set parameter 2 of input to the return value - break - elif [[ $YNQuestionAnswer == "n" ]]; then - tput rc; tput el - printf ". $1?: \e[0;31mNo\e[0m\n" - eval $2=0 # Set parameter 2 of input to the return value - break - fi - done -} - -# Update system -dlgYN ". Update your system" res -if [ $res -eq 1 ]; then - tput sc - apt-get -y update &>> $LOG || die - apt-get -y upgrade &>> $LOG || die - tput rc; tput ed -fi - -# Install packages -dlgYN ". Install required packages" res -if [ $res -eq 1 ]; then - tput sc - apt-get -y install nodejs scons python-dev swig &>> $LOG || die - if [ $? -eq 0 ]; then - tput rc; tput ed - printf "✓" - else - printf "\nInstall failed.\n" - exit 1 - fi -else - tput setaf 2 - printf " We are now assuming that all the following packages exists on your system:\n" - printf " nodejs scons python-dev swig\n" - tput sgr0 -fi - -# Install led-library -dlgYN ". Install jgarff's rpi_ws281x library" res -if [ $res -eq 1 ]; then - tput sc - git clone https://github.com/jgarff/rpi_ws281x /tmp/rpi_ws281x # TODO CHANGE PATH - python /tmp/rpi_ws281x/python/setup.py install # TODO CHANGE PAHT - if [ $? -eq 0 ]; then - tput rc; tput ed - printf "✓" - else - printf "\nInstall failed.\n" - exit 1 - fi -fi - -tput setaf 4 -printf ". Installing the app itself...\n" -tput sgr0 - # Create user 'luxcena-neo' tput setaf 8 -printf '%s\n' " - Creating user 'lux-neo'..." +printf '%s\n' "- Creating user 'lux-neo'..." tput sgr0 username="lux-neo" egrep "^$username" /etc/passwd >/dev/null if [ $? -eq 0 ]; then - echo "User already exists, continuing..." + echo "User already exists, continuing..." else - #pass=$(perl -e 'print crypt($ARGV[0], "password")' $password) - useradd -m $username &>> $LOG || die + useradd -m $username || die fi +usermod -a -G gpio $username +usermod -a -G spi $username # First we make our directories tput setaf 8 -printf '%s\n' " - Making app-dir (/bin/luxcena-neo)..." -tput sgr0 -userDir=$(eval echo "~$username") -#mkdir -p "$userDir/install" &>> $LOG || die -#chown $username:$username "$userDir/install" &>> $LOG || die -mkdir -p "$userDir/src" &>> $LOG || die -chown $username:$username "$userDir/src" &>> $LOG || die -mkdir -p "$userDir/userdata" &>> $LOG || die -chown $username:$username "$userDir/userdata" &>> $LOG || die - -# Third we copy the source into the correct swap-folder -tput setaf 8 -printf '%s\n' " - Copying sourceCode to app-dir..." +printf '%s\n' "- Making directories..." tput sgr0 -cp -r . "$userDir/src" &>> $LOG || die -chown -R $username:$username "$userDir/src" &>> $LOG || die - -# fourth we run npm i -tput setaf 8 -printf '%s\n' " - Running npm i..." -tput sgr0 -tput sc -export NODE_ENV=production &>> $LOG || die -runuser -l $username -c 'npm --prefix ~/src install ~/src --only=production' &>> $LOG || die # This is probably a bit overkill to have --only=... but better safe than sorry? -tput rc; tput ed +[ -d "/opt/luxcena-neo/" ] && echo "Seems like luxcena-neo is already installed, please do update instead" && die +mkdir -p "/opt/luxcena-neo" || die +chown $username:$username "/opt/luxcena-neo" || die +mkdir -p "/var/luxcena-neo" || die +chown $username:$username "/var/luxcena-neo" || die +mkdir -p "/etc/luxcena-neo" || die +chown $username:$username "/etc/luxcena-neo" || die +mkdir -p "/var/log/luxcena-neo" || die +chown $username:$username "/var/log/luxcena-neo" || die + +printf '%s' "Which branch do you want to install (default: master)? " +read BRANCH +if [ -z "$BRANCH" ]; then + BRANCH="master" +fi -# fourth we copy the cli to our bin folder +# Get source code tput setaf 8 -printf '%s\n' " - Adding cli-script..." +printf '%s\n' "- Fetch source code..." tput sgr0 -cp bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh &>> $LOG || die -ln -sf /usr/bin/luxcena-neo-cli.sh /usr/bin/lux-neo &>> $LOG || die -tput rc; tput ed +runuser -l $username -c "git clone -b $BRANCH https://github.com/jakobst1n/luxcena-neo /opt/luxcena-neo/" || die -# Fifth we add the service files +# Install all packages, build the app, and prepare everything tput setaf 8 -printf '%s\n' " - Adding service-file to systemd..." +printf '%s\n' "- Running installer (updater) from newly fetched source code..." tput sgr0 -cp bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service &>> $LOG || die -systemctl daemon-reload &>> $LOG || die +/opt/luxcena-neo/bin/luxcena-neo-cli.sh update || die # Installation is done! printf '\n\e[5m%s\e[0m\n' "🎉Luxcena-Neo is now installed🎉" -printf 'You can now delete this folder' diff --git a/bin/luxcena-neo-cli.sh b/bin/luxcena-neo-cli.sh index defb766..b3c6553 100755 --- a/bin/luxcena-neo-cli.sh +++ b/bin/luxcena-neo-cli.sh @@ -65,14 +65,49 @@ if [ "$action" == "update" ]; then exit 1 fi + # Stop the service if it is running already systemctl stop luxcena-neo - runuser -l 'lux-neo' -c 'git -C ~/src pull' + # Go to source code directory + WDIR="/opt/luxcena-neo" + #cd "$WDIR" + + # Fetch newest changes on branch + runuser -l 'lux-neo' -c "git -C $WDIR pull" || die + + # Add node repo + curl -fsSL https://deb.nodesource.com/setup_14.x | bash - || die + + # Make sure nodejs and prerequisites is installed + apt install nodejs python-pip || die + + # Make sure we have python virtualenv installed + pip3 install virtualenv || die + + # Create and configure python virtualenv + runuser -l 'lux-neo' -c "rm -rf $WDIR/NeoRuntime/Runtime/venv" || die + runuser -l 'lux-neo' -c "virtualenv -p /usr/bin/python3 $WDIR/NeoRuntime/Runtime/venv" || die + runuser -l 'lux-neo' -c "source $WDIR/NeoRuntime/Runtime/venv/bin/activate && pip install rpi_ws281x" || die + + # Build and run all npm scripts if [ "$2" != "skipNode" ]; then - runuser -l 'lux-neo' -c 'export NODE_ENV=production; npm --prefix ~/src install ~/src --only=production' + runuser -l 'lux-neo' -c "export NODE_ENV=development; npm --prefix $WDIR install $WDIR" || die fi + ##runuser -l 'lux-neo' -c "cd $WDIR && npm run build:frontend" || die + ##runuser -l 'lux-neo' -c "cd $WDIR && npm run build:fontawesome" || die + ##runuser -l 'lux-neo' -c "cd $WDIR && npm run build:dialog-polyfill" || die + runuser -l 'lux-neo' -c "npm --prefix \"$WDIR\" run build:frontend" || die + runuser -l 'lux-neo' -c "npm --prefix \"$WDIR\" run build:fontawesome" || die + runuser -l 'lux-neo' -c "npm --prefix \"$WDIR\" run build:dialog-polyfill" || die + + + # Install new cli script + cp /opt/luxcena-neo/bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh || die + + # Install updated systemd script + cp /opt/luxcena-neo/bin/luxcena-neo.service /etc/systemd/system/luxcena-neo.service || die + systemctl daemon-reload || die - cp /home/lux-neo/src/bin/luxcena-neo-cli.sh /usr/bin/luxcena-neo-cli.sh printf "Update complete.\n" systemctl start luxcena-neo exit 0 diff --git a/bin/luxcena-neo.service b/bin/luxcena-neo.service index efea1ad..b4115be 100644 --- a/bin/luxcena-neo.service +++ b/bin/luxcena-neo.service @@ -2,13 +2,13 @@ Description=Luxcena Neo [Service] -ExecStart=/home/lux-neo/src/bin/luxcena-neo.sh +ExecStart=/opt/luxcena-neo/bin/luxcena-neo.sh Restart=always RestartSec=10 Environment=PATH=/usr/bin:/usr/local/bin -Environment=NODE_ENV=production -WorkingDirectory=/home/lux-neo/src/ +Environment=NODE_ENV=development +WorkingDirectory=/opt/luxcena-neo/ [Install] WantedBy=multi-user.target diff --git a/bin/luxcena-neo.sh b/bin/luxcena-neo.sh index fc41f75..a861d87 100755 --- a/bin/luxcena-neo.sh +++ b/bin/luxcena-neo.sh @@ -5,5 +5,5 @@ # the server needs root as well. #runuser -l pi -c "export NODE_ENV=production; node ~/luxcena-neo-install/src/app.js" -export NODE_ENV=production -node /home/lux-neo/src/app.js >> /home/lux-neo/logs/service.log +export NODE_ENV=development +node /opt/luxcena-neo/app.js >> /var/log/luxcena-neo/service.log diff --git a/src/NeoRuntimeManager/RuntimeProcess.js b/src/NeoRuntimeManager/RuntimeProcess.js index 60f6a28..0058382 100644 --- a/src/NeoRuntimeManager/RuntimeProcess.js +++ b/src/NeoRuntimeManager/RuntimeProcess.js @@ -32,8 +32,8 @@ class RuntimeProcess { "python3", [ "-u", // This makes us able to get real-time output - `${__basedir}/NeoRuntime/Runtime/neo_runtime.py`, - `--strip-config="${__datadir}/config/strip.ini"`, + `${__appdir}/NeoRuntime/Runtime/neo_runtime.py`, + `--strip-config="${__configdir}/strip.ini"`, `--mode-path="${this.modePath}"`, `--mode-entry=script` ] diff --git a/src/NeoRuntimeManager/index.js b/src/NeoRuntimeManager/index.js index 62acb8a..e4718e4 100644 --- a/src/NeoRuntimeManager/index.js +++ b/src/NeoRuntimeManager/index.js @@ -8,7 +8,7 @@ const fs = require("fs"); const fsPromises = fs.promises; const RuntimeProcess = require("./RuntimeProcess"); -let logger = require(__basedir + "/src/logger"); +let logger = require(__appdir + "/src/Logger"); const EventEmitter = require('events'); /** @type {object} this should be a pointer to a object referencing all neoModules (see app.js) */ @@ -51,7 +51,7 @@ function isMode(path) { */ function listModes() { let modeDirs = [ - ["builtin/", fs.readdirSync(__basedir + "/NeoRuntime/builtin")], + ["builtin/", fs.readdirSync(__appdir + "/NeoRuntime/builtin")], ["remote/", fs.readdirSync(__datadir + "/remoteCode")], ["user/", fs.readdirSync(__datadir + "/userCode")] ] @@ -167,7 +167,7 @@ function getModePath(modeId) { let location = path.splice(0, 1).toString(); if (location === "user") { path = __datadir + "/userCode/" + path.join("/"); } if (location === "remote") { path = __datadir + "/remoteCode/" + path.join("/"); } - if (location === "builtin") { path = __basedir + "/NeoRuntime/builtin/" + path.join("/"); } + if (location === "builtin") { path = __appdir + "/NeoRuntime/builtin/" + path.join("/"); } return path; } @@ -306,4 +306,4 @@ module.exports = (_neoModules) => { startDebugger, stopDebugger, saveModeCode, startMode, stopMode, restartMode } -}; \ No newline at end of file +}; diff --git a/src/SSLCert/index.js b/src/SSLCert/index.js index 6dad579..d235c9b 100644 --- a/src/SSLCert/index.js +++ b/src/SSLCert/index.js @@ -7,7 +7,7 @@ * @author jakobst1n. * @since 14.16.2019 */ - let logger = require(__basedir + "/src/logger"); + let logger = require(__appdir + "/src/Logger"); const fs = require("fs"); const { execSync } = require("child_process"); @@ -20,7 +20,7 @@ var neoModules; class CertMon { constructor(configPath, certPath, httpsConfig) { - this.certPath = __datadir + "/config/certs/"; + this.certPath = __configdir + "/certs/"; let valid = this.checkValidity(); if (!valid) { @@ -141,4 +141,4 @@ module.exports = (_neoModules) => { neoModules = _neoModules; return new CertMon(); }; - \ No newline at end of file + diff --git a/src/SelfUpdater/index.js b/src/SelfUpdater/index.js index 3c5546b..5a9baa3 100644 --- a/src/SelfUpdater/index.js +++ b/src/SelfUpdater/index.js @@ -2,14 +2,14 @@ let fs = require("fs-extra"); let url = require("url"); let request = require('request'); let exec = require("child_process").exec; -let logger = require(__basedir + "/src/logger"); +let logger = require(__appdir + "/src/Logger"); let neoModules; class VersionChecker { constructor() { - this.CPackageJson = JSON.parse(fs.readFileSync(__basedir + "/package.json")); + this.CPackageJson = JSON.parse(fs.readFileSync(__appdir + "/package.json")); this.version = this.CPackageJson["version"]; this.repoLink = this.CPackageJson["repository"]["url"]; diff --git a/src/SocketIO/index.js b/src/SocketIO/index.js index fdaad56..e20460d 100644 --- a/src/SocketIO/index.js +++ b/src/SocketIO/index.js @@ -8,7 +8,7 @@ * @since 19.12.2019 */ -let logger = require(__basedir + "/src/logger"); +let logger = require(__appdir + "/src/Logger"); var exec = require('child_process').exec; var CryptoJS = require("crypto-js"); let fs = require("fs"); @@ -350,4 +350,4 @@ module.exports = (_neoModules, io) => { authorizedNamespace: createAuthorizedNamespace(io) } }; - \ No newline at end of file + diff --git a/src/UserData/index.js b/src/UserData/index.js index e5318c9..4684a75 100644 --- a/src/UserData/index.js +++ b/src/UserData/index.js @@ -6,7 +6,7 @@ * @since 19.12.2019 */ -let logger = require(__basedir + "/src/logger"); +let logger = require(__appdir + "/src/Logger"); let fse = require("fs-extra"); let ini = require('ini'); @@ -16,7 +16,7 @@ let neoModules; * This method will ensure that all required fields are in config.ini */ function ensureMainConfig() { - var config = ini.decode(fse.readFileSync(__datadir + "/config/config.ini", 'utf-8')) + var config = ini.decode(fse.readFileSync(__configdir + "/config.ini", 'utf-8')) if (config.instanceName == null) { config.instanceName = "neoStrip"; } if (config.activeMode == null) { config.activeMode = "builtin/static"; } @@ -40,14 +40,14 @@ function ensureMainConfig() { if (config.DiscoveryServer.address == null) { config.DiscoveryServer.address = "https://erj46s.deta.dev"; } if (config.DiscoveryServer.broadcastSelf == null) { config.DiscoveryServer.broadcastSelf = false; } - fse.writeFileSync(__datadir + "/config/config.ini", ini.encode(config)) + fse.writeFileSync(__configdir + "/config.ini", ini.encode(config)) } /** * This method will ensure that all required fields are in config.ini */ function ensureStripConfig() { - var config = ini.decode(fse.readFileSync(__datadir + "/config/strip.ini", 'utf-8')) + var config = ini.decode(fse.readFileSync(__configdir + "/strip.ini", 'utf-8')) if (config.DEFAULT == null) { config.DEFAULT = {}; } if (config.DEFAULT.led_pin == null) { config.DEFAULT.led_pin = 18; } @@ -58,7 +58,7 @@ function ensureStripConfig() { if (config.DEFAULT.segments == null) { config.DEFAULT.segments = ""; } if (config.DEFAULT.matrix == null) { config.DEFAULT.matrix = ""; } - fse.writeFileSync(__datadir + "/config/strip.ini", ini.encode(config)) + fse.writeFileSync(__configdir + "/strip.ini", ini.encode(config)) } /** @@ -70,24 +70,24 @@ function init() { logger.info("Ensuring all folder in UserDir exists..."); fse.ensureDirSync(__datadir + "/"); - fse.ensureDirSync(__datadir + "/config/"); - fse.ensureDirSync(__datadir + "/config/certs"); + fse.ensureDirSync(__configdir); + fse.ensureDirSync(__configdir + "/certs"); fse.ensureDirSync(__datadir + "/userCode/"); fse.ensureDirSync(__datadir + "/remoteCode/"); // Generate config-files - if (!fse.existsSync(__datadir + "/config/config.ini")) { - fse.closeSync(fse.openSync(__datadir + "/config/config.ini", 'w')); + if (!fse.existsSync(__configdir + "/config.ini")) { + fse.closeSync(fse.openSync(__configdir + "/config.ini", 'w')); } ensureMainConfig(); - if (!fse.existsSync(__datadir + "/config/strip.ini")) { - fse.closeSync(fse.openSync(__datadir + "/config/strip.ini", 'w')); + if (!fse.existsSync(__configdir + "/strip.ini")) { + fse.closeSync(fse.openSync(__configdir + "/strip.ini", 'w')); } ensureStripConfig(); - if (!fse.existsSync(__datadir + "/config/users.ini")) { - fse.writeFileSync(__datadir + "/config/users.ini", ini.encode({ + if (!fse.existsSync(__configdir + "/users.ini")) { + fse.writeFileSync(__configdir + "/users.ini", ini.encode({ "neo": { "password": "5adbc90fb4716fff62d9cf634837e22f29b011803ba29cee51f921b920fa941651737bd15d00dc72e4cbeee5e64e06ec99cc50ea917285a029797a98740cce0f", "salt": "59b6de1040f3ae3c63de984ca5d61ef46f41dc6ecead3a9d5dab69f0bb3636aa49017e179b74dbcdb407f62bc139a7d55aa78fe2bbdd5327609ea124b2fa03b1" @@ -193,11 +193,11 @@ function getFullConfig(file, addSetters=true) { * @return {object} Standardform return object */ function saveUser(username, salt, password) { - let config = ini.decode(fse.readFileSync(__datadir + "/config/users.ini", 'utf-8')) + let config = ini.decode(fse.readFileSync(__configdir + "/users.ini", 'utf-8')) config[username] = {} config[username].salt = salt config[username].password = password - fse.writeFileSync(__datadir + "/config/users.ini", ini.encode(config)) + fse.writeFileSync(__configdir + "/users.ini", ini.encode(config)) return {success: true} } @@ -207,7 +207,7 @@ function getFullConfig(file, addSetters=true) { * @return {object} with username, salt and hash properties. */ function getUser(username) { - let config = ini.decode(fse.readFileSync(__datadir + "/config/users.ini", 'utf-8')) + let config = ini.decode(fse.readFileSync(__configdir + "/users.ini", 'utf-8')) if (Object.prototype.hasOwnProperty.call(config, username)) { return {...config[username], username: username} } @@ -220,7 +220,7 @@ function getUser(username) { * @return {array} usernames */ function getUsers() { - let config = ini.decode(fse.readFileSync(__datadir + "/config/users.ini", "utf-8")); + let config = ini.decode(fse.readFileSync(__configdir + "/users.ini", "utf-8")); let users = []; for (const username of Object.keys(config)) { users.push(username); @@ -234,11 +234,11 @@ function getUsers() { * @return {object} Standardform success object. */ function deleteUser(username) { - let config = ini.decode(fse.readFileSync(__datadir + "/config/users.ini", 'utf-8')) + let config = ini.decode(fse.readFileSync(__configdir + "/users.ini", 'utf-8')) if (config.length <= 1) { return {success: false, reason: "cannot delete only user"}; } if (!Object.prototype.hasOwnProperty.call(config, username)) { return {success: false, reason: "user not found", detail: username}; } delete config[username]; - fse.writeFileSync(__datadir + "/config/users.ini", ini.encode(config)); + fse.writeFileSync(__configdir + "/users.ini", ini.encode(config)); return {success: true} } @@ -254,7 +254,7 @@ function deleteUser(username) { function createNewUserMode(name, template) { source_script = null; if ((template === "template/base") || (template === "") || (template == null)) { - source_script = __basedir + "/NeoRuntime/special/template_base/"; + source_script = __appdir + "/NeoRuntime/special/template_base/"; } else { source_script = neoModules.neoRuntimeManager.getModePath(template); } @@ -310,7 +310,7 @@ module.exports = (_neoModules) => { }, strip: { get: () => { - let c = getFullConfig(`${__datadir}/config/strip.ini`, addSetters=false); + let c = getFullConfig(`${__configdir}/strip.ini`, addSetters=false); c.DEFAULT.matrix = JSON.parse(c.DEFAULT.matrix); c.DEFAULT.segments = c.DEFAULT.segments.split(" "); return c.DEFAULT; @@ -318,13 +318,13 @@ module.exports = (_neoModules) => { set: (c) => { c.segments = c.segments.join(" "); c.matrix = JSON.stringify(c.matrix); - return saveConfig(`${__datadir}/config/strip.ini`, {DEFAULT: c}, removeSetters=false); + return saveConfig(`${__configdir}/strip.ini`, {DEFAULT: c}, removeSetters=false); }, }, - config: getFullConfig(`${__datadir}/config/config.ini`), + config: getFullConfig(`${__configdir}/config.ini`), mode: { create: createNewUserMode, delete: deleteUserMode } } -}; \ No newline at end of file +}; -- cgit v1.2.3