@@ -904,17 +904,31 @@ async fn get_auth_for_connection(
904904 } )
905905 }
906906 }
907- AuthMode :: Byond => {
908- let config = crate :: config:: get_config ( ) ;
909- if !config. features . auto_launch_byond && !check_byond_pager_running ( ) {
910- return Err ( AuthError {
911- code : "byond_auth_required" . to_string ( ) ,
912- message : "Please log in to BYOND before connecting." . to_string ( ) ,
913- linking_url : None ,
914- } ) ;
915- }
916- Ok ( AccessMethod :: Byond )
917- }
907+ AuthMode :: Byond => Ok ( AccessMethod :: Byond ) ,
908+ }
909+ }
910+
911+ #[ cfg( any( target_os = "windows" , target_os = "linux" ) ) ]
912+ async fn ensure_byond_web_session (
913+ app : AppHandle ,
914+ ) -> CommandResult < crate :: byond_login:: ByondSessionCheck > {
915+ let mut session_check = check_byond_web_session ( app. clone ( ) ) . await . ok ( ) ;
916+
917+ if session_check
918+ . as_ref ( )
919+ . map ( |session| session. logged_in )
920+ . unwrap_or ( false )
921+ {
922+ return session_check. ok_or ( CommandError :: NotAuthenticated ) ;
923+ }
924+
925+ tracing:: info!( "Not logged in to BYOND web auth, opening login flow" ) ;
926+ start_byond_login ( app. clone ( ) ) . await ?;
927+ session_check = check_byond_web_session ( app. clone ( ) ) . await . ok ( ) ;
928+
929+ match session_check {
930+ Some ( session) if session. logged_in => Ok ( session) ,
931+ _ => Err ( CommandError :: NotAuthenticated ) ,
918932 }
919933}
920934
@@ -1074,33 +1088,30 @@ async fn topic_preflight(ip: &str, port: u16, challenge: &str) -> PreflightOutco
10741088 }
10751089 } ;
10761090
1077- match result {
1078- http2byond:: ByondTopicValue :: String ( s) => {
1079- let s = s. trim_end_matches ( '\0' ) ;
1080- let v: serde_json:: Value = match serde_json:: from_str ( s) {
1081- Ok ( v) => v,
1082- Err ( e) => {
1083- tracing:: warn!( "[topic_preflight] invalid JSON from {ip}:{port}: {e}" ) ;
1084- return PreflightOutcome :: NoHubAuth ;
1085- }
1086- } ;
1087- let Some ( server_id) = v[ "server_id" ] . as_str ( ) . map ( String :: from) else {
1088- tracing:: debug!( "[topic_preflight] {ip}:{port} no server_id in response" ) ;
1091+ if let http2byond:: ByondTopicValue :: String ( s) = result {
1092+ let s = s. trim_end_matches ( '\0' ) ;
1093+ let v: serde_json:: Value = match serde_json:: from_str ( s) {
1094+ Ok ( v) => v,
1095+ Err ( e) => {
1096+ tracing:: warn!( "[topic_preflight] invalid JSON from {ip}:{port}: {e}" ) ;
10891097 return PreflightOutcome :: NoHubAuth ;
1090- } ;
1091- let domain = v[ "domain" ] . as_str ( ) . map ( String :: from) ;
1092- let signature = v[ "signature" ] . as_str ( ) . map ( String :: from) ;
1093- tracing:: info!( "[topic_preflight] {ip}:{port} server_id={server_id} domain={domain:?}" ) ;
1094- PreflightOutcome :: Ok ( PreflightResult {
1095- server_id,
1096- domain,
1097- signature,
1098- } )
1099- }
1100- _ => {
1101- tracing:: debug!( "[topic_preflight] {ip}:{port} returned non-string response" ) ;
1102- PreflightOutcome :: NoHubAuth
1103- }
1098+ }
1099+ } ;
1100+ let Some ( server_id) = v[ "server_id" ] . as_str ( ) . map ( String :: from) else {
1101+ tracing:: debug!( "[topic_preflight] {ip}:{port} no server_id in response" ) ;
1102+ return PreflightOutcome :: NoHubAuth ;
1103+ } ;
1104+ let domain = v[ "domain" ] . as_str ( ) . map ( String :: from) ;
1105+ let signature = v[ "signature" ] . as_str ( ) . map ( String :: from) ;
1106+ tracing:: info!( "[topic_preflight] {ip}:{port} server_id={server_id} domain={domain:?}" ) ;
1107+ PreflightOutcome :: Ok ( PreflightResult {
1108+ server_id,
1109+ domain,
1110+ signature,
1111+ } )
1112+ } else {
1113+ tracing:: debug!( "[topic_preflight] {ip}:{port} returned non-string response" ) ;
1114+ PreflightOutcome :: NoHubAuth
11041115 }
11051116}
11061117
@@ -1374,7 +1385,7 @@ pub async fn connect_to_address(
13741385 } ;
13751386 ( method, Some ( server_id) )
13761387 } else {
1377- ( AccessMethod :: None , None )
1388+ ( AccessMethod :: Byond , None )
13781389 } ;
13791390
13801391 let version = select_byond_version ( None , & app) ?;
@@ -1461,48 +1472,17 @@ async fn connect_impl(app: AppHandle, req: ConnectionRequest) -> CommandResult<C
14611472 let webview2_data_dir = get_byond_base_dir ( & app) ?. join ( "webview2_data" ) ;
14621473
14631474 let is_byond_auth = access_method. is_byond ( ) ;
1464- let pager_running = check_byond_pager_running ( ) ;
1465-
1466- let mut session_check = if is_byond_auth {
1467- check_byond_web_session ( app. clone ( ) ) . await . ok ( )
1475+ let session_check = if is_byond_auth {
1476+ Some ( ensure_byond_web_session ( app. clone ( ) ) . await ?)
14681477 } else {
14691478 None
14701479 } ;
14711480
1472- let using_webid = match & session_check {
1473- Some ( session) if session. logged_in => {
1474- tracing:: info!( "User logged in via web (web_id present), using web authentication" ) ;
1475- true
1476- }
1477- _ if !pager_running && is_byond_auth => {
1478- tracing:: info!( "Not logged in to BYOND and pager not running, opening login flow" ) ;
1479- let login_result = start_byond_login ( app. clone ( ) ) . await ;
1480- if login_result. is_err ( ) {
1481- return Err ( CommandError :: Cancelled {
1482- operation : "byond_login" . into ( ) ,
1483- } ) ;
1484- }
1485- session_check = check_byond_web_session ( app. clone ( ) ) . await . ok ( ) ;
1486- true
1487- }
1488- _ => {
1489- if is_byond_auth {
1490- tracing:: info!( "Using BYOND pager for authentication" ) ;
1491- }
1492- false
1493- }
1494- } ;
1495-
14961481 if source. as_deref ( ) != Some ( "control_server_restart" ) {
14971482 app. emit ( "game-connecting" , & server_name) . ok ( ) ;
14981483 }
14991484
1500- if using_webid {
1501- let session = if session_check. as_ref ( ) . map ( |s| s. logged_in ) . unwrap_or ( false ) {
1502- session_check. unwrap ( )
1503- } else {
1504- check_byond_web_session ( app. clone ( ) ) . await ?
1505- } ;
1485+ if let Some ( session) = session_check {
15061486 let web_id = session. web_id . ok_or ( CommandError :: NotAuthenticated ) ?;
15071487 if !session. logged_in {
15081488 return Err ( CommandError :: NotAuthenticated ) ;
@@ -1557,17 +1537,13 @@ async fn connect_impl(app: AppHandle, req: ConnectionRequest) -> CommandResult<C
15571537 if let Some ( path) = crate :: webview2:: get_fixed_runtime_path ( ) {
15581538 let wine_path = wine:: unix_to_wine_path ( & path) ;
15591539 tracing:: info!( "Fixed WebView2 runtime: {:?} -> {}" , path, wine_path) ;
1560- env_vars. push ( (
1561- "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER" ,
1562- wine_path,
1563- ) ) ;
1540+ env_vars. push ( ( "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER" , wine_path) ) ;
15641541 }
15651542 let env_refs: Vec < ( & str , & str ) > =
15661543 env_vars. iter ( ) . map ( |( k, v) | ( * k, v. as_str ( ) ) ) . collect ( ) ;
1567- wine:: launch_with_wine ( & app, & exe_path, & [ & connect_url] , & env_refs)
1568- . map_err ( |e| {
1569- CommandError :: Io ( format ! ( "Failed to launch BYOND via Wine: {e}" ) )
1570- } ) ?
1544+ wine:: launch_with_wine ( & app, & exe_path, & [ & connect_url] , & env_refs) . map_err (
1545+ |e| CommandError :: Io ( format ! ( "Failed to launch BYOND via Wine: {e}" ) ) ,
1546+ ) ?
15711547 } ;
15721548
15731549 existing_pids. insert ( pager_child. id ( ) ) ;
@@ -1636,10 +1612,7 @@ async fn connect_impl(app: AppHandle, req: ConnectionRequest) -> CommandResult<C
16361612 if let Some ( path) = crate :: webview2:: get_fixed_runtime_path ( ) {
16371613 let wine_path = wine:: unix_to_wine_path ( & path) ;
16381614 tracing:: info!( "Fixed WebView2 runtime: {:?} -> {}" , path, wine_path) ;
1639- env_vars. push ( (
1640- "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER" ,
1641- wine_path,
1642- ) ) ;
1615+ env_vars. push ( ( "WEBVIEW2_BROWSER_EXECUTABLE_FOLDER" , wine_path) ) ;
16431616 }
16441617 let env_refs: Vec < ( & str , & str ) > =
16451618 env_vars. iter ( ) . map ( |( k, v) | ( * k, v. as_str ( ) ) ) . collect ( ) ;
0 commit comments