keyclaw 0.2.1

Local MITM proxy that keeps secrets out of LLM traffic
Documentation
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
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::certgen::CaPair;
use crate::config::Config;
use crate::errors::KeyclawError;
use crate::pipeline::Processor;
use crate::proxy::Server;

pub(crate) fn run_proxy_foreground(cfg: &Config, processor: Arc<Processor>, ca: CaPair) -> i32 {
    let allowed_hosts = Config::allowed_hosts("all", cfg);

    let mut proxy_server = Server::new(
        cfg.proxy_listen_addr.clone(),
        allowed_hosts,
        processor,
        ca.cert_pem.clone(),
        ca.key_pem,
    );
    proxy_server.max_body_bytes = cfg.max_body_bytes;
    proxy_server.body_timeout = cfg.detector_timeout;
    proxy_server.audit_log_path = cfg.audit_log_path.clone();

    let running_proxy = match proxy_server.start() {
        Ok(p) => p,
        Err(err) => {
            super::super::print_error(&err);
            return 1;
        }
    };

    let proxy_url = format!("http://{}", running_proxy.addr);

    let keyclaw_dir = crate::certgen::keyclaw_dir();
    let ca_path = keyclaw_dir.join("ca.crt");

    let pid_path = keyclaw_dir.join("proxy.pid");
    let pid = std::process::id();
    let _ = fs::write(&pid_path, pid.to_string());

    let env_path = keyclaw_dir.join("env.sh");
    let current_exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("keyclaw"));
    let env_content = render_proxy_env_script(pid, &proxy_url, &ca_path, &current_exe, &pid_path);
    if let Err(e) = fs::write(&env_path, &env_content) {
        crate::logging::error(&format!("failed to write env file: {e}"));
        return 1;
    }

    crate::logging::info(&format!("proxy listening on {}", running_proxy.addr));
    crate::logging::info("press Ctrl-C to stop");
    println!("source {}", env_path.display());

    let mut signals = match signal_hook::iterator::Signals::new([
        signal_hook::consts::SIGINT,
        signal_hook::consts::SIGTERM,
    ]) {
        Ok(s) => s,
        Err(e) => {
            crate::logging::error(&format!("failed to register signals: {e}"));
            return 1;
        }
    };

    let _ = (&mut signals).into_iter().next();

    let _ = fs::remove_file(&pid_path);
    drop(running_proxy);

    crate::logging::info("proxy stopped");
    0
}

pub(crate) fn run_proxy_detached(cfg: &Config) -> Result<i32, KeyclawError> {
    let keyclaw_dir = crate::certgen::keyclaw_dir();
    fs::create_dir_all(&keyclaw_dir)
        .map_err(|e| KeyclawError::uncoded(format!("create keyclaw dir: {e}")))?;

    let pid_path = keyclaw_dir.join("proxy.pid");
    let env_path = keyclaw_dir.join("env.sh");
    let log_path = keyclaw_dir.join("proxy.log");
    let _ = fs::remove_file(&pid_path);
    let _ = fs::remove_file(&env_path);

    let current_exe = std::env::current_exe()
        .map_err(|e| KeyclawError::uncoded(format!("resolve current executable: {e}")))?;
    let log_file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&log_path)
        .map_err(|e| {
            KeyclawError::uncoded(format!("open proxy log {}: {e}", log_path.display()))
        })?;
    let log_file_err = log_file
        .try_clone()
        .map_err(|e| KeyclawError::uncoded(format!("clone proxy log handle: {e}")))?;

    let mut child = Command::new(current_exe)
        .arg("proxy")
        .arg("--foreground")
        .envs(std::env::vars())
        .envs(detached_proxy_env(cfg))
        .stdin(Stdio::null())
        .stdout(Stdio::from(log_file))
        .stderr(Stdio::from(log_file_err))
        .spawn()
        .map_err(|e| KeyclawError::uncoded(format!("start detached proxy: {e}")))?;

    wait_for_detached_proxy_ready(&mut child, &pid_path, &env_path, &log_path)?;

    crate::logging::info("proxy running in background");
    crate::logging::info(&format!("proxy log file: {}", log_path.display()));
    crate::logging::info(&format!(
        "starting the daemon does not reconfigure this shell; run `source {}` before launching clients here",
        env_path.display()
    ));
    println!("source {}", env_path.display());

    let _ = cfg;
    Ok(0)
}

pub(super) fn detached_proxy_env(cfg: &Config) -> Vec<(String, String)> {
    let mut env = vec![(
        "KEYCLAW_DRY_RUN".to_string(),
        if cfg.dry_run { "true" } else { "false" }.to_string(),
    )];
    if !cfg.include_hosts().is_empty() {
        env.push((
            "KEYCLAW_INCLUDE_HOSTS".to_string(),
            cfg.include_hosts().join(","),
        ));
    }
    env
}

pub(crate) fn run_proxy_stop() -> i32 {
    let keyclaw_dir = crate::certgen::keyclaw_dir();
    let pid_path = keyclaw_dir.join("proxy.pid");

    let pid = match read_and_validate_proxy_pid(&pid_path) {
        Some(pid) => pid,
        None => {
            crate::logging::info("no running proxy found");
            return 0;
        }
    };

    let kill_result = unsafe { libc::kill(pid as libc::pid_t, libc::SIGTERM) };
    if kill_result != 0 {
        crate::logging::info("no running proxy found");
        let _ = fs::remove_file(&pid_path);
        return 0;
    }

    let deadline = Instant::now() + Duration::from_secs(5);
    let mut exited = false;
    while Instant::now() < deadline {
        let alive = unsafe { libc::kill(pid as libc::pid_t, 0) == 0 };
        if !alive {
            exited = true;
            break;
        }
        std::thread::sleep(Duration::from_millis(100));
    }

    let _ = fs::remove_file(&pid_path);

    if exited {
        crate::logging::info(&format!("proxy stopped (pid={pid})"));
        0
    } else {
        crate::logging::error(&format!(
            "proxy (pid={pid}) did not exit within 5 seconds after SIGTERM"
        ));
        1
    }
}

pub(crate) fn run_proxy_status() -> i32 {
    let keyclaw_dir = crate::certgen::keyclaw_dir();
    let pid_path = keyclaw_dir.join("proxy.pid");
    let env_path = keyclaw_dir.join("env.sh");

    let pid = match read_and_validate_proxy_pid(&pid_path) {
        Some(pid) => pid,
        None => {
            crate::logging::info("proxy not running");
            return 1;
        }
    };

    let addr = read_proxy_addr_from_env(&env_path).unwrap_or_else(|| "127.0.0.1:8877".to_string());
    crate::logging::info(&format!("proxy running (pid={pid}, addr={addr})"));
    0
}

pub(super) fn read_and_validate_proxy_pid(pid_path: &Path) -> Option<u32> {
    let pid_str = fs::read_to_string(pid_path).ok()?;

    let pid: u32 = match pid_str.trim().parse() {
        Ok(p) => p,
        Err(_) => {
            let _ = fs::remove_file(pid_path);
            return None;
        }
    };

    let alive = unsafe { libc::kill(pid as libc::pid_t, 0) == 0 };
    if !alive {
        let _ = fs::remove_file(pid_path);
        return None;
    }

    if !is_keyclaw_proxy_process(pid) {
        let _ = fs::remove_file(pid_path);
        return None;
    }

    Some(pid)
}

pub(super) fn is_keyclaw_proxy_process(pid: u32) -> bool {
    let current_exe = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("keyclaw"));
    let exe_name = current_exe
        .file_name()
        .and_then(|name| name.to_str())
        .filter(|name| !name.trim().is_empty())
        .unwrap_or("keyclaw");

    let comm = match Command::new("ps")
        .args(["-ww", "-o", "comm=", "-p", &pid.to_string()])
        .output()
    {
        Ok(output) => String::from_utf8_lossy(&output.stdout).trim().to_string(),
        Err(_) => return false,
    };

    if comm != exe_name {
        return false;
    }

    let args = match Command::new("ps")
        .args(["-ww", "-o", "args=", "-p", &pid.to_string()])
        .output()
    {
        Ok(output) => String::from_utf8_lossy(&output.stdout).trim().to_string(),
        Err(_) => return false,
    };

    args.contains(" proxy") && (args.ends_with(" proxy") || args.contains(" proxy "))
}

pub(super) fn read_proxy_addr_from_env(env_path: &Path) -> Option<String> {
    let content = fs::read_to_string(env_path).ok()?;
    for line in content.lines() {
        let line = line.trim();
        if let Some(rest) = line.strip_prefix("export HTTP_PROXY=") {
            let url = rest.trim_matches('\'');
            return Some(url.strip_prefix("http://").unwrap_or(url).to_string());
        }
    }
    None
}

pub(crate) fn render_proxy_env_script(
    pid: u32,
    proxy_url: &str,
    ca_path: &Path,
    current_exe: &Path,
    pid_path: &Path,
) -> String {
    let exe_name = current_exe
        .file_name()
        .and_then(|name| name.to_str())
        .filter(|name| !name.trim().is_empty())
        .unwrap_or("keyclaw");
    let pid_value = shell_single_quote(&pid.to_string());
    let pid_file = shell_single_quote(&pid_path.display().to_string());
    let exe_name = shell_single_quote(exe_name);
    let proxy_url = shell_single_quote(proxy_url);
    let ca_path = shell_single_quote(&ca_path.display().to_string());

    format!(
        r#"# Generated by keyclaw proxy (PID {pid})
# Source this in any shell to route through keyclaw.
# Safe to keep in .bashrc — exports only while the same keyclaw proxy instance is still active.
keyclaw_proxy_pid={pid_value}
keyclaw_proxy_pid_file={pid_file}
keyclaw_proxy_exe_name={exe_name}

keyclaw_proxy_active() {{
  if ! kill -0 "$keyclaw_proxy_pid" 2>/dev/null; then
    return 1
  fi

  if ! command -v ps >/dev/null 2>&1; then
    return 1
  fi

  keyclaw_proxy_comm="$(ps -ww -o comm= -p "$keyclaw_proxy_pid" 2>/dev/null || true)"
  if [ "$keyclaw_proxy_comm" != "$keyclaw_proxy_exe_name" ]; then
    return 1
  fi

  keyclaw_proxy_args="$(ps -ww -o args= -p "$keyclaw_proxy_pid" 2>/dev/null || true)"
  case "$keyclaw_proxy_args" in
    *" proxy"|*" proxy "*) return 0 ;;
  esac

  return 1
}}

if keyclaw_proxy_active; then
  export HTTP_PROXY={proxy_url}
  export HTTPS_PROXY={proxy_url}
  export ALL_PROXY={proxy_url}
  export http_proxy={proxy_url}
  export https_proxy={proxy_url}
  export all_proxy={proxy_url}
  export SSL_CERT_FILE={ca_path}
  export REQUESTS_CA_BUNDLE={ca_path}
  export NODE_EXTRA_CA_CERTS={ca_path}
else
  rm -f "$keyclaw_proxy_pid_file"
  unset HTTP_PROXY HTTPS_PROXY ALL_PROXY http_proxy https_proxy all_proxy
  unset SSL_CERT_FILE REQUESTS_CA_BUNDLE NODE_EXTRA_CA_CERTS
fi
"#,
    )
}

fn shell_single_quote(value: &str) -> String {
    let escaped = value.replace('\'', r#"'\''"#);
    format!("'{escaped}'")
}

fn wait_for_detached_proxy_ready(
    child: &mut std::process::Child,
    pid_path: &Path,
    env_path: &Path,
    log_path: &Path,
) -> Result<(), KeyclawError> {
    let deadline = Instant::now() + Duration::from_secs(60);

    while Instant::now() < deadline {
        if pid_matches_child(pid_path, child.id())? && env_path.exists() {
            return Ok(());
        }

        if let Some(status) = child
            .try_wait()
            .map_err(|e| KeyclawError::uncoded(format!("check detached proxy status: {e}")))?
        {
            if let Some(detail) = detached_proxy_failure_detail(log_path) {
                return Err(KeyclawError::uncoded(format!(
                    "detached proxy exited early with status {status}: {detail}"
                )));
            }
            return Err(KeyclawError::uncoded(format!(
                "detached proxy exited early with status {status}; inspect {}",
                log_path.display()
            )));
        }

        std::thread::sleep(Duration::from_millis(50));
    }

    Err(KeyclawError::uncoded(format!(
        "detached proxy did not become ready; inspect {}",
        log_path.display()
    )))
}

fn pid_matches_child(pid_path: &Path, child_pid: u32) -> Result<bool, KeyclawError> {
    let pid = match fs::read_to_string(pid_path) {
        Ok(pid) => pid,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
        Err(err) => {
            return Err(KeyclawError::uncoded(format!(
                "read proxy pid file {}: {err}",
                pid_path.display()
            )));
        }
    };

    Ok(pid.trim() == child_pid.to_string())
}

fn detached_proxy_failure_detail(log_path: &Path) -> Option<String> {
    let log = fs::read_to_string(log_path).ok()?;
    log.lines()
        .rev()
        .map(str::trim)
        .find(|line| !line.is_empty())
        .map(|line| {
            line.strip_prefix("keyclaw error: ")
                .or_else(|| line.strip_prefix("keyclaw warn: "))
                .or_else(|| line.strip_prefix("keyclaw info: "))
                .unwrap_or(line)
                .to_string()
        })
}