- 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.
359 lines
11 KiB
MoonBit
359 lines
11 KiB
MoonBit
///|
|
|
fn step_routers(rs : Array[@router.Router], count : Int32) -> Unit {
|
|
for _i = 0; _i < count; _i = _i + 1 {
|
|
for r in rs {
|
|
r.step(poll_ms=1)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn pump_read_line(
|
|
sock : @ntcp2_net.TcpSocket,
|
|
rs : Array[@router.Router],
|
|
max_ticks~ : Int32 = 1000,
|
|
) -> String raise {
|
|
for _i = 0; _i < max_ticks; _i = _i + 1 {
|
|
step_routers(rs, 1)
|
|
if sock.poll_readable(0) {
|
|
match sock.read_line() {
|
|
Some(l) => return l
|
|
None => fail("unexpected EOF while waiting for SAM line")
|
|
}
|
|
}
|
|
}
|
|
fail("timed out waiting for SAM line")
|
|
}
|
|
|
|
///|
|
|
fn pump_read_until_eof(
|
|
sock : @ntcp2_net.TcpSocket,
|
|
rs : Array[@router.Router],
|
|
max_ticks~ : Int32 = 1500,
|
|
) -> Bytes raise {
|
|
let out : Array[Byte] = []
|
|
for _i = 0; _i < max_ticks; _i = _i + 1 {
|
|
step_routers(rs, 1)
|
|
while sock.poll_readable(0) {
|
|
let chunk = sock.read_some(4096)
|
|
if chunk.length() == 0 {
|
|
return Bytes::from_array(out)
|
|
}
|
|
for j = 0; j < chunk.length(); j = j + 1 {
|
|
out.push(chunk[j])
|
|
}
|
|
}
|
|
}
|
|
fail("timed out waiting for EOF on stream socket")
|
|
}
|
|
|
|
///|
|
|
test "Router transient startup, self-signed RouterInfo, and embedded SAMv3 bridge" {
|
|
let r = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
assert_eq(r.router_info.verify_signature(), true)
|
|
assert_eq(r.netdb.router_count(), 1)
|
|
assert_true(r.sam_port() > 0)
|
|
assert_true(r.ntcp2_port() > 0)
|
|
|
|
// Connect to embedded SAMv3 bridge and run HELLO + DEST GENERATE + SESSION CREATE + NAMING LOOKUP
|
|
let sock = @ntcp2_net.TcpSocket::connect("127.0.0.1", r.sam_port())
|
|
r.step(poll_ms=50)
|
|
|
|
sock.write_all(b"HELLO VERSION MIN=3.1 MAX=3.3\n")
|
|
assert_eq(pump_read_line(sock, [r]), "HELLO REPLY RESULT=OK VERSION=3.3")
|
|
|
|
sock.write_all(b"DEST GENERATE SIGNATURE_TYPE=7\n")
|
|
let dest_line = pump_read_line(sock, [r])
|
|
assert_true(dest_line.has_prefix("DEST REPLY PUB="))
|
|
|
|
sock.write_all(
|
|
b"SESSION CREATE STYLE=STREAM ID=test1 DESTINATION=TRANSIENT\n",
|
|
)
|
|
let sess_line = pump_read_line(sock, [r])
|
|
assert_true(sess_line.has_prefix("SESSION STATUS RESULT=OK DESTINATION="))
|
|
assert_eq(r.netdb.leaseset_count(), 1)
|
|
|
|
sock.write_all(b"NAMING LOOKUP NAME=ME\n")
|
|
let lookup_line = pump_read_line(sock, [r])
|
|
assert_true(lookup_line.has_prefix("NAMING REPLY RESULT=OK NAME=ME VALUE="))
|
|
|
|
sock.close()
|
|
r.close()
|
|
}
|
|
|
|
///|
|
|
fn sam_connect_and_hello(
|
|
port : Int32,
|
|
rs : Array[@router.Router],
|
|
) -> @ntcp2_net.TcpSocket raise {
|
|
let sock = @ntcp2_net.TcpSocket::connect("127.0.0.1", port)
|
|
sock.write_all(b"HELLO VERSION MIN=3.1 MAX=3.3\n")
|
|
let reply = pump_read_line(sock, rs)
|
|
assert_eq(reply, "HELLO REPLY RESULT=OK VERSION=3.3")
|
|
sock
|
|
}
|
|
|
|
///|
|
|
fn extract_kv(line : String, key : String) -> String {
|
|
let prefix = key + "="
|
|
let tokens = line.split(" ").collect()
|
|
for t in tokens {
|
|
let ts = t.to_owned()
|
|
if ts.has_prefix(prefix) {
|
|
return ts[prefix.length():].to_owned()
|
|
}
|
|
}
|
|
""
|
|
}
|
|
|
|
///|
|
|
test "Two live routers end-to-end: NTCP2 + ECIES NetDB + Garlic Ratchet + Ed25519 Streaming" {
|
|
let router_b = @router.Router::start(
|
|
{
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
},
|
|
floodfill=true,
|
|
)
|
|
defer router_b.close()
|
|
let router_a = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_a.close()
|
|
let _ = router_a.netdb.store_router_info(router_b.router_info)
|
|
let _ = router_b.netdb.store_router_info(router_a.router_info)
|
|
let rs = [router_a, router_b]
|
|
|
|
// Bob opens SAM control session + STREAM ACCEPT on router B
|
|
let bob_ctrl = sam_connect_and_hello(router_b.sam_port(), rs)
|
|
defer bob_ctrl.close()
|
|
bob_ctrl.write_all(
|
|
b"SESSION CREATE STYLE=STREAM ID=bob DESTINATION=TRANSIENT\n",
|
|
)
|
|
let bob_sess = pump_read_line(bob_ctrl, rs)
|
|
assert_true(bob_sess.has_prefix("SESSION STATUS RESULT=OK"))
|
|
bob_ctrl.write_all(b"NAMING LOOKUP NAME=ME\n")
|
|
let bob_me = pump_read_line(bob_ctrl, rs)
|
|
let bob_pub_b64 = extract_kv(bob_me, "VALUE")
|
|
let bob_dest = @common.Destination::from_base64(bob_pub_b64)
|
|
let bob_b32 = bob_dest.b32_address()
|
|
|
|
let bob_acc = sam_connect_and_hello(router_b.sam_port(), rs)
|
|
defer bob_acc.close()
|
|
bob_acc.write_all(b"STREAM ACCEPT ID=bob SILENT=false\n")
|
|
assert_eq(pump_read_line(bob_acc, rs), "STREAM STATUS RESULT=OK")
|
|
|
|
// Alice creates SAM control session on router A and looks up Bob's .b32.i2p via live NTCP2 ECIES NetDB
|
|
let alice_ctrl = sam_connect_and_hello(router_a.sam_port(), rs)
|
|
defer alice_ctrl.close()
|
|
alice_ctrl.write_all(
|
|
b"SESSION CREATE STYLE=STREAM ID=alice DESTINATION=TRANSIENT\n",
|
|
)
|
|
let alice_sess = pump_read_line(alice_ctrl, rs)
|
|
assert_true(alice_sess.has_prefix("SESSION STATUS RESULT=OK"))
|
|
|
|
alice_ctrl.write_all(
|
|
@encoding.string_to_utf8_bytes("NAMING LOOKUP NAME=\{bob_b32}\n"),
|
|
)
|
|
let alice_lookup = pump_read_line(alice_ctrl, rs)
|
|
assert_true(alice_lookup.has_prefix("NAMING REPLY RESULT=OK"))
|
|
assert_eq(extract_kv(alice_lookup, "VALUE"), bob_pub_b64)
|
|
|
|
// Alice connects over ECIES-X25519-AEAD-Ratchet Garlic + Ed25519 Streaming
|
|
let alice_data = sam_connect_and_hello(router_a.sam_port(), rs)
|
|
defer alice_data.close()
|
|
alice_data.write_all(
|
|
@encoding.string_to_utf8_bytes(
|
|
"STREAM CONNECT ID=alice DESTINATION=\{bob_pub_b64} SILENT=false\n",
|
|
),
|
|
)
|
|
assert_eq(pump_read_line(alice_data, rs), "STREAM STATUS RESULT=OK")
|
|
let seen_peer_b64 = pump_read_line(bob_acc, rs)
|
|
assert_true(seen_peer_b64.length() > 100)
|
|
|
|
// Bidirectional Streaming payload + close exchange
|
|
alice_data.write_all(b"hello-over-ecies-streaming")
|
|
alice_data.shutdown_write()
|
|
let got_on_bob = pump_read_until_eof(bob_acc, rs)
|
|
assert_eq(got_on_bob, b"hello-over-ecies-streaming")
|
|
|
|
bob_acc.write_all(b"ack-from-bob")
|
|
bob_acc.shutdown_write()
|
|
let got_on_alice = pump_read_until_eof(alice_data, rs)
|
|
assert_eq(got_on_alice, b"ack-from-bob")
|
|
}
|
|
|
|
///|
|
|
test "Three-router live Proposal 157 multi-hop tunnel build and fragmented 2500B TunnelData stream" {
|
|
let router_b = @router.Router::start(
|
|
{
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
},
|
|
floodfill=true,
|
|
)
|
|
defer router_b.close()
|
|
let router_c = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_c.close()
|
|
let router_a = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_a.close()
|
|
|
|
for r in [router_a, router_b, router_c] {
|
|
let _ = r.netdb.store_router_info(router_a.router_info)
|
|
let _ = r.netdb.store_router_info(router_b.router_info)
|
|
let _ = r.netdb.store_router_info(router_c.router_info)
|
|
}
|
|
let rs = [router_a, router_b, router_c]
|
|
|
|
// Router A builds 2-hop Outbound [B -> C] and 2-hop Inbound [B -> C -> A] tunnels via Proposal 157
|
|
let out_tid = router_a.build_tunnel(
|
|
[router_b.router_hash(), router_c.router_hash()],
|
|
Outbound,
|
|
)
|
|
let in_tid = router_a.build_tunnel(
|
|
[router_b.router_hash(), router_c.router_hash()],
|
|
Inbound,
|
|
)
|
|
let mut built = false
|
|
for _i = 0; _i < 1000; _i = _i + 1 {
|
|
step_routers(rs, 1)
|
|
if router_a.has_tunnel(out_tid) && router_a.has_tunnel(in_tid) {
|
|
built = true
|
|
break
|
|
}
|
|
}
|
|
assert_true(built)
|
|
assert_true(router_b.tunnels.transit_count() >= 2)
|
|
assert_true(router_c.tunnels.transit_count() >= 2)
|
|
|
|
// Router C hosts Bob; Router A hosts Alice (whose LS2 uses her Inbound [B -> C -> A] tunnel lease)
|
|
let bob_ctrl = sam_connect_and_hello(router_c.sam_port(), rs)
|
|
defer bob_ctrl.close()
|
|
bob_ctrl.write_all(
|
|
b"SESSION CREATE STYLE=STREAM ID=bob3 DESTINATION=TRANSIENT\n",
|
|
)
|
|
assert_true(
|
|
pump_read_line(bob_ctrl, rs).has_prefix("SESSION STATUS RESULT=OK"),
|
|
)
|
|
bob_ctrl.write_all(b"NAMING LOOKUP NAME=ME\n")
|
|
let bob_pub_b64 = extract_kv(pump_read_line(bob_ctrl, rs), "VALUE")
|
|
let bob_b32 = @common.Destination::from_base64(bob_pub_b64).b32_address()
|
|
|
|
let bob_acc = sam_connect_and_hello(router_c.sam_port(), rs)
|
|
defer bob_acc.close()
|
|
bob_acc.write_all(b"STREAM ACCEPT ID=bob3 SILENT=false\n")
|
|
assert_eq(pump_read_line(bob_acc, rs), "STREAM STATUS RESULT=OK")
|
|
|
|
let alice_ctrl = sam_connect_and_hello(router_a.sam_port(), rs)
|
|
defer alice_ctrl.close()
|
|
alice_ctrl.write_all(
|
|
b"SESSION CREATE STYLE=STREAM ID=alice3 DESTINATION=TRANSIENT\n",
|
|
)
|
|
assert_true(
|
|
pump_read_line(alice_ctrl, rs).has_prefix("SESSION STATUS RESULT=OK"),
|
|
)
|
|
alice_ctrl.write_all(
|
|
@encoding.string_to_utf8_bytes("NAMING LOOKUP NAME=\{bob_b32}\n"),
|
|
)
|
|
assert_eq(extract_kv(pump_read_line(alice_ctrl, rs), "VALUE"), bob_pub_b64)
|
|
|
|
let alice_data = sam_connect_and_hello(router_a.sam_port(), rs)
|
|
defer alice_data.close()
|
|
alice_data.write_all(
|
|
@encoding.string_to_utf8_bytes(
|
|
"STREAM CONNECT ID=alice3 DESTINATION=\{bob_pub_b64} SILENT=false\n",
|
|
),
|
|
)
|
|
assert_eq(pump_read_line(alice_data, rs), "STREAM STATUS RESULT=OK")
|
|
let _ = pump_read_line(bob_acc, rs)
|
|
|
|
// Send 2500 bytes (spanning multiple Streaming packets & fragmented 1024B TunnelData cells)
|
|
let big_payload = Bytes::make(2500, b'\x41')
|
|
alice_data.write_all(big_payload)
|
|
alice_data.shutdown_write()
|
|
assert_eq(pump_read_until_eof(bob_acc, rs), big_payload)
|
|
|
|
bob_acc.write_all(b"tunneled-ok")
|
|
bob_acc.shutdown_write()
|
|
assert_eq(pump_read_until_eof(alice_data, rs), b"tunneled-ok")
|
|
}
|
|
|
|
///|
|
|
test "Proposal 157 Outbound tunnel replying via Inbound tunnel with OBEP ECIES Garlic" {
|
|
let router_b = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_b.close()
|
|
let router_c = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_c.close()
|
|
let router_a = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer router_a.close()
|
|
|
|
for r in [router_a, router_b, router_c] {
|
|
let _ = r.netdb.store_router_info(router_a.router_info)
|
|
let _ = r.netdb.store_router_info(router_b.router_info)
|
|
let _ = r.netdb.store_router_info(router_c.router_info)
|
|
}
|
|
let rs = [router_a, router_b, router_c]
|
|
|
|
let in_tid = router_a.build_tunnel([router_b.router_hash()], Inbound)
|
|
for _i = 0; _i < 500; _i = _i + 1 {
|
|
step_routers(rs, 1)
|
|
if router_a.has_tunnel(in_tid) {
|
|
break
|
|
}
|
|
}
|
|
assert_true(router_a.has_tunnel(in_tid))
|
|
|
|
let reply_in = router_a.tunnels.inbound[0]
|
|
let out_tid = router_a.build_tunnel(
|
|
[router_c.router_hash()],
|
|
Outbound,
|
|
reply_tunnel=Some(reply_in),
|
|
)
|
|
let mut built_out = false
|
|
for _i = 0; _i < 800; _i = _i + 1 {
|
|
step_routers(rs, 1)
|
|
if router_a.has_tunnel(out_tid) {
|
|
built_out = true
|
|
break
|
|
}
|
|
}
|
|
assert_true(built_out)
|
|
}
|