use fastest cdn to fetch data

This commit is contained in:
HolographicHat
2022-04-07 22:50:36 +08:00
parent ea168ce96b
commit dd582437bc
4 changed files with 28 additions and 32 deletions

BIN
2MB.bin Normal file

Binary file not shown.

BIN
65536.bin

Binary file not shown.

View File

@@ -35,7 +35,7 @@
[issues](https://github.com/HolographicHat/genshin-achievement-export/issues)或[QQ群: 913777414](https://qm.qq.com/cgi-bin/qm/qr?k=9UGz-chQVTjZa4b82RA_A41vIcBVNpms&jump_from=webapi)
## 常见问题
0. Q: 程序异常退出或被强行终止后,原神启动时报错: 无法连接网络(4201)
0. Q: 程序异常退出或被强行终止后,程序报错:网络错误(22-1) 或 原神启动时报错: 无法连接网络(4201)
A: 用文本编辑器打开```C:\Windows\System32\drivers\etc\hosts```,删除```127.0.0.1 dispatch**global.yuanshen.com```后保存,重启原神
1. Q: 原神启动时报错: 数据异常(31-4302)

View File

@@ -129,7 +129,7 @@ const md5 = str => {
return h.digest("hex")
}
let cdnUrlFormat = null
let cdnUrlFormat = undefined
String.prototype.format = function() {
const args = arguments;
@@ -137,35 +137,31 @@ String.prototype.format = function() {
}
const checkCDN = async () => {
try {
cdnUrlFormat = "https://cdn.jsdelivr.net/gh/{0}@master/{1}"
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
} catch (e) {}
try {
cdnUrlFormat = "https://raw.githubusercontent.com/{0}/master/{1}"
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
} catch (e) {}
try {
const s = conf === undefined ? "" : conf.customCDN.trim()
if (s.length > 0) {
cdnUrlFormat = s
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
}
} catch (e) {}
try {
cdnUrlFormat = "https://raw.fastgit.org/{0}/master/{1}"
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
} catch (e) {}
try {
cdnUrlFormat = "https://ghproxy.net/https://raw.githubusercontent.com/{0}/master/{1}"
await axios.head(cdnUrlFormat.format("github/fetch", ".gitignore"))
return
} catch (e) {}
throw "网络错误,请检查配置文件/网络后重试 (11-3)"
const controller = new AbortController()
const a = new Promise(resolve => axios.get("https://cdn.jsdelivr.net/gh/HolographicHat/genshin-achievement-export@master/2MB.bin", {
signal: controller.signal
}).then(_ => resolve(["jsdelivr", "https://cdn.jsdelivr.net/gh/{0}@master/{1}"])))
const b = new Promise(resolve => axios.get("https://raw.githubusercontent.com/HolographicHat/genshin-achievement-export/master/2MB.bin", {
signal: controller.signal
}).then(_ => resolve(["githubusercontent", "https://raw.githubusercontent.com/{0}/master/{1}"])))
const c = new Promise(resolve => axios.get("https://ghproxy.net/https://raw.githubusercontent.com/HolographicHat/genshin-achievement-export/master/2MB.bin", {
signal: controller.signal
}).then(_ => resolve(["ghproxy", "https://ghproxy.net/https://raw.githubusercontent.com/{0}/master/{1}"])))
const d = new Promise(resolve => {
const s = conf === undefined ? "" : conf.customCDN.trim() // 🤔
if (s.length === 0) throw "empty"
axios.get(s.format("HolographicHat/genshin-achievement-export", "2MB.bin"), {
signal: controller.signal
}).then(_ => resolve(["customCDN", s]))
})
await Promise.any([a, b, c, d]).then(([name, url]) => {
debug(`Fastest: ${name}`)
controller.abort()
cdnUrlFormat = url
}).catch()
if (cdnUrlFormat === undefined) {
throw "网络错误,请检查配置文件/网络后重试 (11-3)"
}
}
const loadCache = async (fp, repo = "Dimbreath/GenshinData") => {
@@ -203,7 +199,7 @@ const loadCache = async (fp, repo = "Dimbreath/GenshinData") => {
}
}
const isDebug = false
const isDebug = true
const debug = (msg, ...params) => {
if (isDebug) log(msg, ...params)