From f024877d6deecafbf4a19116a1e36614aa3198a9 Mon Sep 17 00:00:00 2001 From: kawaiinekololis Date: Thu, 21 Mar 2024 20:42:26 +0100 Subject: [PATCH] feat: hosts check on start-up --- src-tauri/src/app/gui.rs | 14 +++++++++++-- src-tauri/src/minecraft/prelauncher.rs | 9 --------- src-tauri/src/utils/extract.rs | 2 +- src-tauri/src/utils/hosts.rs | 12 ++++-------- src/lib/Window.svelte | 26 ++++++++++++------------- src/lib/common/social/ButtonIcon.svelte | 1 - 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src-tauri/src/app/gui.rs b/src-tauri/src/app/gui.rs index 6da613d..3c38b60 100644 --- a/src-tauri/src/app/gui.rs +++ b/src-tauri/src/app/gui.rs @@ -44,7 +44,17 @@ async fn get_launcher_version() -> Result { } #[tauri::command] -async fn check_online_status() -> Result<(), String> { +async fn check_health() -> Result<(), String> { + // Check hosts + #[cfg(windows)] + { + use crate::utils::check_hosts_file; + + info!("Checking hosts file..."); + check_hosts_file().await + .map_err(|e| format!("{}", e))?; + } + info!("Checking online status"); HTTP_CLIENT.get("https://api.liquidbounce.net/") .send().await @@ -386,7 +396,7 @@ pub fn gui_main() { runner_instance: Arc::new(Mutex::new(None)) }) .invoke_handler(tauri::generate_handler![ - check_online_status, + check_health, get_options, store_options, request_branches, diff --git a/src-tauri/src/minecraft/prelauncher.rs b/src-tauri/src/minecraft/prelauncher.rs index b3524fb..a8fceeb 100644 --- a/src-tauri/src/minecraft/prelauncher.rs +++ b/src-tauri/src/minecraft/prelauncher.rs @@ -40,15 +40,6 @@ use crate::utils::{download_file, get_maven_artifact_path}; /// Prelaunching client /// pub(crate) async fn launch(launch_manifest: LaunchManifest, launching_parameter: LaunchingParameter, additional_mods: Vec, progress: LauncherData, window: Arc>) -> Result<()> { - // Check hosts - #[cfg(windows)] - { - use crate::utils::check_hosts_file; - - info!("Checking hosts file..."); - check_hosts_file(&window).await?; - } - info!("Loading minecraft version manifest..."); let mc_version_manifest = VersionManifest::fetch().await?; diff --git a/src-tauri/src/utils/extract.rs b/src-tauri/src/utils/extract.rs index 655cb32..3a05895 100644 --- a/src-tauri/src/utils/extract.rs +++ b/src-tauri/src/utils/extract.rs @@ -18,7 +18,7 @@ */ use anyhow::{Result, Context}; -use std::{path::{Path, PathBuf}}; +use std::path::{Path, PathBuf}; use async_compression::tokio::bufread::GzipDecoder; use async_zip::read::seek::ZipFileReader; use tokio::fs::{create_dir_all, OpenOptions}; diff --git a/src-tauri/src/utils/hosts.rs b/src-tauri/src/utils/hosts.rs index 683aa1e..d45a014 100644 --- a/src-tauri/src/utils/hosts.rs +++ b/src-tauri/src/utils/hosts.rs @@ -1,14 +1,14 @@ use std::sync::{Arc, Mutex}; use anyhow::{bail, Result}; -use tauri_plugin_shell::ShellExt; const HOSTS_PATH: &str = "C:\\Windows\\System32\\drivers\\etc\\hosts"; +const HOSTS: [&str; 3] = ["mojang.com", "minecraft.net", "liquidbounce.net"]; /// We have noticed many user have modified the hosts file to block the Minecraft authentication server. /// This is likely by using a third-party program. Because LiquidLauncher requires access to the authentication server, we have to modify the hosts file to allow access. /// we need to check the hosts file and alert the user if it has been modified. -pub async fn check_hosts_file(window: &Arc>) -> Result<()> { +pub async fn check_hosts_file() -> Result<()> { // Check if the hosts file has been modified let hosts_file = tokio::fs::read_to_string(HOSTS_PATH).await?; @@ -27,16 +27,12 @@ pub async fn check_hosts_file(window: &Arc>) -> Result<()> Some(domain) => domain, None => return false, }; - - domain.contains("mojang.com") || domain.contains("minecraft.net") + + HOSTS.iter().any(|&entry| domain.contains(entry)) }) .collect::>(); if !flagged_entries.is_empty() { - // Open guide on how to remove the entries - window.lock().unwrap() - .shell().open("https://liquidbounce.net/docs/Tutorials/Fixing%20hosts%20file%20issues", None)?; - bail!( "The hosts file has been modified to block the Minecraft authentication server.\n\ \n\ diff --git a/src/lib/Window.svelte b/src/lib/Window.svelte index 1c77f7a..1a4e0c0 100644 --- a/src/lib/Window.svelte +++ b/src/lib/Window.svelte @@ -44,26 +44,26 @@ // Logout from current account function logout() { // Revoke the actual session - invoke("logout", { accountData: options.currentAccount }).catch((e) => - console.error(e), - ); + invoke("logout", { accountData: options.currentAccount }).catch(console.error); // Remove account data from options data options.currentAccount = null; options.store(); } - invoke("check_online_status") - .then((result) => { - console.debug("Status", result); - }) + // Check if the launcher is online and passes health checks + invoke("check_health") + .then(() => console.info("Health Check passed")) .catch((e) => { - alert( - "You are offline! Please connect to the internet and restart the app.\n If this problem persists, please contact the developer.\n\n (Error: " + - e + - ")", - ); - console.error(e); + let message = e; + if (message.startsWith('"')) message = message.slice(1); + if (message.endsWith('"')) message = message.slice(0, -1); + + console.error(message); + alert(message.replace(/\\n/g, "\n")); + + // Open help page + open("https://liquidbounce.net/docs/Tutorials/Fixing%20LiquidLauncher"); }); diff --git a/src/lib/common/social/ButtonIcon.svelte b/src/lib/common/social/ButtonIcon.svelte index 5eb164d..d32995d 100644 --- a/src/lib/common/social/ButtonIcon.svelte +++ b/src/lib/common/social/ButtonIcon.svelte @@ -1,5 +1,4 @@