From bcebaf979adfef21643e54fc9bc9a91bcf18a991 Mon Sep 17 00:00:00 2001 From: HolographicHat Date: Tue, 22 Mar 2022 02:57:16 +0800 Subject: [PATCH] v1.0.0 --- app.js | 9 ++++---- export.js | 2 +- utils.js | 66 ++++++++++++++++++++++++++----------------------------- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/app.js b/app.js index 63cae22..bf5db58 100644 --- a/app.js +++ b/app.js @@ -3,7 +3,7 @@ const proxy = require("udp-proxy") const cp = require("child_process") const rs = require("./regionServer") 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"); // TODO: i18n @@ -62,6 +62,7 @@ const { exportData } = require("./export"); hServer.close() gameProcess.kill() clearInterval(monitor) + setupHost(true) console.log("正在处理数据,请稍后...") let packets = Array.from(cache.values()) cache.clear() @@ -94,8 +95,8 @@ const { exportData } = require("./export"); const data = zlib.brotliDecompressSync(response.data) const proto = await decodeProto(data,"AllAchievement") await exportData(proto) - console.log("按任意键退出.") - await keypress() + console.log("按任意键退出") + cp.execSync("pause > nul", { stdio: "inherit" }) } process.exit(0) } @@ -131,7 +132,7 @@ const { exportData } = require("./export"); }) }) return server - }).then(() => console.log("加载完毕.")) + }).then(() => console.log("加载完毕")) } catch (e) { if (e instanceof Error) { appcenter.uploadError(e, true) diff --git a/export.js b/export.js index 8fc9ab0..8432928 100644 --- a/export.js +++ b/export.js @@ -62,7 +62,7 @@ const exportToCocogoat = async proto => { }) }) spawnSync("clip", { input: JSON.stringify(out,null,2) }) - console.log("导出内容已复制到剪贴板.") + console.log("导出内容已复制到剪贴板") } const exportToCsv = async proto => { diff --git a/utils.js b/utils.js index f322f36..e5cf647 100644 --- a/utils.js +++ b/utils.js @@ -172,7 +172,7 @@ const loadCache = async (fp, repo = "Dimbreath/GenshinData") => { validateStatus: _ => true }) if (headResponse.status === 304) { - debug("文件 %s 命中缓存", fp) + console.log("文件 %s 命中缓存", fp) const etagLength = fd.readUInt8() return JSON.parse(fd.subarray(1 + etagLength).toString()) } else { @@ -203,14 +203,6 @@ const upload = async 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 data = (await cloud.get("/latest-version")).data if (data["vc"] !== version.code) { @@ -231,34 +223,38 @@ const brotliDecompressSync = data => zlib.brotliDecompressSync(data) let hostsContent = "" -const setupHost = _ => { +const setupHost = (restore = false) => { const path = "C:\\Windows\\System32\\drivers\\etc\\hosts" fs.chmodSync(path, 0o777) - hostsContent = fs.readFileSync(path, "utf-8") - const requireHosts = new Map() - requireHosts.set(conf.dispatchUrl, "127.0.0.1") - requireHosts.set("localdispatch.yuanshen.com", "127.0.0.1") - const currentHosts = new Map() - hostsContent.split("\n").map(l => l.trim()).filter(l => !l.startsWith("#") && l.length > 0).forEach(value => { - const pair = value.trim().split(" ").filter(v => v.trim().length !== 0) - currentHosts.set(pair[1], pair[0]) - }) - requireHosts.forEach((value, key) => { - if (currentHosts.has(key)) { - if (currentHosts.get(key) === value) { - requireHosts.delete(key) - } else { - currentHosts.delete(key) + if (restore) { + fs.writeFileSync(path, hostsContent) + } else { + hostsContent = fs.readFileSync(path, "utf-8") + const requireHosts = new Map() + requireHosts.set(conf.dispatchUrl, "127.0.0.1") + requireHosts.set("localdispatch.yuanshen.com", "127.0.0.1") + const currentHosts = new Map() + hostsContent.split("\n").map(l => l.trim()).filter(l => !l.startsWith("#") && l.length > 0).forEach(value => { + const pair = value.trim().split(" ").filter(v => v.trim().length !== 0) + currentHosts.set(pair[1], pair[0]) + }) + requireHosts.forEach((value, key) => { + if (currentHosts.has(key)) { + if (currentHosts.get(key) === value) { + requireHosts.delete(key) + } else { + currentHosts.delete(key) + } } - } - }) - requireHosts.forEach((ip, host) => { - currentHosts.set(host, ip) - }) - const newContent = Array.from(currentHosts.entries()).map(pair => { - return `${pair[1]} ${pair[0]}` - }).join("\n") - fs.writeFileSync(path, newContent) + }) + requireHosts.forEach((ip, host) => { + currentHosts.set(host, ip) + }) + const newContent = Array.from(currentHosts.entries()).map(pair => { + return `${pair[1]} ${pair[0]}` + }).join("\n") + fs.writeFileSync(path, newContent) + } debug("修改SystemHosts") process.on("exit", () => { fs.writeFileSync(path, hostsContent) @@ -290,5 +286,5 @@ class KPacket { module.exports = { log, sleep, encodeProto, decodeProto, initConfig, splitPacket, upload, brotliCompressSync, brotliDecompressSync, - setupHost, loadCache, debug, checkCDN, checkUpdate, keypress, KPacket, cdnUrlFormat + setupHost, loadCache, debug, checkCDN, checkUpdate, KPacket, cdnUrlFormat }