Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

merge main into the monitor attachment branch

Raine Virta554cf32856cf
diff --cc src/main.rs
index f0b484a,68f8554..e574a47
--- a/src/main.rs
+++ b/src/main.rs
@@@ -193,42 -168,6 +193,62 @@@ fn main() -> Result<()>
}
}
+async fn run_service(config: ServerConfig) -> Result<()> {
++ let mut signals = ServiceShutdownSignals::new()?;
+ let (shutdown, stopped) = tokio::sync::oneshot::channel();
+ let server = server::serve_with_shutdown(config, async {
+ let _ = stopped.await;
+ });
+ tokio::pin!(server);
+ tokio::select! {
+ result = &mut server => result,
- signal = service_shutdown_signal() => {
- signal?;
++ _ = signals.recv() => {
+ let _ = shutdown.send(());
+ tokio::select! {
+ result = &mut server => result,
- signal = service_shutdown_signal() => {
- signal?;
- std::process::exit(130);
- }
++ _ = signals.recv() => std::process::exit(130),
+ }
+ }
+ }
+}
+
+#[cfg(unix)]
- async fn service_shutdown_signal() -> std::io::Result<()> {
- let mut terminate = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
- tokio::select! {
- result = tokio::signal::ctrl_c() => result,
- _ = terminate.recv() => Ok(()),
++struct ServiceShutdownSignals {
++ interrupt: tokio::signal::unix::Signal,
++ terminate: tokio::signal::unix::Signal,
++}
++
++#[cfg(unix)]
++impl ServiceShutdownSignals {
++ fn new() -> std::io::Result<Self> {
++ Ok(Self {
++ interrupt: tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())?,
++ terminate: tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?,
++ })
++ }
++
++ async fn recv(&mut self) {
++ tokio::select! {
++ _ = self.interrupt.recv() => {}
++ _ = self.terminate.recv() => {}
++ }
+ }
+}
+
+#[cfg(not(unix))]
- async fn service_shutdown_signal() -> std::io::Result<()> {
- tokio::signal::ctrl_c().await
++struct ServiceShutdownSignals;
++
++#[cfg(not(unix))]
++impl ServiceShutdownSignals {
++ fn new() -> std::io::Result<Self> {
++ Ok(Self)
++ }
++
++ async fn recv(&mut self) {
++ let _ = tokio::signal::ctrl_c().await;
++ }
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ServeMode {
Monitor,
diff --cc tests/cli.rs
index ad57080,8cc555d..3d3ab17
--- a/tests/cli.rs
+++ b/tests/cli.rs
@@@ -111,164 -102,6 +111,136 @@@ fn models_output_is_stable_order() -> R
Ok(())
}
+#[cfg(unix)]
+struct ChildGuard(Child);
+
+#[cfg(unix)]
+impl Drop for ChildGuard {
+ fn drop(&mut self) {
+ let _ = self.0.kill();
+ let _ = self.0.wait();
+ }
+}
+
+#[cfg(unix)]
+fn wait_for_service(
+ child: &mut ChildGuard,
+ port: u16,
+) -> Result<TcpStream, Box<dyn std::error::Error>> {
+ let deadline = Instant::now() + Duration::from_secs(15);
+ loop {
+ match TcpStream::connect(("127.0.0.1", port)) {
+ Ok(stream) => return Ok(stream),
+ Err(error) if Instant::now() < deadline => {
+ if let Some(status) = child.0.try_wait()? {
+ let mut stderr = String::new();
+ if let Some(mut pipe) = child.0.stderr.take() {
+ pipe.read_to_string(&mut stderr)?;
+ }
+ return Err(format!("service exited with {status}: {stderr}").into());
+ }
+ let _ = error;
+ thread::sleep(Duration::from_millis(20));
+ }
+ Err(error) => return Err(error.into()),
+ }
+ }
+}
+
+#[cfg(unix)]
+fn send_signal(child: &ChildGuard, signal: &str) -> Result<(), Box<dyn std::error::Error>> {
+ let status = std::process::Command::new("kill")
+ .args([signal, &child.0.id().to_string()])
+ .status()?;
+ if !status.success() {
+ return Err(format!("kill {signal} failed with {status}").into());
+ }
+ Ok(())
+}
+
+#[cfg(unix)]
+fn wait_for_exit(
+ child: &mut ChildGuard,
+ timeout: Duration,
+) -> Result<ExitStatus, Box<dyn std::error::Error>> {
+ let deadline = Instant::now() + timeout;
+ loop {
+ if let Some(status) = child.0.try_wait()? {
+ return Ok(status);
+ }
+ if Instant::now() >= deadline {
+ return Err("plain service did not exit after the second signal".into());
+ }
+ thread::sleep(Duration::from_millis(20));
+ }
+}
+
+#[cfg(unix)]
+fn plain_service_exits_on_second_signal(signal: &str) -> Result<(), Box<dyn std::error::Error>> {
- let port = TcpListener::bind("127.0.0.1:0")?.local_addr()?.port();
- let child = std::process::Command::new(env!("CARGO_BIN_EXE_claude-code-proxy"))
- .args(["serve", "--no-monitor", "--port", &port.to_string()])
- .stdin(Stdio::null())
- .stdout(Stdio::null())
- .stderr(Stdio::piped())
- .spawn()?;
- let mut child = ChildGuard(child);
- let mut held_connection = wait_for_service(&mut child, port)?;
- held_connection.write_all(b"GET /healthz HTTP/1.1\r\nHost: localhost\r\n")?;
- thread::sleep(Duration::from_millis(100));
-
- send_signal(&child, signal)?;
- thread::sleep(Duration::from_millis(200));
- assert!(child.0.try_wait()?.is_none());
-
- send_signal(&child, signal)?;
- assert_eq!(
- wait_for_exit(&mut child, Duration::from_secs(4))?.code(),
- Some(130)
- );
- Ok(())
- }
-
- #[cfg(unix)]
- #[test]
- fn second_signal_exits_while_kimi_blocking_request_is_running()
- -> Result<(), Box<dyn std::error::Error>> {
+ let upstream = TcpListener::bind("127.0.0.1:0")?;
+ let upstream_url = format!("http://{}", upstream.local_addr()?);
+ let (accepted_tx, accepted_rx) = mpsc::channel();
+ let (release_tx, release_rx) = mpsc::channel();
+ let fixture = thread::spawn(move || {
+ let (stream, _) = upstream.accept().unwrap();
+ accepted_tx.send(()).unwrap();
+ let _ = release_rx.recv();
+ drop(stream);
+ });
+
+ let config = TempDir::new()?;
+ let auth_dir = config.path().join("kimi");
+ std::fs::create_dir_all(&auth_dir)?;
+ std::fs::write(
+ auth_dir.join("auth.json"),
+ r#"{"access":"test","refresh":"test","expires":4102444800000,"scope":"openid","userId":"test"}"#,
+ )?;
+ let port = TcpListener::bind("127.0.0.1:0")?.local_addr()?.port();
+ let child = std::process::Command::new(env!("CARGO_BIN_EXE_claude-code-proxy"))
+ .args(["serve", "--no-monitor", "--port", &port.to_string()])
+ .env("CCP_CONFIG_DIR", config.path())
+ .env("CCP_KIMI_BASE_URL", upstream_url)
+ .env("NO_PROXY", "127.0.0.1,localhost")
+ .env("no_proxy", "127.0.0.1,localhost")
+ .stdin(Stdio::null())
+ .stdout(Stdio::null())
+ .stderr(Stdio::piped())
+ .spawn()?;
+ let mut child = ChildGuard(child);
+ let mut downstream = wait_for_service(&mut child, port)?;
+ let body = br#"{"model":"kimi-for-coding","max_tokens":64,"messages":[{"role":"user","content":"hello"}]}"#;
+ write!(
+ downstream,
+ "POST /v1/messages HTTP/1.1\r\nHost: localhost\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n",
+ body.len()
+ )?;
+ downstream.write_all(body)?;
- accepted_rx.recv_timeout(Duration::from_secs(10))?;
++ accepted_rx.recv_timeout(Duration::from_secs(20))?;
+
- send_signal(&child, "-TERM")?;
++ send_signal(&child, signal)?;
+ thread::sleep(Duration::from_millis(200));
+ assert!(child.0.try_wait()?.is_none());
- send_signal(&child, "-TERM")?;
++ send_signal(&child, signal)?;
+ let status = wait_for_exit(&mut child, Duration::from_secs(2));
+ let _ = release_tx.send(());
+ fixture.join().unwrap();
+
+ assert_eq!(status?.code(), Some(130));
+ Ok(())
+}
+
+#[cfg(unix)]
+#[test]
+fn plain_service_exits_on_second_ctrl_c() -> Result<(), Box<dyn std::error::Error>> {
+ plain_service_exits_on_second_signal("-INT")
+}
+
+#[cfg(unix)]
+#[test]
+fn plain_service_exits_on_second_sigterm() -> Result<(), Box<dyn std::error::Error>> {
+ plain_service_exits_on_second_signal("-TERM")
+}
+
#[test]
fn kimi_auth_status_reads_stored_auth() -> Result<(), Box<dyn std::error::Error>> {
let temp = TempDir::new()?;