more optimizations

This commit is contained in:
zefie
2025-08-12 01:28:48 -04:00
parent 53ef352636
commit d0735a99e5
2 changed files with 114 additions and 147 deletions

View File

@@ -34,8 +34,8 @@ class WTVClientSessionData {
constructor(minisrv_config, ssid) { constructor(minisrv_config, ssid) {
if (!minisrv_config) throw ("minisrv_config required"); if (!minisrv_config) throw ("minisrv_config required");
var WTVShared = require("./WTVShared.js")['WTVShared']; const WTVShared = require("./WTVShared.js")['WTVShared'];
var WTVMime = require("./WTVMime.js"); const WTVMime = require("./WTVMime.js");
this.minisrv_config = minisrv_config; this.minisrv_config = minisrv_config;
this.cryptoKey = this.minisrv_config.config.keys.user_data_key; this.cryptoKey = this.minisrv_config.config.keys.user_data_key;
this.wtvshared = new WTVShared(minisrv_config); this.wtvshared = new WTVShared(minisrv_config);
@@ -73,12 +73,12 @@ class WTVClientSessionData {
if (this.user_id > 0) return false; // not primary user or pre-login if (this.user_id > 0) return false; // not primary user or pre-login
var total_unread_messages = 0; let total_unread_messages = 0;
var accounts = this.listPrimaryAccountUsers(); const accounts = this.listPrimaryAccountUsers();
var self = this; const self = this;
Object.keys(accounts).forEach((k) => { Object.keys(accounts).forEach((k) => {
var user_id = accounts[k].user_id; const user_id = accounts[k].user_id;
var subUserSession = new self.constructor(self.minisrv_config, self.ssid); const subUserSession = new self.constructor(self.minisrv_config, self.ssid);
subUserSession.switchUserID(user_id, false, false); subUserSession.switchUserID(user_id, false, false);
subUserSession.assignMailStore(); subUserSession.assignMailStore();
if (subUserSession.mailstore) { if (subUserSession.mailstore) {
@@ -158,13 +158,12 @@ class WTVClientSessionData {
findFreeUserSlot() { findFreeUserSlot() {
if (this.user_id != 0) return false; // subscriber only command if (this.user_id != 0) return false; // subscriber only command
var master_directory = this.getUserStoreDirectory(true); let master_directory = this.getUserStoreDirectory(true);
if (this.fs.existsSync(master_directory)) { if (this.fs.existsSync(master_directory)) {
for (var i = 0; i < this.minisrv_config.config.user_accounts.max_users_per_account; i++) { for (let i = 0; i < this.minisrv_config.config.user_accounts.max_users_per_account; i++) {
var test_dir = master_directory + this.path.sep + "user" + i; const test_dir = master_directory + this.path.sep + "user" + i;
if (!this.fs.existsSync(test_dir)) { if (!this.fs.existsSync(test_dir)) {
return i; return i;
break;
} }
} }
} }
@@ -184,13 +183,13 @@ class WTVClientSessionData {
listPrimaryAccountUsers() { listPrimaryAccountUsers() {
if (this.user_id != 0) return false; // subscriber only command if (this.user_id != 0) return false; // subscriber only command
var master_directory = this.getUserStoreDirectory(true); const master_directory = this.getUserStoreDirectory(true);
var account_data = []; let account_data = [];
var self = this; const self = this;
this.fs.readdirSync(master_directory).forEach(f => { this.fs.readdirSync(master_directory).forEach(f => {
if (self.fs.lstatSync(master_directory + self.path.sep + f).isDirectory()) { if (self.fs.lstatSync(master_directory + self.path.sep + f).isDirectory()) {
if (f.startsWith("user")) { if (f.startsWith("user")) {
var user_file = this.path.resolve(master_directory + self.path.sep + f + self.path.sep + f + ".json"); const user_file = this.path.resolve(master_directory + this.path.sep + f + this.path.sep + f + ".json");
if (self.fs.existsSync(user_file)) { if (self.fs.existsSync(user_file)) {
if (f == "user0") { if (f == "user0") {
account_data['subscriber'] = JSON.parse(this.fs.readFileSync(user_file)); account_data['subscriber'] = JSON.parse(this.fs.readFileSync(user_file));
@@ -231,11 +230,10 @@ class WTVClientSessionData {
*/ */
getUserStoreDirectory(subscriber = false, user_id = null) { getUserStoreDirectory(subscriber = false, user_id = null) {
if (user_id === null) user_id = this.user_id; if (user_id === null) user_id = this.user_id;
var userstore = this.getAccountStoreDirectory() + this.path.sep + this.ssid + this.path.sep; let userstore = this.getAccountStoreDirectory() + this.path.sep + this.ssid + this.path.sep;
if (!subscriber) userstore += "user" + user_id + this.path.sep; if (!subscriber) userstore += "user" + user_id + this.path.sep;
// getAccountStoreDirectory() already returns an absolute path, so we don't need getAbsolutePath again // getAccountStoreDirectory() already returns an absolute path, so we don't need getAbsolutePath again
var result = userstore + this.path.sep; return userstore + this.path.sep;
return result;
} }
removeUser(user_id) { removeUser(user_id) {
@@ -243,7 +241,7 @@ class WTVClientSessionData {
if (parseInt(this.user_id) !== 0) return false; // not primary account if (parseInt(this.user_id) !== 0) return false; // not primary account
if (user_id === 0) return false; // cannot delete primary account in this fashion if (user_id === 0) return false; // cannot delete primary account in this fashion
var userstore = this.getUserStoreDirectory(false, user_id); const userstore = this.getUserStoreDirectory(false, user_id);
if (this.fs.existsSync(userstore)) { if (this.fs.existsSync(userstore)) {
this.fs.rmSync(userstore, { recursive: true }); this.fs.rmSync(userstore, { recursive: true });
return true; return true;
@@ -252,23 +250,24 @@ class WTVClientSessionData {
} }
setPendingTransfer(ssid) { setPendingTransfer(ssid) {
var pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json"; const pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json";
var ssidobj = { "ssid": ssid, "type": "source" }; let ssidobj = { "ssid": ssid, "type": "source" };
this.fs.writeFileSync(pending_file, JSON.stringify(ssidobj)); this.fs.writeFileSync(pending_file, JSON.stringify(ssidobj));
var new_userstore = this.getAccountStoreDirectory() + this.path.sep + ssidobj.ssid;
const new_userstore = this.getAccountStoreDirectory() + this.path.sep + ssidobj.ssid;
if (!this.fs.existsSync(new_userstore)) this.fs.mkdirSync(new_userstore); if (!this.fs.existsSync(new_userstore)) this.fs.mkdirSync(new_userstore);
var dest_pending_file = new_userstore + this.path.sep + "pending_transfer.json"; const dest_pending_file = new_userstore + this.path.sep + "pending_transfer.json";
var ssidobj = { "ssid": this.ssid, "type": "target" }; ssidobj = { "ssid": this.ssid, "type": "target" };
this.fs.writeFileSync(dest_pending_file, JSON.stringify(ssidobj)); this.fs.writeFileSync(dest_pending_file, JSON.stringify(ssidobj));
} }
cancelPendingTransfer() { cancelPendingTransfer() {
var pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json"; const pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json";
if (this.fs.existsSync(pending_file)) { if (this.fs.existsSync(pending_file)) {
var file = this.fs.readFileSync(pending_file) const file = this.fs.readFileSync(pending_file)
var ssidobj = JSON.parse(file); const ssidobj = JSON.parse(file);
var new_userstore = this.getAccountStoreDirectory() + this.path.sep + ssidobj.ssid; const new_userstore = this.getAccountStoreDirectory() + this.path.sep + ssidobj.ssid;
var dest_pending_file = new_userstore + this.path.sep + "pending_transfer.json"; const dest_pending_file = new_userstore + this.path.sep + "pending_transfer.json";
if (this.fs.existsSync(dest_pending_file)) this.fs.unlinkSync(dest_pending_file); if (this.fs.existsSync(dest_pending_file)) this.fs.unlinkSync(dest_pending_file);
this.fs.unlinkSync(pending_file); this.fs.unlinkSync(pending_file);
if (this.fs.existsSync(new_userstore)) this.fs.rmdirSync(new_userstore); if (this.fs.existsSync(new_userstore)) this.fs.rmdirSync(new_userstore);
@@ -278,13 +277,13 @@ class WTVClientSessionData {
} }
finalizePendingTransfer() { finalizePendingTransfer() {
var pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json"; const pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json";
var file = this.fs.readFileSync(pending_file) const file = this.fs.readFileSync(pending_file)
var ssidobj = JSON.parse(file); const ssidobj = JSON.parse(file);
if (ssidobj.type != "target") return false; // Only allow completion from target if (ssidobj.type != "target") return false; // Only allow completion from target
var source_ssid = ssidobj.ssid const source_ssid = ssidobj.ssid
var old_account = this.getAccountStoreDirectory() + this.path.sep + source_ssid const old_account = this.getAccountStoreDirectory() + this.path.sep + source_ssid
var new_account = this.getUserStoreDirectory(true); const new_account = this.getUserStoreDirectory(true);
this.fs.cpSync(old_account, new_account, { this.fs.cpSync(old_account, new_account, {
filter: (source, _destination) => { filter: (source, _destination) => {
return source != "pending_transfer.json"; return source != "pending_transfer.json";
@@ -297,9 +296,9 @@ class WTVClientSessionData {
} }
hasPendingTransfer(dtype = null) { hasPendingTransfer(dtype = null) {
var pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json"; const pending_file = this.getUserStoreDirectory(true) + this.path.sep + "pending_transfer.json";
if (this.fs.existsSync(pending_file)) { if (this.fs.existsSync(pending_file)) {
var ssidobj = JSON.parse(this.fs.readFileSync(pending_file)); const ssidobj = JSON.parse(this.fs.readFileSync(pending_file));
console.log(ssidobj) console.log(ssidobj)
if (dtype) { if (dtype) {
(ssidobj.type == dtype) ? ssidobj.ssid : false; (ssidobj.type == dtype) ? ssidobj.ssid : false;
@@ -321,22 +320,22 @@ class WTVClientSessionData {
* @returns {boolean} Whether or not the file was written * @returns {boolean} Whether or not the file was written
*/ */
storeUserStoreFile(path, data, last_modified = null, overwrite = true) { storeUserStoreFile(path, data, last_modified = null, overwrite = true) {
var store_dir = this.getUserStoreDirectory(); let store_dir = this.getUserStoreDirectory();
if (!store_dir) return false; // unregistered if (!store_dir) return false; // unregistered
// FileStore // FileStore
store_dir += "FileStore" + this.path.sep; store_dir += "FileStore" + this.path.sep;
var result = false; let result = false;
var path_split = path.split('/'); const path_split = path.split('/');
var file_name = path_split.pop(); const file_name = path_split.pop();
var store_dir_path = this.wtvshared.makeSafePath(store_dir, path_split.join('/').replace('/', this.path.sep)); const store_dir_path = this.wtvshared.makeSafePath(store_dir, path_split.join('/').replace('/', this.path.sep));
var store_full_path = this.wtvshared.makeSafePath(store_dir_path, file_name); const store_full_path = this.wtvshared.makeSafePath(store_dir_path, file_name);
try { try {
if (!this.fs.existsSync(store_dir_path)) this.fs.mkdirSync(store_dir_path, { recursive: true }); if (!this.fs.existsSync(store_dir_path)) this.fs.mkdirSync(store_dir_path, { recursive: true });
var file_exists = this.fs.existsSync(store_full_path); const file_exists = this.fs.existsSync(store_full_path);
if (!file_exists || (file_exists && overwrite)) result = this.fs.writeFileSync(store_full_path, data); if (!file_exists || (file_exists && overwrite)) result = this.fs.writeFileSync(store_full_path, data);
if (result !== false && last_modified) { if (result !== false && last_modified) {
var file_timestamp = new Date(last_modified * 1000); const file_timestamp = new Date(last_modified * 1000);
fs.utimesSync(store_full_path, Date.now(), file_timestamp) fs.utimesSync(store_full_path, Date.now(), file_timestamp)
} }
} catch (e) { } catch (e) {
@@ -347,8 +346,8 @@ class WTVClientSessionData {
scrapbookExists() { scrapbookExists() {
if (this.scrapbook_dir === null) { if (this.scrapbook_dir === null) {
var userstore_dir = this.getUserStoreDirectory(); const userstore_dir = this.getUserStoreDirectory();
var store_dir = "Scrapbook" + this.path.sep; const store_dir = "Scrapbook" + this.path.sep;
this.scrapbook_dir = userstore_dir + store_dir; this.scrapbook_dir = userstore_dir + store_dir;
} }
return this.fs.existsSync(this.scrapbook_dir); return this.fs.existsSync(this.scrapbook_dir);
@@ -389,8 +388,8 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var id = 1; let id = 1;
var files = this.fs.readdirSync(this.scrapbook_dir); let files = this.fs.readdirSync(this.scrapbook_dir);
if (files.length === 0) { if (files.length === 0) {
return id; return id;
} }
@@ -405,11 +404,11 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var total_size = 0; let total_size = 0;
var files = this.fs.readdirSync(this.scrapbook_dir); let files = this.fs.readdirSync(this.scrapbook_dir);
files.forEach(file => { files.forEach(file => {
if (!file.endsWith('.meta')) { if (!file.endsWith('.meta')) {
var file_path = this.scrapbook_dir + file; const file_path = this.scrapbook_dir + file;
if (this.fs.existsSync(file_path)) { if (this.fs.existsSync(file_path)) {
total_size += this.fs.statSync(file_path).size; total_size += this.fs.statSync(file_path).size;
} }
@@ -422,10 +421,10 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var total_size = this.getScrapbookUsage(); const total_size = this.getScrapbookUsage();
var max_size = this.minisrv_config.config.user_accounts.scrapbook_storage * 1024 * 1024; // convert to bytes const max_size = this.minisrv_config.config.user_accounts.scrapbook_storage * 1024 * 1024; // convert to bytes
if (max_size <= 0) return 0; // no storage limit set if (max_size <= 0) return 0; // no storage limit set
var usage_percent = (total_size / max_size) * 100; const usage_percent = (total_size / max_size) * 100;
return Math.round(usage_percent, 2); return Math.round(usage_percent, 2);
} }
@@ -433,7 +432,7 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var file = this.scrapbook_dir + id; const file = this.scrapbook_dir + id;
if (this.fs.existsSync(file)) { if (this.fs.existsSync(file)) {
return this.fs.readFileSync(file); return this.fs.readFileSync(file);
} }
@@ -444,11 +443,11 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var file = this.scrapbook_dir + id + ".meta"; const file = this.scrapbook_dir + id + ".meta";
if (this.fs.existsSync(file)) { if (this.fs.existsSync(file)) {
var meta = this.fs.readFileSync(file, 'utf8'); const meta = this.fs.readFileSync(file, 'utf8');
try { try {
var metaData = JSON.parse(meta); const metaData = JSON.parse(meta);
return metaData.contentType; return metaData.contentType;
} catch (e) { } catch (e) {
this.debug("getScrapbookImageType", "Error parsing metadata for image ID", id, e); this.debug("getScrapbookImageType", "Error parsing metadata for image ID", id, e);
@@ -462,8 +461,8 @@ class WTVClientSessionData {
if (!this.scrapbookExists()) { if (!this.scrapbookExists()) {
this.createScrapbook(); this.createScrapbook();
} }
var fileout = this.scrapbook_dir + filename; const fileout = this.scrapbook_dir + filename;
var fileout_meta = this.scrapbook_dir + filename + ".meta"; const fileout_meta = this.scrapbook_dir + filename + ".meta";
this.fs.writeFileSync(fileout, data); this.fs.writeFileSync(fileout, data);
this.fs.writeFileSync(fileout_meta, JSON.stringify({ this.fs.writeFileSync(fileout_meta, JSON.stringify({
"contentType": contentType "contentType": contentType
@@ -481,11 +480,11 @@ class WTVClientSessionData {
* @returns {Buffer|false} Buffer data, or false if could not open file * @returns {Buffer|false} Buffer data, or false if could not open file
*/ */
getUserStoreFile(path) { getUserStoreFile(path) {
var store_dir = this.getUserStoreDirectory(); let store_dir = this.getUserStoreDirectory();
if (!store_dir) return false; // unregistered if (!store_dir) return false; // unregistered
// FileStore // FileStore
store_dir += "FileStore" + this.path.sep; store_dir += "FileStore" + this.path.sep;
var store_dir_path = this.wtvshared.getAbsolutePath(this.wtvshared.makeSafePath(store_dir, path.replace('/', this.path.sep))); const store_dir_path = this.wtvshared.getAbsolutePath(this.wtvshared.makeSafePath(store_dir, path.replace('/', this.path.sep)));
if (this.fs.existsSync(store_dir_path)) return this.fs.readFileSync(store_dir_path); if (this.fs.existsSync(store_dir_path)) return this.fs.readFileSync(store_dir_path);
else return false; else return false;
} }
@@ -496,10 +495,10 @@ class WTVClientSessionData {
* @returns {Buffer|false} Buffer data, or false if could not open file * @returns {Buffer|false} Buffer data, or false if could not open file
*/ */
getUserStoreFileByURL(url) { getUserStoreFileByURL(url) {
var path_split = url.split('/'); let path_split = url.split('/');
path_split.shift(); path_split.shift();
path_split.shift(); path_split.shift();
var store_dir_path = path_split.join('/').replace('/', this.path.sep); const store_dir_path = path_split.join('/').replace('/', this.path.sep);
return this.getUserStoreFile(store_dir_path); return this.getUserStoreFile(store_dir_path);
} }
@@ -538,15 +537,16 @@ class WTVClientSessionData {
* @return {boolean} True if the cookie was added successfully, false otherwise * @return {boolean} True if the cookie was added successfully, false otherwise
*/ */
addCookie(domain, path = null, expires = null, data = null) { addCookie(domain, path = null, expires = null, data = null) {
let cookie_data;
if (!this.checkCookies()) this.resetCookies(); if (!this.checkCookies()) this.resetCookies();
if (!domain) return false; if (!domain) return false;
else if (typeof (domain) == 'object') { else if (typeof (domain) == 'object') {
// accept array as first argument // accept array as first argument
if (domain.domain && domain.path && domain.expires && domain.data) var cookie_data = domain; if (domain.domain && domain.path && domain.expires && domain.data) cookie_data = domain;
else return false; else return false;
} else { } else {
if (path && expires && data) { if (path && expires && data) {
var cookie_data = {}; cookie_data = {};
cookie_data['cookie'] = decodeURIComponent(data); cookie_data['cookie'] = decodeURIComponent(data);
cookie_data['expires'] = decodeURIComponent(expires); cookie_data['expires'] = decodeURIComponent(expires);
cookie_data['path'] = decodeURIComponent(path); cookie_data['path'] = decodeURIComponent(path);
@@ -556,8 +556,8 @@ class WTVClientSessionData {
} }
} }
var self = this; const self = this;
var cookie_index = -1; let cookie_index = -1;
// see if we have a cookie for this domain/path // see if we have a cookie for this domain/path
Object.keys(this.session_store.cookies).forEach(function (k) { Object.keys(this.session_store.cookies).forEach(function (k) {
if (cookie_index >= 0) return; if (cookie_index >= 0) return;
@@ -579,15 +579,15 @@ class WTVClientSessionData {
*/ */
getCookie(domain, path) { getCookie(domain, path) {
if (!this.checkCookies()) this.resetCookies(); if (!this.checkCookies()) this.resetCookies();
var self = this; const self = this;
var result = false; let result = false;
Object.keys(this.session_store['cookies']).forEach(function (k) { Object.keys(this.session_store['cookies']).forEach(function (k) {
if (result !== false) return; if (result !== false) return;
if (self.session_store['cookies'][k].domain == domain && if (self.session_store['cookies'][k].domain == domain &&
self.session_store['cookies'][k].path == path) { self.session_store['cookies'][k].path == path) {
var current_epoch_utc = Date.parse((new Date()).toUTCString()); const current_epoch_utc = Date.parse((new Date()).toUTCString());
var cookie_expires_epoch_utc = Date.parse(new Date(Date.parse(self.session_store['cookies'][k].expires)).toUTCString()); const cookie_expires_epoch_utc = Date.parse(new Date(Date.parse(self.session_store['cookies'][k].expires)).toUTCString());
if (cookie_expires_epoch_utc <= current_epoch_utc) self.deleteCookie(self.session_store['cookies'][k]); if (cookie_expires_epoch_utc <= current_epoch_utc) self.deleteCookie(self.session_store['cookies'][k]);
else result = self.session_store['cookies'][k]; else result = self.session_store['cookies'][k];
} }
@@ -602,15 +602,7 @@ class WTVClientSessionData {
* @return {string|false} Cookie string if found, false otherwise * @return {string|false} Cookie string if found, false otherwise
*/ */
getCookieString(domain, path) { getCookieString(domain, path) {
var cookie_data = this.getCookie(domain, path); return this.getCookie(domain, path);
/*
var outstring = "";
Object.keys(cookie_data).forEach(function (k) {
outstring += k + "=" + encodeURIComponent(cookie_data[k]) + "&";
});
return outstring.slice(0, outstring.length - 1);
*/
return cookie_data.cookie;
} }
/** /**
@@ -620,7 +612,7 @@ class WTVClientSessionData {
* @return {boolean} True if the cookie was deleted successfully, false otherwise * @return {boolean} True if the cookie was deleted successfully, false otherwise
*/ */
deleteCookie(domain, path = null) { deleteCookie(domain, path = null) {
var result = false; let result = false;
if (!this.checkCookies()) { if (!this.checkCookies()) {
this.resetCookies(); this.resetCookies();
return true; return true;
@@ -636,7 +628,7 @@ class WTVClientSessionData {
return false; return false;
} }
var self = this; const self = this;
Object.keys(this.session_store['cookies']).forEach(function (k) { Object.keys(this.session_store['cookies']).forEach(function (k) {
if (self.session_store['cookies'][k].domain == domain && self.session_store['cookies'][k].path == path) { if (self.session_store['cookies'][k].domain == domain && self.session_store['cookies'][k].path == path) {
delete self.session_store['cookies'][k]; delete self.session_store['cookies'][k];
@@ -664,8 +656,8 @@ class WTVClientSessionData {
*/ */
listCookies() { listCookies() {
if (!this.checkCookies()) this.resetCookies(); if (!this.checkCookies()) this.resetCookies();
var outstring = ""; let outstring = "";
var self = this; const self = this;
Object.keys(this.session_store.cookies).forEach(function (k) { Object.keys(this.session_store.cookies).forEach(function (k) {
outstring += self.session_store.cookies[k].domain + "\0" + self.session_store.cookies[k].path + "\0"; outstring += self.session_store.cookies[k].domain + "\0" + self.session_store.cookies[k].path + "\0";
}); });
@@ -675,11 +667,9 @@ class WTVClientSessionData {
loadSessionData(raw_data = false) { loadSessionData(raw_data = false) {
try { try {
if (this.fs.lstatSync(this.getUserStoreDirectory() + "user" + this.user_id + ".json")) { if (this.fs.lstatSync(this.getUserStoreDirectory() + "user" + this.user_id + ".json")) {
var json_data = this.fs.readFileSync(this.getUserStoreDirectory() + "user" + this.user_id + ".json", 'Utf8') const json_data = this.fs.readFileSync(this.getUserStoreDirectory() + "user" + this.user_id + ".json", 'Utf8')
if (raw_data) return JSON.parse(json_data); if (raw_data) return JSON.parse(json_data);
this.session_store = JSON.parse(json_data);
var session_data = JSON.parse(json_data);
this.session_store = session_data;
return true; return true;
} }
} catch (e) { } catch (e) {
@@ -700,24 +690,21 @@ class WTVClientSessionData {
} }
encodePassword(passwd) { encodePassword(passwd) {
var encoded_passwd = CryptoJS.SHA512(passwd); return CryptoJS.SHA512(passwd).toString(CryptoJS.enc.Base64);
return encoded_passwd.toString(CryptoJS.enc.Base64);
} }
setUserPassword(passwd) { setUserPassword(passwd) {
var encoded_passwd = this.encodePassword(passwd); this.setSessionData("subscriber_password", this.encodePassword(passwd));
this.setSessionData("subscriber_password", encoded_passwd);
this.saveSessionData(); this.saveSessionData();
} }
setUserSMTPPassword(passwd) { setUserSMTPPassword(passwd) {
var encoded_passwd = this.encryptPassword(passwd); this.setSessionData("subscriber_smtp_password", this.encryptPassword(passwd));
this.setSessionData("subscriber_smtp_password", encoded_passwd);
this.saveSessionData(); this.saveSessionData();
} }
getUserSMTPPassword() { getUserSMTPPassword() {
return this.decryptPassword(this.setSessionData("subscriber_smtp_password")) return this.decryptPassword(this.getSessionData("subscriber_smtp_password"))
} }
disableUserPassword() { disableUserPassword() {
@@ -727,20 +714,17 @@ class WTVClientSessionData {
getUserPasswordEnabled() { getUserPasswordEnabled() {
if (!this.minisrv_config.config.passwords.enabled) return false; // master config override if (!this.minisrv_config.config.passwords.enabled) return false; // master config override
var enabled = this.getSessionData("subscriber_password"); const enabled = this.getSessionData("subscriber_password");
return (enabled !== null && typeof enabled !== 'undefined'); // true if set, false if null/disabled return (enabled !== null && typeof enabled !== 'undefined'); // true if set, false if null/disabled
} }
validateUserPassword(passwd) { validateUserPassword(passwd) {
if (!this.getUserPasswordEnabled()) return true; // no password is set so always validate if (!this.getUserPasswordEnabled()) return true; // no password is set so always validate
return (this.encodePassword(passwd) == this.getSessionData("subscriber_password"));
var encoded_passwd = this.encodePassword(passwd);
return (encoded_passwd == this.getSessionData("subscriber_password"));
} }
isUserLoggedIn() { isUserLoggedIn() {
var password_valid = this.get("password_valid") || false; return (this.get("password_valid") || false);
return (password_valid);
} }
setUserLoggedIn(value) { setUserLoggedIn(value) {
@@ -755,9 +739,8 @@ class WTVClientSessionData {
if (this.isRegistered()) { if (this.isRegistered()) {
if (!skip_merge) { if (!skip_merge) {
// load data from disk and merge new data // load data from disk and merge new data
var temp_data = this.loadSessionData(true); const temp_data = this.loadSessionData(true);
if (temp_data) this.session_store = Object.assign(temp_data, this.session_store); if (temp_data) this.session_store = Object.assign(temp_data, this.session_store);
temp_data = null;
} }
} else { } else {
// do not write file if user is not registered, return true because this is not an error // do not write file if user is not registered, return true because this is not an error
@@ -767,11 +750,11 @@ class WTVClientSessionData {
try { try {
// only save if file has changed // only save if file has changed
var sessionToStore = this.session_store; const sessionToStore = this.session_store;
var json_save_data = JSON.stringify(sessionToStore); const json_save_data = JSON.stringify(sessionToStore);
var json_load_data = (skip_merge) ? {} : this.loadSessionData(true); const json_load_data = (skip_merge) ? {} : this.loadSessionData(true);
var storeDir = this.getUserStoreDirectory(); const storeDir = this.getUserStoreDirectory();
if (!this.fs.existsSync(storeDir)) this.mkdirRecursive(storeDir); if (!this.fs.existsSync(storeDir)) this.mkdirRecursive(storeDir);
if (sessionToStore.password_valid) delete sessionToStore.password_valid; // do not save validity state of password login, resets when session expires if (sessionToStore.password_valid) delete sessionToStore.password_valid; // do not save validity state of password login, resets when session expires
@@ -806,7 +789,7 @@ class WTVClientSessionData {
} }
unregisterBox() { unregisterBox() {
var user_store_base = this.wtvshared.makeSafePath(this.wtvshared.getAbsolutePath(this.getAccountStoreDirectory()), this.path.sep + this.ssid); const user_store_base = this.wtvshared.makeSafePath(this.wtvshared.getAbsolutePath(this.getAccountStoreDirectory()), this.path.sep + this.ssid);
try { try {
if (this.fs.existsSync(user_store_base + ".json")) { if (this.fs.existsSync(user_store_base + ".json")) {
this.fs.unlinkSync(user_store_base + ".json"); this.fs.unlinkSync(user_store_base + ".json");
@@ -939,15 +922,15 @@ class WTVClientSessionData {
} }
checkSecurity() { checkSecurity() {
var self = this; const self = this;
var rejectReason = null; const rejectReason = null;
var ip2long = function (ip) { const ip2long = function (ip) {
var components; let components;
if (components = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) { if (components = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)) {
var iplong = 0; let iplong = 0;
var power = 1; let power = 1;
for (var i = 4; i >= 1; i -= 1) { for (let i = 4; i >= 1; i -= 1) {
iplong += power * parseInt(components[i]); iplong += power * parseInt(components[i]);
power *= 256; power *= 256;
} }
@@ -956,22 +939,7 @@ class WTVClientSessionData {
else return -1; else return -1;
}; };
var isInSubnet = function (ip, subnet) { const rejectSSIDConnection = function (blacklist) {
if (!subnet.includes('/')) {
var mask, base_ip, long_ip = this.ip2long(ip);
var mask2, base_ip2, long_ip2 = this.ip2long(ip);
return (long_ip == long_ip2);
} else {
var mask, base_ip, long_ip = this.ip2long(ip);
if ((mask = subnet.match(/^(.*?)\/(\d{1,2})$/)) && ((base_ip = this.ip2long(mask[1])) >= 0)) {
var freedom = Math.pow(2, 32 - parseInt(mask[2]));
return (long_ip > base_ip) && (long_ip < base_ip + freedom - 1);
}
}
return false;
};
var rejectSSIDConnection = function (blacklist) {
if (blacklist) { if (blacklist) {
rejectReason = self.ssid + " is in the blacklist."; rejectReason = self.ssid + " is in the blacklist.";
console.log(" * Request from SSID", self.wtvshared.filterSSID(self.ssid), "(" + self.clientAddress + "), but that SSID is in the blacklist."); console.log(" * Request from SSID", self.wtvshared.filterSSID(self.ssid), "(" + self.clientAddress + "), but that SSID is in the blacklist.");
@@ -981,13 +949,13 @@ class WTVClientSessionData {
} }
} }
var checkSSIDIPWhitelist = function (ssid, blacklist) { const checkSSIDIPWhitelist = function (ssid, blacklist) {
var ssid_access_list_ip_override = false; let ssid_access_list_ip_override = false;
if (self.minisrv_config.config.ssid_ip_allow_list) { if (self.minisrv_config.config.ssid_ip_allow_list) {
if (self.minisrv_config.config.ssid_ip_allow_list[self.ssid]) { if (self.minisrv_config.config.ssid_ip_allow_list[self.ssid]) {
Object.keys(self.minisrv_config.config.ssid_ip_allow_list[self.ssid]).forEach(function (k) { Object.keys(self.minisrv_config.config.ssid_ip_allow_list[self.ssid]).forEach(function (k) {
if (self.minisrv_config.config.ssid_ip_allow_list[self.ssid][k].includes('/')) { if (self.minisrv_config.config.ssid_ip_allow_list[self.ssid][k].includes('/')) {
if (isInSubnet(self.clientAddress, self.minisrv_config.config.ssid_ip_allow_list[self.ssid][k])) { if (this.wtvshared.isInSubnet(self.clientAddress, self.minisrv_config.config.ssid_ip_allow_list[self.ssid][k])) {
// remoteAddr is in allowed subnet // remoteAddr is in allowed subnet
ssid_access_list_ip_override = true; ssid_access_list_ip_override = true;
} }
@@ -1010,7 +978,7 @@ class WTVClientSessionData {
// process whitelist first // process whitelist first
if (self.ssid && self.minisrv_config.config.ssid_allow_list) { if (self.ssid && self.minisrv_config.config.ssid_allow_list) {
var ssid_is_in_whitelist = self.minisrv_config.config.ssid_allow_list.findIndex(element => element == self.ssid); const ssid_is_in_whitelist = self.minisrv_config.config.ssid_allow_list.findIndex(element => element == self.ssid);
if (ssid_is_in_whitelist === -1) { if (ssid_is_in_whitelist === -1) {
// no whitelist match, but lets see if the remoteAddress is allowed // no whitelist match, but lets see if the remoteAddress is allowed
checkSSIDIPWhitelist(self.ssid, false); checkSSIDIPWhitelist(self.ssid, false);
@@ -1019,7 +987,7 @@ class WTVClientSessionData {
// now check blacklist // now check blacklist
if (self.ssid && self.minisrv_config.config.ssid_block_list) { if (self.ssid && self.minisrv_config.config.ssid_block_list) {
var ssid_is_in_blacklist = self.minisrv_config.config.ssid_block_list.findIndex(element => element == self.ssid); const ssid_is_in_blacklist = self.minisrv_config.config.ssid_block_list.findIndex(element => element == self.ssid);
if (ssid_is_in_blacklist !== -1) { if (ssid_is_in_blacklist !== -1) {
// blacklist match, but lets see if the remoteAddress is allowed // blacklist match, but lets see if the remoteAddress is allowed
checkSSIDIPWhitelist(self.ssid, true); checkSSIDIPWhitelist(self.ssid, true);
@@ -1041,8 +1009,8 @@ class WTVClientSessionData {
if (whitelist == 'lockdown' && !this.lockdown && !ignore_lockdown) return true; if (whitelist == 'lockdown' && !this.lockdown && !ignore_lockdown) return true;
// in lockdown, check whitelisted urls // in lockdown, check whitelisted urls
var self = this; const self = this;
var authorized = false; let authorized = false;
switch (whitelist) { switch (whitelist) {
case "lockdown": case "lockdown":
Object.keys(this.lockdownWhitelist).forEach(function (k) { Object.keys(this.lockdownWhitelist).forEach(function (k) {
@@ -1091,9 +1059,9 @@ class WTVClientSessionData {
} }
getManufacturer(url = false) { getManufacturer(url = false) {
var isPlus = this.hasCap("client-has-tv-experience") const isPlus = this.hasCap("client-has-tv-experience")
var romtype = this.get("wtv-client-rom-type"); const romtype = this.get("wtv-client-rom-type");
var brandId = this.ssid.charAt(8) const brandId = this.ssid.charAt(8)
if (brandId == 0) if (brandId == 0)
if (url && romtype == "US-DTV-disk-0MB-32MB-softmodem-CPU5230") if (url && romtype == "US-DTV-disk-0MB-32MB-softmodem-CPU5230")

View File

@@ -180,12 +180,11 @@ class WTVFavorites {
self.messageArr = []; self.messageArr = [];
this.fs.readdirSync(folderpath) this.fs.readdirSync(folderpath)
.map(function (v) { .map(function (v) {
var favorite_data_raw = null; const favorite_data_raw = null;
var favoritepath = folderpath + self.path.sep + v; const favoritepath = folderpath + self.path.sep + v;
if (self.fs.existsSync(favoritepath)) favorite_data_raw = self.fs.readFileSync(favoritepath); if (self.fs.existsSync(favoritepath)) favorite_data_raw = self.fs.readFileSync(favoritepath);
if (favorite_data_raw) { if (favorite_data_raw) {
var favorite_data = JSON.parse(favorite_data_raw); self.messageArr.push(JSON.parse(favorite_data_raw));
self.messageArr.push(favorite_data);
} }
}) })