Files
moon-i2p/cmd/router/main.mbt
T
nandi 54cfb2faa9
check / check (ubuntu-latest) (push) Failing after 26s
check / check (macos-latest) (push) Failing after 28s
check / check (windows-latest) (push) Failing after 29s
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.
2026-11-28 00:11:28 -08:00

249 lines
6.0 KiB
MoonBit

///|
fn parse_port(s : String) -> Int32? {
if s.length() == 0 {
return None
}
let mut v = 0
for i = 0; i < s.length(); i = i + 1 {
let c = s[i].to_int()
if c < 48 || c > 57 {
return None
}
v = v * 10 + (c - 48)
}
if v >= 0 && v <= 65535 {
Some(v)
} else {
None
}
}
///|
fn split_host_port(spec : String) -> (String, Int32)? {
let mut colon = -1
for i = 0; i < spec.length(); i = i + 1 {
if spec[i] == ':' {
colon = i
}
}
if colon <= 0 {
return None
}
let host = spec[:colon].to_owned()
let port_s = spec[colon + 1:].to_owned()
match parse_port(port_s) {
Some(p) => Some((host, p))
None => None
}
}
///|
fn main {
let args = @env.args()
let mut sam_host = "127.0.0.1"
let mut sam_port = 7656
let mut ntcp2_host = "127.0.0.1"
let mut ntcp2_port = 0
let mut floodfill = false
let mut check_clock_skew = true
let mut public_mode = false
let mut tunnel_hops = 1
let mut debug = false
let peer_files : Array[String] = []
let peer_dirs : Array[String] = []
let su3_files : Array[String] = []
let reseed_urls : Array[String] = []
let hosts_files : Array[String] = []
let mut i = 1
while i < args.length() {
match args[i] {
"--sam" =>
if i + 1 < args.length() {
match split_host_port(args[i + 1]) {
Some((h, p)) => {
sam_host = h
sam_port = p
}
None => println("warning: ignoring invalid --sam \{args[i + 1]}")
}
i += 2
} else {
i += 1
}
"--ntcp2-port" =>
if i + 1 < args.length() {
match parse_port(args[i + 1]) {
Some(p) => ntcp2_port = p
None => println("warning: ignoring invalid --ntcp2-port")
}
i += 2
} else {
i += 1
}
"--ntcp2-host" =>
if i + 1 < args.length() {
ntcp2_host = args[i + 1]
i += 2
} else {
i += 1
}
"--peer" =>
if i + 1 < args.length() {
peer_files.push(args[i + 1])
i += 2
} else {
i += 1
}
"--peer-dir" =>
if i + 1 < args.length() {
peer_dirs.push(args[i + 1])
i += 2
} else {
i += 1
}
"--su3" =>
if i + 1 < args.length() {
su3_files.push(args[i + 1])
i += 2
} else {
i += 1
}
"--reseed" =>
if i + 1 < args.length() {
reseed_urls.push(args[i + 1])
i += 2
} else {
i += 1
}
"--hosts" =>
if i + 1 < args.length() {
hosts_files.push(args[i + 1])
i += 2
} else {
i += 1
}
"--hops" =>
if i + 1 < args.length() {
match parse_port(args[i + 1]) {
Some(h) if h >= 1 && h <= 4 => tunnel_hops = h
_ => println("warning: ignoring invalid --hops \{args[i + 1]}")
}
i += 2
} else {
i += 1
}
"--public" => {
public_mode = true
i += 1
}
"--debug" => {
debug = true
i += 1
}
"--floodfill" => {
floodfill = true
i += 1
}
"--no-clock-skew" => {
check_clock_skew = false
i += 1
}
_ => i += 1
}
}
let r = @i2p_router.Router::start(
{ sam_host, sam_port, ntcp2_host, ntcp2_port, },
floodfill~,
check_clock_skew~,
public_mode~,
tunnel_hops~,
debug~,
) catch {
_ => {
println("error: failed to start moon-i2p router")
return
}
}
for pf in peer_files {
let _ = r.load_peer_file(pf) catch {
_ => {
println("warning: failed to load peer RouterInfo from \{pf}")
continue
}
}
}
for pd in peer_dirs {
let n = r.load_peer_dir(pd) catch {
_ => {
println("warning: failed to load peer dir \{pd}")
continue
}
}
println(" Loaded \{n} peer(s) from \{pd}")
}
for sf in su3_files {
let n = r.load_reseed_su3_file(sf) catch {
_ => {
println("warning: failed to load reseed .su3 from \{sf}")
continue
}
}
println(" Imported \{n} peer(s) from \{sf}")
}
for url in reseed_urls {
let n = r.reseed_from_url(url) catch {
_ => {
println("warning: reseed fetch failed for \{url}")
continue
}
}
println(" Reseeded \{n} peer(s) from \{url}")
}
if public_mode &&
reseed_urls.length() == 0 &&
su3_files.length() == 0 &&
peer_files.length() == 0 &&
peer_dirs.length() == 0 {
println("Reseeding from public I2P network...")
let _ = r.auto_reseed()
}
for hf in hosts_files {
let n = r.load_hosts_file(hf) catch {
_ => {
println("warning: failed to load hosts file \{hf}")
continue
}
}
println(" Loaded \{n} host alias(es) from \{hf}")
}
println("moon-i2p router started (transient identity)")
println(" Router Hash: \{r.router_hash().to_base64()}")
println(" Router B32: \{r.router_hash().to_b32_address()}")
println(
" NTCP2: \{ntcp2_host}:\{r.ntcp2_port()} (public=\{public_mode}, hops=\{tunnel_hops})",
)
println(" SAMv3: \{sam_host}:\{r.sam_port()}")
println(
" NetDB peers: \{r.netdb.router_count()} (floodfills=\{r.netdb.floodfill_count()})",
)
@ntcp2_net.flush_stdout()
if public_mode {
let mut last_log_ms = @ntcp2_net.unix_time_ms()
while true {
r.step(poll_ms=10)
let now_ms = @ntcp2_net.unix_time_ms()
if now_ms >= last_log_ms + 5000UL {
last_log_ms = now_ms
println(
"[public] peers=\{r.peer_count()} dials=\{r.pending_dial_count()} netdb=\{r.netdb.router_count()}(ff=\{r.netdb.floodfill_count()},ls=\{r.netdb.leaseset_count()}) tunnels=in:\{r.inbound_tunnel_count()}/out:\{r.outbound_tunnel_count()} builds=\{r.pending_build_count()}",
)
@ntcp2_net.flush_stdout()
}
}
} else {
r.run()
}
}