quickshell: should be everything I want now
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Utils
|
||||
pragma Singleton
|
||||
|
||||
Singleton {
|
||||
@@ -9,28 +10,93 @@ Singleton {
|
||||
property real fetchInterval: 30 // in s
|
||||
property real fetchTimeout: 10 // in s
|
||||
property string ipURL: "https://api.uyanide.com/ip"
|
||||
property string geoURL: "curl https://api.ipinfo.io/lite/"
|
||||
property string geoURL: "https://api.ipinfo.io/lite/"
|
||||
property string geoURLToken: ""
|
||||
|
||||
function fetchIP() {
|
||||
if (fetchIPProcess.running) {
|
||||
console.warn("Fetch IP process is still running, skipping fetchIP");
|
||||
return ;
|
||||
}
|
||||
fetchIPProcess.running = true;
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.timeout = fetchTimeout * 1000;
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
if (response && response.ip) {
|
||||
let newIP = response.ip;
|
||||
Logger.log("IpService", "Fetched IP: " + newIP);
|
||||
if (newIP !== ip) {
|
||||
ip = newIP;
|
||||
fetchGeoInfo(); // Fetch geo info only if IP has changed
|
||||
}
|
||||
} else {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "IP response does not contain 'ip' field");
|
||||
}
|
||||
} catch (e) {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to parse IP response: " + e);
|
||||
}
|
||||
} else {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to fetch IP, status: " + xhr.status);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.ontimeout = function() {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Fetch IP request timed out");
|
||||
};
|
||||
xhr.open("GET", ipURL);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function fetchGeoInfo() {
|
||||
if (fetchGeoProcess.running) {
|
||||
console.warn("Fetch geo process is still running, skipping fetchGeoInfo");
|
||||
return ;
|
||||
}
|
||||
if (!ip || ip === "N/A") {
|
||||
countryCode = "N/A";
|
||||
return ;
|
||||
}
|
||||
fetchGeoProcess.command = ["sh", "-c", `curl -L -m ${fetchTimeout.toString()} ${geoURL}${ip}${geoURLToken ? "?token=" + geoURLToken : ""}`];
|
||||
fetchGeoProcess.running = true;
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.timeout = fetchTimeout * 1000;
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||
if (xhr.status === 200) {
|
||||
try {
|
||||
const response = JSON.parse(xhr.responseText);
|
||||
if (response && response.country) {
|
||||
let newCountryCode = response.country_code;
|
||||
Logger.log("IpService", "Fetched country code: " + newCountryCode);
|
||||
if (newCountryCode !== countryCode) {
|
||||
countryCode = newCountryCode;
|
||||
SendNotification.show("New IP", `IP: ${ip}\nCountry: ${newCountryCode}`);
|
||||
}
|
||||
} else {
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Geo response does not contain 'country' field");
|
||||
}
|
||||
} catch (e) {
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to parse geo response: " + e);
|
||||
}
|
||||
} else {
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to fetch geo info, status: " + xhr.status);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.ontimeout = function() {
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Fetch geo info request timed out");
|
||||
};
|
||||
let url = geoURL + ip;
|
||||
if (geoURLToken)
|
||||
url += "?token=" + geoURLToken;
|
||||
|
||||
xhr.open("GET", url);
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
@@ -46,11 +112,11 @@ Singleton {
|
||||
FileView {
|
||||
id: tokenFile
|
||||
|
||||
path: Qt.resolvedUrl("../Assets/Ip/token.txt")
|
||||
path: Qt.resolvedUrl("../Assets/Config/GeoInfoToken.txt")
|
||||
onLoaded: {
|
||||
geoURLToken = tokenFile.text();
|
||||
if (!geoURLToken)
|
||||
console.warn("No token found for geoIP service, assuming none is required");
|
||||
Logger.warn("IpService", "No token found for geoIP service, assuming none is required");
|
||||
|
||||
fetchIP();
|
||||
fetchTimer.start();
|
||||
@@ -68,64 +134,4 @@ Singleton {
|
||||
}
|
||||
}
|
||||
|
||||
Process {
|
||||
id: fetchIPProcess
|
||||
|
||||
command: ["sh", "-c", `curl -L -m ${fetchTimeout.toString()} ${ipURL}`]
|
||||
running: false
|
||||
|
||||
stdout: SplitParser {
|
||||
splitMarker: ""
|
||||
onRead: (data) => {
|
||||
let newIP = "";
|
||||
try {
|
||||
const response = JSON.parse(data);
|
||||
if (response && response.ip) {
|
||||
newIP = response.ip;
|
||||
console.log("Fetched IP: " + newIP);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to parse IP response: " + e);
|
||||
}
|
||||
if (newIP && newIP !== ip) {
|
||||
ip = newIP;
|
||||
fetchGeoInfo();
|
||||
} else if (!newIP) {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Process {
|
||||
id: fetchGeoProcess
|
||||
|
||||
command: []
|
||||
running: false
|
||||
|
||||
stdout: SplitParser {
|
||||
splitMarker: ""
|
||||
onRead: (data) => {
|
||||
let newCountryCode = "";
|
||||
try {
|
||||
const response = JSON.parse(data);
|
||||
if (response && response.country) {
|
||||
newCountryCode = response.country_code;
|
||||
console.log("Fetched country code: " + newCountryCode);
|
||||
SendNotification.show("New IP", `IP: ${ip}\nCountry: ${newCountryCode}`);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to parse geo response: " + e);
|
||||
}
|
||||
if (newCountryCode && newCountryCode !== countryCode)
|
||||
countryCode = newCountryCode;
|
||||
else if (!newCountryCode)
|
||||
countryCode = "N/A";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user