@@ -23,6 +23,7 @@ fn main() {}
2323
2424#[ cfg( target_os = "windows" ) ]
2525mod imp {
26+ use std:: net:: SocketAddr ;
2627 use std:: path:: { Path , PathBuf } ;
2728
2829 use anyhow:: { Context , Result , anyhow, bail} ;
@@ -84,12 +85,17 @@ mod imp {
8485
8586 /// Run the lossless split instead of the coarse map.
8687 ///
87- /// Requires `--proxy-port `. Prints the MXC config (with proxy redirect
88- /// and empty `allowedHosts`) and the trimmed proxy policy side-by-side .
88+ /// Requires `--proxy-addr `. Prints and writes the MXC config (with proxy
89+ /// redirect and empty `allowedHosts`) plus the trimmed proxy policy.
8990 #[ arg( long) ]
9091 split : bool ,
9192
92- /// Localhost port the `OpenShell` CONNECT proxy listens on (required with `--split`).
93+ /// Loopback address the `OpenShell` CONNECT proxy listens on. Accepts
94+ /// `IP:PORT`, or a bare port shorthand expanded to `127.0.0.1:PORT`.
95+ #[ arg( long) ]
96+ proxy_addr : Option < String > ,
97+
98+ /// Deprecated shorthand for `--proxy-addr 127.0.0.1:PORT`.
9399 #[ arg( long) ]
94100 proxy_port : Option < u16 > ,
95101 }
@@ -98,9 +104,7 @@ mod imp {
98104 let args = Args :: parse ( ) ;
99105
100106 if args. split {
101- let port = args
102- . proxy_port
103- . ok_or_else ( || anyhow ! ( "--proxy-port is required with --split" ) ) ?;
107+ let proxy_addr = parse_proxy_addr ( args. proxy_addr . as_deref ( ) , args. proxy_port ) ?;
104108 let policy_path = args
105109 . policy
106110 . as_ref ( )
@@ -112,8 +116,8 @@ mod imp {
112116 . into_owned ( ) ;
113117 let slug = args. container_id . clone ( ) . unwrap_or ( stem) ;
114118 let mut opts = build_options ( & args, & slug) ;
115- opts. proxy_localhost_port = Some ( port ) ;
116- return show_split ( policy_path, & opts) ;
119+ opts. proxy_redirect = Some ( proxy_addr ) ;
120+ return show_split ( policy_path, & args . out_dir , & opts) ;
117121 }
118122
119123 if let Some ( examples_root) = & args. examples_root {
@@ -173,10 +177,27 @@ mod imp {
173177 env : args. env . clone ( ) ,
174178 timeout_ms : args. timeout_ms ,
175179 allow_wildcards : args. allow_wildcards ,
176- proxy_localhost_port : None ,
180+ proxy_redirect : None ,
177181 }
178182 }
179183
184+ fn parse_proxy_addr ( addr : Option < & str > , port : Option < u16 > ) -> Result < SocketAddr > {
185+ let Some ( raw) = addr else {
186+ let port = port. ok_or_else ( || anyhow ! ( "--proxy-addr is required with --split" ) ) ?;
187+ return format ! ( "127.0.0.1:{port}" )
188+ . parse ( )
189+ . context ( "parsing deprecated --proxy-port shorthand" ) ;
190+ } ;
191+ let expanded = if raw. chars ( ) . all ( |c| c. is_ascii_digit ( ) ) {
192+ format ! ( "127.0.0.1:{raw}" )
193+ } else {
194+ raw. to_owned ( )
195+ } ;
196+ expanded
197+ . parse ( )
198+ . with_context ( || format ! ( "parsing --proxy-addr {raw:?}" ) )
199+ }
200+
180201 fn convert_policy (
181202 policy_path : & Path ,
182203 out_dir : & Path ,
@@ -245,14 +266,15 @@ mod imp {
245266 // Lossless-split display
246267 // -----------------------------------------------------------------------
247268
248- fn show_split ( policy_path : & Path , opts : & MxcMappingOptions ) -> Result < ( ) > {
269+ fn show_split ( policy_path : & Path , out_dir : & Path , opts : & MxcMappingOptions ) -> Result < ( ) > {
249270 let content = std:: fs:: read_to_string ( policy_path)
250271 . with_context ( || format ! ( "reading {}" , policy_path. display( ) ) ) ?;
251272 let policy = openshell_policy:: parse_sandbox_policy ( & content)
252273 . map_err ( |e| anyhow ! ( "parsing {}: {e}" , policy_path. display( ) ) ) ?;
253274
254275 let result = split_policy ( & policy, opts)
255- . ok_or_else ( || anyhow ! ( "split_policy returned None — is --proxy-port set?" ) ) ?;
276+ . ok_or_else ( || anyhow ! ( "split_policy returned None; is --proxy-addr set?" ) ) ?;
277+ write_split_outputs ( policy_path, out_dir, opts, & result) ?;
256278
257279 println ! ( "=== MXC ContainerConfig (filesystem + proxy redirect) ===" ) ;
258280 println ! ( "{}" , serde_json:: to_string_pretty( & result. mxc_config) ?) ;
@@ -270,15 +292,35 @@ mod imp {
270292 let ports = if ep. ports . is_empty ( ) {
271293 String :: new ( )
272294 } else {
273- format ! ( ":{}" , ep. ports. iter( ) . map( |p| p. to_string( ) ) . collect:: <Vec <_>>( ) . join( "," ) )
295+ format ! (
296+ ":{}" ,
297+ ep. ports
298+ . iter( )
299+ . map( |p| p. to_string( ) )
300+ . collect:: <Vec <_>>( )
301+ . join( "," )
302+ )
274303 } ;
275304 println ! (
276305 " endpoint: {}{} protocol={} tls={} access={} enforcement={}" ,
277- ep. host, ports,
278- if ep. protocol. is_empty( ) { "-" } else { & ep. protocol } ,
306+ ep. host,
307+ ports,
308+ if ep. protocol. is_empty( ) {
309+ "-"
310+ } else {
311+ & ep. protocol
312+ } ,
279313 if ep. tls. is_empty( ) { "-" } else { & ep. tls } ,
280- if ep. access. is_empty( ) { "-" } else { & ep. access } ,
281- if ep. enforcement. is_empty( ) { "-" } else { & ep. enforcement } ,
314+ if ep. access. is_empty( ) {
315+ "-"
316+ } else {
317+ & ep. access
318+ } ,
319+ if ep. enforcement. is_empty( ) {
320+ "-"
321+ } else {
322+ & ep. enforcement
323+ } ,
282324 ) ;
283325 if !ep. rules . is_empty ( ) {
284326 println ! ( " allow rules: {}" , ep. rules. len( ) ) ;
@@ -296,7 +338,10 @@ mod imp {
296338
297339 if !result. loss . is_empty ( ) {
298340 println ! ( ) ;
299- println ! ( "=== Filesystem loss items ({} item(s)) ===" , result. loss. len( ) ) ;
341+ println ! (
342+ "=== Filesystem loss items ({} item(s)) ===" ,
343+ result. loss. len( )
344+ ) ;
300345 for item in & result. loss {
301346 println ! ( " [{}] {}: {}" , item. severity, item. path, item. message) ;
302347 }
@@ -305,6 +350,27 @@ mod imp {
305350 Ok ( ( ) )
306351 }
307352
353+ fn write_split_outputs (
354+ policy_path : & Path ,
355+ out_dir : & Path ,
356+ options : & MxcMappingOptions ,
357+ result : & openshell_driver_mxc:: SplitPolicyResult ,
358+ ) -> Result < ( ) > {
359+ write_outputs (
360+ policy_path,
361+ out_dir,
362+ options,
363+ & result. mxc_config ,
364+ & result. loss ,
365+ ) ?;
366+ let trimmed_path = out_dir. join ( "trimmed-policy.yaml" ) ;
367+ let trimmed = openshell_policy:: serialize_sandbox_policy ( & result. proxy_policy )
368+ . map_err ( |e| anyhow ! ( "serializing trimmed proxy policy: {e}" ) ) ?;
369+ std:: fs:: write ( & trimmed_path, trimmed)
370+ . with_context ( || format ! ( "writing {}" , trimmed_path. display( ) ) ) ?;
371+ Ok ( ( ) )
372+ }
373+
308374 // -----------------------------------------------------------------------
309375 // File discovery & slug helpers
310376 // -----------------------------------------------------------------------
0 commit comments