JDK升级到21,优化ClashUtil的代理切换和网络检查逻辑
- 项目的JDK版本从17升级到21。 - ClashUtil的`switchProxyGroup`方法改为同步执行,并增加了对HTTP响应状态码的检查。 - 新增`checkCountryIsUS`方法,用于通过ipinfo.io判断当前IP是否在美国,并在VPN启动后调用此方法进行验证。 - `LoadDeviceWorker`中的`startProxyVpn`方法现在会根据`checkCountryIsUS`的结果来决定是否继续执行后续操作。 - AutoJs脚本 (`main.js`) 更新,使用Promise和async/await来处理并行的HTTP请求,并分别调用ipv4.geojs.io的接口获取国家代码和详细地理位置信息。
This commit is contained in:
parent
91a60683c5
commit
5ac9d2555f
88
main.js
88
main.js
|
@ -1,13 +1,81 @@
|
|||
var message = "任务已启动";
|
||||
|
||||
function sendMessage(message) {
|
||||
app.sendBroadcast({
|
||||
action: "org.autojs.SCRIPT_FINISHED", // 自定义广播 Action
|
||||
extras: {
|
||||
result: message,
|
||||
package: "org.autojs.autojs6"// 将结果附加到广播中
|
||||
}
|
||||
function promiseHttpLibGet(url) {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.get(url, {}, (res, err) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(res);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
sendMessage(message + "点击开始")
|
||||
async function processIpApi(url, successCallback, errorMsgPrefix) {
|
||||
try {
|
||||
const res = await promiseHttpLibGet(url); // 使用 await等待Promise结果
|
||||
|
||||
if (res && res.statusCode == 200) {
|
||||
try {
|
||||
let data = res.body.json();
|
||||
successCallback(data);
|
||||
} catch (jsonError) {
|
||||
toast(errorMsgPrefix + "解析JSON失败");
|
||||
console.error(errorMsgPrefix + "解析JSON失败: ", jsonError);
|
||||
if (res.body && typeof res.body.string === 'function') {
|
||||
console.log("原始响应体: ", res.body.string());
|
||||
}
|
||||
}
|
||||
} else if (res) {
|
||||
toast(errorMsgPrefix + "获取失败,状态码: " + res.statusCode);
|
||||
console.log(errorMsgPrefix + "获取失败,状态码: " + res.statusCode);
|
||||
if (res.body && typeof res.body.string === 'function') {
|
||||
console.log("原始响应体: ", res.body.string());
|
||||
}
|
||||
} else {
|
||||
toast(errorMsgPrefix + "请求没有返回有效响应");
|
||||
console.log(errorMsgPrefix + "请求没有返回有效响应");
|
||||
}
|
||||
} catch (requestError) {
|
||||
console.error(errorMsgPrefix + "请求失败: ", requestError);
|
||||
toast(errorMsgPrefix + "请求失败,请检查网络");
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// 并行发起两个请求
|
||||
const countryPromise = processIpApi("https://ipv4.geojs.io/v1/ip/country.json", function(data2){
|
||||
toast("国家代码: " + data2.country);
|
||||
console.log("地理位置信息:");
|
||||
console.log("国家代码 (2位): " + data2.country);
|
||||
console.log("国家代码 (3位): " + data2.country_3);
|
||||
console.log("IP 地址: " + data2.ip);
|
||||
console.log("国家名称: " + data2.name);
|
||||
}, "地理位置");
|
||||
|
||||
const geoPromise = processIpApi("https://ipv4.geojs.io/v1/ip/geo.json", function(data3){
|
||||
console.log("新增接口返回的地理数据:");
|
||||
console.log("经度: " + data3.longitude);
|
||||
console.log("纬度: " + data3.latitude);
|
||||
console.log("城市: " + data3.city);
|
||||
console.log("地区: " + data3.region);
|
||||
console.log("国家: " + data3.country);
|
||||
console.log("组织名称: " + data3.organization_name);
|
||||
console.log("IP 地址: " + data3.ip);
|
||||
console.log("国家代码 (2位): " + data3.country_code);
|
||||
console.log("国家代码 (3位): " + data3.country_code3);
|
||||
console.log("时区: " + data3.timezone);
|
||||
console.log("ASN: " + data3.asn);
|
||||
console.log("洲: " + data3.continent_code);
|
||||
}, "详细地理位置");
|
||||
|
||||
// 等待两个请求完成(或失败)
|
||||
try {
|
||||
await Promise.all([countryPromise, geoPromise]);
|
||||
} catch (error) {
|
||||
console.error("一个或多个API请求失败:", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
main(); // 执行主函数
|
Loading…
Reference in New Issue