initial commit
This commit is contained in:
174
src/lib/utils.js
Normal file
174
src/lib/utils.js
Normal file
@ -0,0 +1,174 @@
|
||||
const child_process = require('child_process');
|
||||
const execSync = child_process.execSync;
|
||||
|
||||
const _consoleColorCodes = {
|
||||
Reset: "\x1b[0m",
|
||||
Bright: "\x1b[1m",
|
||||
Dim: "\x1b[2m",
|
||||
Underscore: "\x1b[4m",
|
||||
Blink: "\x1b[5m",
|
||||
Reverse: "\x1b[7m",
|
||||
Hidden: "\x1b[8m",
|
||||
|
||||
FgBlack: "\x1b[30m",
|
||||
FgRed: "\x1b[31m",
|
||||
FgGreen: "\x1b[32m",
|
||||
FgYellow: "\x1b[33m",
|
||||
FgBlue: "\x1b[34m",
|
||||
FgMagenta: "\x1b[35m",
|
||||
FgCyan: "\x1b[36m",
|
||||
FgWhite: "\x1b[37m",
|
||||
|
||||
BgBlack: "\x1b[40m",
|
||||
BgRed: "\x1b[41m",
|
||||
BgGreen: "\x1b[42m",
|
||||
BgYellow: "\x1b[43m",
|
||||
BgBlue: "\x1b[44m",
|
||||
BgMagenta: "\x1b[45m",
|
||||
BgCyan: "\x1b[46m",
|
||||
BgWhite: "\x1b[47m",
|
||||
}
|
||||
|
||||
function Utils() {
|
||||
this.consoleColorCodes = _consoleColorCodes;
|
||||
}
|
||||
|
||||
Utils.prototype.isCurrentUserRoot = function () {
|
||||
return process.getuid() == 0; // UID 0 is always root
|
||||
}
|
||||
|
||||
Utils.prototype.printWelcomeMessage = function () {
|
||||
console.log("###################################")
|
||||
console.log("##### RaspiWiFi Intial Setup #####")
|
||||
console.log("###################################")
|
||||
}
|
||||
|
||||
Utils.prototype.printSetupCanceledMessage = function () {
|
||||
console.log("")
|
||||
console.log("")
|
||||
console.log("===================================================")
|
||||
console.log("---------------------------------------------------")
|
||||
console.log("")
|
||||
console.log("RaspiWiFi installation cancelled. Nothing changed...")
|
||||
console.log("")
|
||||
console.log("---------------------------------------------------")
|
||||
console.log("===================================================")
|
||||
console.log("")
|
||||
console.log("")
|
||||
}
|
||||
|
||||
Utils.prototype.printFinishedMessage = function () {
|
||||
console.log("")
|
||||
console.log("")
|
||||
console.log("#####################################")
|
||||
console.log("##### RaspiWiFi Setup Complete #####")
|
||||
console.log("#####################################")
|
||||
console.log("")
|
||||
console.log("")
|
||||
}
|
||||
|
||||
Utils.prototype.checkForYN = function (input) {
|
||||
if (input.toLowerCase() !== 'y' && input.toLowerCase() !== 'n') {
|
||||
this.printColor(this.consoleColorCodes.FgRed, 'Please enter y or n!');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Utils.prototype.printColor = function (color, ...args) {
|
||||
console.log(color, ...args, this.consoleColorCodes.Reset);
|
||||
}
|
||||
|
||||
Utils.prototype.clearConsole = () => {
|
||||
//process.stdout.write('\033c');
|
||||
process.stdout.write('\x1bc');
|
||||
}
|
||||
|
||||
Utils.prototype.installSystemDependencies = function () {
|
||||
this.clearConsole();
|
||||
execSync('apt update');
|
||||
this.clearConsole();
|
||||
execSync('apt install python3 python3-rpi.gpio python3-pip dnsmasq hostapd -y')
|
||||
}
|
||||
|
||||
Utils.prototype.copyConfigs = function (isWpaEabled) {
|
||||
/* Creating application directories */
|
||||
this.printColor(null, 'Creating application directories');
|
||||
execSync('mkdir /usr/lib/raspiwifi');
|
||||
execSync('mkdir /etc/raspiwifi');
|
||||
execSync('mkdir /etc/cron.raspiwifi');
|
||||
|
||||
/* Copy application files */
|
||||
this.printColor(null, 'Copy application files');
|
||||
execSync('cp -a libs/* /usr/lib/raspiwifi/');
|
||||
|
||||
/* Backing up current files */
|
||||
this.printColor(null, 'Backing up current files');
|
||||
execSync('mv /etc/wpa_supplicant/wpa_supplicant.conf /etc/wpa_supplicant/wpa_supplicant.conf.original');
|
||||
execSync('mv /etc/dnsmasq.conf /etc/dnsmasq.conf.original');
|
||||
execSync('mv /etc/dhcpcd.conf /etc/dhcpcd.conf.original');
|
||||
|
||||
/* Copy default files for basic device setup*/
|
||||
this.printColor(null, 'Copy default files for basic device setup');
|
||||
execSync('cp /usr/lib/raspiwifi/reset_device/static_files/dnsmasq.conf /etc/');
|
||||
execSync('cp /usr/lib/raspiwifi/reset_device/static_files/dhcpcd.conf /etc/');
|
||||
if (isWpaEabled)
|
||||
execSync('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.wpa /etc/hostapd/hostapd.conf');
|
||||
else
|
||||
execSync('cp /usr/lib/raspiwifi/reset_device/static_files/hostapd.conf.nowpa /etc/hostapd/hostapd.conf');
|
||||
|
||||
execSync('cp /usr/lib/raspiwifi/reset_device/static_files/aphost_bootstrapper /etc/cron.raspiwifi')
|
||||
execSync('chmod +x /etc/cron.raspiwifi/aphost_bootstrapper');
|
||||
|
||||
execSync('mv /usr/lib/raspiwifi/reset_device/static_files/raspiwifi.conf /etc/raspiwifi');
|
||||
|
||||
/* TODO: Setting up CRON for running monitoring jobs */
|
||||
this.printColor(null, 'Setting up CRON for running monitoring jobs');
|
||||
execSync('echo "# RaspiWiFi Startup" >> /etc/crontab');
|
||||
execSync('echo "@reboot root run-parts /etc/cron.raspiwifi/" >> /etc/crontab');
|
||||
|
||||
/* Finishing things up */
|
||||
this.printColor(null, 'Finishing things up');
|
||||
execSync('touch /etc/raspiwifi/host_mode');
|
||||
}
|
||||
|
||||
Utils.prototype.updateMainConfigFile = function ({ ssid, autoConfig, autoConfigDelay, sslEnabled, serverPort, wpaEnabled, wpaSecret }) {
|
||||
console.log(this);
|
||||
const outputString = this.serializeMainConfigFile({ ssid, autoConfig, autoConfigDelay, sslEnabled, serverPort, wpaEnabled, wpaSecret });
|
||||
const fileName = '/etc/raspiwifi/raspiwifi.conf'
|
||||
execSync('cat <<EOF >' + fileName + '\n' + outputString)
|
||||
|
||||
}
|
||||
|
||||
Utils.prototype.reboot = function () {
|
||||
execSync('reboot');
|
||||
}
|
||||
|
||||
Utils.prototype.serializeMainConfigFile = function ({ ssid, autoConfig, autoConfigDelay, sslEnabled, serverPort, wpaEnabled, wpaSecret }) {
|
||||
const output = {
|
||||
'SSID': ssid,
|
||||
'AutoReconfig': autoConfig,
|
||||
'AutoReconfigDelay': autoConfigDelay,
|
||||
'SSLEnabled': sslEnabled,
|
||||
'ServerPort': serverPort,
|
||||
'WPAEnabled': wpaEnabled,
|
||||
'PSK': wpaSecret ?? '',
|
||||
}
|
||||
return JSON.stringify(output, null, 2);
|
||||
}
|
||||
|
||||
Utils.prototype.deserializeMainConfigFile = (input) => {
|
||||
const obj = JSON.parse(input);
|
||||
return {
|
||||
ssid: obj['SSID'],
|
||||
autoConfig: obj['AutoReconfig'],
|
||||
autoConfigDelay: obj['AutoReconfigDelay'],
|
||||
sslEnabled: obj['SSLEnabled'],
|
||||
serverPort: obj['ServerPort'],
|
||||
wpaEnabled: obj['WPAEnabled'],
|
||||
wpaSecret: obj['PSK'] ?? '',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = Utils;
|
||||
Reference in New Issue
Block a user