[Desktop] Use fallback mirrors for China users (#2424)

This commit is contained in:
Chenlei Hu
2025-02-05 10:51:32 -05:00
committed by GitHub
parent 0af4768dd2
commit 6b64b74f6c
3 changed files with 66 additions and 2 deletions

View File

@@ -41,11 +41,12 @@ import {
} from '@comfyorg/comfyui-electron-types'
import Divider from 'primevue/divider'
import Panel from 'primevue/panel'
import { ModelRef, computed, ref } from 'vue'
import { ModelRef, computed, onMounted, ref } from 'vue'
import MirrorItem from '@/components/install/mirror/MirrorItem.vue'
import { PYPI_MIRROR, PYTHON_MIRROR, UVMirror } from '@/constants/uvMirrors'
import { t } from '@/i18n'
import { isInChina } from '@/utils/networkUtil'
import { ValidationState, mergeValidationStates } from '@/utils/validationUtil'
const showMirrorInputs = ref(false)
@@ -101,4 +102,13 @@ const validationStateTooltip = computed(() => {
return t('install.settings.checkingMirrors')
}
})
onMounted(async () => {
// Check if user is in China and set fallback mirrors directly
if (await isInChina()) {
for (const [item, modelValue] of mirrors.value) {
modelValue.value = item.fallbackMirror
}
}
})
</script>

View File

@@ -22,6 +22,7 @@
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import UrlInput from '@/components/common/UrlInput.vue'
import { UVMirror } from '@/constants/uvMirrors'
import { normalizeI18nKey } from '@/utils/formatUtil'
import { checkMirrorReachable } from '@/utils/networkUtil'
@@ -43,7 +44,10 @@ const normalizedSettingId = computed(() => {
})
onMounted(() => {
modelValue.value = item.mirror
// Set mirror value if not already set
if (!modelValue.value) {
modelValue.value = item.mirror
}
})
watch(validationState, (newState) => {

View File

@@ -24,3 +24,53 @@ export const checkMirrorReachable = async (mirror: string) => {
isValidUrl(mirror) && (await electronAPI().NetWork.canAccessUrl(mirror))
)
}
/**
* Checks if the user is likely in mainland China by:
* 1. Checking navigator.language
* 2. Testing connectivity to commonly blocked services
* 3. Testing latency to China-specific domains
*/
export async function isInChina(): Promise<boolean> {
// Quick check based on language/locale
const isChineseLocale = navigator.language.toLowerCase().startsWith('zh-cn')
try {
// Test connectivity to Google - commonly blocked in China
const googleTest = await Promise.race([
fetch('https://www.google.com', {
mode: 'no-cors',
cache: 'no-cache'
}),
new Promise((_, reject) => setTimeout(() => reject(), 2000))
])
// If Google is accessible, user is likely not in China
if (googleTest) {
return false
}
} catch {
// Google is not accessible - potential indicator of being in China
if (isChineseLocale) {
return true
}
// Additional check - test latency to a reliable Chinese domain
try {
const start = performance.now()
await fetch('https://www.baidu.com', {
mode: 'no-cors',
cache: 'no-cache'
})
const latency = performance.now() - start
// If Baidu responds quickly (<150ms), user is likely in China
return latency < 150
} catch {
// If both tests fail, default to locale check
return isChineseLocale
}
}
return false
}