diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 19369ceb..ba38dbbf 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -80,24 +80,7 @@ pub fn get_samp_favorite_list() -> String { #[tauri::command] pub fn resolve_hostname(hostname: String) -> std::result::Result { - use std::net::{IpAddr, ToSocketAddrs}; - - if hostname.is_empty() { - return Err("Hostname cannot be empty".to_string()); - } - - let addr = format!("{}:80", hostname); - let addrs = addr - .to_socket_addrs() - .map_err(|e| format!("Failed to resolve hostname '{}': {}", hostname, e))?; - - for ip in addrs { - if let IpAddr::V4(ipv4) = ip.ip() { - return Ok(ipv4.to_string()); - } - } - - Err(format!("No IPv4 address found for hostname '{}'", hostname)) + helpers::resolve_hostname_to_ipv4(&hostname) } #[tauri::command] diff --git a/src-tauri/src/helpers.rs b/src-tauri/src/helpers.rs index 10a1d906..4aadfd6f 100644 --- a/src-tauri/src/helpers.rs +++ b/src-tauri/src/helpers.rs @@ -81,6 +81,28 @@ pub fn decode_buffer(buf: Vec) -> (String, String) { (buff_output, actual_encoding.name().to_string()) } +// resolves a hostname to its first ipv4 address +pub fn resolve_hostname_to_ipv4(hostname: &str) -> std::result::Result { + use std::net::{IpAddr, ToSocketAddrs}; + + if hostname.is_empty() { + return Err("Hostname cannot be empty".to_string()); + } + + let addr = format!("{}:80", hostname); + let addrs = addr + .to_socket_addrs() + .map_err(|e| format!("Failed to resolve hostname '{}': {}", hostname, e))?; + + for ip in addrs { + if let IpAddr::V4(ipv4) = ip.ip() { + return Ok(ipv4.to_string()); + } + } + + Err(format!("No IPv4 address found for hostname '{}'", hostname)) +} + pub fn copy_files(src: impl AsRef, dest: impl AsRef) -> crate::errors::Result<()> { let files = fs::read_dir(src).map_err(|e| crate::errors::LauncherError::from(e))?; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a0c29f60..def58481 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -130,9 +130,17 @@ async fn handle_cli_args() -> Result<()> { &omp_client_path }; + // resolve hostname to ipv4 so the game does not truncate hyphenated hosts + let host = args.host.as_ref().unwrap(); + let resolved_host = + helpers::resolve_hostname_to_ipv4(host).unwrap_or_else(|e| { + info!("Failed to resolve hostname '{}', using raw value: {}", host, e); + host.clone() + }); + run_samp( args.name.as_ref().unwrap(), - args.host.as_ref().unwrap(), + &resolved_host, args.port.unwrap(), gamepath, &format!("{}/{}", gamepath, SAMP_DLL),