mirror of
https://github.com/HugoAura/Seewo-HugoAura.git
synced 2026-06-20 23:14:28 +08:00
[🛠️ Fix] Enc config detection issue & Add hosts file clean for Aikari uninst
This commit is contained in:
@@ -4,6 +4,7 @@ const __SCOPE = "main";
|
||||
|
||||
const { exec, execSync } = require("child_process");
|
||||
const fs = require("fs");
|
||||
const os = require("os");
|
||||
const path = require("path");
|
||||
const nodeHttps = require("https");
|
||||
const { fsComposables } = require("./fsIpcHandler");
|
||||
@@ -268,6 +269,46 @@ const functions = {
|
||||
fs.unlinkSync(binPath);
|
||||
}
|
||||
},
|
||||
clearHostFileItem: async () => {
|
||||
const AIKARI_HOST_STR =
|
||||
"127.11.45.14 iot-broker-mis.seewo.com # This line is generated by HugoAura-Aikari, please do not edit or delete it";
|
||||
const hostPath = path.join(
|
||||
process.env.SystemRoot || "C:\\WINDOWS",
|
||||
"System32",
|
||||
"drivers",
|
||||
"etc",
|
||||
"hosts"
|
||||
);
|
||||
if (fs.existsSync(hostPath)) {
|
||||
try {
|
||||
const content = fs.readFileSync(hostPath, "utf-8");
|
||||
const lines = content.split(/\r?\n/);
|
||||
const newContent = lines.filter((line) => {
|
||||
const shouldKeep = !line.includes(AIKARI_HOST_STR);
|
||||
if (!shouldKeep) {
|
||||
console.log(
|
||||
`[HugoAura / IPC / Aikari] Cleaned hosts file item: ${line}`
|
||||
);
|
||||
}
|
||||
return shouldKeep;
|
||||
});
|
||||
if (lines.length === newContent.length) {
|
||||
return false;
|
||||
}
|
||||
const newData = newContent.join(os.EOL);
|
||||
fs.writeFileSync(hostPath, newData, "utf-8");
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"[HugoAura / IPC / Aikari] Error cleaning hosts file: ",
|
||||
err
|
||||
);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -630,6 +671,7 @@ const applyAikariIpcHandler = (ipcMain) => {
|
||||
case "inst":
|
||||
// TODO: Impl Aikari INST
|
||||
case "uninst":
|
||||
await functions.clearHostFileItem();
|
||||
return await functions.execCommand(
|
||||
logHeader,
|
||||
AIKARI_UNINSTALLER_PATH,
|
||||
|
||||
@@ -114,9 +114,16 @@ class ConfigManager {
|
||||
);
|
||||
|
||||
if (fs.existsSync(oldConfigPath)) {
|
||||
console.log(
|
||||
`[HugoAura / Config] Old plain config file detected at ${oldConfigPath}, migrating...`
|
||||
);
|
||||
fs.copyFileSync(oldConfigPath, this.configPath);
|
||||
fs.unlinkSync(oldConfigPath);
|
||||
this.useEncConfig = false;
|
||||
} else if (fs.existsSync(oldEncConfigPath)) {
|
||||
console.log(
|
||||
`[HugoAura / Config] Old enc config file detected at ${oldEncConfigPath}, migrating...`
|
||||
);
|
||||
fs.copyFileSync(oldEncConfigPath, this.encConfigPath);
|
||||
fs.unlinkSync(oldEncConfigPath);
|
||||
this.useEncConfig = true;
|
||||
@@ -176,9 +183,14 @@ class ConfigManager {
|
||||
return this.getDefaultConfig(); // should be changed, too
|
||||
}
|
||||
} else {
|
||||
console.error("[HugoAura / Config / ERROR] Failed to decrypt config");
|
||||
console.error(
|
||||
"[HugoAura / Config / ERROR] Failed to decrypt config, falling back to use plain config"
|
||||
);
|
||||
this.useEncConfig = false;
|
||||
this.isConfigReadFailed = true;
|
||||
return this.getDefaultConfig(); // This behaviour should be changed later
|
||||
config = JSON.parse(fs.readFileSync(this.configPath, "utf8"));
|
||||
if (this.isConfigReadFailed) this.isConfigReadFailed = false;
|
||||
return config; // This behaviour should be changed later
|
||||
}
|
||||
} else {
|
||||
config = JSON.parse(fs.readFileSync(this.configPath, "utf8"));
|
||||
@@ -208,7 +220,15 @@ class ConfigManager {
|
||||
console.error(
|
||||
"[HugoAura / Config / Write / ERROR] Failed to write config: Retrieve enc password failed"
|
||||
);
|
||||
return false;
|
||||
console.warn(
|
||||
"[HugoAura / Config / Write / WARN] Falling back to use plain config."
|
||||
);
|
||||
fs.writeFileSync(
|
||||
this.configPath,
|
||||
JSON.stringify(config, null, 2),
|
||||
"utf8"
|
||||
);
|
||||
this.useEncConfig = false;
|
||||
}
|
||||
} else {
|
||||
fs.writeFileSync(
|
||||
|
||||
@@ -207,10 +207,12 @@ if (!global.__HUGO_AURA_UI_REACTIVES__.subConfig)
|
||||
global.__HUGO_AURA_UI_FUNCTIONS__.subConfig.aikariStatus.updateToast(
|
||||
"success",
|
||||
"Aikari 已完成卸载",
|
||||
null,
|
||||
`<p>
|
||||
您可能需要重启 SeewoCore 以重新建立 IoT 连接
|
||||
</p>`,
|
||||
true,
|
||||
true,
|
||||
2000
|
||||
5000
|
||||
);
|
||||
} else {
|
||||
global.__HUGO_AURA_UI_FUNCTIONS__.subConfig.aikariStatus.updateToast(
|
||||
|
||||
5
src/aura/utils/string.js
Normal file
5
src/aura/utils/string.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const checkIfNonAscii = (str) => {
|
||||
return Buffer.byteLength(str) !== str.length;
|
||||
};
|
||||
|
||||
module.exports = { checkIfNonAscii };
|
||||
@@ -48,6 +48,7 @@ const ConfigManager = require("../aura/init/shared/configManager");
|
||||
const RegistryManager = require("../aura/init/shared/registryManager");
|
||||
const { buildIpcMain } = require("../aura/init/main/ipcHandler");
|
||||
const plsUtils = require("../aura/utils/pls");
|
||||
const stringUtils = require("../aura/utils/string");
|
||||
|
||||
const { initLogger } = require("../aura/init/main/logger");
|
||||
|
||||
@@ -60,18 +61,26 @@ const getUserDocumentsDirPath = () => {
|
||||
true,
|
||||
/REG_EXPAND_SZ\s+(.+)/
|
||||
);
|
||||
if (pathInfo.success && pathInfo.data) {
|
||||
const resolvedPath = pathInfo.data.replace(
|
||||
/%([^%]+)%/g,
|
||||
(match, varName) => {
|
||||
return process.env[varName] || match;
|
||||
}
|
||||
);
|
||||
try {
|
||||
if (pathInfo.success && pathInfo.data) {
|
||||
const resolvedPath = pathInfo.data.replace(
|
||||
/%([^%]+)%/g,
|
||||
(match, varName) => {
|
||||
return process.env[varName] || match;
|
||||
}
|
||||
);
|
||||
|
||||
return resolvedPath;
|
||||
} else {
|
||||
if (stringUtils.checkIfNonAscii(resolvedPath)) {
|
||||
console.warn("[HugoAura / Init] Detected non-ASCII char in resolved user personal folder: ", resolvedPath);
|
||||
throw new Error("Non-ASCII char detected");
|
||||
}
|
||||
return resolvedPath;
|
||||
} else {
|
||||
throw new Error("Registry data failed to get");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"[HugoAura / Init / Logger] Failed to get the path of documents dir, using default val."
|
||||
"[HugoAura / Init / Logger] Failed to get the path of documents dir, using default val. | Error: ", err
|
||||
);
|
||||
return path.join(os.homedir(), "Documents");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user