kde-widget/com.umorist47.meowrelaygui/contents/ui/worker.mjs
2026-03-10 11:52:28 +03:00

457 lines
14 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// чисто в теории это должно помочь долгой загрузке пакетов (так гемини сказал)
// на деле же хуй знает ибо там иногда хендшейк слетает и он просто заного его шлет
const xhrPool = [];
const MAX_POOL_SIZE = 6; // Qt's typical max concurrent connections
function getXhr() {
// Look for an idle object
for (let i = 0; i < xhrPool.length; i++) {
if (xhrPool[i].readyState === 0 || xhrPool[i].readyState === 4) {
return xhrPool[i];
}
}
// If none idle and we have room, make a new one
if (xhrPool.length < MAX_POOL_SIZE) {
let newXhr = new XMLHttpRequest();
xhrPool.push(newXhr);
return newXhr;
}
// If all busy, return null (you should queue the request)
return null;
}
// KeepAlive ping
function keepAlivePing() {
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => keepAlivePing(), 100);
return;
}
xhr.open("HEAD", "http://10.7.0.1:7777/me/profile");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
//console.log("ping pong")
}
}
}
xhr.send();
}
// GET routes
function fetchRoutes() {
//isLoading = true;
//routeModel.clear();
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => fetchRoutes(), 100);
return;
}
xhr.open("GET", "http://10.7.0.1:7777/routes");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
let response = JSON.parse(xhr.responseText);
WorkerScript.sendMessage({ "action": "fetch_routes", "data": response });
//for (var i = 0; i < response.length; i++) {
// routeModel.append(response[i]);
//}
} else {
//WorkerScript.sendMessage({ "action": "fetch_routes", "data": [{"code":"ERR","name":"Error, please try again :<","provider":xhr.status}] });
console.error("Failed to fetch routes:", xhr.status);
}
}
}
xhr.send();
}
// GET profile
function fetchProfile() {
//isLoading = true;
//profileModel.clear();
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => fetchProfile(), 100);
return;
}
xhr.open("GET", "http://10.7.0.1:7777/me/profile");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
let response = JSON.parse(xhr.responseText);
WorkerScript.sendMessage({ "action": "fetch_profile", "data": response });
//for (var i = 0; i < response.length; i++) {
// profileModel.append(response[i]);
//}
} else {
//WorkerScript.sendMessage({ "action": "fetch_profile", "data": {"provider":xhr.status, "current_device":"error"} });
console.error("Failed to fetch profile:", xhr.status);
}
}
}
xhr.send();
}
// GET stats
function fetchStats() {
//isLoading = true;
//statsModel.clear();
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => fetchStats(), 100);
return;
}
xhr.open("GET", "http://10.7.0.1:7777/stats");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
let response = JSON.parse(xhr.responseText);
WorkerScript.sendMessage({ "action": "fetch_stats", "data": response });
//for (var i = 0; i < response.length; i++) {
// profileModel.append(response[i]);
//}
} else {
//WorkerScript.sendMessage({ "action": "fetch_stats", "data": {} });
console.error("Failed to fetch profile:", xhr.status);
}
}
}
xhr.send();
}
// GET firewall
function fetchFirewall() {
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => fetchFirewall(), 100);
return;
}
xhr.open("GET", "http://10.7.0.1:7777/meowrok/get");
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
let response = JSON.parse(xhr.responseText);
WorkerScript.sendMessage({ "action": "fetch_firewall", "data": response });
} else {
console.error("Failed to fetch firewall:", xhr.status);
}
}
}
xhr.send();
}
// POST select country
function selectCountry(code, provider) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => selectCountry(code, provider), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/country");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"countryCode": code,
"provider": provider
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Changed to " + code);
WorkerScript.sendMessage({ "action": "post_country", "status": xhr.status});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_country", "status": xhr.status});
}
}
}
xhr.send(data);
}
// POST switch share
function switchShare(bool) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => switchShare(bool), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/profile");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"shareTraffic": bool
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Share changed to " + bool);
WorkerScript.sendMessage({ "action": "post_share", "status": xhr.status});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_share", "status": xhr.status});
}
}
}
xhr.send(data);
}
// POST switch ebalka
function switchEbalka(bool, ip) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => switchEbalka(bool, ip), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/ebalka");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"ebalka": bool,
"ip": ip
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Ebalka for " + ip + " changed to " + bool);
WorkerScript.sendMessage({ "action": "post_ebalka", "status": xhr.status, "ip": ip});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_ebalka", "status": xhr.status, "ip": ip});
}
}
}
xhr.send(data);
}
// POST switch ebalka
function renameDevice(ip, name) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => renameDevice(ip, name), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/device");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"ip": ip,
"name": name
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Name for " + ip + " changed to " + name);
WorkerScript.sendMessage({ "action": "post_name", "status": xhr.status, "ip": ip});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_name", "status": xhr.status, "ip": ip});
}
}
}
xhr.send(data);
}
// POST feature switch
function switchFeature(name, value) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => switchFeature(name, value), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/feature");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"name": name,
"value": value
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Feature " + name + " changed to " + value);
WorkerScript.sendMessage({ "action": "post_feature" });
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_feature" });
}
}
}
xhr.send(data);
}
// POST connect by server id
function connectById(iface) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => connectById(iface), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/me/connect_by_id");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"iface": iface
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: Changed to " + iface);
WorkerScript.sendMessage({ "action": "post_idconnect", "status": xhr.status});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_idconnect", "status": xhr.status});
}
}
}
xhr.send(data);
}
// POST aff rule to firewall
function firewallAddRule(dst, ext, local_port, port, proto) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => firewallAddRule(dst, ext, local_port, port, proto), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/meowrok/add");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"dst": dst,
"ext": ext,
"local_port": local_port,
"port": port,
"proto": proto
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: rule added!");
WorkerScript.sendMessage({ "action": "post_ruleadd", "status": xhr.status});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_ruleadd", "status": xhr.status});
}
}
}
xhr.send(data);
}
// POST drop rule from firewall
function firewallDropRule(dst, ext, local_port, port, proto) {
//isLoading = true;
let xhr = getXhr();
if (!xhr) {
console.log("Pool full, retrying...");
setTimeout(() => firewallDropRule(dst, ext, local_port, port, proto), 100);
return;
}
xhr.open("POST", "http://10.7.0.1:7777/meowrok/drop");
xhr.setRequestHeader("Content-Type", "application/json");
var data = JSON.stringify({
"dst": dst,
"ext": ext,
"local_port": local_port,
"port": port,
"proto": proto
});
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
//isLoading = false;
if (xhr.status === 200) {
console.log("Success: rule dropped!");
WorkerScript.sendMessage({ "action": "post_ruledrop", "status": xhr.status});
} else {
console.error("POST failed:", xhr.status);
WorkerScript.sendMessage({ "action": "post_ruledrop", "status": xhr.status});
}
}
}
xhr.send(data);
}
WorkerScript.onMessage = function(message) {
const actions = {
"fetch_routes": () => fetchRoutes(message),
"fetch_stats": () => fetchStats(message),
"fetch_profile": () => fetchProfile(message),
"fetch_firewall": () => fetchFirewall(message),
"post_country": () => selectCountry(message.code, message.provider),
"post_share": () => switchShare(message.bool),
"post_ebalka": () => switchEbalka(message.bool, message.ip),
"post_name": () => renameDevice(message.ip, message.name),
"post_feature": () => switchFeature(message.name, message.value),
"post_idconnect": () => connectById(message.iface),
"post_ruleadd": () => firewallAddRule(message.dst, message.ext, message.local_port, message.port, message.proto),
"post_ruledrop": () => firewallDropRule(message.dst, message.ext, message.local_port, message.port, message.proto),
"ping": () => keepAlivePing(message)
};
if (actions[message.action]) {
actions[message.action]();
}
}