feat: implement NTCP2 protocol, native TCP transport, and live I2P interop
This commit is contained in:
1 parent
dff69dfac6
commit
e6d1c464dd
12 files changed
+2557
-2
No files matched your search
@@ -0,0 +1,134 @@
|
||||
///|
|
||||
/// Live interop test against local `i2pd`'s `router.info` AND real public I2P NTCP2 peers
|
||||
/// (when `/tmp/i2pd-interop/router.info` and `/tmp/i2pd-interop/seeds/peer_*.dat` exist).
|
||||
test "live i2pd RouterInfo Ed25519 verify + real I2P NTCP2 handshake + Data Phase" {
|
||||
// 1. Verify local i2pd's generated RouterInfo parsing & Ed25519 signature
|
||||
let local_i2pd_ri_bytes = @ntcp2_net.read_file_bytes(
|
||||
"/tmp/i2pd-interop/router.info",
|
||||
) catch {
|
||||
_ => return // skip when local i2pd interop fixture is not present
|
||||
}
|
||||
let local_i2pd_ri = @common.RouterInfo::decode(
|
||||
@encoding.Reader::new(local_i2pd_ri_bytes),
|
||||
)
|
||||
assert_eq(local_i2pd_ri.verify_signature(), true)
|
||||
let local_peer = @ntcp2_net.extract_ntcp2_peer(local_i2pd_ri)
|
||||
assert_eq(local_peer.static_key.length(), 32)
|
||||
assert_eq(local_peer.iv.length(), 16)
|
||||
|
||||
// 2. Create transient MoonBit router identity & signed RouterInfo
|
||||
let ed_seed = @ntcp2_net.random_bytes(32)
|
||||
let ed_pub = @crypto.ed25519_public_key(ed_seed)
|
||||
let static_priv = @ntcp2_net.random_bytes(32)
|
||||
let local_static = @noise.KeyPair::from_private(static_priv)
|
||||
let local_iv = @ntcp2_net.random_bytes(16)
|
||||
let local_id = @common.RouterIdentity::new_x25519_ed25519(
|
||||
local_static.public_key,
|
||||
ed_pub,
|
||||
)
|
||||
let addr_opts = @common.Mapping::from_entries([
|
||||
("i", @encoding.base64_encode(local_iv)),
|
||||
("s", @encoding.base64_encode(local_static.public_key)),
|
||||
("v", "2"),
|
||||
])
|
||||
let local_ri : @common.RouterInfo = {
|
||||
identity: local_id,
|
||||
published: @ntcp2_net.unix_time_ms(),
|
||||
addresses: [@common.RouterAddress::new(b'\x0e', "NTCP2", addr_opts)],
|
||||
options: @common.Mapping::from_entries([
|
||||
("caps", "LR"),
|
||||
("netId", "2"),
|
||||
("router.version", "0.9.64"),
|
||||
]),
|
||||
signature: b"",
|
||||
}
|
||||
let signed_local_ri = local_ri.sign_ed25519(ed_seed)
|
||||
assert_eq(signed_local_ri.verify_signature(), true)
|
||||
|
||||
// 3. Complete a REAL NTCP2 handshake + Data Phase exchange against a live public I2P peer
|
||||
let mut handshake_ok = false
|
||||
let mut data_phase_ok = false
|
||||
for idx = 0; idx < 15; idx = idx + 1 {
|
||||
let path = "/tmp/i2pd-interop/seeds/peer_\{idx}.dat"
|
||||
let raw = @ntcp2_net.read_file_bytes(path) catch { _ => continue }
|
||||
let peer_ri = @common.RouterInfo::decode(@encoding.Reader::new(raw)) catch {
|
||||
_ => continue
|
||||
}
|
||||
if !peer_ri.verify_signature() {
|
||||
continue
|
||||
}
|
||||
// Prefer a floodfill peer so DatabaseLookup elicits a real Data Phase I2NP reply
|
||||
match peer_ri.options.get("caps") {
|
||||
Some(caps) => if !caps.contains("f") { continue }
|
||||
None => continue
|
||||
}
|
||||
let peer = @ntcp2_net.extract_ntcp2_peer(peer_ri) catch { _ => continue }
|
||||
// Skip IPv6 addresses in IPv4-only VM
|
||||
if peer.host.contains(":") {
|
||||
continue
|
||||
}
|
||||
let conn = @ntcp2_net.dial_ntcp2(
|
||||
peer,
|
||||
local_static,
|
||||
signed_local_ri,
|
||||
flood=false,
|
||||
timeout_ms=5000,
|
||||
) catch {
|
||||
_ => continue
|
||||
}
|
||||
handshake_ok = true
|
||||
println(
|
||||
"HANDSHAKE OK with floodfill peer_\{idx} (\{peer.host}:\{peer.port})",
|
||||
)
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
let lookup_ri = @i2np.I2npPayload::DatabaseLookup(
|
||||
key=peer_ri.identity.hash(),
|
||||
from=local_id.hash(),
|
||||
flags=b'\x08', // direct reply, RouterInfo lookup
|
||||
reply_tunnel_id=None,
|
||||
excluded_peers=[],
|
||||
)
|
||||
let lookup_exp = @i2np.I2npPayload::DatabaseLookup(
|
||||
key=@common.Hash::sha256(b"moon-i2p-probe"),
|
||||
from=local_id.hash(),
|
||||
flags=b'\x0c', // direct reply, exploration lookup
|
||||
reply_tunnel_id=None,
|
||||
excluded_peers=[],
|
||||
)
|
||||
conn.send_blocks([
|
||||
DateTime(now_sec),
|
||||
@ntcp2.i2np_block(4242U, now_sec + 60U, lookup_ri),
|
||||
@ntcp2.i2np_block(4243U, now_sec + 60U, lookup_exp),
|
||||
Padding(b"\x00\x00\x00\x00"),
|
||||
]) catch {
|
||||
_ => {
|
||||
println("send_blocks failed")
|
||||
conn.close()
|
||||
continue
|
||||
}
|
||||
}
|
||||
// Read at least one decrypted Data Phase frame back from the real I2P router
|
||||
let blocks = conn.recv_blocks() catch {
|
||||
e => {
|
||||
match e {
|
||||
Timeout => println("peer_\{idx} recv_blocks: Timeout")
|
||||
Closed => println("peer_\{idx} recv_blocks: Closed")
|
||||
IoError(m) => println("peer_\{idx} recv_blocks: IoError(\{m})")
|
||||
Protocol(_) => println("peer_\{idx} recv_blocks: ProtocolError")
|
||||
_ => println("peer_\{idx} recv_blocks: OtherError")
|
||||
}
|
||||
[]
|
||||
}
|
||||
}
|
||||
conn.close()
|
||||
if blocks.length() > 0 {
|
||||
data_phase_ok = true
|
||||
println(
|
||||
"LIVE NTCP2 DATA PHASE OK with \{peer.host}:\{peer.port} (blocks=\{blocks.length()})",
|
||||
)
|
||||
break
|
||||
}
|
||||
}
|
||||
assert_eq(handshake_ok, true)
|
||||
assert_eq(data_phase_ok, true)
|
||||
}
|
||||
Reference in new issue
Block a user