feat: add HTTP-over-I2P proxy to router daemon
check / check (macos-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 28s
check / check (windows-latest) (push) Failing after 26s

- Add --http-proxy/--proxy [host:port] (default 127.0.0.1:4444) to route .i2p and .b32.i2p HTTP requests over I2P streams
- Block clearnet hosts with 403 and reject CONNECT and https:// with 501
- Support ?i2paddresshelper= jump links that register host aliases
- Add `just proxy` target and -h/--help usage output
- Expire idle ratchet sessions after 300s and add RatchetSessionManager::drop_session for fresh NewSession handshakes
- Add NetDb::remove_lease_set2 and bound the outbound pending queue to 64 entries
This commit is contained in:
nandi committed 2026-11-28 01:46:36 -08:00
1 parent 54cfb2faa9
commit 14eae00f5c
15 files changed
+1941 -41

No files matched your search

+42 -1
View File
@@ -295,6 +295,7 @@ pub struct RatchetPeerSession {
mut local_next_priv : Bytes?
mut local_next_pub : Bytes?
mut remote_next_pub : Bytes?
mut last_used_sec : UInt32
}
///|
@@ -433,6 +434,7 @@ pub fn encrypt_new_session_reply(
local_next_priv: None,
local_next_pub: None,
remote_next_pub: None,
last_used_sec: 0U,
}
sess.refill_recv_window()
(w.to_bytes(), sess)
@@ -505,6 +507,7 @@ pub fn decrypt_new_session_reply(
local_next_priv: None,
local_next_pub: None,
remote_next_pub: None,
last_used_sec: 0U,
}
sess.refill_recv_window()
(blocks, sess)
@@ -642,6 +645,34 @@ fn RatchetSessionManager::find_session(
-1
}
///|
/// Drops any established or pending outbound ratchet state for `remote_static_pub`
/// so subsequent sends start a fresh Bound `NewSession` handshake.
pub fn RatchetSessionManager::drop_session(
self : RatchetSessionManager,
remote_static_pub : Bytes,
) -> Unit {
let mut si = 0
while si < self.sessions.length() {
if @crypto.ct_eq(self.sessions[si].remote_static_pub, remote_static_pub) {
let _ = self.sessions.remove(si)
} else {
si += 1
}
}
let mut pi = 0
while pi < self.outbound_pending.length() {
if @crypto.ct_eq(
self.outbound_pending[pi].1.remote_static_pub,
remote_static_pub,
) {
let _ = self.outbound_pending.remove(pi)
} else {
pi += 1
}
}
}
///|
/// Initiates a DH ratchet step (`NextKey` block type 7) for `remote_static_pub`,
/// returning `Some(NextKey(...))` if a session is established.
@@ -715,7 +746,13 @@ pub fn RatchetSessionManager::encrypt_to_peer(
) -> Bytes raise RatchetError {
let sidx = self.find_session(remote_static_pub)
if sidx >= 0 {
return self.sessions[sidx].encrypt_existing(blocks)
let sess = self.sessions[sidx]
if sess.last_used_sec != 0U && now_sec > sess.last_used_sec + 300U {
let _ = self.sessions.remove(sidx)
} else {
sess.last_used_sec = now_sec
return sess.encrypt_existing(blocks)
}
}
// Check if we owe this peer an NSR
for i = 0; i < self.inbound_pending.length(); i = i + 1 {
@@ -724,6 +761,7 @@ pub fn RatchetSessionManager::encrypt_to_peer(
let _ = self.inbound_pending.remove(i)
let ephem_b = generate_elligator_keypair(next_rand_32)
let (wire, sess) = encrypt_new_session_reply(p, ephem_b, blocks)
sess.last_used_sec = now_sec
self.sessions.push(sess)
return wire
}
@@ -754,6 +792,9 @@ pub fn RatchetSessionManager::encrypt_to_peer(
// Pre-register first 4 NSR tags so any reply matches
for _k = 0; _k < 4; _k = _k + 1 {
let (_n, tag, _k32) = p.nsr_tagset.next_entry()
while self.outbound_pending.length() >= 64 {
let _ = self.outbound_pending.remove(0)
}
self.outbound_pending.push((tag, p))
}
None => ()
+2
View File
@@ -108,6 +108,7 @@ pub struct RatchetPeerSession {
mut local_next_priv : Bytes?
mut local_next_pub : Bytes?
mut remote_next_pub : Bytes?
mut last_used_sec : UInt32
}
pub fn RatchetPeerSession::apply_dh_ratchet(Self, Bytes, Bytes, for_send~ : Bool) -> Unit raise RatchetError
pub fn RatchetPeerSession::encrypt_existing(Self, Array[RatchetBlock]) -> Bytes raise RatchetError
@@ -120,6 +121,7 @@ pub struct RatchetSessionManager {
}
pub fn RatchetSessionManager::create_next_key_block(Self, Bytes, () -> Bytes) -> RatchetBlock? raise RatchetError
pub fn RatchetSessionManager::decrypt_incoming(Self, Bytes) -> (Bytes?, Array[RatchetBlock]) raise RatchetError
pub fn RatchetSessionManager::drop_session(Self, Bytes) -> Unit
pub fn RatchetSessionManager::encrypt_to_peer(Self, Bytes, Array[RatchetBlock], UInt32, () -> Bytes) -> Bytes raise RatchetError
pub fn RatchetSessionManager::new(@noise.KeyPair) -> Self
+108
View File
@@ -190,3 +190,111 @@ test "Proposal 144 ECIES-X25519-AEAD-Ratchet NS -> NSR -> bidirectional ES + DH
}
assert_eq(wrong_failed, true)
}
///|
test "Proposal 144 ECIES Ratchet idle session expiry (>300s) and drop_session recovery" {
let alice_s = @noise.KeyPair::from_private(@crypto.sha256(b"alice-idle-s"))
let bob_s = @noise.KeyPair::from_private(@crypto.sha256(b"bob-idle-s"))
let alice_mgr = @ratchet.RatchetSessionManager::new(alice_s)
let bob_mgr = @ratchet.RatchetSessionManager::new(bob_s)
let rand_a = det_rand_gen(b'\x21')
let rand_b = det_rand_gen(b'\x61')
// Initial NS -> NSR -> ES at t=1700000100
let c1 = @ratchet.garlic_clove_block(
Local,
1U,
1700000600U,
Data(data=b"init"),
)
let w1 = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c1],
1700000100U,
rand_a,
)
let _ = bob_mgr.decrypt_incoming(w1)
let c2 = @ratchet.garlic_clove_block(
Local,
2U,
1700000600U,
Data(data=b"nsr"),
)
let w2 = bob_mgr.encrypt_to_peer(
alice_s.public_key,
[c2],
1700000101U,
rand_b,
)
let _ = alice_mgr.decrypt_incoming(w2)
// Encrypting within window at t=1700000105 uses ExistingSession (accepted by bob_mgr)
let c3 = @ratchet.garlic_clove_block(
Local,
3U,
1700000600U,
Data(data=b"es-alive"),
)
let w3 = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c3],
1700000105U,
rand_a,
)
let (_, dec3) = bob_mgr.decrypt_incoming(w3)
assert_eq(dec3, [c3])
// After >300s idle (t=1700000406 > 1700000105+300), alice initiates a fresh Bound NS
// that even a restarted responder with empty session state can decrypt
let bob_restarted = @ratchet.RatchetSessionManager::new(bob_s)
let c4 = @ratchet.garlic_clove_block(
Local,
4U,
1700000900U,
Data(data=b"after-idle"),
)
let w4 = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c4],
1700000406U,
rand_a,
)
let (from4, dec4) = bob_restarted.decrypt_incoming(w4)
assert_eq(from4, Some(alice_s.public_key))
assert_eq(dec4.length(), 2) // DateTime + c4
// Responder replies with NSR so alice establishes new session
let c5 = @ratchet.garlic_clove_block(
Local,
5U,
1700000900U,
Data(data=b"nsr2"),
)
let w5 = bob_restarted.encrypt_to_peer(
alice_s.public_key,
[c5],
1700000407U,
rand_b,
)
let (_, dec5) = alice_mgr.decrypt_incoming(w5)
assert_eq(dec5, [c5])
// Explicit drop_session also forces fresh Bound NS immediately
alice_mgr.drop_session(bob_s.public_key)
let bob_fresh = @ratchet.RatchetSessionManager::new(bob_s)
let c6 = @ratchet.garlic_clove_block(
Local,
6U,
1700000900U,
Data(data=b"after-drop"),
)
let w6 = alice_mgr.encrypt_to_peer(
bob_s.public_key,
[c6],
1700000408U,
rand_a,
)
let (from6, dec6) = bob_fresh.decrypt_incoming(w6)
assert_eq(from6, Some(alice_s.public_key))
assert_eq(dec6.length(), 2)
}