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
+321
-20
@@ -5,16 +5,20 @@
|
||||
///|
|
||||
priv struct PendingNetDbLookup {
|
||||
target_hash : @common.Hash
|
||||
lookup_type : Int32 // 1 = LeaseSet, 2 = RouterInfo, 3 = Exploration
|
||||
reply_key : Bytes
|
||||
reply_tag : Bytes
|
||||
queried_peers : Array[@common.Hash]
|
||||
candidate_peers : Array[@common.Hash]
|
||||
started_ms : UInt64
|
||||
kind : Int32 // 0 = NAMING LOOKUP (.b32), 1 = STREAM CONNECT awaiting LeaseSet2
|
||||
mut last_query_ms : UInt64
|
||||
kind : Int32 // 0 = NAMING LOOKUP (.b32), 1 = STREAM CONNECT awaiting LeaseSet2, 2 = background RI/exploration
|
||||
client_idx : Int32
|
||||
req_name : String
|
||||
session_id : String
|
||||
silent : Bool
|
||||
from_port : UInt32
|
||||
to_port : UInt32
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -57,16 +61,39 @@ fn Router::build_session_leaseset2(
|
||||
end_date_sec: now_sec + 600U,
|
||||
})
|
||||
}
|
||||
let enc_key = match sess.x25519_pub {
|
||||
Some(k) if k.length() == 32 => k
|
||||
_ => {
|
||||
let fk = sess.dest.inner.crypto_public_key()
|
||||
if fk.length() == 32 {
|
||||
fk
|
||||
} else {
|
||||
return None
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut max_end = now_sec + 60U
|
||||
for l in leases {
|
||||
if l.end_date_sec > max_end {
|
||||
max_end = l.end_date_sec
|
||||
}
|
||||
}
|
||||
let diff = if max_end > now_sec { max_end - now_sec } else { 60U }
|
||||
let expires_offset = if diff < 60U {
|
||||
60U
|
||||
} else if diff > 660U {
|
||||
660U
|
||||
} else {
|
||||
diff
|
||||
}
|
||||
let ls2 : @common.LeaseSet2 = {
|
||||
destination: sess.dest,
|
||||
published_sec: now_sec,
|
||||
expires_offset: 600U,
|
||||
expires_offset,
|
||||
flags: 0U,
|
||||
offline_sig: None,
|
||||
options: @common.Mapping::new(),
|
||||
encryption_keys: [
|
||||
{ key_type: X25519, key_data: sess.dest.inner.crypto_public_key(), },
|
||||
],
|
||||
encryption_keys: [{ key_type: X25519, key_data: enc_key, }],
|
||||
leases,
|
||||
signature: b"",
|
||||
}
|
||||
@@ -78,6 +105,9 @@ fn Router::build_session_leaseset2(
|
||||
///|
|
||||
fn Router::publish_session_leaseset2(self : Router, sess : SamSession) -> Unit {
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
if self.public_mode && self.tunnels.inbound.length() == 0 {
|
||||
return
|
||||
}
|
||||
let ls2 = match self.build_session_leaseset2(sess, now_sec) {
|
||||
Some(l) => l
|
||||
None => return
|
||||
@@ -100,7 +130,7 @@ fn Router::publish_session_leaseset2(self : Router, sess : SamSession) -> Unit {
|
||||
data=raw,
|
||||
)
|
||||
for ff in floodfills {
|
||||
let _ = self.send_i2np_to_peer(ff.identity.hash(), payload)
|
||||
self.send_routed_i2np(ff.identity.hash(), None, payload)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,19 +188,191 @@ fn Router::send_lookup_query(
|
||||
if !is_hash_in(peer_hash, lk.queried_peers) {
|
||||
lk.queried_peers.push(peer_hash)
|
||||
}
|
||||
// Proposal 154 ECIES encrypted reply (0x10) + LeaseSet lookup type (0x04)
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
lk.last_query_ms = now_ms
|
||||
let excl : Array[@common.Hash] = []
|
||||
let excl_cap = if lk.queried_peers.length() < 8 {
|
||||
lk.queried_peers.length()
|
||||
} else {
|
||||
8
|
||||
}
|
||||
for ei = lk.queried_peers.length() - excl_cap
|
||||
ei < lk.queried_peers.length()
|
||||
ei = ei + 1 {
|
||||
excl.push(lk.queried_peers[ei])
|
||||
}
|
||||
|
||||
let has_tunnels = self.tunnels.inbound.length() > 0 &&
|
||||
self.tunnels.outbound.length() > 0
|
||||
if has_tunnels && self.tunnels.inbound[0].hops.length() > 0 {
|
||||
let in_idx = lk.queried_peers.length() % self.tunnels.inbound.length()
|
||||
let in_t = if self.tunnels.inbound[in_idx].hops.length() > 0 {
|
||||
self.tunnels.inbound[in_idx]
|
||||
} else {
|
||||
self.tunnels.inbound[0]
|
||||
}
|
||||
let out_idx = lk.queried_peers.length() % self.tunnels.outbound.length()
|
||||
let out_t = self.tunnels.outbound[out_idx]
|
||||
let ibgw = in_t.hops[0]
|
||||
let flags_int = 0x01 | (lk.lookup_type << 2) | 0x10
|
||||
let lookup : @i2np.I2npPayload = DatabaseLookup(
|
||||
key=lk.target_hash,
|
||||
from=ibgw.router_hash,
|
||||
flags=flags_int.to_byte(),
|
||||
reply_tunnel_id=Some(ibgw.receive_tunnel_id),
|
||||
excluded_peers=excl,
|
||||
reply_key=Some(lk.reply_key),
|
||||
reply_tags=[lk.reply_tag],
|
||||
)
|
||||
let mut wrapped : @i2np.I2npPayload? = None
|
||||
match self.netdb.get_router_info(peer_hash) {
|
||||
Some(ri) =>
|
||||
if ri.identity.inner.crypto_type() == X25519 {
|
||||
let xpub = ri.identity.inner.crypto_public_key()
|
||||
if xpub.length() == 32 {
|
||||
let ephem_opt = Some(@noise.KeyPair::from_private(next_rand_32())) catch {
|
||||
_ => None
|
||||
}
|
||||
match ephem_opt {
|
||||
Some(ephem) => {
|
||||
let clove_opt = Some(
|
||||
@ratchet.garlic_clove_block(
|
||||
Local,
|
||||
self.alloc_msg_id(),
|
||||
now_sec + 60U,
|
||||
lookup,
|
||||
),
|
||||
) catch {
|
||||
_ => None
|
||||
}
|
||||
match clove_opt {
|
||||
Some(clove) => {
|
||||
let enc_opt = Some(
|
||||
@ratchet.encrypt_router_garlic(xpub, ephem, [
|
||||
DateTime(now_sec),
|
||||
clove,
|
||||
]),
|
||||
) catch {
|
||||
_ => None
|
||||
}
|
||||
match enc_opt {
|
||||
Some(enc) => wrapped = Some(Garlic(data=enc))
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
let wire_payload = wrapped.unwrap_or(lookup)
|
||||
let ok = try {
|
||||
self.send_through_tunnel(
|
||||
out_t.id,
|
||||
Router(router_hash=peer_hash),
|
||||
wire_payload,
|
||||
)
|
||||
true
|
||||
} catch {
|
||||
_ => false
|
||||
}
|
||||
if ok {
|
||||
if self.debug {
|
||||
println(
|
||||
"[debug] NetDB lookup type=\{lk.lookup_type} key=\{lk.target_hash.to_b32_address()} -> peer=\{peer_hash.to_b32_address()} via tunnel \{out_t.id} (reply_ibgw=\{ibgw.router_hash.to_b32_address()}:\{ibgw.receive_tunnel_id})",
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback direct query (used during bootstrap when 0 tunnels exist, or in 2-router tests)
|
||||
let flags_int = (lk.lookup_type << 2) | 0x10
|
||||
let lookup : @i2np.I2npPayload = DatabaseLookup(
|
||||
key=lk.target_hash,
|
||||
from=self.router_hash(),
|
||||
flags=b'\x14',
|
||||
flags=flags_int.to_byte(),
|
||||
reply_tunnel_id=None,
|
||||
excluded_peers=lk.queried_peers,
|
||||
excluded_peers=excl,
|
||||
reply_key=Some(lk.reply_key),
|
||||
reply_tags=[lk.reply_tag],
|
||||
)
|
||||
let _ = self.send_i2np_to_peer(peer_hash, lookup)
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::has_pending_lookup(
|
||||
self : Router,
|
||||
target_hash : @common.Hash,
|
||||
lookup_type : Int32,
|
||||
) -> Bool {
|
||||
for lk in self.pending_lookups {
|
||||
if lk.target_hash == target_hash && lk.lookup_type == lookup_type {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::start_background_lookup(
|
||||
self : Router,
|
||||
target_hash : @common.Hash,
|
||||
lookup_type : Int32,
|
||||
preferred_peer~ : @common.Hash? = None,
|
||||
) -> Unit {
|
||||
if self.has_pending_lookup(target_hash, lookup_type) {
|
||||
return
|
||||
}
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let peers = self.select_lookup_peers(target_hash, [], now_sec, 3)
|
||||
let ordered : Array[@common.Hash] = []
|
||||
match preferred_peer {
|
||||
Some(p) if p != self.router_hash() => ordered.push(p)
|
||||
_ => ()
|
||||
}
|
||||
for p in peers {
|
||||
if !is_hash_in(p, ordered) {
|
||||
ordered.push(p)
|
||||
}
|
||||
}
|
||||
if ordered.length() == 0 {
|
||||
return
|
||||
}
|
||||
let reply_key = next_rand_32()
|
||||
let tag_rand = @ntcp2_net.random_bytes(8) catch {
|
||||
_ => Bytes::make(8, b'\x77')
|
||||
}
|
||||
let lk : PendingNetDbLookup = {
|
||||
target_hash,
|
||||
lookup_type,
|
||||
reply_key,
|
||||
reply_tag: tag_rand,
|
||||
queried_peers: [],
|
||||
candidate_peers: [],
|
||||
started_ms: now_ms,
|
||||
last_query_ms: now_ms,
|
||||
kind: 2,
|
||||
client_idx: -1,
|
||||
req_name: "",
|
||||
session_id: "",
|
||||
silent: true,
|
||||
from_port: 0U,
|
||||
to_port: 0U,
|
||||
}
|
||||
self.pending_lookups.push(lk)
|
||||
self.send_lookup_query(lk, ordered[0])
|
||||
for i = 1; i < ordered.length(); i = i + 1 {
|
||||
lk.candidate_peers.push(ordered[i])
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::start_netdb_lookup(
|
||||
self : Router,
|
||||
@@ -180,9 +382,12 @@ fn Router::start_netdb_lookup(
|
||||
req_name : String,
|
||||
session_id : String,
|
||||
silent : Bool,
|
||||
from_port~ : UInt32 = 0U,
|
||||
to_port~ : UInt32 = 0U,
|
||||
) -> Bool {
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
let initial_peers = self.select_lookup_peers(target_hash, [], now_sec, 3)
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let initial_peers = self.select_lookup_peers(target_hash, [], now_sec, 6)
|
||||
if initial_peers.length() == 0 {
|
||||
return false
|
||||
}
|
||||
@@ -192,20 +397,31 @@ fn Router::start_netdb_lookup(
|
||||
}
|
||||
let lk : PendingNetDbLookup = {
|
||||
target_hash,
|
||||
lookup_type: 1, // LeaseSet lookup
|
||||
reply_key,
|
||||
reply_tag: tag_rand,
|
||||
queried_peers: [],
|
||||
candidate_peers: [],
|
||||
started_ms: @ntcp2_net.unix_time_ms(),
|
||||
started_ms: now_ms,
|
||||
last_query_ms: now_ms,
|
||||
kind,
|
||||
client_idx,
|
||||
req_name,
|
||||
session_id,
|
||||
silent,
|
||||
from_port,
|
||||
to_port,
|
||||
}
|
||||
self.pending_lookups.push(lk)
|
||||
self.send_lookup_query(lk, initial_peers[0])
|
||||
for i = 1; i < initial_peers.length(); i = i + 1 {
|
||||
let fanout = if self.public_mode && initial_peers.length() >= 2 {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
}
|
||||
for k = 0; k < fanout; k = k + 1 {
|
||||
self.send_lookup_query(lk, initial_peers[k])
|
||||
}
|
||||
for i = fanout; i < initial_peers.length(); i = i + 1 {
|
||||
lk.candidate_peers.push(initial_peers[i])
|
||||
}
|
||||
true
|
||||
@@ -223,11 +439,39 @@ fn Router::handle_database_store_msg(
|
||||
data : Bytes,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
let stype = store_type.to_int() & 0x0F
|
||||
let stored = self.netdb.handle_database_store(store_type, key, data, now_sec) catch {
|
||||
_ => false
|
||||
}
|
||||
if stored && (store_type.to_int() & 0x0F) == 3 {
|
||||
if self.debug && (stype == 3 || stored) {
|
||||
println(
|
||||
"[debug] NetDB DatabaseStore type=\{stype} stored=\{stored} key=\{key.to_b32_address()} (len=\{data.length()})",
|
||||
)
|
||||
}
|
||||
if stype == 3 && stored {
|
||||
self.complete_lookups_for_hash(key, now_sec)
|
||||
} else if stype == 0 && stored {
|
||||
// Complete any background RouterInfo lookups for `key` and feed new floodfill into active LS lookups
|
||||
let mut idx = 0
|
||||
while idx < self.pending_lookups.length() {
|
||||
let lk = self.pending_lookups[idx]
|
||||
if lk.kind == 2 && lk.lookup_type == 2 && lk.target_hash == key {
|
||||
let _ = self.pending_lookups.remove(idx)
|
||||
} else {
|
||||
idx += 1
|
||||
}
|
||||
}
|
||||
match self.netdb.get_router_info(key) {
|
||||
Some(ri) if @netdb.is_floodfill(ri) =>
|
||||
for lk in self.pending_lookups {
|
||||
if lk.lookup_type == 1 &&
|
||||
!is_hash_in(key, lk.queried_peers) &&
|
||||
!is_hash_in(key, lk.candidate_peers) {
|
||||
lk.candidate_peers.push(key)
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
if reply_token != 0U {
|
||||
let status : @i2np.I2npPayload = DeliveryStatus(
|
||||
@@ -345,6 +589,13 @@ fn Router::handle_database_search_reply_msg(
|
||||
peer_hashes : Array[@common.Hash],
|
||||
from : @common.Hash,
|
||||
) -> Unit {
|
||||
if self.debug {
|
||||
println(
|
||||
"[debug] NetDB DatabaseSearchReply key=\{key.to_b32_address()} refs=\{peer_hashes.length()} from=\{from.to_b32_address()}",
|
||||
)
|
||||
}
|
||||
let max_queries = if self.public_mode { 16 } else { 8 }
|
||||
let unknown_to_fetch : Array[@common.Hash] = []
|
||||
for lk in self.pending_lookups {
|
||||
if lk.target_hash == key {
|
||||
if !is_hash_in(from, lk.queried_peers) {
|
||||
@@ -353,12 +604,18 @@ fn Router::handle_database_search_reply_msg(
|
||||
for ph in peer_hashes {
|
||||
if ph != self.router_hash() &&
|
||||
!is_hash_in(ph, lk.queried_peers) &&
|
||||
!is_hash_in(ph, lk.candidate_peers) &&
|
||||
self.netdb.get_router_info(ph) is Some(_) {
|
||||
lk.candidate_peers.push(ph)
|
||||
!is_hash_in(ph, lk.candidate_peers) {
|
||||
if self.netdb.get_router_info(ph) is Some(_) {
|
||||
lk.candidate_peers.push(ph)
|
||||
} else if lk.lookup_type != 2 &&
|
||||
unknown_to_fetch.length() < 4 &&
|
||||
!is_hash_in(ph, unknown_to_fetch) {
|
||||
unknown_to_fetch.push(ph)
|
||||
}
|
||||
}
|
||||
}
|
||||
while lk.candidate_peers.length() > 0 && lk.queried_peers.length() < 8 {
|
||||
while lk.candidate_peers.length() > 0 &&
|
||||
lk.queried_peers.length() < max_queries {
|
||||
let next_peer = lk.candidate_peers.remove(0)
|
||||
if !is_hash_in(next_peer, lk.queried_peers) {
|
||||
self.send_lookup_query(lk, next_peer)
|
||||
@@ -367,6 +624,9 @@ fn Router::handle_database_search_reply_msg(
|
||||
}
|
||||
}
|
||||
}
|
||||
for ph in unknown_to_fetch {
|
||||
self.start_background_lookup(ph, 2, preferred_peer=Some(from))
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -454,9 +714,28 @@ fn Router::complete_lookups_for_hash(
|
||||
fn Router::poll_netdb_lookups(self : Router) -> Unit {
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let timeout_ms = if self.public_mode { 25000UL } else { 8000UL }
|
||||
let max_queries = if self.public_mode { 16 } else { 8 }
|
||||
let mut i = 0
|
||||
while i < self.pending_lookups.length() {
|
||||
let lk = self.pending_lookups[i]
|
||||
if lk.kind == 2 {
|
||||
if lk.lookup_type == 2 &&
|
||||
self.netdb.get_router_info(lk.target_hash) is Some(_) {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
continue
|
||||
}
|
||||
if now_ms > lk.started_ms + 15000UL {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
continue
|
||||
}
|
||||
if now_ms > lk.last_query_ms + 3000UL && lk.candidate_peers.length() > 0 {
|
||||
let next_p = lk.candidate_peers.remove(0)
|
||||
self.send_lookup_query(lk, next_p)
|
||||
}
|
||||
i += 1
|
||||
continue
|
||||
}
|
||||
match self.netdb.get_lease_set2(lk.target_hash, now_sec) {
|
||||
Some(ls2) => {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
@@ -475,7 +754,29 @@ fn Router::poll_netdb_lookups(self : Router) -> Unit {
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
if now_ms > lk.started_ms + 8000UL {
|
||||
if self.public_mode &&
|
||||
lk.queried_peers.length() < max_queries &&
|
||||
now_ms > lk.last_query_ms + 2500UL {
|
||||
if lk.candidate_peers.length() > 0 {
|
||||
let next_p = lk.candidate_peers.remove(0)
|
||||
if !is_hash_in(next_p, lk.queried_peers) {
|
||||
self.send_lookup_query(lk, next_p)
|
||||
}
|
||||
} else {
|
||||
let fresh = self.select_lookup_peers(
|
||||
lk.target_hash,
|
||||
lk.queried_peers,
|
||||
now_sec,
|
||||
2,
|
||||
)
|
||||
if fresh.length() > 0 {
|
||||
self.send_lookup_query(lk, fresh[0])
|
||||
} else {
|
||||
lk.last_query_ms = now_ms
|
||||
}
|
||||
}
|
||||
}
|
||||
if now_ms > lk.started_ms + timeout_ms {
|
||||
let _ = self.pending_lookups.remove(i)
|
||||
if lk.client_idx < self.sam_clients.length() {
|
||||
let c = self.sam_clients[lk.client_idx]
|
||||
|
||||
Reference in new issue
Block a user