railwayapp 4.5.3

Interact with Railway via CLI
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
use anyhow::{bail, Result};
use async_tungstenite::tungstenite::Message;
use async_tungstenite::WebSocketStream;
use futures_util::stream::StreamExt;
use indicatif::ProgressBar;
use std::io::Write;
use tokio::sync::mpsc;
use tokio::time::{interval, timeout, Duration};

use crate::commands::ssh::{SSH_MAX_EMPTY_MESSAGES, SSH_MESSAGE_TIMEOUT_SECS};

use super::connection::{establish_connection, SSHConnectParams};
use super::messages::{ClientMessage, ClientPayload, DataPayload, ServerMessage};
use super::SSH_PING_INTERVAL_SECS;

pub struct TerminalClient {
    ws_stream: WebSocketStream<async_tungstenite::tokio::ConnectStream>,
    initialized: bool,
    ready: bool,
    in_command_progress: bool,
    ready_tx: Option<mpsc::Sender<bool>>,
}

impl TerminalClient {
    pub async fn new(
        url: &str,
        token: &str,
        params: &SSHConnectParams,
        spinner: &mut ProgressBar,
        max_attempts: Option<u32>,
    ) -> Result<Self> {
        // Use the correct establish_connection function that handles authentication
        let ws_stream = establish_connection(url, token, params, spinner, max_attempts).await?;

        let mut client = Self {
            ws_stream,
            initialized: false,
            ready: false,
            in_command_progress: false,
            ready_tx: None,
        };

        // Wait for the initial welcome message from the server
        if let Some(msg_result) = client.ws_stream.next().await {
            let msg = msg_result.map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;

            if let Message::Text(text) = msg {
                let server_msg: ServerMessage = serde_json::from_str(&text)
                    .map_err(|e| anyhow::anyhow!("Failed to parse server message: {}", e))?;

                if server_msg.r#type != "welcome" {
                    bail!("Expected welcome message, received: {:?}", server_msg);
                }

                Ok(client)
            } else {
                bail!("Expected text message for welcome, received different message type");
            }
        } else {
            bail!("Connection closed before receiving welcome message");
        }
    }

    /// Sends a WebSocket message
    async fn send_message(&mut self, msg: Message) -> Result<()> {
        timeout(
            Duration::from_secs(SSH_MESSAGE_TIMEOUT_SECS),
            self.ws_stream.send(msg),
        )
        .await
        .map_err(|_| {
            anyhow::anyhow!(
                "Message send timed out after {} seconds",
                SSH_MESSAGE_TIMEOUT_SECS
            )
        })??;
        Ok(())
    }

    /// Initializes an interactive shell session
    pub async fn init_shell(&mut self, shell: Option<String>) -> Result<()> {
        if self.initialized {
            bail!("Session already initialized");
        }

        let message = ClientMessage {
            r#type: "init_shell".to_string(),
            payload: ClientPayload::InitShell { shell },
        };

        let msg = serde_json::to_string(&message)?;
        self.send_message(Message::Text(msg))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to initialize shell: {}", e))?;

        self.initialized = true;
        self.ready = false;

        Ok(())
    }

    /// Executes a single command
    pub async fn send_command(&mut self, command: &str, args: Vec<String>) -> Result<()> {
        if self.initialized {
            bail!("Session already initialized");
        }

        let message = ClientMessage {
            r#type: "exec_command".to_string(),
            payload: ClientPayload::Command {
                command: command.to_string(),
                args,
                env: std::collections::HashMap::new(),
            },
        };

        let msg = serde_json::to_string(&message)?;
        self.send_message(Message::Text(msg))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send command: {}", e))?;

        self.initialized = true;
        self.ready = true; // Exec commands are immediately ready

        Ok(())
    }

    /// Sends data to the terminal session
    pub async fn send_data(&mut self, data: &str) -> Result<()> {
        if !self.initialized {
            bail!("Session not initialized");
        }

        if !self.ready {
            bail!("Shell not ready yet");
        }

        // Do not send data when in command progress (stand_by) mode
        if self.in_command_progress {
            return Ok(());
        }

        let message = ClientMessage {
            r#type: "session_data".to_string(),
            payload: ClientPayload::Data {
                data: data.to_string(),
            },
        };

        let msg = serde_json::to_string(&message)?;
        self.send_message(Message::Text(msg))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send data: {}", e))?;
        Ok(())
    }

    /// Updates the terminal window size
    pub async fn send_window_size(&mut self, cols: u16, rows: u16) -> Result<()> {
        // Allow window resize before initialization (needed for initial setup)
        // But block it when initialized but not yet ready
        if self.initialized && !self.ready {
            bail!("Shell not ready yet");
        }

        let message = ClientMessage {
            r#type: "window_resize".to_string(),
            payload: ClientPayload::WindowSize { cols, rows },
        };

        let msg = serde_json::to_string(&message)?;
        self.send_message(Message::Text(msg))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send window size: {}", e))?;
        Ok(())
    }

    /// Sends a signal to the terminal session
    pub async fn send_signal(&mut self, signal: u8) -> Result<()> {
        if !self.initialized {
            bail!("Session not initialized");
        }

        if !self.ready {
            bail!("Shell not ready yet");
        }

        let message = ClientMessage {
            r#type: "signal".to_string(),
            payload: ClientPayload::Signal { signal },
        };

        let msg = serde_json::to_string(&message)?;
        self.send_message(Message::Text(msg))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send signal: {}", e))?;
        Ok(())
    }

    /// Sends a ping message to keep the connection alive
    async fn send_ping(&mut self) -> Result<()> {
        self.send_message(Message::Ping(vec![]))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send ping: {}", e))?;
        Ok(())
    }

    /// Process incoming messages from the server
    pub async fn handle_server_messages_with_writer<W: Write>(
        &mut self,
        writer: &mut W,
        fail_on_exit_code: bool,
    ) -> Result<i32> {
        let mut consecutive_empty_messages = 0;

        let mut ping_interval = interval(Duration::from_secs(SSH_PING_INTERVAL_SECS));

        loop {
            tokio::select! {
                msg_option = self.ws_stream.next() => {
                    match msg_option {
                        Some(msg_result) => {
                            let msg = msg_result.map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
                            match msg {
                                Message::Text(text) => {
                                    let server_msg: ServerMessage = serde_json::from_str(&text)
                                        .map_err(|e| anyhow::anyhow!("Failed to parse server message: {}", e))?;

                                    match server_msg.r#type.as_str() {
                                        "session_data" => match server_msg.payload.data {
                                            DataPayload::String(text) => {
                                                if !text.trim().is_empty() {
                                                    writer.write_all(text.as_bytes())?;
                                                    writer.flush()?;
                                                } else {
                                                    consecutive_empty_messages = 0;
                                                }
                                            }
                                            DataPayload::Buffer { data } => {
                                                if !data.is_empty() {
                                                    writer.write_all(&data)?;
                                                    writer.flush()?;
                                                } else {
                                                    consecutive_empty_messages = 0;
                                                }
                                            }
                                            DataPayload::Empty {} => {
                                                consecutive_empty_messages += 1;
                                                if consecutive_empty_messages >= SSH_MAX_EMPTY_MESSAGES {
                                                    bail!("Received too many empty messages in a row, connection may be stale");
                                                }
                                            }
                                        },
                                        "ready" => {
                                            // Client can start sending data/events
                                            self.ready = true;
                                            self.in_command_progress = false;

                                            // Notify waiting functions that the shell is ready
                                            if let Some(tx) = &self.ready_tx {
                                                let _ = tx.send(true).await;
                                            }
                                        },
                                        "stand_by" => {
                                            // This indicates command is in progress
                                            self.ready = true;
                                            self.in_command_progress = true;

                                            // Notify waiting functions that the shell is ready
                                            if let Some(tx) = &self.ready_tx {
                                                let _ = tx.send(true).await;
                                            }
                                        },
                                        "command_exit" => {
                                            if let Some(code) = server_msg.payload.code {
                                                writer.flush()?;
                                                // If exit code is non-zero, exit with that code
                                                if code != 0 {
                                                    if fail_on_exit_code {
                                                        std::process::exit(code);
                                                    } else {
                                                        bail!("Command exited with code: {}", code);
                                                    }
                                                }

                                                return Ok(code);
                                            }
                                        },
                                        "error" => {
                                            // Notify waiting functions that the shell initialization failed
                                            if let Some(tx) = &self.ready_tx {
                                                let _ = tx.send(false).await;
                                            }
                                            bail!(server_msg.payload.message);
                                        }
                                        "welcome" => {
                                            // Ignore welcome messages after initialization
                                        }
                                        "pty_closed" => {
                                            return Ok(0);
                                        }
                                        unknown_type => {
                                            writeln!(writer, "Warning: Received unknown message type: {}", unknown_type)?;
                                        }
                                    }
                                }
                                Message::Close(frame) => {
                                    if let Some(tx) = &self.ready_tx {
                                        let _ = tx.send(false).await;
                                    }
                                    if let Some(frame) = frame {
                                        bail!(
                                            "WebSocket closed with code {}: {}",
                                            frame.code,
                                            frame.reason
                                        );
                                    } else {
                                        bail!("WebSocket closed unexpectedly");
                                    }
                                }
                                Message::Ping(data) => {
                                    self.send_message(Message::Pong(data)).await?;
                                }
                                Message::Pong(_) => {
                                    // Pong received, connection is still alive
                                }
                                Message::Binary(_) => {
                                    writeln!(writer, "Warning: Unexpected binary message received")?;
                                }
                                Message::Frame(_) => {
                                    writeln!(writer, "Warning: Unexpected raw frame received")?;
                                }
                            }
                        },
                        None => {
                            bail!("WebSocket connection closed unexpectedly");
                        }
                    }
                },

                _ = ping_interval.tick() => {
                    self.send_ping().await?;
                }
            }
        }
    }

    /// Process incoming messages from the server and write to stdout
    pub async fn handle_server_messages(&mut self) -> Result<()> {
        self.handle_server_messages_with_writer(&mut std::io::stdout(), true)
            .await?;

        Ok(())
    }

    /// Directly waits for a ready or stand_by message from the server
    pub async fn wait_for_shell_ready(&mut self, timeout_secs: u64) -> Result<()> {
        let timeout_future = tokio::time::sleep(Duration::from_secs(timeout_secs));
        tokio::pin!(timeout_future);

        loop {
            tokio::select! {
                msg_option = self.ws_stream.next() => {
                    match msg_option {
                        Some(msg_result) => {
                            let msg = msg_result.map_err(|e| anyhow::anyhow!("WebSocket error: {}", e))?;
                            match msg {
                                Message::Text(text) => {
                                    let server_msg: ServerMessage = serde_json::from_str(&text)
                                        .map_err(|e| anyhow::anyhow!("Failed to parse server message: {}", e))?;

                                    if server_msg.r#type == "ready" || server_msg.r#type == "stand_by" {
                                        self.ready = true;
                                        self.in_command_progress = server_msg.r#type == "stand_by";
                                        return Ok(());
                                    }

                                    // Handle specially recognized messages
                                    match server_msg.r#type.as_str() {
                                        "session_data" => match server_msg.payload.data {
                                            DataPayload::String(text) => {
                                                if !text.trim().is_empty() {
                                                    print!("{}", text);
                                                    std::io::stdout().flush()?;
                                                }
                                            }
                                            DataPayload::Buffer { data } => {
                                                if !data.is_empty() {
                                                    std::io::stdout().write_all(&data)?;
                                                    std::io::stdout().flush()?;
                                                }
                                            }
                                            DataPayload::Empty {} => {}
                                        },
                                        "error" => {
                                            bail!("Error from server: {}", server_msg.payload.message);
                                        }
                                        _ => {}
                                    }
                                },
                                // Handle other message types
                                Message::Ping(data) => {
                                    self.send_message(Message::Pong(data)).await?;
                                }
                                Message::Close(frame) => {
                                    if let Some(frame) = frame {
                                        bail!("WebSocket closed with code {}: {}", frame.code, frame.reason);
                                    } else {
                                        bail!("WebSocket closed unexpectedly");
                                    }
                                }
                                // Ignore other message types
                                _ => {}
                            }
                        },
                        None => {
                            bail!("WebSocket connection closed unexpectedly");
                        }
                    }
                },
                _ = &mut timeout_future => {
                    bail!("Timed out waiting for shell to be ready");
                }
            }
        }
    }

    /// Check if the shell is ready for input
    pub fn is_ready(&self) -> bool {
        self.ready
    }

    /// Check if the shell is ready and not currently processing a command
    pub fn is_ready_for_input(&self) -> bool {
        self.ready && !self.in_command_progress
    }
}