From 9d96ac0f6aff60f41b61744c6238c352e8aae69a Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 18 Sep 2025 19:35:14 +0800 Subject: [PATCH] feat: Integrate HTTP plugin and update IP detection to use fetch API #4712 --- UPDATELOG.md | 1 + package.json | 7 +-- pnpm-lock.yaml | 70 +++++++---------------------- src-tauri/Cargo.lock | 31 +++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/capabilities/desktop.json | 8 +++- src-tauri/src/lib.rs | 3 +- src/services/api.ts | 17 ++++--- 8 files changed, 71 insertions(+), 67 deletions(-) diff --git a/UPDATELOG.md b/UPDATELOG.md index 159e8b63..7daa1253 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -13,6 +13,7 @@ - 修复静默启动和自动进入轻量模式时,托盘状态刷新不再依赖窗口创建流程 - macOS Tun/系统代理 模式下图标大小不统一 - 托盘节点切换不再显示隐藏组 +- 修复前端 IP 检测无法使用 ipapi, ipsb 提供商 ## v2.4.2 diff --git a/package.json b/package.json index da9a3701..92adbbde 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@tauri-apps/plugin-clipboard-manager": "^2.3.0", "@tauri-apps/plugin-dialog": "^2.4.0", "@tauri-apps/plugin-fs": "^2.4.2", + "@tauri-apps/plugin-http": "~2", "@tauri-apps/plugin-process": "^2.3.0", "@tauri-apps/plugin-shell": "2.3.1", "@tauri-apps/plugin-updater": "2.9.0", @@ -72,9 +73,6 @@ "zustand": "^5.0.8" }, "devDependencies": { - "tar": "^7.4.3", - "glob": "^11.0.3", - "cli-color": "^2.0.4", "@actions/github": "^6.0.1", "@eslint/js": "^9.35.0", "@tauri-apps/cli": "2.8.4", @@ -85,10 +83,12 @@ "@vitejs/plugin-legacy": "^7.2.1", "@vitejs/plugin-react": "5.0.2", "adm-zip": "^0.5.16", + "cli-color": "^2.0.4", "commander": "^14.0.1", "cross-env": "^10.0.0", "eslint": "^9.35.0", "eslint-plugin-react": "^7.37.5", + "glob": "^11.0.3", "globals": "^16.4.0", "https-proxy-agent": "^7.0.6", "jiti": "^2.5.1", @@ -96,6 +96,7 @@ "node-fetch": "^3.3.2", "prettier": "^3.6.2", "sass": "^1.92.1", + "tar": "^7.4.3", "terser": "^5.44.0", "typescript": "^5.9.2", "typescript-eslint": "^8.44.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 880c6ed9..54589a32 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,9 +50,9 @@ importers: '@tauri-apps/plugin-fs': specifier: ^2.4.2 version: 2.4.2 - '@tauri-apps/plugin-notification': - specifier: ^2.3.1 - version: 2.3.1 + '@tauri-apps/plugin-http': + specifier: ~2 + version: 2.5.2 '@tauri-apps/plugin-process': specifier: ^2.3.0 version: 2.3.0 @@ -71,18 +71,12 @@ importers: axios: specifier: ^1.12.2 version: 1.12.2 - cli-color: - specifier: ^2.0.4 - version: 2.0.4 dayjs: specifier: 1.11.18 version: 1.11.18 foxact: specifier: ^0.2.49 version: 0.2.49(react@19.1.1) - glob: - specifier: ^11.0.3 - version: 11.0.3 i18next: specifier: ^25.5.2 version: 25.5.2(typescript@5.9.2) @@ -131,15 +125,9 @@ importers: react-virtuoso: specifier: ^4.14.0 version: 4.14.0(react-dom@19.1.1(react@19.1.1))(react@19.1.1) - sockette: - specifier: ^2.0.6 - version: 2.0.6 swr: specifier: ^2.3.6 version: 2.3.6(react@19.1.1) - tar: - specifier: ^7.4.3 - version: 7.4.3 types-pac: specifier: ^1.0.3 version: 1.0.3 @@ -177,6 +165,9 @@ importers: adm-zip: specifier: ^0.5.16 version: 0.5.16 + cli-color: + specifier: ^2.0.4 + version: 2.0.4 commander: specifier: ^14.0.1 version: 14.0.1 @@ -189,6 +180,9 @@ importers: eslint-plugin-react: specifier: ^7.37.5 version: 7.37.5(eslint@9.35.0(jiti@2.5.1)) + glob: + specifier: ^11.0.3 + version: 11.0.3 globals: specifier: ^16.4.0 version: 16.4.0 @@ -204,18 +198,15 @@ importers: node-fetch: specifier: ^3.3.2 version: 3.3.2 - path: - specifier: ^0.12.7 - version: 0.12.7 prettier: specifier: ^3.6.2 version: 3.6.2 - process: - specifier: ^0.11.10 - version: 0.11.10 sass: specifier: ^1.92.1 version: 1.92.1 + tar: + specifier: ^7.4.3 + version: 7.4.3 terser: specifier: ^5.44.0 version: 5.44.0 @@ -1629,8 +1620,8 @@ packages: '@tauri-apps/plugin-fs@2.4.2': resolution: {integrity: sha512-YGhmYuTgXGsi6AjoV+5mh2NvicgWBfVJHHheuck6oHD+HC9bVWPaHvCP0/Aw4pHDejwrvT8hE3+zZAaWf+hrig==} - '@tauri-apps/plugin-notification@2.3.1': - resolution: {integrity: sha512-7gqgfANSREKhh35fY1L4j3TUjUdePmU735FYDqRGeIf8nMXWpcx6j4FhN9/4nYz+m0mv79DCTPLqIPTySggGgg==} + '@tauri-apps/plugin-http@2.5.2': + resolution: {integrity: sha512-x1mQKHSLDk4mS2S938OTeyk8L7QyLpCrKZCZcjkljGsvTvRMojCvI9SeJ1kaxc7t8xSilkC7WdId8xER9TIGLg==} '@tauri-apps/plugin-process@2.3.0': resolution: {integrity: sha512-0DNj6u+9csODiV4seSxxRbnLpeGYdojlcctCuLOCgpH9X3+ckVZIEj6H7tRQ7zqWr7kSTEWnrxtAdBb0FbtrmQ==} @@ -2501,9 +2492,6 @@ packages: resolution: {integrity: sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==} engines: {node: '>=0.8.19'} - inherits@2.0.3: - resolution: {integrity: sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==} - inline-style-parser@0.2.4: resolution: {integrity: sha512-0aO8FkhNZlj/ZIbNi7Lxxr12obT7cL1moPfE4tg1LkX7LlLfC6DeX4l2ZEud1ukP9jNQyNnfzQVqwbwmAATY4Q==} @@ -3040,9 +3028,6 @@ packages: resolution: {integrity: sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==} engines: {node: '>=8'} - path@0.12.7: - resolution: {integrity: sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==} - picocolors@1.1.1: resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} @@ -3071,10 +3056,6 @@ packages: engines: {node: '>=14'} hasBin: true - process@0.11.10: - resolution: {integrity: sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==} - engines: {node: '>= 0.6.0'} - prop-types@15.8.1: resolution: {integrity: sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==} @@ -3334,9 +3315,6 @@ packages: snake-case@3.0.4: resolution: {integrity: sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==} - sockette@2.0.6: - resolution: {integrity: sha512-W6iG8RGV6Zife3Cj+FhuyHV447E6fqFM2hKmnaQrTvg3OydINV3Msj3WPFbX76blUlUxvQSMMMdrJxce8NqI5Q==} - source-map-js@1.2.1: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} @@ -3566,9 +3544,6 @@ packages: peerDependencies: react: ^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 - util@0.10.4: - resolution: {integrity: sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==} - vfile-message@4.0.2: resolution: {integrity: sha512-jRDZ1IMLttGj41KcZvlrYAaI3CfqpLpfpf+Mfig13viT6NKvRzWZ+lXz0Y5D60w6uJIBAOGq9mSHf0gktF0duw==} @@ -5172,7 +5147,7 @@ snapshots: dependencies: '@tauri-apps/api': 2.8.0 - '@tauri-apps/plugin-notification@2.3.1': + '@tauri-apps/plugin-http@2.5.2': dependencies: '@tauri-apps/api': 2.8.0 @@ -6285,8 +6260,6 @@ snapshots: imurmurhash@0.1.4: {} - inherits@2.0.3: {} - inline-style-parser@0.2.4: {} internal-slot@1.1.0: @@ -6958,11 +6931,6 @@ snapshots: path-type@4.0.0: {} - path@0.12.7: - dependencies: - process: 0.11.10 - util: 0.10.4 - picocolors@1.1.1: {} picomatch@2.3.1: {} @@ -6981,8 +6949,6 @@ snapshots: prettier@3.6.2: {} - process@0.11.10: {} - prop-types@15.8.1: dependencies: loose-envify: 1.4.0 @@ -7297,8 +7263,6 @@ snapshots: dot-case: 3.0.4 tslib: 2.8.1 - sockette@2.0.6: {} - source-map-js@1.2.1: {} source-map-support@0.5.21: @@ -7581,10 +7545,6 @@ snapshots: dependencies: react: 19.1.1 - util@0.10.4: - dependencies: - inherits: 2.0.3 - vfile-message@4.0.2: dependencies: '@types/unist': 3.0.3 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 7927d4f4..b61def69 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1149,6 +1149,7 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-fs", "tauri-plugin-global-shortcut", + "tauri-plugin-http", "tauri-plugin-notification", "tauri-plugin-process", "tauri-plugin-shell", @@ -1656,6 +1657,12 @@ dependencies = [ "parking_lot_core 0.9.11", ] +[[package]] +name = "data-url" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" + [[package]] name = "deelevate" version = "0.2.0" @@ -7315,6 +7322,30 @@ dependencies = [ "thiserror 2.0.16", ] +[[package]] +name = "tauri-plugin-http" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938a3d7051c9a82b431e3a0f3468f85715b3442b3c3a3913095e9fa509e2652c" +dependencies = [ + "bytes", + "cookie_store", + "data-url", + "http 1.3.1", + "regex", + "reqwest", + "schemars 0.8.22", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "tauri-plugin-fs", + "thiserror 2.0.16", + "tokio", + "url", + "urlpattern", +] + [[package]] name = "tauri-plugin-notification" version = "2.3.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index d94cb21b..3b61ecd8 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -83,6 +83,7 @@ isahc = { version = "1.7.2", default-features = false, features = [ "parking_lot", ] } backoff = { version = "0.4.0", features = ["tokio"] } +tauri-plugin-http = "2" [target.'cfg(windows)'.dependencies] diff --git a/src-tauri/capabilities/desktop.json b/src-tauri/capabilities/desktop.json index a61c708a..7a09c952 100755 --- a/src-tauri/capabilities/desktop.json +++ b/src-tauri/capabilities/desktop.json @@ -18,6 +18,12 @@ "autostart:allow-disable", "autostart:allow-is-enabled", "core:window:allow-set-theme", - "notification:default" + "notification:default", + "http:default", + "http:allow-fetch", + { + "identifier": "http:default", + "allow": [{ "url": "https://*/*" }, { "url": "http://*/*" }] + } ] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4889da7e..07c9dec3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -74,7 +74,8 @@ mod app_init { .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) - .plugin(tauri_plugin_deep_link::init()); + .plugin(tauri_plugin_deep_link::init()) + .plugin(tauri_plugin_http::init()); #[cfg(all(debug_assertions, not(feature = "tokio-trace")))] { diff --git a/src/services/api.ts b/src/services/api.ts index a75ccc0d..a7fb8672 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -1,3 +1,4 @@ +import { fetch } from "@tauri-apps/plugin-http"; import axios, { AxiosInstance } from "axios"; import { getClashInfo } from "./cmds"; @@ -212,17 +213,19 @@ export const getIpInfo = async (): Promise => { timeoutController.abort(); }, service.timeout || serviceTimeout); - const response = await axios.get(service.url, { + const response = await fetch(service.url, { + method: "GET", signal: timeoutController.signal, - timeout: service.timeout || serviceTimeout, - // 移除了headers参数(默认会使用axios的默认User-Agent) + connectTimeout: service.timeout || serviceTimeout, }); + const data = await response.json(); + if (timeoutId) clearTimeout(timeoutId); - if (response.data && response.data.ip) { + if (data && data.ip) { console.log(`IP检测成功,使用服务: ${service.url}`); - return service.mapping(response.data); + return service.mapping(data); } else { throw new Error(`无效的响应格式 from ${service.url}`); } @@ -230,9 +233,9 @@ export const getIpInfo = async (): Promise => { if (timeoutId) clearTimeout(timeoutId); lastError = error; - console.log( + console.warn( `尝试 ${attempt + 1}/${maxRetries} 失败 (${service.url}):`, - error.message, + error, ); if (error.name === "AbortError") {