Skip to content

friglet: optional Tor (SOCKS5) for all P2P connections (SNB-857) - #87

Open
setavenger wants to merge 5 commits into
snb-856-broadcastfrom
snb-857-tor
Open

setavenger wants to merge 5 commits into
snb-856-broadcastfrom
snb-857-tor

Conversation

@setavenger

@setavenger setavenger commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Optional Tor for friglet: with p2p_proxy = "127.0.0.1:9050" (or --p2p-proxy, FRIGLET_P2P_PROXY, the tray's new "Tor proxy" field) every Bitcoin P2P connection goes through that SOCKS5 proxy: block downloads, headers, fee sampling, mempool checks, broadcasts, rechecks and peer discovery. Stacked on #86 (SNB-856). Implements SNB-857.

What

  • SOCKS5 dialer (blindbit-lib/src/net/socks5.rs, Dialer::Socks5): blocking RFC 1928/1929 client. Each connection offers no-auth and a username/password login with random credentials, so Tor puts it on a circuit of its own (each sender and observer of a broadcast sees a different exit). Fail-closed: with the proxy set nothing ever goes out directly, also when it is down.
  • No local DNS: the own node's host name (also .onion) goes to the proxy unresolved; an .onion node without a proxy is a config error. Seeding through the proxy connects by name to x9.<seed> (else the bare name) of 3 random seeds, asks that node for addresses (getaddr) and hangs up.
  • Proxy down: no peer is blamed, rounds end at once, and every error names p2p_proxy (block fetch, headers, broadcast, mempool checks).
  • Address book on disk (with or without proxy): peers-<network>-{new,tried}.book beside the wallet state (bitcoin-address-book FileExt format, 0600), loaded at start, saved every 10 min when changed and at exit; unreadable → warning, ignored; seeds asked at start only below 50 known addresses.
  • Tray: "Tor proxy" field under the node field; Save checks the proxy with a 2 s TCP connect and, with a proxy, does no DNS lookup of the node. The daemon warns when a settings save clears p2p_proxy (an older tray drops the field).
  • blindbit-cli --p2p-proxy, fetch_block --proxy, README "Tor" section (what is proxied; the oracle is not), AGENTS.md notes.

Deviations from the plan, and why

Measured live through Tor on signet: an address nobody answers at holds a connect for the whole 20 s timeout (Tor keeps retrying), and many gossiped addresses are days old and gone.

  • Racing connects: a block fetch from discovered peers through a proxy races 3 connects and uses the first to open (before: first block after 148 s, 7 dead peers in a row). Header requests race their first connection likewise (a tip header behind dead peers held a broadcast's answer for 40 s). Losing connects finish on blocking threads within the timeout.
  • Fresh-first seeding: a seed node's getaddr answer enters the book most recently seen first (half of it is > 5 days old).
  • Broadcast timing through a proxy (Timing::PROXIED): 6 observer connects at the start, more every 6 s while < 3 are ready; once senders start, more senders every 6 s while < 2 have handed it over, hand-overs capped at 2. Observer window 15 s (5), limit 30 s (12), pong wait 15 s (5). Budget 45 s instead of 25: Sparrow sends a request up to 5 times with read timeouts 3, 8, 16, 34, 34 s (0-2 s apart), so its requests end at about 3, 12, 29, 64 and 99 s and an answer at 45 s lands in the fourth with ~19 s to spare; a later resend is answered from the cached outcome. Direct timings are unchanged.
  • Senders keep listening after the pong for a parent request, bounded by the budget (as in friglet: broadcast to several peers, confirm via an unrelated peer (SNB-856) #86; their circuit is isolated).
  • No CONNECT reply in time, or SOCKS reply 0x01/0x06 (what Tor gives without a working circuit), only rests the peer, so a Tor outage cannot empty the saved address book. An empty FRIGLET_P2P_PROXY does not override the file's p2p_proxy.
  • The address book is the daemon's only (blindbit-cli is one-shot).

Verification

  • Gates: cargo build --workspace; cargo test --workspace (blindbit-lib 124 with integration's 10 testkit tests, friglet 106, friglet-ipc 30+1, friglet-tray 68+7, thread soak 1, console 1: all pass); CI clippy line clean; cargo fmt --check clean; new network tests 5x without failures.
  • New tests: SOCKS5 reply codes, proxy down / not SOCKS5, one login per connection; own node node.invalid reached by name through the proxy; seeding by x9.proxied-seed.invalid with no local lookup; proxy down or refusing → message names p2p_proxy and the fake node sees no connection; two broadcast senders (and every other connection) use different credentials and nothing bypasses the proxy; hedging and header racing past stalled addresses; address-book round trip and corrupt files; .onion without proxy rejected; empty env var.
  • Live, signet, Tor in Docker on 127.0.0.1:9050:
    • own node 152.53.151.148:38333 through Tor: old block 1.0 s, tip block 0.6 s;
    • random peers through Tor, cold start, first block: old block 3.6 / 3.7 / 5.2 / 12.2 / 43 s (median 5.2 s), tip 7.6 / 13.6 / 15.7 s;
    • daemon broadcast through Tor: re-broadcast of mempool tx 37181c9c… → accepted in 4.8 s, confirmed by observer 147.182.229.68:38333; old confirmed tx 2fec1b9b… → bad-txns-inputs-missingorspent after 42.3 s (both senders asked for a parent), never success;
    • ss -tnp: the daemon's sockets were only 127.0.0.1:9050 and the oracle (152.53.151.148:443); strace -f -e trace=network: connects only to 127.0.0.1:9050, the oracle and the local resolver, DNS queries only for signet.oracle.setor.dev; seed names appear only inside SOCKS CONNECT requests;
    • restart: the saved book (127-223 peers) loaded, no seeding.
  • The handshake through a proxy gets the proxied connect budget, not the 5-10 s direct one.

Known limits

  • blockchain.relayfee through Tor can time out once (2 s) and report the default 1 sat/vB while sampling continues.
  • Onion addresses from gossip are not dialled yet; an own node on the LAN is not reachable through Tor (README says to leave p2p_proxy empty for it or use its onion address).

setavenger and others added 5 commits November 1, 2026 15:47
Every P2P connection goes through the proxy when p2p_proxy is set: SOCKS5 dialer with fresh credentials per connection, seeding through the proxy by name, racing connects, and the address book saved beside the state file. Folds this branch's first four commits (SOCKS5 dialer and saved address book; p2p_proxy setting; docs; racing connects and fresh seed addresses) and their merge with SNB-856 into one commit while rebasing onto integration.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Every sender and observer connects through `peers.dialer()`, so through
  p2p_proxy each one logs in to Tor with its own credentials and gets a
  circuit of its own. Senders keep 856's handshake (relay off, height 0,
  placeholder addresses, shared user agent, no getaddr/feefilter) and keep
  listening after the pong for a parent request, bounded by the budget.
- Through a proxy a connection takes 1 to 5 s to a live peer and the full
  20 s to a dead one. Proxied timing: 6 observer connects at the start
  (3 directly), senders start when 3 are ready or after 15 s (5 s), no
  observer by 30 s (12 s) ends the broadcast, 12 observer tries (9).
  Budget 45 s (25 s): Sparrow's requests end at about 3, 12, 29, 64 and
  99 s (read timeouts 3, 8, 16, 34, 34 s, up to 5 attempts), so the
  answer lands in the fourth with ~19 s to spare.
- Mempool asks (rechecks, resends) race 2 extra connects through a proxy.
- Proxy down: no peer is blamed and the error names p2p_proxy.
- Tests: two senders and every other connection log in with different
  credentials and nothing bypasses the proxy; proxy down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Tor running without a working circuit answers no CONNECT in time (or
  replies 0x01): that only rests the peer now (`is_proxy_unclear`,
  `PeerEvent::Unhelpful`) instead of counting it unreachable, so an
  outage cannot empty the saved address book. A clear refusal (0x02-0x05)
  still counts.
- An empty FRIGLET_P2P_PROXY (a template default) no longer overrides
  the config file's p2p_proxy: that silently turned Tor off.
- Electrum connections through a proxy get at least 20 s for the
  handshake too, not only for the connect.
- The saved address book files are owner-only (0600).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Live on signet through Tor, 5 of the first 6 observers were dead
addresses: each held its connect for the full 20 s, and the broadcast
ended with "no Bitcoin peers reachable". A broadcast that succeeded in
2.5 s was then answered after 40 s, because the new-tip notification
first fetched a header from three dead peers in a row.

- Broadcast through a proxy: while fewer than 3 observers are ready,
  start more every 6 s; once senders start, likewise more senders while
  fewer than 2 have handed the transaction over. Hand-overs stay capped
  at 2 (`claim_hand_over`); a rejection no longer waits for hedged
  senders that can no longer get it. Pong wait 15 s through a proxy.
- Header requests through a proxy race their first connection over 3
  peers.
- Tests: hedging past stalled addresses (FakeSocks5) finishes in
  < 19 s with at most 2 hand-overs; headers race past stalled peers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…te (SNB-857)

CI's newer stable toolchain deprecates AtomicUsize::fetch_update in
favour of try_update, which older toolchains lack, so clippy -D warnings
failed there. A compare_exchange loop builds cleanly on both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant