feat: add public I2P network mode to router daemon
- 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:
1 parent
ae72b00323
commit
54cfb2faa9
25 files changed
+2033
-191
No files matched your search
+246
-20
@@ -11,6 +11,8 @@ priv struct PendingTunnelBuild {
|
||||
created_sec : UInt32
|
||||
expires_sec : UInt32
|
||||
started_ms : UInt64
|
||||
obep_garlic_key : Bytes?
|
||||
obep_garlic_tag : Bytes?
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -33,11 +35,15 @@ pub fn Router::has_tunnel(self : Router, tunnel_id : UInt32) -> Bool {
|
||||
/// Initiates a Proposal 157 `ShortTunnelBuild` along `hop_hashes` (1..8 routers).
|
||||
/// Returns the allocated local tunnel ID; drive completion with `Router::step()`
|
||||
/// and check readiness via `Router::has_tunnel(tid)`.
|
||||
/// When `reply_tunnel` is supplied for an `Outbound` tunnel, the OBEP encrypts the
|
||||
/// `ShortTunnelBuildReply` with `RGarlicKeyAndTag` and routes it via that Inbound tunnel.
|
||||
pub fn Router::build_tunnel(
|
||||
self : Router,
|
||||
hop_hashes : Array[@common.Hash],
|
||||
direction : @tunnel.TunnelDirection,
|
||||
expiration_sec~ : UInt32 = 600U,
|
||||
reply_tunnel~ : @tunnel.Tunnel? = None,
|
||||
outbound_via~ : @tunnel.Tunnel? = None,
|
||||
) -> UInt32 raise RouterError {
|
||||
let hop_count = hop_hashes.length()
|
||||
if hop_count < 1 || hop_count > 8 {
|
||||
@@ -56,6 +62,9 @@ pub fn Router::build_tunnel(
|
||||
Some(r) => r
|
||||
None => raise Protocol("missing RouterInfo for tunnel hop")
|
||||
}
|
||||
if ri.identity.inner.crypto_type() != X25519 {
|
||||
raise Protocol("tunnel hop lacks X25519 crypto identity type")
|
||||
}
|
||||
let x_pub = ri.identity.inner.crypto_public_key()
|
||||
if x_pub.length() != 32 {
|
||||
raise Protocol("tunnel hop lacks X25519 identity key")
|
||||
@@ -65,6 +74,20 @@ pub fn Router::build_tunnel(
|
||||
}
|
||||
|
||||
let total_slots = if hop_count <= 4 { 4 } else { 8 }
|
||||
let slot_perm : Array[Int32] = Array(capacity=total_slots)
|
||||
for s = 0; s < total_slots; s = s + 1 {
|
||||
slot_perm.push(s)
|
||||
}
|
||||
let rand_perm = @ntcp2_net.random_bytes(total_slots) catch {
|
||||
_ => Bytes::make(total_slots, b'\x01')
|
||||
}
|
||||
for k = total_slots - 1; k > 0; k = k - 1 {
|
||||
let ridx = rand_perm[k].to_int() % (k + 1)
|
||||
let tmp = slot_perm[k]
|
||||
slot_perm[k] = slot_perm[ridx]
|
||||
slot_perm[ridx] = tmp
|
||||
}
|
||||
|
||||
let specs : Array[@tunnel.ShortHopBuildSpec] = Array(capacity=hop_count)
|
||||
let next_tids : Array[UInt32] = Array(capacity=hop_count)
|
||||
for i = 0; i < hop_count; i = i + 1 {
|
||||
@@ -73,11 +96,19 @@ pub fn Router::build_tunnel(
|
||||
} else if direction == Inbound {
|
||||
local_tid
|
||||
} else {
|
||||
0U
|
||||
match reply_tunnel {
|
||||
Some(in_t) if in_t.hops.length() > 0 => in_t.hops[0].receive_tunnel_id
|
||||
_ => 0U
|
||||
}
|
||||
}
|
||||
next_tids.push(next_tid)
|
||||
let next_ident = if i + 1 < hop_count {
|
||||
hop_hashes[i + 1]
|
||||
} else if direction == Outbound {
|
||||
match reply_tunnel {
|
||||
Some(in_t) if in_t.hops.length() > 0 => in_t.hops[0].router_hash
|
||||
_ => self.router_hash()
|
||||
}
|
||||
} else {
|
||||
self.router_hash()
|
||||
}
|
||||
@@ -93,8 +124,13 @@ pub fn Router::build_tunnel(
|
||||
e => raise Noise(e)
|
||||
}
|
||||
let pad = @ntcp2_net.random_bytes(8) catch { _ => b"" }
|
||||
let hop_send_msg_id = if i + 1 < hop_count {
|
||||
self.alloc_msg_id()
|
||||
} else {
|
||||
build_msg_id
|
||||
}
|
||||
specs.push({
|
||||
slot_idx: i,
|
||||
slot_idx: slot_perm[i],
|
||||
hop_ident: hop_hashes[i],
|
||||
hop_static_pub: hop_static_pubs[i],
|
||||
ephemeral: ephem,
|
||||
@@ -106,15 +142,15 @@ pub fn Router::build_tunnel(
|
||||
layer_enc_type: b'\x00',
|
||||
request_time_min: req_time_min,
|
||||
request_expiration_sec: expiration_sec,
|
||||
send_message_id: build_msg_id,
|
||||
send_message_id: hop_send_msg_id,
|
||||
options: @common.Mapping::new(),
|
||||
},
|
||||
padding: pad,
|
||||
})
|
||||
}
|
||||
|
||||
let dummies : Array[Bytes] = []
|
||||
for _d = hop_count; _d < total_slots; _d = _d + 1 {
|
||||
let dummies : Array[Bytes] = Array(capacity=total_slots)
|
||||
for _d = 0; _d < total_slots; _d = _d + 1 {
|
||||
let dummy = @ntcp2_net.random_bytes(218) catch {
|
||||
_ => Bytes::make(218, b'\x5a')
|
||||
}
|
||||
@@ -137,6 +173,13 @@ pub fn Router::build_tunnel(
|
||||
iv_key: keys.iv_key,
|
||||
})
|
||||
}
|
||||
let last_keys = hop_slots_and_keys[hop_count - 1].1
|
||||
let (obep_garlic_key, obep_garlic_tag) = if direction == Outbound &&
|
||||
reply_tunnel is Some(_) {
|
||||
(last_keys.garlic_key, last_keys.garlic_tag)
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
self.pending_builds.push({
|
||||
tunnel_id: local_tid,
|
||||
direction,
|
||||
@@ -146,12 +189,59 @@ pub fn Router::build_tunnel(
|
||||
created_sec: now_sec,
|
||||
expires_sec: now_sec + expiration_sec,
|
||||
started_ms: now_ms,
|
||||
obep_garlic_key,
|
||||
obep_garlic_tag,
|
||||
})
|
||||
let _ = self.send_i2np_to_peer(
|
||||
hop_hashes[0],
|
||||
ShortTunnelBuild(records~),
|
||||
msg_id_override=Some(build_msg_id),
|
||||
)
|
||||
let build_payload : @i2np.I2npPayload = ShortTunnelBuild(records~)
|
||||
let outer_mid = if hop_count == 1 && direction == Inbound {
|
||||
Some(build_msg_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
match outbound_via {
|
||||
Some(out_t) if self.has_tunnel(out_t.id) => {
|
||||
let obep_differs = out_t.hops.length() == 0 ||
|
||||
out_t.hops[out_t.hops.length() - 1].router_hash != hop_hashes[0]
|
||||
if obep_differs {
|
||||
let ephem_n = @noise.KeyPair::from_private(next_rand_32()) catch {
|
||||
e => raise Noise(e)
|
||||
}
|
||||
let clove = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
build_msg_id,
|
||||
now_sec + 60U,
|
||||
build_payload,
|
||||
) catch {
|
||||
_ => raise Protocol("garlic_clove failed")
|
||||
}
|
||||
let enc = @ratchet.encrypt_router_garlic(hop_static_pubs[0], ephem_n, [
|
||||
DateTime(now_sec),
|
||||
clove,
|
||||
]) catch {
|
||||
_ => raise Protocol("encrypt_router_garlic failed")
|
||||
}
|
||||
self.send_through_tunnel(
|
||||
out_t.id,
|
||||
Router(router_hash=hop_hashes[0]),
|
||||
Garlic(data=enc),
|
||||
)
|
||||
} else {
|
||||
self.send_through_tunnel(
|
||||
out_t.id,
|
||||
Router(router_hash=hop_hashes[0]),
|
||||
build_payload,
|
||||
msg_id_override=outer_mid,
|
||||
)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
hop_hashes[0],
|
||||
build_payload,
|
||||
msg_id_override=outer_mid,
|
||||
)
|
||||
}
|
||||
}
|
||||
local_tid
|
||||
}
|
||||
|
||||
@@ -162,6 +252,7 @@ pub fn Router::send_through_tunnel(
|
||||
outbound_tunnel_id : UInt32,
|
||||
target : @tunnel.DeliveryTarget,
|
||||
payload : @i2np.I2npPayload,
|
||||
msg_id_override~ : UInt32? = None,
|
||||
) -> Unit raise RouterError {
|
||||
let mut out_tun : @tunnel.Tunnel? = None
|
||||
for t in self.tunnels.outbound {
|
||||
@@ -178,7 +269,10 @@ pub fn Router::send_through_tunnel(
|
||||
raise Protocol("outbound tunnel has zero hops")
|
||||
}
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let inner_mid = self.alloc_msg_id()
|
||||
let inner_mid = match msg_id_override {
|
||||
Some(id) => id
|
||||
None => self.alloc_msg_id()
|
||||
}
|
||||
let std_bytes = @i2np.encode_standard_message(
|
||||
inner_mid,
|
||||
now_ms + 60000UL,
|
||||
@@ -230,13 +324,30 @@ fn Router::try_complete_pending_tunnel_build(
|
||||
records,
|
||||
pb.hop_slots_and_keys,
|
||||
) catch {
|
||||
_ => return true
|
||||
}
|
||||
for c in codes {
|
||||
if c != b'\x00' {
|
||||
_ => {
|
||||
if self.debug {
|
||||
println("[debug] tunnel build \{pb.tunnel_id} reply decrypt failed")
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
let mut all_ok = true
|
||||
for hi = 0; hi < codes.length(); hi = hi + 1 {
|
||||
if codes[hi] != b'\x00' {
|
||||
all_ok = false
|
||||
if self.public_mode && hi < pb.hops.length() {
|
||||
self.mark_peer_failed(pb.hops[hi].router_hash, 45000UL)
|
||||
}
|
||||
if self.debug {
|
||||
println(
|
||||
"[debug] tunnel build \{pb.tunnel_id} hop \{hi} rejected code=\{codes[hi].to_int()}",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !all_ok {
|
||||
return true
|
||||
}
|
||||
let dir = pb.direction
|
||||
self.tunnels.add_tunnel({
|
||||
id: pb.tunnel_id,
|
||||
@@ -245,12 +356,82 @@ fn Router::try_complete_pending_tunnel_build(
|
||||
created_sec: pb.created_sec,
|
||||
expires_sec: pb.expires_sec,
|
||||
})
|
||||
if self.debug {
|
||||
let kind_s = match dir {
|
||||
Inbound => "Inbound"
|
||||
Outbound => "Outbound"
|
||||
}
|
||||
println(
|
||||
"[debug] built \{kind_s} tunnel \{pb.tunnel_id} (\{pb.hops.length()}h) [in=\{self.tunnels.inbound_count()} out=\{self.tunnels.outbound_count()}]",
|
||||
)
|
||||
}
|
||||
if dir == Inbound {
|
||||
self.republish_all_sessions()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::try_handle_build_garlic_reply(
|
||||
self : Router,
|
||||
garlic_data : Bytes,
|
||||
) -> Bool {
|
||||
if garlic_data.length() < 24 {
|
||||
return false
|
||||
}
|
||||
let wire_tag = garlic_data[:8].to_owned()
|
||||
for i = 0; i < self.pending_builds.length(); i = i + 1 {
|
||||
let pb = self.pending_builds[i]
|
||||
match (pb.obep_garlic_key, pb.obep_garlic_tag) {
|
||||
(Some(gk), Some(gtag)) =>
|
||||
if @crypto.ct_eq(wire_tag, gtag) {
|
||||
let blocks = @ratchet.decrypt_ecies_lookup_reply(
|
||||
gk, gtag, garlic_data,
|
||||
) catch {
|
||||
_ => return false
|
||||
}
|
||||
for blk in blocks {
|
||||
match blk {
|
||||
GarlicClove(i2np_short~, ..) => {
|
||||
let decoded = Some(
|
||||
@ratchet.decode_garlic_clove_i2np(i2np_short),
|
||||
) catch {
|
||||
_ => None
|
||||
}
|
||||
match decoded {
|
||||
Some((hdr, ShortTunnelBuildReply(records~))) => {
|
||||
let _ = hdr
|
||||
let _ = self.try_complete_pending_tunnel_build(
|
||||
pb.build_msg_id,
|
||||
records,
|
||||
)
|
||||
return true
|
||||
}
|
||||
Some((hdr, inner)) => {
|
||||
let _ = self.try_complete_pending_tunnel_build(
|
||||
pb.build_msg_id,
|
||||
match inner {
|
||||
ShortTunnelBuild(records~) => records
|
||||
_ => []
|
||||
},
|
||||
)
|
||||
let _ = hdr
|
||||
return true
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_short_tunnel_build(
|
||||
self : Router,
|
||||
@@ -292,10 +473,32 @@ fn Router::handle_short_tunnel_build(
|
||||
records=out_records,
|
||||
)
|
||||
if rec.next_tunnel_id != 0U {
|
||||
let outer_payload : @i2np.I2npPayload = if rec.next_ident !=
|
||||
self.router_hash() {
|
||||
match (keys.garlic_key, keys.garlic_tag) {
|
||||
(Some(gk), Some(gt)) => {
|
||||
let clove = @ratchet.garlic_clove_block(
|
||||
Local,
|
||||
rec.send_message_id,
|
||||
now_sec + 60U,
|
||||
reply_payload,
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
let enc = @ratchet.encrypt_ecies_lookup_reply(gk, gt, [clove]) catch {
|
||||
_ => return
|
||||
}
|
||||
Garlic(data=enc)
|
||||
}
|
||||
_ => reply_payload
|
||||
}
|
||||
} else {
|
||||
reply_payload
|
||||
}
|
||||
let std = @i2np.encode_standard_message(
|
||||
rec.send_message_id,
|
||||
@ntcp2_net.unix_time_ms() + 60000UL,
|
||||
reply_payload,
|
||||
outer_payload,
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
@@ -373,7 +576,14 @@ fn Router::consume_cleartext_cell(
|
||||
cleartext_cell : Bytes,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
let frags = @tunnel.unpack_tunnel_cell(cleartext_cell) catch { _ => return }
|
||||
let frags = @tunnel.unpack_tunnel_cell(cleartext_cell) catch {
|
||||
_ => {
|
||||
if self.debug {
|
||||
println("[debug] unpack_tunnel_cell checksum/format error")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
for frag in frags {
|
||||
match self.reassembler.push(frag) {
|
||||
Some((target, full_payload)) =>
|
||||
@@ -393,7 +603,14 @@ fn Router::handle_tunnel_data(
|
||||
// 1. Check if we are the inbound tunnel endpoint (IBEP)
|
||||
for in_t in self.tunnels.inbound {
|
||||
if in_t.id == tunnel_id {
|
||||
let clear = in_t.decrypt_inbound_cell(data) catch { _ => return }
|
||||
let clear = in_t.decrypt_inbound_cell(data) catch {
|
||||
_ => {
|
||||
if self.debug {
|
||||
println("[debug] inbound tunnel \{tunnel_id} decrypt error")
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
self.consume_cleartext_cell(clear, now_sec)
|
||||
return
|
||||
}
|
||||
@@ -469,10 +686,19 @@ fn Router::poll_tunnel_builds(self : Router) -> Unit {
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let _ = self.tunnels.prune_expired(now_sec)
|
||||
let build_timeout = if self.public_mode { 20000UL } else { 10000UL }
|
||||
let mut i = 0
|
||||
while i < self.pending_builds.length() {
|
||||
if now_ms > self.pending_builds[i].started_ms + 10000UL {
|
||||
let _ = self.pending_builds.remove(i)
|
||||
if now_ms > self.pending_builds[i].started_ms + build_timeout {
|
||||
let timed_out = self.pending_builds.remove(i)
|
||||
if self.public_mode {
|
||||
for h in timed_out.hops {
|
||||
self.mark_peer_failed(h.router_hash, 30000UL)
|
||||
}
|
||||
if self.debug {
|
||||
println("[debug] tunnel build \{timed_out.tunnel_id} timed out")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user