70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import { invoke } from '@tauri-apps/api/core'
|
|
|
|
import { check_mojang_services } from '@/helpers/auth.js'
|
|
import { ensureFallenAuthProxyArgs, removeFallenAuthProxyArgs } from '@/helpers/java-arguments'
|
|
import { type AppSettings, get, set } from '@/helpers/settings'
|
|
|
|
const DEFAULT_RETRIES = 3
|
|
const RETRY_DELAY_MS = 1000
|
|
const REQUIRED_MOJANG_SERVICES = new Set(['account', 'session', 'services', 'profiles'])
|
|
|
|
export async function checkMojangAuthServers(retries = DEFAULT_RETRIES): Promise<boolean> {
|
|
for (let attempt = 0; attempt < retries; attempt++) {
|
|
try {
|
|
const statuses = await check_mojang_services()
|
|
const requiredStatuses = statuses.filter((status) =>
|
|
REQUIRED_MOJANG_SERVICES.has(status.service),
|
|
)
|
|
if (requiredStatuses.length > 0 && requiredStatuses.every((status) => status.reachable)) {
|
|
return true
|
|
}
|
|
} catch {
|
|
if (attempt < retries - 1) {
|
|
await new Promise((resolve) => setTimeout(resolve, RETRY_DELAY_MS))
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
function sameArgs(left: string[], right: string[]) {
|
|
return left.length === right.length && left.every((arg, index) => arg === right[index])
|
|
}
|
|
|
|
export async function setMojangAuthUseMirror(useMirror: boolean, automatic: boolean) {
|
|
await invoke('plugin:auth|set_mojang_auth_use_mirror', { useMirror, automatic })
|
|
}
|
|
|
|
export async function reconcileMojangAuthSource(settings: AppSettings): Promise<boolean> {
|
|
const mode = settings.mojang_auth_source ?? 'auto'
|
|
const automatic = mode === 'auto' || mode === 'official_preferred'
|
|
let useMirror: boolean
|
|
if (mode === 'mirror_preferred') {
|
|
useMirror = true
|
|
} else if (mode === 'official_only') {
|
|
useMirror = false
|
|
} else {
|
|
// Automatic and official-preferred check the services used by current
|
|
// Minecraft versions, falling back only when one of them is down.
|
|
useMirror = !(await checkMojangAuthServers())
|
|
}
|
|
|
|
await setMojangAuthUseMirror(useMirror, automatic)
|
|
|
|
const nextArgs = useMirror
|
|
? ensureFallenAuthProxyArgs(settings.extra_launch_args)
|
|
: removeFallenAuthProxyArgs(settings.extra_launch_args)
|
|
|
|
if (sameArgs(nextArgs, settings.extra_launch_args)) return false
|
|
|
|
settings.extra_launch_args = nextArgs
|
|
return true
|
|
}
|
|
|
|
export async function reconcileMojangAuthSourceAtStartup(): Promise<void> {
|
|
const settings = await get()
|
|
if (await reconcileMojangAuthSource(settings)) {
|
|
await set(settings)
|
|
}
|
|
}
|