mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-02 11:40:04 +08:00
refactor(frontend): outfox foxact, foxts, and eslint-plugin-sukka (#8808)
* chore(eslint): remove eslint-plugin-sukka * refactor(frontend): remove foxts and inline local utilities * refactor(frontend): remove foxact and replace with local hooks * refactor(proxy): reuse local storage for group preferences
This commit is contained in:
+78
-62
@@ -1,8 +1,5 @@
|
||||
import { getName, getVersion } from '@tauri-apps/api/app'
|
||||
import { fetch } from '@tauri-apps/plugin-http'
|
||||
import { asyncRetry } from 'foxts/async-retry'
|
||||
import { extractErrorMessage } from 'foxts/extract-error-message'
|
||||
import { once } from 'foxts/once'
|
||||
import i18n from 'i18next'
|
||||
import { getBaseConfig } from 'tauri-plugin-mihomo-api'
|
||||
|
||||
@@ -10,15 +7,42 @@ import { debugLog } from '@/utils/debug'
|
||||
|
||||
import { getRuntimeConfig } from './cmds'
|
||||
|
||||
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'
|
||||
let userAgentPromise: Promise<string> | null = null
|
||||
const getUserAgentPromise = () =>
|
||||
(userAgentPromise ??= Promise.all([getName(), getVersion()])
|
||||
.then(([name, version]) => `${name}/${version}`)
|
||||
.catch((error) => {
|
||||
console.debug('Failed to build User-Agent, fallback to default', error)
|
||||
return 'clash-verge-rev'
|
||||
}))
|
||||
|
||||
const extractErrorMessage = (error: unknown): string | null =>
|
||||
error instanceof Error ? `${error.name}: ${error.message}`.trim() : null
|
||||
|
||||
const retryWithBackoff = async <T>(
|
||||
fn: (bail: (error: unknown) => never) => Promise<T>,
|
||||
retries: number,
|
||||
): Promise<T> => {
|
||||
let bailed = false
|
||||
const bail = (error: unknown): never => {
|
||||
bailed = true
|
||||
throw error
|
||||
}
|
||||
})
|
||||
|
||||
for (let attempt = 0; ; attempt++) {
|
||||
try {
|
||||
return await fn(bail)
|
||||
} catch (error) {
|
||||
if (bailed || attempt >= retries) throw error
|
||||
|
||||
const delay = Math.min(
|
||||
Math.round((Math.random() + 1) * 1000 * 2 ** attempt),
|
||||
4000,
|
||||
)
|
||||
await new Promise((resolve) => setTimeout(resolve, delay))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Get current IP and geolocation information (refactored IP detection with service-specific mappings)
|
||||
interface IpInfo {
|
||||
ip: string
|
||||
@@ -249,61 +273,53 @@ export const getIpInfo = async (): Promise<
|
||||
debugLog(`尝试IP检测服务: ${service.url}`)
|
||||
|
||||
try {
|
||||
return await asyncRetry(
|
||||
async (bail) => {
|
||||
console.debug('Fetching IP information:', service.url)
|
||||
return await retryWithBackoff(async (bail) => {
|
||||
console.debug('Fetching IP information:', service.url)
|
||||
|
||||
const timeoutController = new AbortController()
|
||||
const timeoutId = setTimeout(() => {
|
||||
timeoutController.abort()
|
||||
}, serviceTimeout)
|
||||
const timeoutController = new AbortController()
|
||||
const timeoutId = setTimeout(() => {
|
||||
timeoutController.abort()
|
||||
}, serviceTimeout)
|
||||
|
||||
try {
|
||||
const response = await fetch(service.url, {
|
||||
method: 'GET',
|
||||
proxy,
|
||||
signal: timeoutController.signal,
|
||||
connectTimeout: serviceTimeout,
|
||||
headers: {
|
||||
'User-Agent': userAgent,
|
||||
},
|
||||
})
|
||||
try {
|
||||
const response = await fetch(service.url, {
|
||||
method: 'GET',
|
||||
proxy,
|
||||
signal: timeoutController.signal,
|
||||
connectTimeout: serviceTimeout,
|
||||
headers: {
|
||||
'User-Agent': userAgent,
|
||||
},
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
return bail(
|
||||
new Error(
|
||||
`IP 检测服务出错,状态码: ${response.status} from ${service.url}`,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
let data: any
|
||||
try {
|
||||
data = await response.json()
|
||||
} catch {
|
||||
return bail(new Error(`无法解析 JSON 响应 from ${service.url}`))
|
||||
}
|
||||
|
||||
if (data && data.ip) {
|
||||
debugLog(`IP检测成功,使用服务: ${service.url}`)
|
||||
return Object.assign(service.mapping(data), {
|
||||
// use last fetch success timestamp
|
||||
lastFetchTs: Date.now(),
|
||||
})
|
||||
} else {
|
||||
return bail(new Error(`无效的响应格式 from ${service.url}`))
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeoutId)
|
||||
if (!response.ok) {
|
||||
return bail(
|
||||
new Error(
|
||||
`IP 检测服务出错,状态码: ${response.status} from ${service.url}`,
|
||||
),
|
||||
)
|
||||
}
|
||||
},
|
||||
{
|
||||
retries: maxRetries,
|
||||
minTimeout: 1000,
|
||||
maxTimeout: 4000,
|
||||
randomize: true,
|
||||
},
|
||||
)
|
||||
|
||||
let data: any
|
||||
try {
|
||||
data = await response.json()
|
||||
} catch {
|
||||
return bail(new Error(`无法解析 JSON 响应 from ${service.url}`))
|
||||
}
|
||||
|
||||
if (data && data.ip) {
|
||||
debugLog(`IP检测成功,使用服务: ${service.url}`)
|
||||
return Object.assign(service.mapping(data), {
|
||||
// use last fetch success timestamp
|
||||
lastFetchTs: Date.now(),
|
||||
})
|
||||
} else {
|
||||
return bail(new Error(`无效的响应格式 from ${service.url}`))
|
||||
}
|
||||
} finally {
|
||||
clearTimeout(timeoutId)
|
||||
}
|
||||
}, maxRetries)
|
||||
} catch (error) {
|
||||
debugLog(`IP检测服务失败: ${service.url}`, error)
|
||||
lastError = error
|
||||
|
||||
Reference in New Issue
Block a user