From 432593083ee8783f60b2517842710b4162493910 Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:36:45 +0800 Subject: [PATCH] viewer: terminal on the official Windows host Live against an official Windows host (PowerShell via ConPTY): - Its rpc_main.proto has no check_env (requests end at 25), so a missing answer within 3 s no longer aborts; a negative answer still does. - OPEN carries "option":"none"; the host rejects an OPEN without it ("terminal open: missing 'option' field", kError 1004). This project's host ignores the key. - The host answers OPEN with {code,message}, header session 0, and keeps one terminal per connection as session 0; a nonzero header still overrides the body for this project's host. - Echo requests whose SimpleAction omits the default action 0 are answered too. Create/reattach on this project's host unchanged. Co-Authored-By: Claude Opus 5.5 --- src/viewer/tools/terminal.rs | 24 ++++++++++++++++++------ src/viewer/wire.rs | 3 ++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/viewer/tools/terminal.rs b/src/viewer/tools/terminal.rs index 1b6fb5d..5d838a3 100644 --- a/src/viewer/tools/terminal.rs +++ b/src/viewer/tools/terminal.rs @@ -57,7 +57,9 @@ impl Terminal { if let Some(event)=event {if tx.try_send(event).is_err(){overflow=true;break}} let key=if f.kind==OPEN_RESP {(OPEN_RESP,0)}else{(f.kind,f.session as u64)}; let payload=if f.kind==OPEN_RESP { - serde_json::from_slice::(&f.payload).ok().filter(Value::is_object).map(|mut v|{v["session_id"]=json!(f.session);v.to_string().into_bytes()}).unwrap_or(f.payload) + // Our host puts the new session in the frame header; the official Windows host answers with header 0, + // so only a nonzero header overrides the JSON body. + serde_json::from_slice::(&f.payload).ok().filter(Value::is_object).map(|mut v|{if f.session!=0{v["session_id"]=json!(f.session);}v.to_string().into_bytes()}).unwrap_or(f.payload) }else{f.payload}; if let Some(t)=p.lock().unwrap().remove(&key){let _=t.send(Ok(payload));} } @@ -79,8 +81,12 @@ impl Terminal { } pub async fn check_env(&self)->Result<(),String>{ let id=self.rpc.fetch_add(1,Ordering::Relaxed);let mut r=nested(1,&ints(&[(1,id)]));wire::bytes(28,&[],&mut r); - let b=self.request((24,id),CONTROL,wire::envelope(id,21,&r)).await?; - if uint(&b,1)!=0 {return Err("Terminal environment unavailable".into())}Ok(()) + // The official Windows host's rpc_main.proto has no check_env (requests end at 25): no answer means + // "not implemented", not "unavailable"; go on and let OPEN report availability. + match tokio::time::timeout(std::time::Duration::from_secs(3),self.request((24,id),CONTROL,wire::envelope(id,21,&r))).await{ + Err(_)=>Ok(()), + Ok(b)=>{let b=b?;if uint(&b,1)!=0 {return Err("Terminal environment unavailable".into())}Ok(())} + } } pub async fn list_sessions(&self)->Result,String>{ let id=self.next.fetch_add(1,Ordering::Relaxed); @@ -94,10 +100,16 @@ impl Terminal { self.open_session(id,cols,rows).await } async fn open_session(&self,id:u32,cols:u16,rows:u16)->Result{ - // Host TerminalConn handles OPEN(nonzero) as reattachment. - let b=self.request((OPEN_RESP,0),BINARY,encode(OPEN,id,json!({"cols":cols.max(1),"rows":rows.max(1)}).to_string().as_bytes())).await?; + // Host TerminalConn handles OPEN(nonzero) as reattachment. The official Windows host rejects an OPEN + // without "option" ("terminal open: missing 'option' field", kError 1004); "none" is its plain mode. + let b=self.request((OPEN_RESP,0),BINARY,encode(OPEN,id,json!({"cols":cols.max(1),"rows":rows.max(1),"option":"none"}).to_string().as_bytes())).await?; let v:Value=serde_json::from_slice(&b).map_err(|_|"Invalid terminal open response")?; - if v["code"].as_i64()!=Some(0){return Err("Terminal open rejected".into())}v["session_id"].as_u64().and_then(|n|u32::try_from(n).ok()).filter(|n|*n!=0).ok_or("Invalid terminal session id".into()) + if v["code"].as_i64()!=Some(0){return Err("Terminal open rejected".into())} + let id=v["session_id"].as_u64().or_else(||v["session_id"].as_str().and_then(|s|s.parse().ok())).and_then(|n|u32::try_from(n).ok()).filter(|n|*n!=0); + // The official Windows host answers {"code":0,"message"} with header 0 and keeps one terminal per + // connection addressed as session 0 (it then prints "[terminal] resumed existing session"). + if id.is_none(){log::info!("Terminal open response without a session id: keys={:?}; using session 0",v.as_object().map(|o|o.keys().collect::>()));} + Ok(id.unwrap_or(0)) } pub async fn write(&self,id:u32,data:&[u8])->Result<(),String>{for b in data.chunks(16*1024){self.io.send(BINARY,encode(STDIN,id,b)).await?}Ok(())} pub async fn resize(&self,id:u32,cols:u16,rows:u16)->Result<(),String>{self.io.send(BINARY,encode(RESIZE,id,json!({"cols":cols.max(1),"rows":rows.max(1)}).to_string().as_bytes())).await} diff --git a/src/viewer/wire.rs b/src/viewer/wire.rs index 2ae3e40..f774711 100644 --- a/src/viewer/wire.rs +++ b/src/viewer/wire.rs @@ -143,7 +143,8 @@ pub fn echo_request_seq(data:&[u8]) -> Option { let fields=decode_wire(data).ok()?; let seq=fields.iter().find_map(|f|match (f.number,&f.value){(1,WireValue::Varint(v))=>Some(*v),_=>None})?; fields.iter().find_map(|f|match (f.number,&f.value) { - (3,WireValue::Bytes(action))=>decode_wire(action).ok()?.iter().any(|f|f.number==1 && f.value==WireValue::Varint(0)).then_some(seq), + // proto3 omits the default action 0, so a SimpleAction without field 1 is an echo request too. + (3,WireValue::Bytes(action))=>decode_wire(action).ok()?.iter().find_map(|f|match (f.number,&f.value){(1,WireValue::Varint(v))=>Some(*v),_=>None}).unwrap_or(0).eq(&0).then_some(seq), _=>None, }) }