1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
use std::io::{self, BufRead, Write as _};
use base64::Engine;
use interprocess::local_socket::traits::Stream as _;
use interprocess::TryClone as _;
use tracing::info;
use crate::ipc::LocalStream;
use crate::protocol::{
self, AttachScrollDirection, AttachScrollSource, ClientMessage, RenderEncoding, ServerMessage,
MAX_GRAPHICS_FRAME_SIZE,
};
use crate::server::socket_paths::client_socket_path;
use super::{do_handshake, init_logging, write_to_server, ClientError};
/// Runs a read-only terminal session observer and prints one JSON envelope per frame.
pub fn run_terminal_session_observe(target: String, cols: u16, rows: u16) -> io::Result<()> {
let mut stream =
connect_terminal_session_stream(target.clone(), cols, rows, "observing terminal session")?;
write_to_server(&mut stream, &ClientMessage::ObserveTerminal { target })?;
write_terminal_session_output(stream)
}
/// Runs a writable terminal session controller.
pub fn run_terminal_session_control(
target: String,
takeover: bool,
cols: u16,
rows: u16,
) -> io::Result<()> {
let mut stream = connect_terminal_session_stream(
target.clone(),
cols,
rows,
"controlling terminal session",
)?;
write_to_server(
&mut stream,
&ClientMessage::ControlTerminal { target, takeover },
)?;
let mut write_stream = stream.try_clone()?;
let _input_thread = std::thread::spawn(move || {
let stdin = io::stdin();
for line in stdin.lock().lines() {
let Ok(line) = line else {
break;
};
if line.trim().is_empty() {
continue;
}
match terminal_control_command_from_json(&line) {
Ok(message) => {
let release = matches!(message, ClientMessage::Detach);
if write_to_server(&mut write_stream, &message).is_err() {
return;
}
if release {
return;
}
}
Err(err) => eprintln!("herdr: terminal session control input ignored: {err}"),
}
}
let _ = write_to_server(&mut write_stream, &ClientMessage::Detach);
});
write_terminal_session_output(stream)
}
fn connect_terminal_session_stream(
target: String,
cols: u16,
rows: u16,
log_message: &'static str,
) -> io::Result<LocalStream> {
init_logging();
let socket_path = client_socket_path();
crate::logging::startup("client");
info!(path = %socket_path.display(), target = %target, cols, rows, "{log_message}");
let mut stream = match crate::ipc::connect_local_stream(&socket_path) {
Ok(stream) => stream,
Err(err) => {
eprintln!("herdr: {}", ClientError::ConnectionFailed(err));
std::process::exit(1);
}
};
match do_handshake(
&mut stream,
cols,
rows,
0,
0,
false,
None,
false,
false,
true,
true,
) {
Ok(handshake) if handshake.encoding == RenderEncoding::TerminalAnsi => {}
Ok(handshake) => {
eprintln!(
"herdr: terminal session observe negotiated unsupported encoding {:?}",
handshake.encoding
);
std::process::exit(1);
}
Err(err) => {
eprintln!("herdr: {err}");
std::process::exit(1);
}
}
stream.set_nonblocking(false)?;
Ok(stream)
}
fn write_terminal_session_output(mut stream: LocalStream) -> io::Result<()> {
let mut stdout = io::stdout().lock();
loop {
match protocol::read_message(&mut stream, MAX_GRAPHICS_FRAME_SIZE) {
Ok(ServerMessage::Terminal(frame)) => {
let encoded = base64::engine::general_purpose::STANDARD.encode(&frame.bytes);
let line = serde_json::json!({
"type": "terminal.frame",
"seq": frame.seq,
"encoding": "ansi",
"width": frame.width,
"height": frame.height,
"full": frame.full,
"bytes": encoded,
});
serde_json::to_writer(&mut stdout, &line)?;
stdout.write_all(b"\n")?;
stdout.flush()?;
}
Ok(ServerMessage::ServerShutdown { reason }) => {
let line = serde_json::json!({
"type": "terminal.closed",
"reason": reason,
});
serde_json::to_writer(&mut stdout, &line)?;
stdout.write_all(b"\n")?;
stdout.flush()?;
return Ok(());
}
Ok(ServerMessage::Graphics { .. }) => {}
Ok(_) => {}
Err(protocol::FramingError::UnexpectedEof) => return Ok(()),
Err(err) => return Err(io::Error::other(err.to_string())),
}
}
}
#[derive(serde::Deserialize)]
#[serde(tag = "type")]
enum TerminalControlCommand {
#[serde(rename = "terminal.input")]
Input {
text: Option<String>,
bytes: Option<String>,
},
#[serde(rename = "terminal.resize")]
Resize {
cols: u16,
rows: u16,
#[serde(default)]
cell_width_px: u32,
#[serde(default)]
cell_height_px: u32,
},
#[serde(rename = "terminal.scroll")]
Scroll {
direction: TerminalControlScrollDirection,
lines: u16,
#[serde(default)]
source: TerminalControlScrollSource,
#[serde(default)]
column: Option<u16>,
#[serde(default)]
row: Option<u16>,
#[serde(default)]
modifiers: u8,
},
#[serde(rename = "terminal.release")]
Release {},
}
#[derive(Clone, Copy, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
enum TerminalControlScrollDirection {
Up,
Down,
}
#[derive(Clone, Copy, Default, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
enum TerminalControlScrollSource {
#[default]
Wheel,
PageKey,
}
pub(super) fn terminal_control_command_from_json(raw: &str) -> Result<ClientMessage, String> {
let command = serde_json::from_str::<TerminalControlCommand>(raw)
.map_err(|err| format!("invalid json command: {err}"))?;
match command {
TerminalControlCommand::Input { text, bytes } => {
let data = match (text, bytes) {
(Some(_), Some(_)) => {
return Err("terminal.input accepts text or bytes, not both".into())
}
(Some(text), None) => text.into_bytes(),
(None, Some(bytes)) => base64::engine::general_purpose::STANDARD
.decode(bytes)
.map_err(|err| format!("invalid terminal.input bytes: {err}"))?,
(None, None) => Vec::new(),
};
Ok(ClientMessage::Input { data })
}
TerminalControlCommand::Resize {
cols,
rows,
cell_width_px,
cell_height_px,
} => {
if cols == 0 || rows == 0 {
return Err("terminal.resize cols and rows must be greater than 0".into());
}
Ok(ClientMessage::Resize {
cols,
rows,
cell_width_px,
cell_height_px,
pixel_mouse: false,
})
}
TerminalControlCommand::Scroll {
direction,
lines,
source,
column,
row,
modifiers,
} => {
if lines == 0 {
return Err("terminal.scroll lines must be greater than 0".into());
}
let direction = match direction {
TerminalControlScrollDirection::Up => AttachScrollDirection::Up,
TerminalControlScrollDirection::Down => AttachScrollDirection::Down,
};
let source = match source {
TerminalControlScrollSource::Wheel => AttachScrollSource::Wheel,
TerminalControlScrollSource::PageKey => AttachScrollSource::PageKey {
input: match direction {
AttachScrollDirection::Up => b"\x1b[5~".to_vec(),
AttachScrollDirection::Down => b"\x1b[6~".to_vec(),
},
},
};
Ok(ClientMessage::AttachScroll {
source,
direction,
lines,
column,
row,
modifiers,
})
}
TerminalControlCommand::Release {} => Ok(ClientMessage::Detach),
}
}
|