feat: add non-blocking NTCP2 sends and Firefox settings sync
check / check (macos-latest) (push) Failing after 26s
check / check (ubuntu-latest) (push) Failing after 27s
check / check (windows-latest) (push) Failing after 30s

- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking)
- NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets
- Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
This commit is contained in:
nandi committed 2026-11-28 18:39:32 -08:00
1 parent 82aaa9f11e
commit 9aa15f9ae8
18 files changed
+1323 -239

No files matched your search

+90 -18
View File
@@ -192,9 +192,51 @@ fn Router::resolve_name_local(
None
}
///|
fn Router::has_active_bridge_to(
self : Router,
peer_hash : @common.Hash,
) -> Bool {
for br in self.remote_bridges {
if !br.closed && br.peer_hash == peer_hash && br.state == 1 && !br.local_eof {
return true
}
}
false
}
///|
fn Router::try_acquire_ratchet_reset(
self : Router,
peer_hash : @common.Hash,
now_ms : UInt64,
cooldown_ms : UInt64,
) -> Bool {
let mut i = 0
let mut recent = false
while i < self.ratchet_resets.length() {
if now_ms > self.ratchet_resets[i].1 + cooldown_ms {
let _ = self.ratchet_resets.remove(i)
} else {
if self.ratchet_resets[i].0 == peer_hash {
recent = true
}
i += 1
}
}
if recent {
return false
}
if self.ratchet_resets.length() >= 256 {
let _ = self.ratchet_resets.remove(0)
}
self.ratchet_resets.push((peer_hash, now_ms))
true
}
///|
fn send_sam_line(sock : @ntcp2_net.TcpSocket, line : String) -> Bool {
sock.write_all(@encoding.string_to_utf8_bytes(line + "\n")) catch {
sock.write_all(@encoding.string_to_utf8_bytes(line + "\n"), timeout_ms=500) catch {
_ => return false
}
true
@@ -243,9 +285,11 @@ fn Router::flush_stream_outbound(
_ => ()
}
let exp_sec = now_sec + 60U
let effective_send_ls2 = send_ls2 ||
!ratchet.has_active_session(peer_static_pub, now_sec~)
for idx = 0; idx < packets.length(); idx = idx + 1 {
let blocks : Array[@ratchet.RatchetBlock] = []
if send_ls2 && idx == 0 {
if effective_send_ls2 && idx == 0 {
match self.build_session_leaseset2(sess, now_sec) {
Some(ls2) => {
let ls_bytes = ls2.to_bytes() catch { _ => b"" }
@@ -287,7 +331,7 @@ fn Router::flush_stream_outbound(
}
if self.debug {
println(
"[debug] send Garlic (\{wire.length()}B, ls2=\{send_ls2}) -> gw=\{gw.to_b32_address()} tid=\{tid}",
"[debug] send Garlic (\{wire.length()}B, ls2=\{effective_send_ls2}) -> gw=\{gw.to_b32_address()} tid=\{tid}",
)
}
self.send_routed_i2np(gw, Some(tid), Garlic(data=wire))
@@ -436,7 +480,9 @@ fn Router::handle_incoming_garlic(
Some(r) => r
None => continue
}
let res = Some(ratchet.decrypt_incoming(garlic_data)) catch { _ => None }
let res = Some(ratchet.decrypt_incoming(garlic_data, now_sec~)) catch {
_ => None
}
match res {
Some((_remote_static, blocks)) => {
if self.debug {
@@ -1158,20 +1204,29 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
}
}
// 1b. Self-heal stalled SynSent streams by dropping potentially expired ECIES ratchet state
// before the next SYN retransmit so a fresh Bound NewSession + LeaseSet2 is sent
// 1b. Self-heal stalled SynSent streams by marking outbound ECIES send state stale
// so a fresh Bound NewSession + LeaseSet2 is sent without dropping active recv windows
for br in self.remote_bridges {
if br.state == 0 && !br.ratchet_reset && now_ms > br.started_ms + 7000UL {
br.ratchet_reset = true
if self.has_active_bridge_to(br.peer_hash) {
continue
}
match self.find_session_by_id(br.session_id) {
Some(sess) =>
match sess.ratchet {
Some(r) => {
r.drop_session(br.peer_static_pub)
if self.debug {
println(
"[debug] resetting ECIES ratchet for stalled SYN to \{br.peer_hash.to_b32_address()}",
)
let lrx = r.last_recv_sec(br.peer_static_pub)
let recent_rx = lrx != 0U && now_sec <= lrx + 8U
if r.has_active_session(br.peer_static_pub, now_sec~) &&
!recent_rx &&
self.try_acquire_ratchet_reset(br.peer_hash, now_ms, 12000UL) {
r.mark_send_stale(br.peer_static_pub)
if self.debug {
println(
"[debug] marking ECIES ratchet send-stale for stalled SYN to \{br.peer_hash.to_b32_address()}",
)
}
}
}
None => ()
@@ -1264,11 +1319,18 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
"[debug] Streaming failed/timed out to \{br.peer_hash.to_b32_address()}",
)
}
match sess.ratchet {
Some(r) => r.drop_session(br.peer_static_pub)
None => ()
if !self.has_active_bridge_to(br.peer_hash) {
match sess.ratchet {
Some(r) => {
let lrx = r.last_recv_sec(br.peer_static_pub)
if lrx == 0U || now_sec > lrx + 15U {
r.mark_send_stale(br.peer_static_pub)
let _ = self.netdb.remove_lease_set2(br.peer_hash)
}
}
None => ()
}
}
let _ = self.netdb.remove_lease_set2(br.peer_hash)
if br.is_http_proxy {
send_http_proxy_error(
br.sock,
@@ -1295,7 +1357,7 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
"[debug] stream \{br.local_stream_id} recv \{avail.length()}B",
)
}
br.sock.write_all(avail) catch {
br.sock.write_all(avail, timeout_ms=500) catch {
_ => {
br.local_eof = true
if br.closing_since_ms == 0UL {
@@ -1318,6 +1380,14 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
let _ = conn.close(now_ms) catch { _ => () }
}
}
if conn.state == Reset {
did_work = true
remove_stream_by_id(mgr, br.local_stream_id)
br.sock.close()
br.closed = true
let _ = self.remote_bridges.remove(ri)
continue
}
if br.pending_buf.length() > 0 {
if conn.state != Established && conn.state != SynSent {
br.pending_buf.clear()
@@ -1367,11 +1437,13 @@ fn Router::step_remote_bridges(self : Router) -> Bool {
br.lease_tid,
send_ls2=false,
)
if br.local_eof && conn.is_eof() {
if br.local_eof {
if br.closing_since_ms == 0UL {
br.closing_since_ms = now_ms
}
if !conn.has_unacked() || now_ms > br.closing_since_ms + 3000UL {
let done_gracefully = conn.is_eof() &&
(!conn.has_unacked() || now_ms > br.closing_since_ms + 3000UL)
if done_gracefully || now_ms > br.closing_since_ms + 5000UL {
did_work = true
remove_stream_by_id(mgr, br.local_stream_id)
br.sock.close()