unlinkable-inference-cli 0.2.0

Local proxy for unlinkable inference: OpenAI and Anthropic endpoints on localhost paid from a voucher wallet, one session per conversation
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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! The local proxy server.
//!
//! Agents and SDKs talk to it as if it were the OpenAI or Anthropic API. For
//! each conversation it opens a session paid with vouchers from the wallet,
//! sends the request with the session secret in place of any key, streams the
//! answer back, and closes the session once the conversation goes quiet,
//! putting the change back in the wallet. Whatever key the client sends stays
//! here: it only names which session to use.

use crate::{dollars, Options};
use anyhow::{anyhow, Result};
use axum::{
    body::Body,
    extract::{Request, State},
    http::{HeaderMap, HeaderValue, StatusCode},
    response::{Html, IntoResponse, Response},
    Json, Router,
};
use futures::StreamExt;
use serde_json::{json, Value};
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use unlinkable_inference::net::{Client, Observed, Request as Inner, Session};
use unlinkable_inference::{Family, Voucher, Wallet};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mode {
    /// A new session for each conversation (system prompt + first user message)
    Conversation,
    /// One session per API key the client sends (name keys per agent run)
    Key,
    /// One session for everything until it goes idle
    Run,
}

impl std::str::FromStr for Mode {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, String> {
        match s {
            "conversation" => Ok(Mode::Conversation),
            "key" => Ok(Mode::Key),
            "run" => Ok(Mode::Run),
            _ => Err("use conversation, key or run".into()),
        }
    }
}

#[derive(Debug, Clone)]
pub struct Settings {
    /// What each new session is funded with, in cents
    pub funding_cents: i64,
    pub mode: Mode,
    /// Close a session after this long without a request
    pub idle_close: Duration,
    pub show_traffic: bool,
    /// The account's API key, for keeping the wallet and passes topped up.
    /// Never sent with a chat. If unset, the proxy learns it from the first
    /// `sk-obs1_` key a client sends.
    pub account_key: Option<String>,
    /// Withdraw when the wallet holds less than this many cents…
    pub keep_cents: i64,
    /// …this much (one of the fixed withdrawal sizes)
    pub refill_cents: i64,
    /// Fetch passes when fewer than this many are left this month
    pub keep_passes: usize,
    /// Client keys starting with this are taken to be the account's key
    pub account_key_prefix: String,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            funding_cents: 50,
            mode: Mode::Conversation,
            idle_close: Duration::from_secs(300),
            show_traffic: false,
            account_key: None,
            keep_cents: 50,
            refill_cents: 100,
            keep_passes: 10,
            account_key_prefix: "sk-obs1_".into(),
        }
    }
}

struct Entry {
    session: Session,
    last_used: AtomicU64,
    busy: AtomicUsize,
    opened_at: u64,
    requests: AtomicU64,
}

pub struct Proxy {
    client: Client,
    wallet: tokio::sync::Mutex<Wallet>,
    sessions: Mutex<HashMap<String, Arc<Entry>>>,
    /// Held-conversation revs (x-obscurify-thread) to the session that owns them
    threads: Mutex<HashMap<String, String>>,
    traffic: Arc<Mutex<VecDeque<(u64, Observed)>>>,
    opening: tokio::sync::Mutex<()>,
    settings: Settings,
    opts: Options,
    account_key: Mutex<Option<String>>,
    /// After a failed top-up, wait before trying again
    refill_backoff: Mutex<Option<std::time::Instant>>,
    /// The labels the account gets passes for, as last asked (at most every 10 minutes), and when
    offered: tokio::sync::Mutex<Option<(unlinkable_inference::protocol::PassAllowance, std::time::Instant)>>,
    /// label → when a session last needed one and there was none to present
    refused_labels: Mutex<HashMap<String, std::time::Instant>>,
    /// The helper session's last seen balance (see [`Proxy::helper_secret`])
    helper_balance: Mutex<String>,
}

/// The session handed out by `/_unlinkable/key` for agents that talk to the
/// server directly (Claude Code's `apiKeyHelper`).
const HELPER: &str = "helper";

/// The server no longer knows this session (expired or closed elsewhere).
fn gone(e: &anyhow::Error) -> bool {
    matches!(
        e.downcast_ref::<unlinkable_inference::net::ApiError>(),
        Some(unlinkable_inference::net::ApiError::Server { status: 401 | 404 | 410, .. })
    )
}

fn now() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// Request headers passed on. Everything else (user agents, SDK telemetry,
/// the client's own key) stays on this machine.
const PASSED_HEADERS: &[&str] = &[
    "content-type",
    "accept",
    "anthropic-version",
    "anthropic-beta",
    "openai-beta",
];

const DROPPED_RESPONSE_HEADERS: &[&str] = &[
    "content-length",
    "transfer-encoding",
    "connection",
    "set-cookie",
];

const MAX_BODY: usize = 32 * 1024 * 1024;

/// POST endpoints (and anything under them) forwarded to the server.
const FORWARDED_POSTS: &[&str] = &[
    "/v1/chat/completions",
    "/v1/completions",
    "/v1/responses",
    "/v1/messages",
    "/v1/embeddings",
    "/v1/images",
    "/v1/tools",
];

fn text(status: StatusCode, message: &str) -> Response {
    (
        status,
        Json(json!({"error": {"message": message, "type": "unlinkable_inference_cli_error"}})),
    )
        .into_response()
}

/// The part of a request that stays the same for a whole conversation.
pub fn conversation_fingerprint(path: &str, body: &Value) -> String {
    let parts = if path.ends_with("/chat/completions") {
        let messages = body["messages"].as_array().cloned().unwrap_or_default();
        let end = messages
            .iter()
            .position(|m| m["role"] == "user")
            .map(|i| i + 1)
            .unwrap_or(messages.len());
        json!(messages[..end])
    } else if path.ends_with("/messages") {
        json!([body["system"], body["messages"][0]])
    } else if path.ends_with("/responses") {
        let first = match &body["input"] {
            Value::Array(items) => items.first().cloned().unwrap_or(Value::Null),
            other => other.clone(),
        };
        json!([body["instructions"], first])
    } else {
        Value::Null
    };
    unlinkable_inference::sha256_hex(parts.to_string())[..16].to_string()
}

fn client_key(headers: &HeaderMap) -> String {
    headers
        .get("authorization")
        .and_then(|v| v.to_str().ok())
        .map(|v| v.trim_start_matches("Bearer ").trim().to_string())
        .or_else(|| {
            headers
                .get("x-api-key")
                .and_then(|v| v.to_str().ok())
                .map(str::to_string)
        })
        .unwrap_or_default()
}

impl Proxy {
    pub fn new(mut client: Client, wallet: Wallet, settings: Settings, opts: Options) -> Arc<Self> {
        let traffic: Arc<Mutex<VecDeque<(u64, Observed)>>> = Arc::new(Mutex::new(VecDeque::new()));
        let log = traffic.clone();
        let show = settings.show_traffic;
        client.transport = client.transport.clone().observe(Arc::new(move |o: &Observed| {
            if show {
                eprintln!("→ {}: {}", o.server, o.server_sees);
            }
            let mut log = log.lock().unwrap();
            log.push_front((now(), o.clone()));
            log.truncate(100);
        }));
        Arc::new(Self {
            client,
            wallet: tokio::sync::Mutex::new(wallet),
            sessions: Mutex::new(HashMap::new()),
            threads: Mutex::new(HashMap::new()),
            traffic,
            opening: tokio::sync::Mutex::new(()),
            account_key: Mutex::new(settings.account_key.clone()),
            refill_backoff: Mutex::new(None),
            offered: tokio::sync::Mutex::new(None),
            refused_labels: Mutex::new(HashMap::new()),
            helper_balance: Mutex::new(String::new()),
            settings,
            opts,
        })
    }

    fn session_key(&self, path: &str, headers: &HeaderMap, body: &Value) -> String {
        if let Some(rev) = body["thread"]["rev"].as_str() {
            if let Some(key) = self.threads.lock().unwrap().get(rev) {
                return key.clone();
            }
        }
        match self.settings.mode {
            Mode::Run => "run".to_string(),
            Mode::Key => format!("key:{}", client_key(headers)),
            Mode::Conversation => format!(
                "conv:{}:{}",
                client_key(headers),
                conversation_fingerprint(path, body)
            ),
        }
    }

    async fn family_to_spend(&self) -> Family {
        let wallet = self.wallet.lock().await;
        if wallet.balance_cents(Family::Paid) == 0 && wallet.balance_cents(Family::Promo) > 0 {
            Family::Promo
        } else {
            Family::Paid
        }
    }

    /// Keep the wallet and passes topped up from the account, in the
    /// background, so customers never move money by hand. Withdrawals happen
    /// when the wallet runs low, not when a session opens, so their timing
    /// says little about any one conversation. `urgent` skips the backoff
    /// (the wallet is empty and a request is waiting).
    pub async fn refill(&self, urgent: bool) {
        let Some(key) = self.account_key.lock().unwrap().clone() else {
            return;
        };
        if !urgent {
            if let Some(at) = *self.refill_backoff.lock().unwrap() {
                if at.elapsed() < Duration::from_secs(600) {
                    return;
                }
            }
        }
        let mut wallet = self.wallet.lock().await;
        let mut failed = false;
        // One wallet per account: what other devices withdrew, or got back as change from
        // sessions they closed, comes first, before more is taken from the account.
        if wallet.balance_cents(Family::Paid) < self.settings.keep_cents && self.client.seed.is_some() {
            if let Err(e) = self.client.restore(&mut wallet, Some(&key)).await {
                tracing::warn!("could not look for the account's other vouchers: {e:#}");
            }
        }
        if wallet.balance_cents(Family::Paid) < self.settings.keep_cents {
            // The account's last cents (under $1) too, down to a session's 10¢.
            match self
                .client
                .withdraw_up_to(&mut wallet, &key, self.settings.refill_cents, 10)
                .await
            {
                Ok(c) => tracing::info!("topped up the wallet with {} from the account", dollars(c)),
                Err(e) => {
                    failed = true;
                    tracing::warn!("could not top up the wallet: {e:#}");
                }
            }
        }
        match self.stock(&mut wallet, &key, None, false).await {
            Ok(0) => {}
            Ok(n) => tracing::info!("got {n} passes for this month"),
            Err(e) => {
                failed = true;
                tracing::warn!("could not get passes: {e:#}");
            }
        }
        if self.client.seed.is_some() {
            // Let the server forget what is fully spent (at this quiet moment,
            // not when a session opens).
            if let Err(e) = self.client.flush_done(&mut wallet, Some(&key)).await {
                tracing::warn!("could not mark spent records done: {e:#}");
            }
        }
        let _ = wallet.save();
        *self.refill_backoff.lock().unwrap() = failed.then(std::time::Instant::now);
    }

    /// Keep `keep_passes` of each label the account gets (only `only`, when given). The
    /// account is asked which labels those are at most every 10 minutes, or now with `fresh`.
    async fn stock(&self, wallet: &mut Wallet, key: &str, only: Option<&str>, fresh: bool) -> Result<usize> {
        if self.client.keyset.pass_key.is_none() {
            return Ok(0);
        }
        let mut offered = self.offered.lock().await;
        if fresh || offered.as_ref().is_none_or(|(_, at)| at.elapsed() >= Duration::from_secs(600)) {
            *offered = Some((self.client.pass_allowance(key).await?, std::time::Instant::now()));
        }
        let Some((allowance, _)) = offered.as_mut() else { return Ok(0) };
        Ok(self.client.stock_passes(wallet, key, allowance, self.settings.keep_passes, 30, only).await)
    }

    /// The server asked this session for a pass with `label`: present one. With none held,
    /// what the account was last told decides: a label it has used up for the month means no,
    /// with no word to the account (its request right before this session's next one would
    /// tie the two by their timing); one it wasn't offered may be new, so the account is
    /// asked (at most once a minute per label) and some are fetched. False: none to present.
    async fn present_pass(&self, entry: &Entry, label: &str) -> bool {
        let mut wallet = self.wallet.lock().await;
        match self.client.present_pass(&mut wallet, &entry.session, label).await {
            Ok(true) => return true,
            Ok(false) => {}
            Err(e) => {
                tracing::warn!("could not present a {label} pass: {e:#}");
                return false;
            }
        }
        let Some(key) = self.account_key.lock().unwrap().clone() else { return false };
        let known = self
            .offered
            .lock()
            .await
            .as_ref()
            .filter(|(_, at)| at.elapsed() < Duration::from_secs(600))
            .and_then(|(a, _)| a.labels.iter().find(|l| l.label == label))
            .map(|l| l.remaining);
        let fresh = match known {
            Some(left) if left <= 0 => return false,
            Some(_) => false,
            None => {
                let mut refused = self.refused_labels.lock().unwrap();
                if refused.get(label).is_some_and(|at| at.elapsed() < Duration::from_secs(60)) {
                    return false;
                }
                refused.insert(label.to_string(), std::time::Instant::now());
                true
            }
        };
        let _ = self.stock(&mut wallet, &key, Some(label), fresh).await;
        self.client.present_pass(&mut wallet, &entry.session, label).await.unwrap_or(false)
    }

    fn learn_account_key(&self, headers: &HeaderMap) {
        let key = client_key(headers);
        if !self.settings.account_key_prefix.is_empty() && key.starts_with(&self.settings.account_key_prefix) {
            let mut known = self.account_key.lock().unwrap();
            if known.is_none() {
                *known = Some(key);
            }
        }
    }

    async fn entry(&self, key: &str) -> Result<Arc<Entry>> {
        if let Some(e) = self.sessions.lock().unwrap().get(key) {
            return Ok(e.clone());
        }
        let _guard = self.opening.lock().await;
        if let Some(e) = self.sessions.lock().unwrap().get(key) {
            return Ok(e.clone());
        }
        let family = self.family_to_spend().await;
        let opened = {
            let mut wallet = self.wallet.lock().await;
            self.client
                .open(&mut wallet, family, self.settings.funding_cents)
                .await
        };
        let session = match opened {
            Ok(s) => s,
            Err(e) if e.to_string().contains("has no") => {
                // Empty wallet and a request waiting: top up now rather
                // than fail (the background refill should prevent this).
                self.refill(true).await;
                let mut wallet = self.wallet.lock().await;
                self.client
                    .open(&mut wallet, family, self.settings.funding_cents)
                    .await?
            }
            Err(e) => return Err(e),
        };
        let entry = Arc::new(Entry {
            session,
            last_used: AtomicU64::new(now()),
            busy: AtomicUsize::new(0),
            opened_at: now(),
            requests: AtomicU64::new(0),
        });
        self.sessions
            .lock()
            .unwrap()
            .insert(key.to_string(), entry.clone());
        Ok(entry)
    }

    /// The secret of the helper session, opening one if there is none or the
    /// server has closed it. Agents that send it straight to the server
    /// (Claude Code's `apiKeyHelper`) ask again after a 401.
    pub async fn helper_secret(&self) -> Result<String> {
        let existing = self.sessions.lock().unwrap().get(HELPER).cloned();
        if let Some(entry) = existing {
            match self.client.status(&entry.session).await {
                Ok(st) if st.closed => self.retire_entry(HELPER).await,
                Err(e) if gone(&e) => self.retire_entry(HELPER).await,
                // Open, or the server is unreachable: keep it, never drop a
                // session that may still hold money.
                _ => return Ok(entry.session.secret.clone()),
            }
        }
        let entry = self.entry(HELPER).await?;
        // Its requests go straight to the server, so nothing here sees the server ask for a
        // pass: present one of each label the account gets now.
        let key = self.account_key.lock().unwrap().clone();
        let mut wallet = self.wallet.lock().await;
        if let Some(key) = key {
            let _ = self.stock(&mut wallet, &key, None, false).await;
        }
        let period = unlinkable_inference::pass::current_period();
        let mut labels: Vec<String> = wallet.passes(&period).into_iter().map(|p| p.label).collect();
        labels.sort();
        labels.dedup();
        for label in labels {
            if let Err(e) = self.client.present_pass(&mut wallet, &entry.session, &label).await {
                tracing::warn!("could not present a {label} pass for the helper session: {e:#}");
            }
        }
        Ok(entry.session.secret.clone())
    }

    /// The helper session's requests never pass through here, so watch its
    /// balance instead: a change means it is in use (not idle), and a low
    /// balance gets topped up before a request fails for want of money.
    pub async fn watch_helper(&self) {
        let Some(entry) = self.sessions.lock().unwrap().get(HELPER).cloned() else {
            return;
        };
        let st = match self.client.status(&entry.session).await {
            Ok(st) if !st.closed => st,
            Ok(_) => return self.retire_entry(HELPER).await,
            Err(e) if gone(&e) => return self.retire_entry(HELPER).await,
            Err(_) => return,
        };
        let changed = {
            let mut last = self.helper_balance.lock().unwrap();
            let changed = *last != st.balance_usd;
            *last = st.balance_usd.clone();
            changed
        };
        if changed {
            entry.last_used.store(now(), Ordering::SeqCst);
        }
        if unlinkable_inference::net::usd_to_cents_floor(&st.balance_usd) < self.settings.funding_cents {
            let mut wallet = self.wallet.lock().await;
            if wallet.balance_cents(entry.session.family) < self.settings.funding_cents {
                drop(wallet);
                self.refill(true).await;
                wallet = self.wallet.lock().await;
            }
            if let Err(e) = self
                .client
                .topup(&mut wallet, &entry.session, self.settings.funding_cents)
                .await
            {
                tracing::warn!("could not top up the helper session: {e:#}");
            }
        }
    }

    /// Stop using a session the server no longer serves and close it, so
    /// its change comes back (an expired session can still be closed; if
    /// closing fails it stays in the wallet for `recover`).
    async fn retire_entry(&self, key: &str) {
        if let Some(old) = self.drop_entry(key) {
            self.close_entry(&old).await;
        }
    }

    fn drop_entry(&self, key: &str) -> Option<Arc<Entry>> {
        self.sessions.lock().unwrap().remove(key)
    }

    async fn close_entry(&self, entry: &Entry) {
        let mut wallet = self.wallet.lock().await;
        // Dates what the session cost (the wallet's spending log) by its last request.
        wallet.used_session(&entry.session.secret, entry.last_used.load(Ordering::SeqCst));
        match self.client.close(&mut wallet, &entry.session).await {
            Ok(cents) => tracing::info!("closed a session; {} back in the wallet", dollars(cents)),
            Err(e) => tracing::warn!("could not close a session (will retry at next start): {e}"),
        }
    }

    /// Close every session that has been quiet for `idle_close`.
    pub async fn close_idle(&self) {
        let cutoff = now().saturating_sub(self.settings.idle_close.as_secs());
        let idle: Vec<(String, Arc<Entry>)> = {
            let mut sessions = self.sessions.lock().unwrap();
            let keys: Vec<String> = sessions
                .iter()
                .filter(|(_, e)| {
                    e.busy.load(Ordering::SeqCst) == 0 && e.last_used.load(Ordering::SeqCst) <= cutoff
                })
                .map(|(k, _)| k.clone())
                .collect();
            keys.into_iter()
                .filter_map(|k| sessions.remove(&k).map(|e| (k, e)))
                .collect()
        };
        for (key, entry) in idle {
            self.threads.lock().unwrap().retain(|_, v| *v != key);
            self.close_entry(&entry).await;
        }
    }

    pub async fn close_all(&self) {
        let all: Vec<Arc<Entry>> = self.sessions.lock().unwrap().drain().map(|(_, e)| e).collect();
        self.threads.lock().unwrap().clear();
        for entry in all {
            self.close_entry(&entry).await;
        }
    }

    async fn send(&self, entry: &Entry, method: &str, path: &str, headers: &[(String, String)], body: &[u8]) -> Result<unlinkable_inference::net::Response> {
        let mut inner = Inner {
            method: method.to_string(),
            path: path.to_string(),
            headers: headers.to_vec(),
            body: body.to_vec(),
        };
        inner = inner.bearer(&entry.session.secret);
        self.client.transport.send(inner).await
    }

    async fn forward(&self, method: &str, path: &str, headers: HeaderMap, body: bytes::Bytes) -> Response {
        let passed: Vec<(String, String)> = PASSED_HEADERS
            .iter()
            .filter_map(|&h| {
                headers
                    .get(h)
                    .and_then(|v| v.to_str().ok())
                    .map(|v| (h.to_string(), v.to_string()))
            })
            .collect();

        // Model lists need no session.
        if method == "GET" && (path == "/v1/models" || path.starts_with("/v1/models/")) {
            let req = Inner {
                method: method.into(),
                path: path.into(),
                headers: passed,
                body: Vec::new(),
            };
            return match self.client.transport.send(req).await {
                Ok(r) => pass_through(r),
                Err(e) => text(StatusCode::BAD_GATEWAY, &format!("{e:#}")),
            };
        }

        self.learn_account_key(&headers);
        let json_body: Value = serde_json::from_slice(&body).unwrap_or(Value::Null);
        let key = self.session_key(path, &headers, &json_body);
        let has_thread = json_body["thread"]["rev"].is_string();

        let mut reopened = false;
        let mut topups = 0;
        let mut presented = 0;
        let mut none: Vec<String> = Vec::new();
        loop {
            let entry = match self.entry(&key).await {
                Ok(e) => e,
                Err(e) => return text(StatusCode::PAYMENT_REQUIRED, &format!("{e:#}")),
            };
            entry.busy.fetch_add(1, Ordering::SeqCst);
            let result = self.send(&entry, method, path, &passed, &body).await;
            entry.last_used.store(now(), Ordering::SeqCst);
            entry.requests.fetch_add(1, Ordering::SeqCst);
            let response = match result {
                Ok(r) => r,
                Err(e) => {
                    entry.busy.fetch_sub(1, Ordering::SeqCst);
                    return text(StatusCode::BAD_GATEWAY, &format!("{e:#}"));
                }
            };
            match response.status {
                402 if topups < 5 => {
                    // Out of money for this request: add more from the wallet
                    // (the top-up links only to this session) and try again.
                    topups += 1;
                    let cents = self.settings.funding_cents << topups;
                    let result = {
                        let mut wallet = self.wallet.lock().await;
                        self.client.topup(&mut wallet, &entry.session, cents).await
                    };
                    entry.busy.fetch_sub(1, Ordering::SeqCst);
                    if let Err(e) = result {
                        return text(
                            StatusCode::PAYMENT_REQUIRED,
                            &format!("The session ran out and the wallet could not top it up: {e:#}"),
                        );
                    }
                    continue;
                }
                403 if presented < 8 && response.header(unlinkable_inference::protocol::PASS_REQUIRED_HEADER).is_some() => {
                    // The server wants a pass for this request: present one (it links only
                    // to this session) and try again. With none to present, send it once more
                    // as it was: a server may serve it another way (at another price).
                    let label = response.header(unlinkable_inference::protocol::PASS_REQUIRED_HEADER).unwrap_or_default().to_string();
                    if self.present_pass(&entry, &label).await {
                        presented += 1;
                        entry.busy.fetch_sub(1, Ordering::SeqCst);
                        continue;
                    }
                    if !none.contains(&label) {
                        none.push(label);
                        entry.busy.fetch_sub(1, Ordering::SeqCst);
                        continue;
                    }
                }
                401 if !reopened && !has_thread => {
                    // The session expired: close it for its change, start a new one.
                    entry.busy.fetch_sub(1, Ordering::SeqCst);
                    reopened = true;
                    self.retire_entry(&key).await;
                    continue;
                }
                _ => {}
            }
            if let Some(rev) = response.header("x-obscurify-thread") {
                self.threads
                    .lock()
                    .unwrap()
                    .insert(rev.to_string(), key.clone());
            }
            let out = pass_through_tracked(response, entry.clone());
            return out;
        }
    }

    async fn status_json(&self) -> Value {
        let mut wallet = self.wallet.lock().await;
        let period = unlinkable_inference::pass::current_period();
        let passes = wallet.passes(&period);
        let mut pass_labels = std::collections::BTreeMap::<String, usize>::new();
        for p in &passes {
            *pass_labels.entry(p.label.clone()).or_default() += 1;
        }
        let sessions: Vec<Value> = self
            .sessions
            .lock()
            .unwrap()
            .values()
            .map(|e| {
                json!({
                    "family": e.session.family,
                    "age_seconds": now() - e.opened_at,
                    "idle_seconds": now() - e.last_used.load(Ordering::SeqCst),
                    "requests": e.requests.load(Ordering::SeqCst),
                })
            })
            .collect();
        let traffic: Vec<Value> = self
            .traffic
            .lock()
            .unwrap()
            .iter()
            .take(40)
            .map(|(at, o)| {
                json!({"at": at, "bytes_sent": o.bytes_sent, "server_sees": o.server_sees})
            })
            .collect();
        json!({
            "server": self.opts.server,
            "keyset": self.client.keyset.fingerprint(),
            "wallet": {
                "paid_cents": wallet.balance_cents(Family::Paid),
                "promo_cents": wallet.balance_cents(Family::Promo),
                "vouchers": wallet.vouchers().len(),
                "passes": passes.len(),
                "pass_labels": pass_labels,
            },
            "settings": {
                "session_funding_cents": self.settings.funding_cents,
                "idle_close_seconds": self.settings.idle_close.as_secs(),
            },
            "sessions": sessions,
            "traffic": traffic,
        })
    }

    /// What private use cost, oldest first: closed sessions from the wallet's
    /// log plus what open ones have used so far (each asked with its own secret).
    async fn spending_json(&self) -> Value {
        let (mut spent, open, mut prepaid) = {
            let wallet = self.wallet.lock().await;
            (wallet.spending().to_vec(), wallet.sessions().to_vec(), wallet.balance_millicents(Family::Paid))
        };
        for o in open.into_iter().filter(|o| o.funded_millicents > 0) {
            let session = unlinkable_inference::net::Session { secret: o.secret.clone(), family: o.family };
            if let Ok(status) = self.client.status(&session).await {
                let left = unlinkable_inference::net::usd_to_millicents_ceil(&status.balance_usd);
                prepaid += left;
                if o.funded_millicents > left {
                    spent.push(unlinkable_inference::Spend { at: o.used_at.max(o.opened_at), millicents: o.funded_millicents - left });
                }
            }
        }
        json!({ "spent": spent, "prepaid_millicents": prepaid })
    }

    async fn withdraw(&self, api_key: &str, cents: i64) -> Result<i64> {
        let mut wallet = self.wallet.lock().await;
        self.client.withdraw(&mut wallet, api_key, cents).await
    }

    /// Get up to `count` passes of each label the account gets.
    async fn passes(&self, api_key: &str, count: i64) -> Result<usize> {
        let mut wallet = self.wallet.lock().await;
        let allowance = self.client.pass_allowance(api_key).await?;
        let mut got = 0;
        for l in &allowance.labels {
            got += self.client.get_passes(&mut wallet, api_key, &l.label, &allowance.period, count.min(l.remaining)).await?;
        }
        Ok(got)
    }

    async fn import(&self, codes: &[String]) -> Result<i64> {
        let mut added = 0;
        let mut wallet = self.wallet.lock().await;
        for code in codes {
            let voucher = Voucher::parse(code).ok_or_else(|| anyhow!("not a voucher: {code}"))?;
            let family = unlinkable_inference::blank::verify(&self.client.keyset, &voucher)
                .ok_or_else(|| anyhow!("this server did not sign {code}"))?;
            added += voucher.denomination_cents;
            wallet.add(family, [voucher]);
        }
        wallet.save()?;
        Ok(added)
    }
}

fn copy_headers(resp: &unlinkable_inference::net::Response, out: &mut HeaderMap) {
    for (k, v) in &resp.headers {
        let k = k.to_ascii_lowercase();
        if DROPPED_RESPONSE_HEADERS.contains(&k.as_str()) {
            continue;
        }
        if let (Ok(name), Ok(value)) = (
            axum::http::HeaderName::from_bytes(k.as_bytes()),
            HeaderValue::from_str(v),
        ) {
            out.insert(name, value);
        }
    }
}

fn pass_through(resp: unlinkable_inference::net::Response) -> Response {
    let mut headers = HeaderMap::new();
    copy_headers(&resp, &mut headers);
    let status = StatusCode::from_u16(resp.status).unwrap_or(StatusCode::BAD_GATEWAY);
    let body = Body::from_stream(resp.body.map(|r| r.map_err(std::io::Error::other)));
    (status, headers, body).into_response()
}

/// Like [`pass_through`], keeping the session marked busy until the
/// stream ends, so it is never closed mid-answer.
fn pass_through_tracked(resp: unlinkable_inference::net::Response, entry: Arc<Entry>) -> Response {
    let mut headers = HeaderMap::new();
    copy_headers(&resp, &mut headers);
    let status = StatusCode::from_u16(resp.status).unwrap_or(StatusCode::BAD_GATEWAY);
    struct Done(Arc<Entry>);
    impl Drop for Done {
        fn drop(&mut self) {
            self.0.last_used.store(now(), Ordering::SeqCst);
            self.0.busy.fetch_sub(1, Ordering::SeqCst);
        }
    }
    let done = Done(entry);
    let body = resp.body.map(move |r| {
        let _keep = &done;
        r.map_err(std::io::Error::other)
    });
    (status, headers, Body::from_stream(body)).into_response()
}

/// Only local clients, and never a web page: a site you visit must not be
/// able to spend your wallet through this proxy.
fn local_only(headers: &HeaderMap) -> Option<Response> {
    let host = headers
        .get("host")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("");
    let host_name = host.rsplit_once(':').map(|(h, _)| h).unwrap_or(host);
    if !matches!(host_name, "127.0.0.1" | "localhost" | "[::1]" | "") {
        return Some(text(StatusCode::FORBIDDEN, "local clients only"));
    }
    if let Some(origin) = headers.get("origin").and_then(|v| v.to_str().ok()) {
        let ok = ["http://127.0.0.1", "http://localhost", "http://[::1]"]
            .iter()
            .any(|p| origin.starts_with(p));
        if !ok {
            return Some(text(StatusCode::FORBIDDEN, "web pages cannot use this proxy"));
        }
    }
    None
}

async fn handle(State(proxy): State<Arc<Proxy>>, req: Request) -> Response {
    let (parts, body) = req.into_parts();
    if let Some(refusal) = local_only(&parts.headers) {
        return refusal;
    }
    let path = parts
        .uri
        .path_and_query()
        .map(|p| p.as_str().to_string())
        .unwrap_or_else(|| "/".into());
    let method = parts.method.as_str().to_string();
    let Ok(body) = axum::body::to_bytes(body, MAX_BODY).await else {
        return text(StatusCode::PAYLOAD_TOO_LARGE, "request too large");
    };

    if path.starts_with("/_unlinkable") {
        return control(&proxy, &method, &path, &parts.headers, &body).await;
    }
    if method == "POST" {
        let json = parts
            .headers
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .is_some_and(|v| v.starts_with("application/json"));
        if !json {
            return text(StatusCode::UNSUPPORTED_MEDIA_TYPE, "send application/json");
        }
    }
    // Clients configured with a base URL without /v1 (some Anthropic
    // setups) still reach the API.
    let path = if path.starts_with("/v1/") {
        path
    } else {
        format!("/v1{path}")
    };
    // Only API calls go out (and only they open sessions): a browser's
    // favicon request or a typo must not spend the wallet.
    let route = path.split('?').next().unwrap_or("");
    let api = match method.as_str() {
        "GET" => route == "/v1/models" || route.starts_with("/v1/models/"),
        "POST" => FORWARDED_POSTS.iter().any(|p| route == *p || route.starts_with(&format!("{p}/"))),
        _ => false,
    };
    if !api {
        return text(StatusCode::NOT_FOUND, "not an API path this proxy forwards");
    }
    proxy.forward(&method, &path, parts.headers, body).await
}

async fn control(proxy: &Proxy, method: &str, path: &str, headers: &HeaderMap, body: &[u8]) -> Response {
    if method == "POST" {
        let json = headers
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .is_some_and(|v| v.starts_with("application/json"));
        if !json {
            return text(StatusCode::UNSUPPORTED_MEDIA_TYPE, "send application/json");
        }
    }
    let body: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
    match (method, path) {
        ("GET", "/_unlinkable") | ("GET", "/_unlinkable/") => Html(PAGE).into_response(),
        ("GET", "/_unlinkable/status") => Json(proxy.status_json().await).into_response(),
        ("GET", "/_unlinkable/spending") => Json(proxy.spending_json().await).into_response(),
        ("GET", "/_unlinkable/key") => match proxy.helper_secret().await {
            Ok(secret) => secret.into_response(),
            Err(e) => text(StatusCode::PAYMENT_REQUIRED, &format!("{e:#}")),
        },
        ("POST", "/_unlinkable/withdraw") => {
            let cents = body["amount_cents"].as_i64().unwrap_or(0);
            let key = body["api_key"].as_str().unwrap_or("");
            match proxy.withdraw(key, cents).await {
                Ok(c) => Json(json!({"withdrawn_cents": c})).into_response(),
                Err(e) => text(StatusCode::BAD_REQUEST, &format!("{e:#}")),
            }
        }
        ("POST", "/_unlinkable/passes") => {
            let count = body["count"].as_i64().unwrap_or(30);
            let key = body["api_key"].as_str().unwrap_or("");
            match proxy.passes(key, count).await {
                Ok(n) => Json(json!({"got": n})).into_response(),
                Err(e) => text(StatusCode::BAD_REQUEST, &format!("{e:#}")),
            }
        }
        ("POST", "/_unlinkable/import") => {
            let codes: Vec<String> = body["vouchers"]
                .as_array()
                .map(|a| a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect())
                .unwrap_or_default();
            match proxy.import(&codes).await {
                Ok(c) => Json(json!({"added_cents": c})).into_response(),
                Err(e) => text(StatusCode::BAD_REQUEST, &format!("{e:#}")),
            }
        }
        ("POST", "/_unlinkable/close") => {
            proxy.close_all().await;
            Json(json!({"closed": true})).into_response()
        }
        _ => text(StatusCode::NOT_FOUND, "no such control endpoint"),
    }
}

pub fn router(proxy: Arc<Proxy>) -> Router {
    Router::new().fallback(handle).with_state(proxy)
}

/// Serve until `shutdown` resolves, closing idle sessions as it goes and
/// every session at the end.
pub async fn run(
    listener: tokio::net::TcpListener,
    proxy: Arc<Proxy>,
    shutdown: impl std::future::Future<Output = ()> + Send + 'static,
) -> Result<()> {
    let closer = proxy.clone();
    let ticker = tokio::spawn(async move {
        let mut tick = tokio::time::interval(Duration::from_secs(5));
        let mut n: u64 = 0;
        loop {
            tick.tick().await;
            closer.watch_helper().await;
            closer.close_idle().await;
            if n.is_multiple_of(12) {
                closer.refill(false).await;
            }
            n += 1;
        }
    });
    axum::serve(listener, router(proxy.clone()))
        .with_graceful_shutdown(shutdown)
        .await?;
    ticker.abort();
    proxy.close_all().await;
    Ok(())
}

const PAGE: &str = r#"<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>unlinkable-inference: what leaves this device</title>
<style>
body{font:15px/1.45 system-ui,sans-serif;background:#111;color:#ddd;margin:0;padding:1rem 1rem 6rem}
h1{font-size:1.2rem;margin:.2rem 0 1rem}h2{font-size:1rem;margin:1.5rem 0 .5rem;color:#aaa}
.card{background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;padding:.8rem;margin:.5rem 0}
code{color:#9cf}.peer{color:#fc9}.server{color:#9f9}.warn{color:#f99}
table{border-collapse:collapse;width:100%}td{padding:.35rem .4rem;border-top:1px solid #2a2a2a;vertical-align:top}
.dock{position:fixed;right:1rem;bottom:1rem;display:flex;gap:.5rem;flex-wrap:wrap;justify-content:flex-end}
button,input{font:inherit;border-radius:6px;border:1px solid #444;background:#222;color:#eee;padding:.5rem .8rem}
button{background:#2d4f7c;border-color:#3d6aa5}
</style></head><body>
<h1>What leaves this device</h1>
<div id="summary" class="card">Loading…</div>
<h2>Spent</h2><div id="spent" class="card">Loading…</div>
<h2>Open sessions</h2><div id="sessions" class="card"></div>
<h2>Last requests</h2><div class="card"><table id="traffic"></table></div>
<div class="dock">
<input id="key" placeholder="account API key" size="18">
<select id="amt"><option value="100">$1</option><option value="500" selected>$5</option><option value="2000">$20</option><option value="5000">$50</option><option value="10000">$100</option></select>
<button onclick="passes()">Get passes</button>
<button onclick="withdraw()">Withdraw into wallet</button>
</div>
<script>
const $=id=>document.getElementById(id);
const esc=s=>String(s).replace(/[&<>"]/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));
const usd=c=>'$'+(c/100).toFixed(2);
async function load(){
 const s=await (await fetch('/_unlinkable/status')).json();
 $('summary').innerHTML=`Server <code>${esc(s.server)}</code><br>`+
  `The server sees your IP address unless Tor or a VPN hides it. It never sees your account or how you paid.`+
  `<br>Keyset <code>${esc(s.keyset)}</code><br>Wallet ${usd(s.wallet.paid_cents)} paid, ${usd(s.wallet.promo_cents)} promo, in ${s.wallet.vouchers} vouchers`+
  `<br>Passes this month: ${s.wallet.passes}${s.wallet.passes?' ('+Object.entries(s.wallet.pass_labels).map(([l,n])=>esc(l)+' '+n).join(', ')+')':''}. A conversation presents one when the server asks for it.`;
 $('sessions').innerHTML=s.sessions.length?s.sessions.map(x=>`${x.requests} request${x.requests==1?'':'s'}, open ${x.age_seconds}s, idle ${x.idle_seconds}s`).join('<br>'):'None. Sessions close after '+s.settings.idle_close_seconds+'s without a request; change goes back to the wallet.';
 $('traffic').innerHTML=s.traffic.map(t=>`<tr><td>${new Date(t.at*1000).toLocaleTimeString()}</td><td><span class="server">server sees</span> ${esc(t.server_sees)}</td></tr>`).join('')||'<tr><td>Nothing sent yet.</td></tr>';
}
async function spent(){
 const list=(await (await fetch('/_unlinkable/spending')).json()).spent;
 const day=t=>new Date(t*1000).toLocaleDateString(undefined,{weekday:'short',month:'short',day:'numeric'});
 const m=x=>'$'+(x/100000).toFixed(x&&x<1000?4:2);
 const now=new Date(), today=day(now/1000), month=now.getFullYear()*12+now.getMonth();
 const days=new Map(); let t=0,mo=0;
 for(const e of list){const d=new Date(e.at*1000); if(day(e.at)===today)t+=e.millicents; if(d.getFullYear()*12+d.getMonth()===month)mo+=e.millicents; days.set(day(e.at),(days.get(day(e.at))||0)+e.millicents);}
 $('spent').innerHTML=`Today ${m(t)} · this month ${m(mo)}`+([...days].reverse().slice(0,30).map(([d,v])=>`<br><span style="color:#888">${d}</span> ${m(v)}`).join(''))+
  '<br><span style="color:#888">Kept on this device only: the server cannot tie these sessions to your account.</span>';
}
async function withdraw(){
 const r=await fetch('/_unlinkable/withdraw',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({api_key:$('key').value.trim(),amount_cents:+$('amt').value})});
 const j=await r.json(); alert(r.ok?'Added '+usd(j.withdrawn_cents)+' to the wallet':j.error.message); $('key').value=''; load();
}
async function passes(){
 const r=await fetch('/_unlinkable/passes',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({api_key:$('key').value.trim(),count:30})});
 const j=await r.json(); alert(r.ok?'Got '+j.got+' passes':j.error.message); $('key').value=''; load();
}
load(); setInterval(load,4000); spent(); setInterval(spent,30000);
</script></body></html>"#;