mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-13 05:20:28 +08:00
feat: allow pass user-agent for IP detection (#6272)
* feat: allow pass user-agent when lookup ip API * Update src/services/api.ts Co-authored-by: Sukka <isukkaw@gmail.com> * refactor: optimize user-agent retrieval with memoization --------- Co-authored-by: Sukka <isukkaw@gmail.com>
This commit is contained in:
parent
5bcb057bf9
commit
5480e57e67
@ -1,9 +1,20 @@
|
|||||||
|
import { getName, getVersion } from "@tauri-apps/api/app";
|
||||||
import { fetch } from "@tauri-apps/plugin-http";
|
import { fetch } from "@tauri-apps/plugin-http";
|
||||||
import { asyncRetry } from "foxts/async-retry";
|
import { asyncRetry } from "foxts/async-retry";
|
||||||
import { extractErrorMessage } from "foxts/extract-error-message";
|
import { extractErrorMessage } from "foxts/extract-error-message";
|
||||||
|
import { once } from "foxts/once";
|
||||||
|
|
||||||
import { debugLog } from "@/utils/debug";
|
import { debugLog } from "@/utils/debug";
|
||||||
|
|
||||||
|
const getUserAgentPromise = once(async () => {
|
||||||
|
try {
|
||||||
|
const [name, version] = await Promise.all([getName(), getVersion()]);
|
||||||
|
return `${name}/${version}`;
|
||||||
|
} catch (error) {
|
||||||
|
console.debug("Failed to build User-Agent, fallback to default", error);
|
||||||
|
return "clash-verge-rev";
|
||||||
|
}
|
||||||
|
});
|
||||||
// Get current IP and geolocation information (refactored IP detection with service-specific mappings)
|
// Get current IP and geolocation information (refactored IP detection with service-specific mappings)
|
||||||
interface IpInfo {
|
interface IpInfo {
|
||||||
ip: string;
|
ip: string;
|
||||||
@ -192,6 +203,8 @@ export const getIpInfo = async (): Promise<
|
|||||||
|
|
||||||
const shuffledServices = shuffleServices();
|
const shuffledServices = shuffleServices();
|
||||||
let lastError: unknown | null = null;
|
let lastError: unknown | null = null;
|
||||||
|
const userAgent = await getUserAgentPromise();
|
||||||
|
console.debug("User-Agent for IP detection:", userAgent);
|
||||||
|
|
||||||
for (const service of shuffledServices) {
|
for (const service of shuffledServices) {
|
||||||
debugLog(`尝试IP检测服务: ${service.url}`);
|
debugLog(`尝试IP检测服务: ${service.url}`);
|
||||||
@ -210,6 +223,9 @@ export const getIpInfo = async (): Promise<
|
|||||||
method: "GET",
|
method: "GET",
|
||||||
signal: timeoutController.signal, // AbortSignal.timeout(service.timeout || serviceTimeout),
|
signal: timeoutController.signal, // AbortSignal.timeout(service.timeout || serviceTimeout),
|
||||||
connectTimeout: service.timeout || serviceTimeout,
|
connectTimeout: service.timeout || serviceTimeout,
|
||||||
|
headers: {
|
||||||
|
"User-Agent": userAgent,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user