Skip to content

Commit

Permalink
feat: hosts check on start-up
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Mar 21, 2024
1 parent 3283534 commit 62f64f2
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
14 changes: 12 additions & 2 deletions src-tauri/src/app/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ async fn get_launcher_version() -> Result<String, String> {
}

#[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
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 0 additions & 9 deletions src-tauri/src/minecraft/prelauncher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ use crate::utils::{download_file, get_maven_artifact_path};
/// Prelaunching client
///
pub(crate) async fn launch<D: Send + Sync>(launch_manifest: LaunchManifest, launching_parameter: LaunchingParameter, additional_mods: Vec<LoaderMod>, progress: LauncherData<D>, window: Arc<Mutex<tauri::Window>>) -> 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?;

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/utils/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
29 changes: 16 additions & 13 deletions src-tauri/src/utils/hosts.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
use std::sync::{Arc, Mutex};
use std::env;

use anyhow::{bail, Result};
use tauri_plugin_shell::ShellExt;
use anyhow::{bail, Context, Result};
use tokio::fs;

const HOSTS_PATH: &str = "C:\\Windows\\System32\\drivers\\etc\\hosts";
const HOSTS_PATH: &str = "Windows\\System32\\drivers\\etc\\hosts";
const HOSTS: [&str; 4] = ["mojang.com", "minecraft.net", "liquidbounce.net", "ccbluex.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<Mutex<tauri::Window>>) -> Result<()> {
pub async fn check_hosts_file() -> Result<()> {
// Get location of Windows hosts file dynamically
// "SystemDrive" env, if not assigned default to C:
let system_drive = env::var("SystemDrive").unwrap_or("C:".to_string());
let hosts_path = format!("{}\\{}", system_drive, HOSTS_PATH);

// Check if the hosts file has been modified
let hosts_file = tokio::fs::read_to_string(HOSTS_PATH).await?;
let hosts_file = fs::read_to_string(&hosts_path).await
.context(format!("Failed to read hosts file at {}", hosts_path))?;

let flagged_entries = hosts_file.lines()
.filter(|line| {
Expand All @@ -27,16 +34,12 @@ pub async fn check_hosts_file(window: &Arc<Mutex<tauri::Window>>) -> Result<()>
Some(domain) => domain,
None => return false,
};
domain.contains("mojang.com") || domain.contains("minecraft.net")

HOSTS.iter().any(|&entry| domain.contains(entry))
})
.collect::<Vec<_>>();

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\
Expand All @@ -45,7 +48,7 @@ pub async fn check_hosts_file(window: &Arc<Mutex<tauri::Window>>) -> Result<()>
The file is located at:\n\
{}",
flagged_entries.join("\n"),
HOSTS_PATH
hosts_path
);
}

Expand Down
26 changes: 13 additions & 13 deletions src/lib/Window.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
</script>

Expand Down
1 change: 0 additions & 1 deletion src/lib/common/social/ButtonIcon.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
import { invoke } from "@tauri-apps/api/core";
import ToolTip from "../ToolTip.svelte";
import { open } from "@tauri-apps/plugin-shell";
Expand Down

0 comments on commit 62f64f2

Please sign in to comment.