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 fs = wtvshared.fs;
const zlib = wtvshared.zlib; const zlib = wtvshared.zlib;
const process = wtvshared.process; const process = wtvshared.process;
const util = require('util'); const util = wtvshared.util;
const nunjucks = require('nunjucks'); const nunjucks = require('nunjucks');
const {serialize, unserialize} = require('php-serialize'); const {serialize, unserialize} = require('php-serialize');
const {spawn} = require('child_process'); const {spawn} = require('child_process');
@@ -41,7 +41,9 @@ const wtvmime = new WTVMime(minisrv_config);
process process
.on('SIGTERM', shutdown('SIGTERM')) .on('SIGTERM', shutdown('SIGTERM'))
.on('SIGINT', shutdown('SIGINT')) .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') { function shutdown(signal = 'SIGTERM') {
@@ -196,10 +198,10 @@ let socket_sessions = [];
const ports = []; const ports = [];
const pc_ports = []; const pc_ports = [];
function moveArrayKey(array, from, to) { function getServiceString(service_name, overrides = {}) {
array.splice(to, 0, array.splice(from, 1)[0]); wtvshared.getServiceString(service_name, overrides);
return array; }
};
// Deprecation warnings configuration // Deprecation warnings configuration
const deprecationWarnings = { const deprecationWarnings = {
@@ -211,11 +213,17 @@ const deprecationWarnings = {
message: "session_data.hasCap() is deprecated and will be removed", message: "session_data.hasCap() is deprecated and will be removed",
removeVersion: "0.9.80", removeVersion: "0.9.80",
replacement: "Use session_data.capabilities.get() instead" 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 // Enable/disable deprecation warnings globally
enabled: false, enabled: true,
// Log level for deprecation warnings (console.warn, console.log, etc.) // Log level for deprecation warnings (console.warn, console.log, etc.)
logLevel: 'warn' 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) { 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 + "]"); 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); const contypes = wtvmime.getContentType(path);
@@ -329,12 +333,12 @@ const runScriptInVM = function (script_data, user_contextObj = {}, privileged =
"data": null, "data": null,
"request_is_async": false, "request_is_async": false,
"minisrv_version_string": z_title, "minisrv_version_string": z_title,
"getServiceString": getServiceString, "getServiceString": getServiceString, // deprecated - use wtvshared.getServiceString() instead
"sendToClient": sendToClient, "sendToClient": sendToClient,
"service_vaults": service_vaults, "service_vaults": service_vaults,
"service_deps": service_deps, "service_deps": service_deps,
"ssid_sessions": ssid_sessions, "ssid_sessions": ssid_sessions,
"moveArrayKey": moveArrayKey, "moveArrayKey": wtvshared.moveArrayKey,
"cwd": (filename) ? path.dirname(filename) : __dirname, // current working directory "cwd": (filename) ? path.dirname(filename) : __dirname, // current working directory
// Our prototype overrides // Our prototype overrides
@@ -360,32 +364,38 @@ const runScriptInVM = function (script_data, user_contextObj = {}, privileged =
} catch (e) { } catch (e) {
console.error(" *!* Could not load module", module_file, "requested by service", contextObj.service_name, 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; 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) { switch (contextObj.service_name) {
case "wtv-guide": case "wtv-guide":
// wtv-guide is a special case due to needing this function // 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); contextObj.wtvguide = new contextObj["WTVGuide"](minisrv_config, ssid_sessions[contextObj.socket.ssid], contextObj.socket, runScriptInVM);
break; break;
case "wtv-admin":
// wtv-admin needs util.isArray in validation of certain actions.
contextObj["util"] = require("util");
case "wtv-1800": case "wtv-1800":
case "wtv-flashrom": // special case for bf0app (wtv-1800:/noflash)
// these are special cases because the primary app already loaded this
contextObj["WTVFlashrom"] = WTVFlashrom; contextObj["WTVFlashrom"] = WTVFlashrom;
break; break;
} }
if (contextObj.socket) { if (contextObj.socket) {
if (contextObj.socket.id) if(contextObj.socket.id) {
if (socket_sessions[contextObj.socket.id]) contextObj.wtv_encrypted = (socket_sessions[contextObj.socket.id].secure === true); if (socket_sessions[contextObj.socket.id]) {
contextObj.wtv_encrypted = (socket_sessions[contextObj.socket.id].secure === true);
}
}
} }
if (privileged) { 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["privileged"] = true;
contextObj["require"] = require; // this is dangerous but needed for some scripts at this time contextObj["require"] = require; // this is dangerous but needed for some scripts at this time
contextObj["SessionStore"] = SessionStore; contextObj["SessionStore"] = SessionStore;
@@ -768,7 +778,7 @@ async function processPath(socket, service_vault_file_path, request_headers = []
} }
} }
} else { } else {
// php is not enabled, don't expose source code // cgi is not enabled, don't expose source code
service_vault_found = true; service_vault_found = true;
const errpage = wtvshared.doErrorPage(403, null, null, pc_services); const errpage = wtvshared.doErrorPage(403, null, null, pc_services);
sendToClient(socket, errpage[0], errpage[1]); 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"); handlerModules[minisrv_config.services[k].handler_module + "_main"] = require(classPath + "/" + minisrv_config.services[k].handler_module + ".js");
var args = []; var args = [];
for (let i = 0; i < (minisrv_config.services[k].handler_extra_vars || []).length; i++) { 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]); let extraVar = eval(minisrv_config.services[k].handler_extra_vars[i]);
args.push(extraVar); 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"); if (minisrv_config.config.shenanigans) console.log(" * WARNING: Shenanigans level", minisrv_config.config.shenanigans, "enabled");
else console.log(" * Shenanigans disabled"); else console.log(" * Shenanigans disabled");
// PNG // WTVImage
if (minisrv_config.config.decode_png) console.log(" * WebTV Unsupported images will be processed and converted for WebTV clients"); 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"); 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(); ports.sort();
pc_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) + ` wtv-initial-key: ` + wtvsec_login.challenge_key.toString(CryptoJS.enc.Base64) + `
Content-Type: text/tellyscript Content-Type: text/tellyscript
wtv-service: reset wtv-service: reset
${getServiceString('wtv-1800')} ${wtvshared.getServiceString('wtv-1800')}
${getServiceString('wtv-head-waiter')} ${wtvshared.getServiceString('wtv-head-waiter')}
${getServiceString('wtv-star', { "no_star_word": true })} ${wtvshared.getServiceString('wtv-star', { "no_star_word": true })}
${getServiceString('wtv-flashrom')} ${wtvshared.getServiceString('wtv-flashrom')}
wtv-boot-url: wtv-head-waiter:/login? wtv-boot-url: wtv-head-waiter:/login?
wtv-visit: wtv-head-waiter:/login? wtv-visit: wtv-head-waiter:/login?
wtv-client-time-zone: GMT -0000 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 += "wtv-initial-key: " + session_data.data_store.wtvsec_login.challenge_key.toString(CryptoJS.enc.Base64) + "\n";
headers += "Content-Type: " + prereg_contype + "\n"; headers += "Content-Type: " + prereg_contype + "\n";
if (!request_headers.query.reconnect) headers += "wtv-service: reset\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"; if (bf0app_update) headers += wtvshared.getServiceString('wtv-head-waiter', { "flags": "0x00000001" }) + "\n";
else headers += getServiceString('wtv-head-waiter') + "\n"; else headers += wtvshared.getServiceString('wtv-head-waiter') + "\n";
if (bf0app_update) headers += getServiceString('wtv-star', { "no_star_word": true }) + "\n"; if (bf0app_update) headers += wtvshared.getServiceString('wtv-star', { "no_star_word": true }) + "\n";
else headers += getServiceString('wtv-star') + "\n"; else headers += wtvshared.getServiceString('wtv-star') + "\n";
if (request_headers.query.reconnect && !session_data.isRegistered() && !session_data.lockdown) headers += getServiceString('wtv-register') + "\n"; if (request_headers.query.reconnect && !session_data.isRegistered() && !session_data.lockdown) headers += wtvshared.getServiceString('wtv-register') + "\n";
if (!session_data.lockdown) headers += getServiceString('wtv-flashrom') + "\n"; if (!session_data.lockdown) headers += wtvshared.getServiceString('wtv-flashrom') + "\n";
if (bf0app_update) headers += "wtv-boot-url: " + gourl + "\n"; if (bf0app_update) headers += "wtv-boot-url: " + gourl + "\n";
else { else {
headers += "wtv-boot-url: wtv-head-waiter:/login?relogin=true"; headers += "wtv-boot-url: wtv-head-waiter:/login?relogin=true";

View File

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

View File

@@ -48,7 +48,7 @@ if (!send_to_relogin) {
Connection: Keep-Alive Connection: Keep-Alive
Expires: Wed, 09 Oct 1991 22:00:00 GMT Expires: Wed, 09 Oct 1991 22:00:00 GMT
wtv-expire-all: wtv-head-waiter: wtv-expire-all: wtv-head-waiter:
`+ getServiceString('wtv-log') + ` `+ wtvshared.getServiceString('wtv-log') + `
wtv-log-url: wtv-log:/log`; wtv-log-url: wtv-log:/log`;
if (challenge_header !== "") headers += "\n" + challenge_header; if (challenge_header !== "") headers += "\n" + challenge_header;
headers += ` 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-head-waiter:
wtv-expire-all: wtv-1800: wtv-expire-all: wtv-1800:
wtv-service: reset wtv-service: reset
${getServiceString('wtv-1800')} ${wtvshared.getServiceString('wtv-1800')}
wtv-visit: ${gourl} wtv-visit: ${gourl}
Content-type: text/html`; Content-type: text/html`;
data = ''; data = '';

View File

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

View File

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

View File

@@ -16,7 +16,7 @@ class WTVShared {
sanitizeHtml = require('sanitize-html'); sanitizeHtml = require('sanitize-html');
iconv = require('iconv-lite'); iconv = require('iconv-lite');
parentDirectory = process.cwd() parentDirectory = process.cwd()
extend = require('util')._extend; util = require('util');
debug = require('debug')('WTVShared') debug = require('debug')('WTVShared')
process = require('process'); process = require('process');
shenanigans = null; shenanigans = null;
@@ -139,6 +139,19 @@ class WTVShared {
return false; 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) * Converts a byte array to a 32-bit unsigned integer (big-endian)
* @param {Uint8Array} bytes The byte array * @param {Uint8Array} bytes The byte array