Skip to content

Commit

Permalink
feat: dynamic system drive
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 committed Mar 21, 2024
1 parent f024877 commit 445e767
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 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 anyhow::{bail, Context, Result};
use tokio::fs;

const HOSTS_PATH: &str = "C:\\Windows\\System32\\drivers\\etc\\hosts";
const HOSTS: [&str; 3] = ["mojang.com", "minecraft.net", "liquidbounce.net"];
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() -> 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 Down Expand Up @@ -41,7 +48,7 @@ pub async fn check_hosts_file() -> Result<()> {
The file is located at:\n\
{}",
flagged_entries.join("\n"),
HOSTS_PATH
hosts_path
);
}

Expand Down

0 comments on commit 445e767

Please sign in to comment.