move stuff around

This commit is contained in:
zefie
2026-04-26 19:16:18 -04:00
parent 4489966380
commit 6342f51d22
9 changed files with 79 additions and 59 deletions

View File

@@ -8,7 +8,7 @@ const wtvshared = new WTVShared(); // creates minisrv_config
const fs = wtvshared.fs;
const zlib = wtvshared.zlib;
const process = wtvshared.process;
const util = require('util');
const util = wtvshared.util;
const nunjucks = require('nunjucks');
const {serialize, unserialize} = require('php-serialize');
const {spawn} = require('child_process');
@@ -41,7 +41,9 @@ const wtvmime = new WTVMime(minisrv_config);
process
.on('SIGTERM', shutdown('SIGTERM'))
.on('SIGINT', shutdown('SIGINT'))
.on('uncaughtException', (e => { console.error(e); }));
.on('uncaughtException', function (err) {
console.error((err && err.stack) ? err.stack : err);
});
function shutdown(signal = 'SIGTERM') {
@@ -196,10 +198,10 @@ let socket_sessions = [];
const ports = [];
const pc_ports = [];
function moveArrayKey(array, from, to) {
array.splice(to, 0, array.splice(from, 1)[0]);
return array;
};
function getServiceString(service_name, overrides = {}) {
wtvshared.getServiceString(service_name, overrides);
}
// Deprecation warnings configuration
const deprecationWarnings = {
@@ -211,11 +213,17 @@ const deprecationWarnings = {
message: "session_data.hasCap() is deprecated and will be removed",
removeVersion: "0.9.80",
replacement: "Use session_data.capabilities.get() instead"
},
{
pattern: /(?<!wtvshared\.)getServiceString\s*\(/g,
message: "getServiceString() is deprecated and will be removed",
removeVersion: "0.9.80",
replacement: "Use wtvshared.getServiceString() instead"
}
],
// Enable/disable deprecation warnings globally
enabled: false,
enabled: true,
// Log level for deprecation warnings (console.warn, console.log, etc.)
logLevel: 'warn'
@@ -256,10 +264,6 @@ function checkDeprecationWarnings(script_data, filename = null, debug_name = nul
}
}
function getServiceString(service, overrides = {}) {
return wtvshared.getServiceString(service, overrides);
}
async function sendRawFile(socket, path) {
if (!minisrv_config.config.debug_flags.quiet) console.debug(" * Found " + path + " to handle request (Direct File Mode) [Socket " + socket.id + "]");
const contypes = wtvmime.getContentType(path);
@@ -329,12 +333,12 @@ const runScriptInVM = function (script_data, user_contextObj = {}, privileged =
"data": null,
"request_is_async": false,
"minisrv_version_string": z_title,
"getServiceString": getServiceString,
"getServiceString": getServiceString, // deprecated - use wtvshared.getServiceString() instead
"sendToClient": sendToClient,
"service_vaults": service_vaults,
"service_deps": service_deps,
"ssid_sessions": ssid_sessions,
"moveArrayKey": moveArrayKey,
"moveArrayKey": wtvshared.moveArrayKey,
"cwd": (filename) ? path.dirname(filename) : __dirname, // current working directory
// Our prototype overrides
@@ -360,32 +364,38 @@ const runScriptInVM = function (script_data, user_contextObj = {}, privileged =
} catch (e) {
console.error(" *!* Could not load module", module_file, "requested by service", contextObj.service_name, e)
}
// Special case for WTVNews services which require a reference to the server for its functionality
if (vm_modules[k] === "WTVNews") contextObj['wtvnewsserver'] = wtvnewsserver;
})
}
// Provide flashrom class to any service that is reported as a flashrom service in the config
if (minisrv_config.services[contextObj.service_name].is_flashrom_service) {
contextObj["WTVFlashrom"] = WTVFlashrom;
}
}
switch (contextObj.service_name) {
case "wtv-guide":
// wtv-guide is a special case due to needing this function
contextObj.wtvguide = new contextObj["WTVGuide"](minisrv_config, ssid_sessions[contextObj.socket.ssid], contextObj.socket, runScriptInVM);
break;
case "wtv-admin":
// wtv-admin needs util.isArray in validation of certain actions.
contextObj["util"] = require("util");
case "wtv-1800":
case "wtv-flashrom":
// these are special cases because the primary app already loaded this
// special case for bf0app (wtv-1800:/noflash)
contextObj["WTVFlashrom"] = WTVFlashrom;
break;
}
if (contextObj.socket) {
if (contextObj.socket.id)
if (socket_sessions[contextObj.socket.id]) contextObj.wtv_encrypted = (socket_sessions[contextObj.socket.id].secure === true);
if(contextObj.socket.id) {
if (socket_sessions[contextObj.socket.id]) {
contextObj.wtv_encrypted = (socket_sessions[contextObj.socket.id].secure === true);
}
}
}
if (privileged) {
// Grant the service additional access to global variables, such as the session stores.
// This is needed for privileged services such as wtv-1800, wtv-setup, wtv-head-waiter, etc. but should be avoided for regular services for security.
contextObj["privileged"] = true;
contextObj["require"] = require; // this is dangerous but needed for some scripts at this time
contextObj["SessionStore"] = SessionStore;
@@ -768,7 +778,7 @@ async function processPath(socket, service_vault_file_path, request_headers = []
}
}
} else {
// php is not enabled, don't expose source code
// cgi is not enabled, don't expose source code
service_vault_found = true;
const errpage = wtvshared.doErrorPage(403, null, null, pc_services);
sendToClient(socket, errpage[0], errpage[1]);
@@ -2284,6 +2294,9 @@ Object.keys(minisrv_config.services).forEach(function (k) {
handlerModules[minisrv_config.services[k].handler_module + "_main"] = require(classPath + "/" + minisrv_config.services[k].handler_module + ".js");
var args = [];
for (let i = 0; i < (minisrv_config.services[k].handler_extra_vars || []).length; i++) {
// is there a better way to do this than eval ? ...
// concept: server owner can specify handler_extra_vars in the config as an array of
// variable/function/module names (as strings in the config) that they want to pass to the handler module constructor
let extraVar = eval(minisrv_config.services[k].handler_extra_vars[i]);
args.push(extraVar);
}
@@ -2367,16 +2380,10 @@ if (minisrv_config.config.user_accounts.max_users_per_account > 99) {
if (minisrv_config.config.shenanigans) console.log(" * WARNING: Shenanigans level", minisrv_config.config.shenanigans, "enabled");
else console.log(" * Shenanigans disabled");
// PNG
if (minisrv_config.config.decode_png) console.log(" * WebTV Unsupported images will be processed and converted for WebTV clients");
// WTVImage
if (minisrv_config.config.image_decoder.enabled) console.log(" * WebTV Unsupported images will be processed and converted for WebTV clients");
else console.log(" * WebTV Unsupported images will not be processed, and sent to client as-is");
process.on('uncaughtException', function (err) {
console.error((err && err.stack) ? err.stack : err);
});
ports.sort();
pc_ports.sort();

View File

@@ -17,10 +17,10 @@ minisrv-no-mail-count: true
wtv-initial-key: ` + wtvsec_login.challenge_key.toString(CryptoJS.enc.Base64) + `
Content-Type: text/tellyscript
wtv-service: reset
${getServiceString('wtv-1800')}
${getServiceString('wtv-head-waiter')}
${getServiceString('wtv-star', { "no_star_word": true })}
${getServiceString('wtv-flashrom')}
${wtvshared.getServiceString('wtv-1800')}
${wtvshared.getServiceString('wtv-head-waiter')}
${wtvshared.getServiceString('wtv-star', { "no_star_word": true })}
${wtvshared.getServiceString('wtv-flashrom')}
wtv-boot-url: wtv-head-waiter:/login?
wtv-visit: wtv-head-waiter:/login?
wtv-client-time-zone: GMT -0000

View File

@@ -166,15 +166,15 @@ if (session_data.data_store.wtvsec_login) {
headers += "wtv-initial-key: " + session_data.data_store.wtvsec_login.challenge_key.toString(CryptoJS.enc.Base64) + "\n";
headers += "Content-Type: " + prereg_contype + "\n";
if (!request_headers.query.reconnect) headers += "wtv-service: reset\n";
if (!bf0app_update) headers += getServiceString('wtv-1800') + "\n";
if (!bf0app_update) headers += wtvshared.getServiceString('wtv-1800') + "\n";
if (bf0app_update) headers += getServiceString('wtv-head-waiter', { "flags": "0x00000001" }) + "\n";
else headers += getServiceString('wtv-head-waiter') + "\n";
if (bf0app_update) headers += wtvshared.getServiceString('wtv-head-waiter', { "flags": "0x00000001" }) + "\n";
else headers += wtvshared.getServiceString('wtv-head-waiter') + "\n";
if (bf0app_update) headers += getServiceString('wtv-star', { "no_star_word": true }) + "\n";
else headers += getServiceString('wtv-star') + "\n";
if (request_headers.query.reconnect && !session_data.isRegistered() && !session_data.lockdown) headers += getServiceString('wtv-register') + "\n";
if (!session_data.lockdown) headers += getServiceString('wtv-flashrom') + "\n";
if (bf0app_update) headers += wtvshared.getServiceString('wtv-star', { "no_star_word": true }) + "\n";
else headers += wtvshared.getServiceString('wtv-star') + "\n";
if (request_headers.query.reconnect && !session_data.isRegistered() && !session_data.lockdown) headers += wtvshared.getServiceString('wtv-register') + "\n";
if (!session_data.lockdown) headers += wtvshared.getServiceString('wtv-flashrom') + "\n";
if (bf0app_update) headers += "wtv-boot-url: " + gourl + "\n";
else {
headers += "wtv-boot-url: wtv-head-waiter:/login?relogin=true";

View File

@@ -14,9 +14,9 @@ if (gourl) {
// fake logged in for reg
session_data.setUserLoggedIn(true);
headers += `wtv-encrypted: ${(request_headers['wtv-encrypted']) ? wtvshared.parseBool(request_headers['wtv-encrypted']) : true}
${getServiceString('wtv-register')}
${getServiceString('wtv-head-waiter')}
${getServiceString('wtv-star')}
${wtvshared.getServiceString('wtv-register')}
${wtvshared.getServiceString('wtv-head-waiter')}
${wtvshared.getServiceString('wtv-star')}
wtv-boot-url: wtv-head-waiter:/relogin?
`
}
@@ -102,7 +102,7 @@ wtv-expire-all: client:closeallpanels
if (!limitedLogin && !limitedLoginRegistered) {
session_data.assignMailStore();
headers += "wtv-service: reset\n";
headers += getServiceString('all', { "exceptions": ["wtv-register"] });
headers += wtvshared.getServiceString('all', { "exceptions": ["wtv-register"] });
if (offline_user_list) headers += "wtv-offline-user-list: " + offline_user_list + "\n";
headers += `wtv-messenger-authorized: ${messenger_authorized}
wtv-messenger-enable: ${messenger_enabled}
@@ -113,11 +113,11 @@ wtv-messenger-login-url: wtv-passport:/messengerlogin
`;
} else {
/*
headers += getServiceString('wtv-1800') + "\n";
headers += getServiceString('wtv-head-waiter') + "\n";
headers += getServiceString('wtv-log') + "\n";
headers += getServiceString('wtv-star') + "\n";
headers += getServiceString('wtv-flashrom') + "\n";
headers += wtvshared.getServiceString('wtv-1800') + "\n";
headers += wtvshared.getServiceString('wtv-head-waiter') + "\n";
headers += wtvshared.getServiceString('wtv-log') + "\n";
headers += wtvshared.getServiceString('wtv-star') + "\n";
headers += wtvshared.getServiceString('wtv-flashrom') + "\n";
*/
headers += `wtv-messenger-authorized: 0
wtv-messenger-enable: 0

View File

@@ -48,7 +48,7 @@ if (!send_to_relogin) {
Connection: Keep-Alive
Expires: Wed, 09 Oct 1991 22:00:00 GMT
wtv-expire-all: wtv-head-waiter:
`+ getServiceString('wtv-log') + `
`+ wtvshared.getServiceString('wtv-log') + `
wtv-log-url: wtv-log:/log`;
if (challenge_header !== "") headers += "\n" + challenge_header;
headers += `

View File

@@ -12,7 +12,7 @@ Expires: Wed, 09 Oct 1991 22:00:00 GMT
wtv-expire-all: wtv-head-waiter:
wtv-expire-all: wtv-1800:
wtv-service: reset
${getServiceString('wtv-1800')}
${wtvshared.getServiceString('wtv-1800')}
wtv-visit: ${gourl}
Content-type: text/html`;
data = '';

View File

@@ -7,9 +7,9 @@ wtv-noback-all: wtv-register:
wtv-expire-all: wtv-
wtv-ticket: ${session_data.data_store.wtvsec_login.ticket_b64}
wtv-service: reset
${getServiceString('wtv-1800')}
${getServiceString('wtv-head-waiter')}
${getServiceString('wtv-star')}
${wtvshared.getServiceString('wtv-1800')}
${wtvshared.getServiceString('wtv-head-waiter')}
${wtvshared.getServiceString('wtv-star')}
wtv-relogin-url: wtv-1800:/preregister?relogin=true
wtv-reconnect-url: wtv-1800:/preregister?reconnect=true
wtv-boot-url: wtv-1800:/preregister?relogin=true

View File

@@ -5,7 +5,7 @@ wtv-noback-all: wtv-
wtv-expire-all: wtv-
Content-type: text/html
wtv-service: reset
`+getServiceString('wtv-1800');
`+wtvshared.getServiceString('wtv-1800');
// HackTV Homepage is default

View File

@@ -16,7 +16,7 @@ class WTVShared {
sanitizeHtml = require('sanitize-html');
iconv = require('iconv-lite');
parentDirectory = process.cwd()
extend = require('util')._extend;
util = require('util');
debug = require('debug')('WTVShared')
process = require('process');
shenanigans = null;
@@ -139,6 +139,19 @@ class WTVShared {
return false;
}
/**
* Moves an array element from one index to another
* @param {Array} array The array to modify
* @param {number} from The index of the element to move
* @param {number} to The index to move the element to
* @return {Array} The modified array with the element moved
* @notice This function modifies the original array and also returns it for convenience
*/
moveArrayKey(array, from, to) {
array.splice(to, 0, array.splice(from, 1)[0]);
return array;
};
/**
* Converts a byte array to a 32-bit unsigned integer (big-endian)
* @param {Uint8Array} bytes The byte array