This commit is contained in:
HolographicHat
2022-03-22 02:57:16 +08:00
parent 9b56cb683e
commit bcebaf979a
3 changed files with 37 additions and 40 deletions

9
app.js
View File

@@ -3,7 +3,7 @@ const proxy = require("udp-proxy")
const cp = require("child_process") const cp = require("child_process")
const rs = require("./regionServer") const rs = require("./regionServer")
const appcenter = require("./appcenter") const appcenter = require("./appcenter")
const { initConfig, splitPacket, upload, decodeProto, log, setupHost, KPacket, debug, checkCDN, checkUpdate, keypress} = require("./utils") const { initConfig, splitPacket, upload, decodeProto, log, setupHost, KPacket, debug, checkCDN, checkUpdate } = require("./utils")
const { exportData } = require("./export"); const { exportData } = require("./export");
// TODO: i18n // TODO: i18n
@@ -62,6 +62,7 @@ const { exportData } = require("./export");
hServer.close() hServer.close()
gameProcess.kill() gameProcess.kill()
clearInterval(monitor) clearInterval(monitor)
setupHost(true)
console.log("正在处理数据,请稍后...") console.log("正在处理数据,请稍后...")
let packets = Array.from(cache.values()) let packets = Array.from(cache.values())
cache.clear() cache.clear()
@@ -94,8 +95,8 @@ const { exportData } = require("./export");
const data = zlib.brotliDecompressSync(response.data) const data = zlib.brotliDecompressSync(response.data)
const proto = await decodeProto(data,"AllAchievement") const proto = await decodeProto(data,"AllAchievement")
await exportData(proto) await exportData(proto)
console.log("按任意键退出.") console.log("按任意键退出")
await keypress() cp.execSync("pause > nul", { stdio: "inherit" })
} }
process.exit(0) process.exit(0)
} }
@@ -131,7 +132,7 @@ const { exportData } = require("./export");
}) })
}) })
return server return server
}).then(() => console.log("加载完毕.")) }).then(() => console.log("加载完毕"))
} catch (e) { } catch (e) {
if (e instanceof Error) { if (e instanceof Error) {
appcenter.uploadError(e, true) appcenter.uploadError(e, true)

View File

@@ -62,7 +62,7 @@ const exportToCocogoat = async proto => {
}) })
}) })
spawnSync("clip", { input: JSON.stringify(out,null,2) }) spawnSync("clip", { input: JSON.stringify(out,null,2) })
console.log("导出内容已复制到剪贴板.") console.log("导出内容已复制到剪贴板")
} }
const exportToCsv = async proto => { const exportToCsv = async proto => {

View File

@@ -172,7 +172,7 @@ const loadCache = async (fp, repo = "Dimbreath/GenshinData") => {
validateStatus: _ => true validateStatus: _ => true
}) })
if (headResponse.status === 304) { if (headResponse.status === 304) {
debug("文件 %s 命中缓存", fp) console.log("文件 %s 命中缓存", fp)
const etagLength = fd.readUInt8() const etagLength = fd.readUInt8()
return JSON.parse(fd.subarray(1 + etagLength).toString()) return JSON.parse(fd.subarray(1 + etagLength).toString())
} else { } else {
@@ -203,14 +203,6 @@ const upload = async data => {
return await cloud.post("/achievement-export", data) return await cloud.post("/achievement-export", data)
} }
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise(resolve => process.stdin.once("data", () => {
process.stdin.setRawMode(false)
resolve()
}))
}
const checkUpdate = async () => { const checkUpdate = async () => {
const data = (await cloud.get("/latest-version")).data const data = (await cloud.get("/latest-version")).data
if (data["vc"] !== version.code) { if (data["vc"] !== version.code) {
@@ -231,34 +223,38 @@ const brotliDecompressSync = data => zlib.brotliDecompressSync(data)
let hostsContent = "" let hostsContent = ""
const setupHost = _ => { const setupHost = (restore = false) => {
const path = "C:\\Windows\\System32\\drivers\\etc\\hosts" const path = "C:\\Windows\\System32\\drivers\\etc\\hosts"
fs.chmodSync(path, 0o777) fs.chmodSync(path, 0o777)
hostsContent = fs.readFileSync(path, "utf-8") if (restore) {
const requireHosts = new Map() fs.writeFileSync(path, hostsContent)
requireHosts.set(conf.dispatchUrl, "127.0.0.1") } else {
requireHosts.set("localdispatch.yuanshen.com", "127.0.0.1") hostsContent = fs.readFileSync(path, "utf-8")
const currentHosts = new Map() const requireHosts = new Map()
hostsContent.split("\n").map(l => l.trim()).filter(l => !l.startsWith("#") && l.length > 0).forEach(value => { requireHosts.set(conf.dispatchUrl, "127.0.0.1")
const pair = value.trim().split(" ").filter(v => v.trim().length !== 0) requireHosts.set("localdispatch.yuanshen.com", "127.0.0.1")
currentHosts.set(pair[1], pair[0]) const currentHosts = new Map()
}) hostsContent.split("\n").map(l => l.trim()).filter(l => !l.startsWith("#") && l.length > 0).forEach(value => {
requireHosts.forEach((value, key) => { const pair = value.trim().split(" ").filter(v => v.trim().length !== 0)
if (currentHosts.has(key)) { currentHosts.set(pair[1], pair[0])
if (currentHosts.get(key) === value) { })
requireHosts.delete(key) requireHosts.forEach((value, key) => {
} else { if (currentHosts.has(key)) {
currentHosts.delete(key) if (currentHosts.get(key) === value) {
requireHosts.delete(key)
} else {
currentHosts.delete(key)
}
} }
} })
}) requireHosts.forEach((ip, host) => {
requireHosts.forEach((ip, host) => { currentHosts.set(host, ip)
currentHosts.set(host, ip) })
}) const newContent = Array.from(currentHosts.entries()).map(pair => {
const newContent = Array.from(currentHosts.entries()).map(pair => { return `${pair[1]} ${pair[0]}`
return `${pair[1]} ${pair[0]}` }).join("\n")
}).join("\n") fs.writeFileSync(path, newContent)
fs.writeFileSync(path, newContent) }
debug("修改SystemHosts") debug("修改SystemHosts")
process.on("exit", () => { process.on("exit", () => {
fs.writeFileSync(path, hostsContent) fs.writeFileSync(path, hostsContent)
@@ -290,5 +286,5 @@ class KPacket {
module.exports = { module.exports = {
log, sleep, encodeProto, decodeProto, initConfig, splitPacket, upload, brotliCompressSync, brotliDecompressSync, log, sleep, encodeProto, decodeProto, initConfig, splitPacket, upload, brotliCompressSync, brotliDecompressSync,
setupHost, loadCache, debug, checkCDN, checkUpdate, keypress, KPacket, cdnUrlFormat setupHost, loadCache, debug, checkCDN, checkUpdate, KPacket, cdnUrlFormat
} }