feat: add public I2P network mode to router daemon
check / check (ubuntu-latest) (push) Failing after 26s
check / check (macos-latest) (push) Failing after 28s
check / check (windows-latest) (push) Failing after 29s

- Add `--public`, `--hops`, `--hosts`, and `--debug` router flags; `--public` auto-reseeds from the live network and keeps a status loop running.
- Route NetDB lookups through tunnels with Noise_N garlic wrapping, fan out to two floodfills, and fetch unknown RouterInfos in the background.
- Back off failed NTCP2 peers and fall back to IPv6 addresses when no IPv4 NTCP2 address is available.
- Size session LeaseSet2 expiry from lease end dates and use the destination's X25519 key when present.
- Add canonical Ed25519 Destination KeyCertificate constructors with round-trip tests.
- Fix DatabaseStore gzip handling to honor the length prefix, and send `Wget/1.11.4` as the reseed User-Agent.
This commit is contained in:
nandi committed 2026-11-28 00:11:28 -08:00
1 parent ae72b00323
commit 54cfb2faa9
25 files changed
+2033 -191

No files matched your search

+355
View File
@@ -0,0 +1,355 @@
///|
/// Autonomous public I2P network bootstrap: live `.su3` reseed, NAT-friendly
/// Proposal 157 Inbound/Outbound tunnel pool maintenance, `hosts.txt` aliases,
/// and periodic NetDB floodfill exploration.
///|
pub let default_reseed_urls : Array[String] = [
"https://reseed.stormycloud.org/i2pseeds.su3", "https://reseed.diva.exchange/i2pseeds.su3",
"https://i2p.novg.net/i2pseeds.su3", "https://reseed2.i2p.net/i2pseeds.su3",
]
///|
/// Reseeds `NetDb` from verified public I2P HTTPS reseed servers until >= 20
/// routers are imported or all built-in URLs have been tried.
pub fn Router::auto_reseed(self : Router) -> Int32 {
let mut total = 0
for url in default_reseed_urls {
let n = self.reseed_from_url(url) catch {
_ => {
println(" Reseed skipped (\{url})")
continue
}
}
println(" Reseeded \{n} peer(s) from \{url}")
total += n
if total >= 20 {
break
}
}
total
}
///|
/// Loads `hostname=base64dest` addressbook aliases from a local `hosts.txt` file.
pub fn Router::load_hosts_file(
self : Router,
path : String,
) -> Int32 raise RouterError {
let raw = @ntcp2_net.read_file_bytes(path) catch { e => raise Net(e) }
let text = @encoding.utf8_bytes_to_string(raw) catch {
_ => raise Protocol("hosts file is not valid UTF-8")
}
let mut count = 0
for line_view in text.split("\n") {
let line = line_view.to_owned().trim().to_owned()
if line.length() == 0 || line.has_prefix("#") {
continue
}
let mut eq = -1
for i = 0; i < line.length(); i = i + 1 {
if line[i] == '=' {
eq = i
break
}
}
if eq > 0 && eq + 1 < line.length() {
let host = line[:eq].to_owned().trim().to_owned()
let dest_b64 = line[eq + 1:].to_owned().trim().to_owned()
if host.length() > 0 && dest_b64.length() > 100 {
self.register_alias(host, dest_b64)
count += 1
}
}
}
count
}
///|
fn Router::is_usable_public_peer(
self : Router,
h : @common.Hash,
ri : @common.RouterInfo,
now_ms : UInt64,
) -> Bool {
if h == self.router_hash() ||
self.is_peer_backed_off(h, now_ms) ||
ri.identity.inner.crypto_type() != X25519 {
return false
}
let info = @ntcp2_net.extract_ntcp2_peer(ri) catch { _ => return false }
if info.host.contains(":") ||
info.host == "127.0.0.1" ||
info.host == "0.0.0.0" {
return false
}
true
}
///|
fn Router::is_inbound_build_pending(self : Router) -> Int32 {
let mut n = 0
for pb in self.pending_builds {
if pb.direction == Inbound {
n += 1
}
}
n
}
///|
fn Router::is_outbound_build_pending(self : Router) -> Int32 {
let mut n = 0
for pb in self.pending_builds {
if pb.direction == Outbound {
n += 1
}
}
n
}
///|
fn is_peer_in_list(h : @common.Hash, list : Array[@common.Hash]) -> Bool {
for x in list {
if x == h {
return true
}
}
false
}
///|
fn Router::pick_usable_peers(
self : Router,
count : Int32,
excluded : Array[@common.Hash],
now_ms : UInt64,
) -> Array[@common.Hash] {
let out : Array[@common.Hash] = []
let total = self.netdb.routers.length()
if total == 0 || count <= 0 {
return out
}
let rbytes = @ntcp2_net.random_bytes(4) catch { _ => Bytes::make(4, b'\x01') }
let start = u32_from_be_prefix(rbytes).reinterpret_as_int() % total
for step = 0; step < total && out.length() < count; step = step + 1 {
let idx = (start + step) % total
let (h, ri) = self.netdb.routers[idx]
if is_peer_in_list(h, excluded) || is_peer_in_list(h, out) {
continue
}
if self.is_usable_public_peer(h, ri, now_ms) {
out.push(h)
}
}
out
}
///|
fn Router::pick_connected_hop(
self : Router,
excluded : Array[@common.Hash],
now_ms : UInt64,
) -> @common.Hash? {
if self.peers.length() == 0 {
return None
}
let rbytes = @ntcp2_net.random_bytes(4) catch { _ => Bytes::make(4, b'\x01') }
let start = u32_from_be_prefix(rbytes).reinterpret_as_int() %
self.peers.length()
for step = 0; step < self.peers.length(); step = step + 1 {
let idx = (start + step) % self.peers.length()
let ph = self.peers[idx].hash
if is_peer_in_list(ph, excluded) || self.is_peer_backed_off(ph, now_ms) {
continue
}
match self.netdb.get_router_info(ph) {
Some(ri) =>
if ri.identity.inner.crypto_type() == X25519 {
return Some(ph)
}
None => ()
}
}
None
}
///|
fn Router::step_public_bootstrap(self : Router) -> Unit {
let now_ms = @ntcp2_net.unix_time_ms()
if now_ms < self.last_bootstrap_ms + 1500UL {
return
}
self.last_bootstrap_ms = now_ms
// 1. Keep up to 6 live outbound NTCP2 TCP peer sessions active
let target_peers = 6
if self.peers.length() + self.pending_dials.length() < target_peers {
let excl : Array[@common.Hash] = []
for p in self.peers {
excl.push(p.hash)
}
for pd in self.pending_dials {
excl.push(pd.peer_hash)
}
let need = target_peers -
(self.peers.length() + self.pending_dials.length())
let to_dial = self.pick_usable_peers(need, excl, now_ms)
for ph in to_dial {
self.ensure_peer_dial(ph)
}
}
// 2. Maintain >= 2 Inbound tunnels (behind NAT: 1-hop on connected peers first,
// then upgrade to `tunnel_hops`-hop `[IBGW -> connected_last_hop -> self]` once Outbound exists)
let pending_in = self.is_inbound_build_pending()
let mut in_full = 0
for t in self.tunnels.inbound {
if t.hops.length() >= self.tunnel_hops {
in_full += 1
}
}
let need_in_bootstrap = self.tunnels.inbound.length() + pending_in < 2
let need_in_upgrade = self.tunnel_hops >= 2 &&
self.tunnels.inbound.length() >= 1 &&
self.tunnels.outbound.length() >= 1 &&
in_full < 2 &&
self.tunnels.inbound.length() + pending_in < 4
if (need_in_bootstrap || need_in_upgrade) && pending_in < 2 {
let excl_in : Array[@common.Hash] = []
for t in self.tunnels.inbound {
if t.hops.length() > 0 {
excl_in.push(t.hops[t.hops.length() - 1].router_hash)
}
}
for pb in self.pending_builds {
if pb.direction == Inbound && pb.hops.length() > 0 {
excl_in.push(pb.hops[pb.hops.length() - 1].router_hash)
}
}
match self.pick_connected_hop(excl_in, now_ms) {
Some(h0) =>
if self.tunnel_hops >= 2 &&
self.tunnels.inbound.length() >= 1 &&
self.tunnels.outbound.length() >= 1 {
let out_t = self.tunnels.outbound[0]
let excl_ibgw : Array[@common.Hash] = [h0]
if out_t.hops.length() > 0 {
excl_ibgw.push(out_t.hops[out_t.hops.length() - 1].router_hash)
}
let ibgw_opt = match self.pick_connected_hop(excl_ibgw, now_ms) {
Some(c) => Some(c)
None => {
let usables = self.pick_usable_peers(1, excl_ibgw, now_ms)
if usables.length() == 1 {
Some(usables[0])
} else {
None
}
}
}
match ibgw_opt {
Some(ibgw) => {
let _ = self.build_tunnel(
[ibgw, h0],
Inbound,
outbound_via=Some(out_t),
) catch {
_ => 0U
}
}
None => ()
}
} else {
let _ = self.build_tunnel([h0], Inbound) catch { _ => 0U }
}
None => ()
}
}
// 3. Maintain >= 2 Outbound tunnels (replying via an active Inbound tunnel)
let pending_out = self.is_outbound_build_pending()
if self.tunnels.inbound.length() >= 1 &&
self.tunnels.outbound.length() + pending_out < 2 &&
pending_out < 3 {
let reply_in = self.tunnels.inbound[0]
if reply_in.hops.length() > 0 {
let reply_ibgw = reply_in.hops[0].router_hash
let excl_out : Array[@common.Hash] = [reply_ibgw]
for pb in self.pending_builds {
if pb.direction == Outbound {
for h in pb.hops {
excl_out.push(h.router_hash)
}
}
}
if self.tunnel_hops >= 2 {
match self.pick_connected_hop(excl_out, now_ms) {
Some(h0) => {
excl_out.push(h0)
let obep_opt = match self.pick_connected_hop(excl_out, now_ms) {
Some(c) => Some(c)
None => {
let obeps = self.pick_usable_peers(1, excl_out, now_ms)
if obeps.length() == 1 {
Some(obeps[0])
} else {
None
}
}
}
match obep_opt {
Some(obep) => {
let _ = self.build_tunnel(
[h0, obep],
Outbound,
reply_tunnel=Some(reply_in),
) catch {
_ => 0U
}
}
None => ()
}
}
None => ()
}
} else {
// Prefer an OBEP peer != reply_ibgw; if connected peer available use it, else any usable peer
match self.pick_connected_hop(excl_out, now_ms) {
Some(h0) => {
let _ = self.build_tunnel(
[h0],
Outbound,
reply_tunnel=Some(reply_in),
) catch {
_ => 0U
}
}
None => {
let obeps = self.pick_usable_peers(1, excl_out, now_ms)
if obeps.length() == 1 {
let _ = self.build_tunnel(
[obeps[0]],
Outbound,
reply_tunnel=Some(reply_in),
) catch {
_ => 0U
}
}
}
}
}
}
}
// 4. Periodic NetDB exploration once tunnels are up to learn additional live floodfills
if self.tunnels.inbound.length() >= 1 &&
self.tunnels.outbound.length() >= 1 &&
(self.last_explore_ms == 0UL || now_ms > self.last_explore_ms + 30000UL) {
self.last_explore_ms = now_ms
let rkey = @common.Hash::from_bytes(next_rand_32()) catch {
_ => self.router_hash()
}
self.start_background_lookup(rkey, 3)
}
}