quickshell: use curl to fetch instead of XMLHttpRequest
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import qs.Services
|
||||
import qs.Utils
|
||||
pragma Singleton
|
||||
|
||||
@@ -14,44 +15,35 @@ Singleton {
|
||||
property string geoURLToken: ""
|
||||
|
||||
function fetchIP() {
|
||||
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");
|
||||
curl.fetch(ipURL, function(success, data) {
|
||||
if (success) {
|
||||
try {
|
||||
const response = JSON.parse(data);
|
||||
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
|
||||
SendNotification.show("New IP", `IP: ${ip}\nCountry: ${countryCode}`);
|
||||
cacheFile.writeAdapter();
|
||||
}
|
||||
} catch (e) {
|
||||
} else {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to parse IP response: " + e);
|
||||
Logger.error("IpService", "IP response does not contain 'ip' field");
|
||||
}
|
||||
} else {
|
||||
} catch (e) {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to fetch IP, status: " + xhr.status);
|
||||
Logger.error("IpService", "Failed to parse IP response: " + e);
|
||||
}
|
||||
} else {
|
||||
ip = "N/A";
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Failed to fetch IP");
|
||||
}
|
||||
};
|
||||
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() {
|
||||
@@ -59,47 +51,35 @@ Singleton {
|
||||
countryCode = "N/A";
|
||||
return ;
|
||||
}
|
||||
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_code) {
|
||||
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");
|
||||
}
|
||||
cacheFileAdapter.ip = ip;
|
||||
cacheFileAdapter.geoInfo = response;
|
||||
cacheFile.writeAdapter();
|
||||
} 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();
|
||||
curl.fetch(url, function(success, data) {
|
||||
if (success) {
|
||||
try {
|
||||
const response = JSON.parse(data);
|
||||
if (response && response.country_code) {
|
||||
let newCountryCode = response.country_code;
|
||||
Logger.log("IpService", "Fetched country code: " + newCountryCode);
|
||||
if (newCountryCode !== countryCode)
|
||||
countryCode = newCountryCode;
|
||||
|
||||
} else {
|
||||
countryCode = "N/A";
|
||||
Logger.error("IpService", "Geo response does not contain 'country_code' field");
|
||||
}
|
||||
cacheFileAdapter.ip = ip;
|
||||
cacheFileAdapter.geoInfo = response;
|
||||
} 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");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
@@ -112,6 +92,10 @@ Singleton {
|
||||
Component.onCompleted: {
|
||||
}
|
||||
|
||||
NetworkFetch {
|
||||
id: curl
|
||||
}
|
||||
|
||||
FileView {
|
||||
id: tokenFile
|
||||
|
||||
|
||||
Reference in New Issue
Block a user