update: change config variable name, allow user override (user_config.json)

This commit is contained in:
zefie
2021-07-19 04:26:32 -04:00
parent ea3235957b
commit 8b5a390773
11 changed files with 103 additions and 35 deletions

1
.gitignore vendored
View File

@@ -364,3 +364,4 @@ FodyWeavers.xsd
/hacktv_updsrv/ServiceLogPost/1626307222_warning_812bf30600b002bb
/hacktv_updsrv/ServiceVault/wtv-music/midi
/zefie_wtvp_minisrv/ServiceVault/wtv-home/6969.html
/zefie_wtvp_minisrv/user_config.json

View File

@@ -16,7 +16,7 @@ if (request_headers.query.relogin) { // skip tellyscript
// if relogin, skip tellyscript
var romtype, file_path = null;
if (!request_headers.query.relogin && services_configured.config.send_tellyscripts) {
if (!request_headers.query.relogin && minisrv_config.config.send_tellyscripts) {
var romtype = getSessionData(socket_session_data[socket.id].ssid, 'wtv-client-rom-type');
}

View File

@@ -6,7 +6,7 @@ headers = "200 OK\n"
if (request_headers.query.raw) {
if ((/\.brom$/).test(request_path)) headers += "Content-Type: binary/x-wtv-bootrom"; // maybe?
else headers += "Content-Type: binary/x-wtv-flashblock";
if (services_configured.services[service_name].use_zefie_server) {
if (minisrv_config.services[service_name].use_zefie_server) {
// get flashrom files from archive.midnightchannel.net
var options = {
host: "archive.midnightchannel.net",

View File

@@ -10,7 +10,7 @@ if (!request_headers.query.path) {
data = errpage[1];
} else {
var request_path = unescape(request_headers.query.path);
if (services_configured.services[service_name].use_zefie_server) {
if (minisrv_config.services[service_name].use_zefie_server) {
// read first 256 bytes of flashrom file from archive.midnightchannel.net
// to get `flashrom_message` and `numparts` if missing
var options = {

View File

@@ -58,7 +58,7 @@ Updating complete
<td colspan=9 width=100 height=258 valign=top align=left>
<font size=+1>
The update is complete.<br>
<p><font size=+1>Choose <b>Connect Now</b> if you<br>want to connect to ${services_configured.config.service_name}.
<p><font size=+1>Choose <b>Connect Now</b> if you<br>want to connect to ${minisrv_config.config.service_name}.
<p><font size=+1>Press the <b>power</b> button to switch<br>off your unit.
</font>
<tr>

View File

@@ -12,7 +12,7 @@ for (const [key, value] of Object.entries(request_headers.query)) {
proxy_query += "&" + key + "=" + value;
}
if (!services_configured.services[service_name].use_zefie_server) {
if (!minisrv_config.services[service_name].use_zefie_server) {
proxy_query += "&minisrv_local_mode=true";
}

View File

@@ -21,7 +21,7 @@ Content-length: 0`;
logdata_outstring += "\r\n";
var logdata_outstring_hex = Buffer.from(logdata_outstring, 'utf8').toString('hex');
logdata_outstring_hex += request_headers.post_data.toString(CryptoJS.enc.Hex);
if (services_configured.services[service_name].write_logs_to_disk) {
if (minisrv_config.services[service_name].write_logs_to_disk) {
fs.writeFile(fullpath, logdata_outstring_hex, "Hex", function () {
if (!zquiet) console.log(" * Wrote POST log data from", processSSID(socket_session_data[socket.id].ssid), "for", socket.id);
sendToClient(socket, headers, data);
@@ -41,7 +41,7 @@ Content-length: 0`;
logdata_outstring += k + "=" + unescape(request_headers.query[k].toString()) + "\r\n";
});
var logdata_outstring_hex = Buffer.from(logdata_outstring, 'utf8').toString('hex');
if (services_configured.services[service_name].write_logs_to_disk) {
if (minisrv_config.services[service_name].write_logs_to_disk) {
fs.writeFile(fullpath, logdata_outstring_hex, "Hex", function () {
if (!zquiet) console.log(" * Wrote GET log data from", processSSID(socket_session_data[socket.id].ssid), "for", socket.id);
sendToClient(socket, headers, data);

View File

@@ -25,15 +25,15 @@ String.prototype.reverse = function () {
function getServiceString(service) {
if (service === "all") {
var out = "";
Object.keys(services_configured.services).forEach(function (k) {
out += services_configured.services[k].toString() + "\n";
Object.keys(minisrv_config.services).forEach(function (k) {
out += minisrv_config.services[k].toString() + "\n";
});
return out;
} else {
if (!services_configured.services[service]) {
if (!minisrv_config.services[service]) {
throw ("SERVICE ERROR: Attempted to provision unconfigured service: " + service)
} else {
return services_configured.services[service].toString();
return minisrv_config.services[service].toString();
}
}
}
@@ -195,7 +195,7 @@ async function processPath(socket, path, request_headers = new Array(), service_
}
function processSSID(obj) {
if (services_configured.config.hide_ssid_in_logs) {
if (minisrv_config.config.hide_ssid_in_logs) {
if (typeof (obj) == "string") {
if (obj.substr(0, 8) == "MSTVSIMU") {
return obj.substr(0, 10) + ('*').repeat(10) + obj.substr(20);
@@ -319,9 +319,9 @@ async function doHTTPProxy(socket, request_headers) {
if (request_headers["Content-length"]) options.headers["Content-length"] = request_headers["Content-length"];
}
if (services_configured.services[request_type].use_external_proxy && services_configured.services[request_type].external_proxy_port) {
options.host = services_configured.services[request_type].external_proxy_host;
options.port = services_configured.services[request_type].external_proxy_port;
if (minisrv_config.services[request_type].use_external_proxy && minisrv_config.services[request_type].external_proxy_port) {
options.host = minisrv_config.services[request_type].external_proxy_host;
options.port = minisrv_config.services[request_type].external_proxy_port;
options.path = request_headers.request.split(' ')[1];
options.headers.Host = request_data.host;
}
@@ -757,25 +757,68 @@ async function handleSocket(socket) {
});
}
function integrateConfig(main, user) {
Object.keys(user).forEach(function (k) {
if (typeof (user[k]) == 'object') {
// new entry
if (!main[k]) main[k] = new Array();
// go down the rabbit hole
main[k] = integrateConfig(main[k], user[k]);
} else {
// update main config
main[k] = user[k];
}
});
return main;
}
var z_title = "zefie's wtv minisrv v" + require('./package.json').version;
console.log("**** Welcome to " + z_title + " ****");
console.log(" *** Reading service configuration...");
console.log(" *** Reading global configuration...");
try {
var services_configured = JSON.parse(fs.readFileSync(__dirname + "/services.json"));
var minisrv_config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
} catch (e) {
throw("ERROR: Could not read services.json", e);
throw ("ERROR: Could not read config.json", e);
}
var service_ip = services_configured.config.service_ip;
Object.keys(services_configured.services).forEach(function (k) {
services_configured.services[k].name = k;
if (!services_configured.services[k].host) {
services_configured.services[k].host = service_ip;
try {
if (fs.lstatSync(__dirname + "/user_config.json")) {
console.log(" *** Reading user configuration...");
try {
var minisrv_user_config = JSON.parse(fs.readFileSync(__dirname + "/user_config.json"));
} catch (e) {
console.log("ERROR: Could not read user_config.json", e);
var throw_me = true;
}
// file exists and we read and parsed it, but the variable is undefined
// Likely a syntax parser error that did not trip the exception check above
try {
minisrv_config = integrateConfig(minisrv_config, minisrv_user_config)
} catch (e) {
console.log("ERROR: Could not read user_config.json", e);
}
}
if (services_configured.services[k].port && !services_configured.services[k].nobind) {
ports.push(services_configured.services[k].port);
} catch (e) {
if (zdebug) console.log(" * Notice: Could not find user configuration (user_config.json). Using default configuration.");
}
if (throw_me) {
throw ("An error has occured while reading the configuration files.");
}
var service_ip = minisrv_config.config.service_ip;
Object.keys(minisrv_config.services).forEach(function (k) {
if (minisrv_config.services[k].disabled) return;
minisrv_config.services[k].name = k;
if (!minisrv_config.services[k].host) {
minisrv_config.services[k].host = service_ip;
}
if (minisrv_config.services[k].port && !minisrv_config.services[k].nobind) {
ports.push(minisrv_config.services[k].port);
}
services_configured.services[k].toString = function () {
minisrv_config.services[k].toString = function () {
var outstr = "wtv-service: name=" + this.name + " host=" + this.host + " port=" + this.port;
if (this.flags) outstr += " flags=" + this.flags;
if (this.connections) outstr += " flags=" + this.connections;
@@ -786,17 +829,17 @@ Object.keys(services_configured.services).forEach(function (k) {
}
return outstr;
}
console.log(" * Configured Service", k, "on Port", services_configured.services[k].port, "- Host", services_configured.services[k].host, "- Bind Port:", !services_configured.services[k].nobind);
console.log(" * Configured Service", k, "on Port", minisrv_config.services[k].port, "- Host", minisrv_config.services[k].host, "- Bind Port:", !minisrv_config.services[k].nobind);
})
if (services_configured.config.hide_ssid_in_logs) console.log(" * Masking SSIDs in the console for security");
if (minisrv_config.config.hide_ssid_in_logs) console.log(" * Masking SSIDs in the console for security");
// defaults
var zdebug = false;
var zquiet = true; // will squash zdebug even if its true
var zshowheaders = false;
if (services_configured.config.verbosity) {
switch (services_configured.config.verbosity) {
if (minisrv_config.config.verbosity) {
switch (minisrv_config.config.verbosity) {
case 0:
zdebug = false;
zquiet = true;

View File

@@ -1,6 +1,6 @@
{
"config": {
"service_ip": "192.168.11.8",
"service_ip": "0.0.0.0",
"service_name": "HackTV",
"send_tellyscripts": false,
"hide_ssid_in_logs": true,
@@ -29,7 +29,7 @@
"wtv-log": {
"port": 1609,
"connections": 1,
"write_logs_to_disk": true
"write_logs_to_disk": false
},
"wtv-home": {
"port": 1612,

View File

@@ -0,0 +1,24 @@
{
"config": {
"service_ip": "192.168.11.8",
"service_name": "MyWebTV",
"send_tellyscripts": true,
"hide_ssid_in_logs": true,
"verbosity": 0
},
"services": {
"wtv-log": {
"write_logs_to_disk": true
},
"wtv-some-custom-service": {
"port": 1609,
"connections": 1
},
"wtv-tricks": {
"port": 1702
},
"wtv-music": {
"disabled": true
}
}
}

View File

@@ -31,9 +31,7 @@
<ItemGroup>
<Content Include=".gitignore" />
<Content Include="app.js" />
<Content Include="services.json">
<SubType>Code</SubType>
</Content>
<Content Include="config.json" />
<Content Include="ServiceVault\wtv-flashrom\get-by-path.js">
<SubType>Code</SubType>
</Content>
@@ -200,6 +198,8 @@
<Content Include="ServiceVault\wtv-head-waiter\login.js" />
<Content Include="ServiceVault\wtv-home\splash.txt" />
<Content Include="ServiceVault\wtv-log\log.js" />
<Content Include="user_config.example.json" />
<Content Include="user_config.json" />
<Content Include="wtvsec.js">
<SubType>Code</SubType>
</Content>