@@ -282,6 +282,21 @@ pub fn blocking_client_builder() -> Result<reqwest::blocking::ClientBuilder, Str
282282 blocking_client_builder_for_mode ( & current_snapshot ( ) . mode )
283283}
284284
285+ /// Resolved proxy URL for consumers that configure their own HTTP client rather
286+ /// than using `client_builder()`/`cached_client()` (e.g. `tauri-plugin-updater`,
287+ /// which only accepts a `Url` on its builder). `Ok(None)` means the app proxy is
288+ /// disabled — callers must still explicitly disable their own client's proxy in
289+ /// that case instead of leaving it to fall back to OS proxy env vars.
290+ pub fn current_proxy_url ( ) -> Result < Option < reqwest:: Url > , String > {
291+ match current_snapshot ( ) . mode {
292+ ProxyMode :: Disabled => Ok ( None ) ,
293+ ProxyMode :: Invalid ( error) => Err ( error) ,
294+ ProxyMode :: Enabled ( config) => reqwest:: Url :: parse ( & config. proxy_url ( ) )
295+ . map ( Some )
296+ . map_err ( |_| format ! ( "应用代理地址无效:{}" , config. display_target( ) ) ) ,
297+ }
298+ }
299+
285300#[ cfg( test) ]
286301mod tests {
287302 use super :: * ;
@@ -403,4 +418,28 @@ mod tests {
403418 . expect( "disabled proxy envs" )
404419 . is_empty( ) ) ;
405420 }
421+
422+ #[ test]
423+ fn current_proxy_url_reflects_mode ( ) {
424+ set_config ( None ) ;
425+ assert_eq ! ( current_proxy_url( ) . expect( "disabled proxy url" ) , None ) ;
426+
427+ set_config ( Some ( & json ! ( {
428+ "enabled" : true , "type" : "http" , "host" : "proxy.local" , "port" : 8080
429+ } ) ) ) ;
430+ assert_eq ! (
431+ current_proxy_url( )
432+ . expect( "enabled proxy url" )
433+ . map( |url| url. to_string( ) ) ,
434+ Some ( "http://proxy.local:8080/" . to_string( ) )
435+ ) ;
436+
437+ set_config ( Some ( & json ! ( {
438+ "enabled" : true , "type" : "http" , "host" : "bad host/@" , "port" : 8080
439+ } ) ) ) ;
440+ assert ! ( current_proxy_url( ) . is_err( ) ) ;
441+
442+ // reset so other tests in this module observe the default disabled state
443+ set_config ( None ) ;
444+ }
406445}
0 commit comments