1+ use std:: ffi:: OsString ;
12use std:: fs:: File ;
23use std:: io:: BufReader ;
34use std:: path:: { Path , PathBuf } ;
45use std:: {
56 net:: { IpAddr , SocketAddr , ToSocketAddrs } ,
6- os:: unix:: prelude:: AsRawFd ,
7+ os:: unix:: prelude:: { AsRawFd , OsStringExt } ,
78 time:: Duration ,
89} ;
910
@@ -17,6 +18,7 @@ use propolis_client::handmade::{
1718 } ,
1819 Client ,
1920} ;
21+ use regex:: bytes:: Regex ;
2022use slog:: { o, Drain , Level , Logger } ;
2123use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
2224use tokio_tungstenite:: tungstenite:: protocol:: Role ;
@@ -90,6 +92,29 @@ enum Command {
9092 /// Defaults to the most recent 16 KiB of console output (-16384).
9193 #[ clap( long, short) ]
9294 byte_offset : Option < i64 > ,
95+
96+ /// If this sequence of bytes is typed, the client will exit.
97+ /// Defaults to "^]^C" (Ctrl+], Ctrl+C). Note that the string passed
98+ /// for this argument is used verbatim without any parsing; in most
99+ /// shells, if you wish to include a special character (such as Enter
100+ /// or a Ctrl+letter combo), you can insert the character by preceding
101+ /// it with Ctrl+V at the command line.
102+ #[ clap( long, short, default_value = "\x1d \x03 " ) ]
103+ escape_string : OsString ,
104+
105+ /// The number of bytes from the beginning of the escape string to pass
106+ /// to the VM before beginning to buffer inputs until a mismatch.
107+ /// Defaults to 0, such that input matching the escape string does not
108+ /// get sent to the VM at all until a non-matching character is typed.
109+ /// To mimic the escape sequence for exiting SSH (Enter, tilde, dot),
110+ /// you may pass `-e '^M~.' --escape-prefix-length=1` such that normal
111+ /// Enter presses are sent to the VM immediately.
112+ #[ clap( long, default_value = "0" ) ]
113+ escape_prefix_length : usize ,
114+
115+ /// Disable escape string altogether (to exit, use pkill or similar).
116+ #[ clap( long, short = 'E' ) ]
117+ no_escape : bool ,
93118 } ,
94119
95120 /// Migrate instance to new propolis-server
@@ -221,60 +246,86 @@ async fn put_instance(
221246async fn stdin_to_websockets_task (
222247 mut stdinrx : tokio:: sync:: mpsc:: Receiver < Vec < u8 > > ,
223248 wstx : tokio:: sync:: mpsc:: Sender < Vec < u8 > > ,
249+ escape_vector : Option < Vec < u8 > > ,
250+ escape_prefix_length : usize ,
224251) {
225- // next_raw must live outside loop, because Ctrl-A should work across
226- // multiple inbuf reads.
227- let mut next_raw = false ;
252+ if let Some ( esc_sequence) = & escape_vector {
253+ // esc_pos must live outside loop, because escape string should work
254+ // across multiple inbuf reads.
255+ let mut esc_pos = 0 ;
228256
229- loop {
230- let inbuf = if let Some ( inbuf) = stdinrx. recv ( ) . await {
231- inbuf
232- } else {
233- continue ;
234- } ;
257+ // matches partial increments of "\x1b[14;30R"
258+ let ansi_curs_pat =
259+ Regex :: new ( "^\x1b (\\ [([0-9]{1,2}(;([0-9]{1,2}R?)?)?)?)?$" ) . unwrap ( ) ;
260+ let mut ansi_curs_check = Vec :: new ( ) ;
235261
236- // Put bytes from inbuf to outbuf, but don't send Ctrl-A unless
237- // next_raw is true.
238- let mut outbuf = Vec :: with_capacity ( inbuf. len ( ) ) ;
239-
240- let mut exit = false ;
241- for c in inbuf {
242- match c {
243- // Ctrl-A means send next one raw
244- b'\x01' => {
245- if next_raw {
246- // Ctrl-A Ctrl-A should be sent as Ctrl-A
247- outbuf. push ( c) ;
248- next_raw = false ;
262+ loop {
263+ let inbuf = if let Some ( inbuf) = stdinrx. recv ( ) . await {
264+ inbuf
265+ } else {
266+ continue ;
267+ } ;
268+
269+ // Put bytes from inbuf to outbuf, but don't send characters in the
270+ // escape string sequence unless we bail.
271+ let mut outbuf = Vec :: with_capacity ( inbuf. len ( ) ) ;
272+
273+ let mut exit = false ;
274+ for c in inbuf {
275+ // ignore ANSI escape sequence for the cursor position
276+ // response sent by xterm-alikes in response to shells
277+ // requesting one after receiving a newline.
278+ if esc_pos > 0
279+ && esc_pos <= escape_prefix_length
280+ && b"\r \n " . contains ( & esc_sequence[ esc_pos - 1 ] )
281+ {
282+ ansi_curs_check. push ( c) ;
283+ if ansi_curs_pat. is_match ( & ansi_curs_check) {
284+ if c == b'R' {
285+ // end of the sequence
286+ ansi_curs_check. clear ( ) ;
287+ }
288+ continue ;
249289 } else {
250- next_raw = true ;
290+ ansi_curs_check . clear ( ) ;
251291 }
252292 }
253- b'\x03' => {
254- if !next_raw {
255- // Exit on non-raw Ctrl-C
293+
294+ if c == esc_sequence[ esc_pos] {
295+ esc_pos += 1 ;
296+ if esc_pos == esc_sequence. len ( ) {
297+ // Exit on completed escape string
256298 exit = true ;
257299 break ;
258- } else {
259- // Otherwise send Ctrl-C
300+ } else if esc_pos <= escape_prefix_length {
301+ // let through incomplete prefix up to the given limit
260302 outbuf. push ( c) ;
261- next_raw = false ;
262303 }
263- }
264- _ => {
304+ } else {
305+ // they bailed from the sequence,
306+ // feed everything that matched so far through
307+ if esc_pos != 0 {
308+ outbuf. extend (
309+ & esc_sequence[ escape_prefix_length..esc_pos] ,
310+ )
311+ }
312+ esc_pos = 0 ;
265313 outbuf. push ( c) ;
266- next_raw = false ;
267314 }
268315 }
269- }
270316
271- // Send what we have, even if there's a Ctrl-C at the end .
272- if !outbuf. is_empty ( ) {
273- wstx. send ( outbuf) . await . unwrap ( ) ;
274- }
317+ // Send what we have, even if we're about to exit .
318+ if !outbuf. is_empty ( ) {
319+ wstx. send ( outbuf) . await . unwrap ( ) ;
320+ }
275321
276- if exit {
277- break ;
322+ if exit {
323+ break ;
324+ }
325+ }
326+ } else {
327+ while let Some ( buf) = stdinrx. recv ( ) . await {
328+ wstx. send ( buf) . await . unwrap ( ) ;
278329 }
279330 }
280331}
@@ -286,7 +337,10 @@ async fn test_stdin_to_websockets_task() {
286337 let ( stdintx, stdinrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
287338 let ( wstx, mut wsrx) = tokio:: sync:: mpsc:: channel ( 16 ) ;
288339
289- tokio:: spawn ( async move { stdin_to_websockets_task ( stdinrx, wstx) . await } ) ;
340+ let escape_vector = Some ( vec ! [ 0x1d , 0x03 ] ) ;
341+ tokio:: spawn ( async move {
342+ stdin_to_websockets_task ( stdinrx, wstx, escape_vector, 0 ) . await
343+ } ) ;
290344
291345 // send characters, receive characters
292346 stdintx
@@ -296,33 +350,22 @@ async fn test_stdin_to_websockets_task() {
296350 let actual = wsrx. recv ( ) . await . unwrap ( ) ;
297351 assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "test post please ignore" ) ;
298352
299- // don't send ctrl-a
300- stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
353+ // don't send a started escape sequence
354+ stdintx. send ( "\x1d " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
301355 assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
302356
303- // the "t" here is sent "raw" because of last ctrl-a but that doesn't change anything
357+ // since we didn't enter the \x03, the previous \x1d shows up here
304358 stdintx. send ( "test" . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
305359 let actual = wsrx. recv ( ) . await . unwrap ( ) ;
306- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "test" ) ;
307-
308- // ctrl-a ctrl-c = only ctrl-c sent
309- stdintx. send ( "\x01 \x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
310- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
311- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
360+ assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x1d test" ) ;
312361
313- // same as above, across two messages
314- stdintx. send ( "\x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
362+ // \x03 gets sent if not preceded by \x1d
315363 stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
316- assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
317364 let actual = wsrx. recv ( ) . await . unwrap ( ) ;
318365 assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x03 " ) ;
319366
320- // ctrl-a ctrl-a = only ctrl-a sent
321- stdintx. send ( "\x01 \x01 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
322- let actual = wsrx. recv ( ) . await . unwrap ( ) ;
323- assert_eq ! ( String :: from_utf8( actual) . unwrap( ) , "\x01 " ) ;
324-
325- // ctrl-c on its own means exit
367+ // \x1d followed by \x03 means exit, even if they're separate messages
368+ stdintx. send ( "\x1d " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
326369 stdintx. send ( "\x03 " . chars ( ) . map ( |c| c as u8 ) . collect ( ) ) . await . unwrap ( ) ;
327370 assert_eq ! ( wsrx. try_recv( ) , Err ( TryRecvError :: Empty ) ) ;
328371
@@ -333,6 +376,8 @@ async fn test_stdin_to_websockets_task() {
333376async fn serial (
334377 addr : SocketAddr ,
335378 byte_offset : Option < i64 > ,
379+ escape_vector : Option < Vec < u8 > > ,
380+ escape_prefix_length : usize ,
336381) -> anyhow:: Result < ( ) > {
337382 let client = propolis_client:: Client :: new ( & format ! ( "http://{}" , addr) ) ;
338383 let mut req = client. instance_serial ( ) ;
@@ -375,7 +420,23 @@ async fn serial(
375420 }
376421 } ) ;
377422
378- tokio:: spawn ( async move { stdin_to_websockets_task ( stdinrx, wstx) . await } ) ;
423+ let escape_len = escape_vector. as_ref ( ) . map ( |x| x. len ( ) ) . unwrap_or ( 0 ) ;
424+ if escape_prefix_length > escape_len {
425+ anyhow:: bail!(
426+ "prefix length {} is greater than length of escape string ({})" ,
427+ escape_prefix_length,
428+ escape_len
429+ ) ;
430+ }
431+ tokio:: spawn ( async move {
432+ stdin_to_websockets_task (
433+ stdinrx,
434+ wstx,
435+ escape_vector,
436+ escape_prefix_length,
437+ )
438+ . await
439+ } ) ;
379440
380441 loop {
381442 tokio:: select! {
@@ -569,7 +630,20 @@ async fn main() -> anyhow::Result<()> {
569630 }
570631 Command :: Get => get_instance ( & client) . await ?,
571632 Command :: State { state } => put_instance ( & client, state) . await ?,
572- Command :: Serial { byte_offset } => serial ( addr, byte_offset) . await ?,
633+ Command :: Serial {
634+ byte_offset,
635+ escape_string,
636+ escape_prefix_length,
637+ no_escape,
638+ } => {
639+ let escape_vector = if no_escape || escape_string. is_empty ( ) {
640+ None
641+ } else {
642+ Some ( escape_string. into_vec ( ) )
643+ } ;
644+ serial ( addr, byte_offset, escape_vector, escape_prefix_length)
645+ . await ?
646+ }
573647 Command :: Migrate { dst_server, dst_port, dst_uuid } => {
574648 let dst_addr = SocketAddr :: new ( dst_server, dst_port) ;
575649 let dst_client = Client :: new ( dst_addr, log. clone ( ) ) ;
0 commit comments