Files
2026-03-25 12:29:03 -05:00

55 lines
1.6 KiB
TypeScript

export type EasyTierConfig = {
networkName: string
networkSecret: string
peerUrls: string[]
}
export const DEFAULT_EASYTIER_PEER_URLS = ['tcp://public.easytier.top:11010']
function normalizePeerUrls(value: unknown): string[] {
const seen = new Set<string>()
const next = Array.isArray(value) ? value.map(String) : []
const normalized = next
.map((item) => item.trim())
.filter((item) => {
if (!item || seen.has(item)) return false
seen.add(item)
return true
})
return normalized.length > 0 ? normalized : [...DEFAULT_EASYTIER_PEER_URLS]
}
/** 与 Electron `remote:get-bind-payload` 输出一致,供扫码导入 */
export type BindPayload = {
v: 1
baseUrl: string
port: number
token: string
easytier: EasyTierConfig
}
export function parseBindPayload(raw: string): BindPayload | null {
try {
const j = JSON.parse(raw) as Record<string, unknown>
if (j.v !== 1) return null
const et = j.easytier as Record<string, unknown> | undefined
if (!et || typeof j.baseUrl !== 'string' || typeof j.token !== 'string') return null
const networkName = String(et.networkName ?? '').trim()
const networkSecret = String(et.networkSecret ?? '').trim()
if (!networkName || !networkSecret) return null
return {
v: 1,
baseUrl: String(j.baseUrl).replace(/\/$/, ''),
port: typeof j.port === 'number' ? j.port : Number(j.port) || 8001,
token: String(j.token),
easytier: {
networkName,
networkSecret,
peerUrls: normalizePeerUrls(et.peerUrls),
},
}
} catch {
return null
}
}