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
+76
-10
@@ -47,6 +47,9 @@ pub struct Router {
|
||||
ntcp2_listener : @ntcp2_net.TcpListener
|
||||
sam_listener : @ntcp2_net.TcpListener
|
||||
check_clock_skew : Bool
|
||||
public_mode : Bool
|
||||
tunnel_hops : Int32
|
||||
debug : Bool
|
||||
priv mut next_msg_id : UInt32
|
||||
priv mut next_tunnel_id : UInt32
|
||||
priv mut next_stream_id : UInt32
|
||||
@@ -63,6 +66,11 @@ pub struct Router {
|
||||
priv bridges : Array[StreamBridge]
|
||||
priv remote_bridges : Array[RemoteStreamBridge]
|
||||
priv aliases : Array[(String, String)] // hostname -> pub_b64
|
||||
priv failed_peers : Array[(@common.Hash, UInt64)] // (router_hash, retry_after_ms)
|
||||
priv mut lease_rr : Int32
|
||||
priv mut outbound_rr : Int32
|
||||
priv mut last_bootstrap_ms : UInt64
|
||||
priv mut last_explore_ms : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -128,10 +136,15 @@ fn non_zero_random_bytes(n : Int32) -> Bytes {
|
||||
///|
|
||||
/// Starts the MoonBit I2P Router Core with transient keys, self-signed `RouterInfo`,
|
||||
/// NTCP2 TCP listener, and embedded SAMv3 TCP bridge.
|
||||
/// In `public_mode` with loopback/unspecified `ntcp2_host`, advertises an unpublished
|
||||
/// firewalled `LU` RouterInfo (required for outbound NTCP2 to live public routers behind NAT).
|
||||
pub fn Router::start(
|
||||
config : RouterConfig,
|
||||
floodfill~ : Bool = false,
|
||||
check_clock_skew~ : Bool = true,
|
||||
public_mode~ : Bool = false,
|
||||
tunnel_hops~ : Int32 = 1,
|
||||
debug~ : Bool = false,
|
||||
) -> Router raise RouterError {
|
||||
let ed_seed = @ntcp2_net.random_bytes(32) catch { e => raise Net(e) }
|
||||
let ed_pub = @crypto.ed25519_public_key(ed_seed) catch {
|
||||
@@ -142,6 +155,7 @@ pub fn Router::start(
|
||||
e => raise Noise(e)
|
||||
}
|
||||
let ntcp2_iv = @ntcp2_net.random_bytes(16) catch { e => raise Net(e) }
|
||||
let id_pad = @ntcp2_net.random_bytes(320) catch { e => raise Net(e) }
|
||||
let ntcp2_listener = @ntcp2_net.TcpListener::bind(
|
||||
config.ntcp2_host,
|
||||
config.ntcp2_port,
|
||||
@@ -160,21 +174,34 @@ pub fn Router::start(
|
||||
let id = @common.RouterIdentity::new_x25519_ed25519(
|
||||
static_key.public_key,
|
||||
ed_pub,
|
||||
padding=id_pad,
|
||||
) catch {
|
||||
e => raise Common(e)
|
||||
}
|
||||
let addr_opts = @common.Mapping::from_entries([
|
||||
("host", config.ntcp2_host),
|
||||
("i", @encoding.base64_encode(ntcp2_iv)),
|
||||
("port", "\{ntcp2_listener.local_port()}"),
|
||||
("s", @encoding.base64_encode(static_key.public_key)),
|
||||
("v", "2"),
|
||||
])
|
||||
let caps = if floodfill { "LRf" } else { "LR" }
|
||||
let is_firewalled = public_mode &&
|
||||
(config.ntcp2_host == "127.0.0.1" || config.ntcp2_host == "0.0.0.0")
|
||||
let addr_opts = if is_firewalled {
|
||||
@common.Mapping::from_entries([
|
||||
("caps", "4"),
|
||||
("s", @encoding.base64_encode(static_key.public_key)),
|
||||
("v", "2"),
|
||||
])
|
||||
} else {
|
||||
@common.Mapping::from_entries([
|
||||
("caps", "4"),
|
||||
("host", config.ntcp2_host),
|
||||
("i", @encoding.base64_encode(ntcp2_iv)),
|
||||
("port", "\{ntcp2_listener.local_port()}"),
|
||||
("s", @encoding.base64_encode(static_key.public_key)),
|
||||
("v", "2"),
|
||||
])
|
||||
}
|
||||
let addr_cost : Byte = if is_firewalled { b'\x0e' } else { b'\x03' }
|
||||
let caps = if floodfill { "LRf" } else if is_firewalled { "LU" } else { "LR" }
|
||||
let ri : @common.RouterInfo = {
|
||||
identity: id,
|
||||
published: @ntcp2_net.unix_time_ms(),
|
||||
addresses: [@common.RouterAddress::new(b'\x03', "NTCP2", addr_opts)],
|
||||
addresses: [@common.RouterAddress::new(addr_cost, "NTCP2", addr_opts)],
|
||||
options: @common.Mapping::from_entries([
|
||||
("caps", caps),
|
||||
("netId", "2"),
|
||||
@@ -191,6 +218,13 @@ pub fn Router::start(
|
||||
let m0 = u32_from_be_prefix(rseed[:4].to_owned()) | 1U
|
||||
let t0 = u32_from_be_prefix(rseed[4:8].to_owned()) % 0x0FFFFFFFU + 10000U
|
||||
let s0 = u32_from_be_prefix(rseed[8:12].to_owned()) % 0x0FFFFFFFU + 100U
|
||||
let clamped_hops = if tunnel_hops < 1 {
|
||||
1
|
||||
} else if tunnel_hops > 4 {
|
||||
4
|
||||
} else {
|
||||
tunnel_hops
|
||||
}
|
||||
{
|
||||
ed_seed,
|
||||
static_key,
|
||||
@@ -201,6 +235,9 @@ pub fn Router::start(
|
||||
ntcp2_listener,
|
||||
sam_listener,
|
||||
check_clock_skew,
|
||||
public_mode,
|
||||
tunnel_hops: clamped_hops,
|
||||
debug,
|
||||
next_msg_id: m0,
|
||||
next_tunnel_id: t0,
|
||||
next_stream_id: s0,
|
||||
@@ -217,9 +254,34 @@ pub fn Router::start(
|
||||
bridges: [],
|
||||
remote_bridges: [],
|
||||
aliases: [],
|
||||
failed_peers: [],
|
||||
lease_rr: 0,
|
||||
outbound_rr: 0,
|
||||
last_bootstrap_ms: 0UL,
|
||||
last_explore_ms: 0UL,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::inbound_tunnel_count(self : Router) -> Int32 {
|
||||
self.tunnels.inbound_count()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::outbound_tunnel_count(self : Router) -> Int32 {
|
||||
self.tunnels.outbound_count()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::pending_build_count(self : Router) -> Int32 {
|
||||
self.pending_builds.length()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::pending_dial_count(self : Router) -> Int32 {
|
||||
self.pending_dials.length()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Router::sam_port(self : Router) -> Int32 {
|
||||
self.sam_listener.local_port()
|
||||
@@ -404,7 +466,8 @@ fn Router::handle_i2np_message(
|
||||
TunnelGateway(tunnel_id~, data~) =>
|
||||
self.handle_tunnel_gateway(tunnel_id, data, now_sec)
|
||||
Garlic(data~) =>
|
||||
if !self.try_handle_lookup_garlic_reply(data, now_sec) {
|
||||
if !self.try_handle_build_garlic_reply(data) &&
|
||||
!self.try_handle_lookup_garlic_reply(data, now_sec) {
|
||||
self.handle_incoming_garlic(data, now_sec)
|
||||
}
|
||||
_ => ()
|
||||
@@ -448,6 +511,9 @@ pub fn Router::step(self : Router, poll_ms~ : Int32 = 10) -> Unit {
|
||||
}
|
||||
self.poll_netdb_lookups()
|
||||
self.poll_tunnel_builds()
|
||||
if self.public_mode {
|
||||
self.step_public_bootstrap()
|
||||
}
|
||||
if self.step_bridges() {
|
||||
did_work = true
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user