Skip to main content

test_encrypted/
test-encrypted.rs

1use kitty_rc::{Kitty};
2use std::error::Error;
3
4#[tokio::main]
5async fn main() -> Result<(), Box<dyn std::error::Error>> {
6    // Find kitty socket
7    let socket_path = find_kitty_socket()?;
8
9    println!("Connecting to: {}", socket_path);
10
11    // Test encrypted connection
12    let mut kitty = Kitty::builder()
13        .socket_path(&socket_path)
14        .password(&read_password()?)
15        .connect()
16        .await
17        .map_err(|e| format!("Connection failed: {}", e))?;
18
19    println!("✓ Connected successfully (encrypted)!");
20
21    // Test sending a simple command
22    use kitty_rc::command::CommandBuilder;
23
24    let cmd = CommandBuilder::new("ls")
25        .build();
26
27    match kitty.execute(&cmd).await {
28        Ok(response) => {
29            println!("✓ Encrypted command executed successfully!");
30            println!("  Response: {:?}", response);
31        }
32        Err(e) => {
33            eprintln!("✗ Encrypted command failed: {}", e);
34            if let Some(source) = Error::source(&e) {
35                eprintln!("  Error source: {}", source);
36            }
37        }
38    }
39
40    Ok(())
41}
42
43fn read_password() -> Result<String, Box<dyn std::error::Error>> {
44    let password_file = format!("{}/.config/kitty/rc.password",
45        std::env::var("HOME").unwrap_or_else(|_| ".".to_string()));
46    std::fs::read_to_string(&password_file)
47        .map_err(|e| Box::<dyn std::error::Error>::from(e))
48        .map(|s| s.to_string())
49}
50
51fn find_kitty_socket() -> Result<String, Box<dyn std::error::Error>> {
52    // Try XDG runtime directory first
53    if let Ok(runtime) = std::env::var("XDG_RUNTIME_DIR") {
54        let dir = std::path::Path::new(&runtime).join("kitty");
55        if let Some(sock) = find_socket_in_dir(&dir) {
56            return Ok(sock);
57        }
58    }
59
60    // Try /run/user/<uid>/kitty
61    let uid = std::env::var("UID").unwrap_or_else(|_| "1000".to_string());
62    let dir = format!("/run/user/{}/kitty", uid);
63    let dir = std::path::Path::new(&dir);
64    if let Some(sock) = find_socket_in_dir(&dir) {
65        return Ok(sock);
66    }
67
68    // Try /tmp/kitty
69    let dir = std::path::Path::new("/tmp/kitty");
70    if let Some(sock) = find_socket_in_dir(&dir) {
71        return Ok(sock);
72    }
73
74    Err("Could not find kitty socket. Please ensure kitty is running with remote control enabled.".into())
75}
76
77fn find_socket_in_dir(dir: &std::path::Path) -> Option<String> {
78    if let Ok(entries) = dir.read_dir() {
79        for entry in entries.flatten() {
80            if let Some(name) = entry.file_name().to_str() {
81                if name.ends_with(".sock") {
82                    return Some(dir.join(name).to_string_lossy().to_string());
83                }
84            }
85        }
86    }
87    None
88}