- NTCP2: queue outbound data frames and flush them without blocking (send_blocks_nb, send_i2np_nb, flush_nonblocking) - NTCP2: bound write_all with a deadline and set TCP keepalive and user timeout on peer sockets - Firefox extension 0.1.3: save settings as a patch, keep unsaved form fields from being overwritten on refresh, add timeouts to router API calls, and honor the interceptTorrents toggle in the content script
855 lines
27 KiB
MoonBit
855 lines
27 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")
|
|
}
|
|
|
|
///|
|
|
fn pump_read_until_bytes_contains(
|
|
sock : @ntcp2_net.TcpSocket,
|
|
rs : Array[@router.Router],
|
|
marker : String,
|
|
max_ticks~ : Int32 = 1500,
|
|
) -> String 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 @encoding.utf8_bytes_to_string(Bytes::from_array(out))
|
|
}
|
|
for j = 0; j < chunk.length(); j = j + 1 {
|
|
out.push(chunk[j])
|
|
}
|
|
}
|
|
let s = @encoding.utf8_bytes_to_string(Bytes::from_array(out)) catch {
|
|
_ => ""
|
|
}
|
|
if s.contains(marker) {
|
|
return s
|
|
}
|
|
}
|
|
fail("timed out waiting for marker \{marker}")
|
|
}
|
|
|
|
///|
|
|
test "Two-router HTTP-over-I2P proxy GET, POST, clearnet block, CONNECT block, and addresshelper" {
|
|
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 proxy_port = router_a.start_http_proxy(host="127.0.0.1", port=0)
|
|
assert_true(proxy_port > 0)
|
|
assert_eq(router_a.http_proxy_port(), proxy_port)
|
|
let rs = [router_a, router_b]
|
|
|
|
// Bob creates a hidden eepsite destination on Router B and opens SAM STREAM ACCEPT
|
|
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=bobhttp 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()
|
|
|
|
// 1. Proxied HTTP/1.1 GET over .b32.i2p -> NetDB -> ECIES Garlic -> Streaming
|
|
let bob_acc1 = sam_connect_and_hello(router_b.sam_port(), rs)
|
|
defer bob_acc1.close()
|
|
bob_acc1.write_all(b"STREAM ACCEPT ID=bobhttp SILENT=false\n")
|
|
assert_eq(pump_read_line(bob_acc1, rs), "STREAM STATUS RESULT=OK")
|
|
|
|
let p1 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
|
defer p1.close()
|
|
let req1 = "GET http://\{bob_b32}/hello?x=1 HTTP/1.1\r\nHost: \{bob_b32}\r\nCookie: sid=abc\r\nProxy-Connection: keep-alive\r\nReferer: http://clearnet.example.com/\r\nConnection: keep-alive\r\n\r\n"
|
|
p1.write_all(@encoding.string_to_utf8_bytes(req1))
|
|
|
|
let peer_b64_1 = pump_read_line(bob_acc1, rs)
|
|
assert_true(peer_b64_1.length() > 100)
|
|
let got_req1 = pump_read_until_bytes_contains(bob_acc1, rs, "\r\n\r\n")
|
|
assert_true(got_req1.has_prefix("GET /hello?x=1 HTTP/1.1\r\n"))
|
|
assert_true(got_req1.contains("Host: \{bob_b32}\r\n"))
|
|
assert_true(got_req1.contains("Cookie: sid=abc\r\n"))
|
|
assert_true(got_req1.contains("Connection: close\r\n"))
|
|
assert_true(!got_req1.contains("Proxy-Connection"))
|
|
assert_true(!got_req1.contains("clearnet.example.com"))
|
|
|
|
bob_acc1.write_all(
|
|
b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 9\r\n\r\npong-http",
|
|
)
|
|
bob_acc1.shutdown_write()
|
|
let resp1_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p1, rs))
|
|
assert_true(resp1_str.has_prefix("HTTP/1.1 200 OK\r\n"))
|
|
assert_true(resp1_str.has_suffix("pong-http"))
|
|
|
|
// 2. Proxied HTTP/1.1 POST over .b32.i2p alias with body forwarding & .i2p Referer
|
|
router_a.register_alias("bobsite.i2p", bob_b32)
|
|
let bob_acc2 = sam_connect_and_hello(router_b.sam_port(), rs)
|
|
defer bob_acc2.close()
|
|
bob_acc2.write_all(b"STREAM ACCEPT ID=bobhttp SILENT=false\n")
|
|
assert_eq(pump_read_line(bob_acc2, rs), "STREAM STATUS RESULT=OK")
|
|
|
|
let p2 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
|
defer p2.close()
|
|
p2.write_all(
|
|
b"POST http://bobsite.i2p/form HTTP/1.1\r\nHost: bobsite.i2p\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\nReferer: http://bobsite.i2p/\r\n\r\na=1&b=2",
|
|
)
|
|
let _ = pump_read_line(bob_acc2, rs)
|
|
let got_req2 = pump_read_until_bytes_contains(bob_acc2, rs, "a=1&b=2")
|
|
assert_true(got_req2.has_prefix("POST /form HTTP/1.1\r\n"))
|
|
assert_true(got_req2.contains("Referer: http://bobsite.i2p/\r\n"))
|
|
assert_true(got_req2.has_suffix("a=1&b=2"))
|
|
|
|
bob_acc2.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 7\r\n\r\npost-ok")
|
|
bob_acc2.shutdown_write()
|
|
let resp2_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p2, rs))
|
|
assert_true(resp2_str.has_suffix("post-ok"))
|
|
|
|
// 3. Clearnet host rejection (403 Forbidden)
|
|
let p3 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
|
defer p3.close()
|
|
p3.write_all(b"GET http://example.com/ HTTP/1.1\r\nHost: example.com\r\n\r\n")
|
|
let resp3_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p3, rs))
|
|
assert_true(resp3_str.has_prefix("HTTP/1.1 403 Forbidden\r\n"))
|
|
|
|
// 4. CONNECT rejection (501 Not Implemented)
|
|
let p4 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
|
defer p4.close()
|
|
p4.write_all(
|
|
b"CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n",
|
|
)
|
|
let resp4_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p4, rs))
|
|
assert_true(resp4_str.has_prefix("HTTP/1.1 501 Not Implemented\r\n"))
|
|
|
|
// 5. ?i2paddresshelper= jump link registration + 302 redirect
|
|
let p5 = @ntcp2_net.TcpSocket::connect("127.0.0.1", proxy_port)
|
|
defer p5.close()
|
|
let req5 = "GET http://jumpnew.i2p/page?foo=1&i2paddresshelper=\{bob_pub_b64}&bar=2 HTTP/1.1\r\nHost: jumpnew.i2p\r\n\r\n"
|
|
p5.write_all(@encoding.string_to_utf8_bytes(req5))
|
|
let resp5_str = @encoding.utf8_bytes_to_string(pump_read_until_eof(p5, rs))
|
|
assert_true(resp5_str.has_prefix("HTTP/1.1 302 Found\r\n"))
|
|
assert_true(
|
|
resp5_str.contains("Location: http://jumpnew.i2p/page?foo=1&bar=2\r\n"),
|
|
)
|
|
}
|
|
|
|
///|
|
|
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)
|
|
}
|
|
|
|
///|
|
|
fn test_http_api_request(
|
|
port : Int32,
|
|
rs : Array[@router.Router],
|
|
http_method : String,
|
|
path : String,
|
|
host_hdr : String,
|
|
headers : Array[(String, String)],
|
|
body : String,
|
|
) -> String raise {
|
|
let sock = @ntcp2_net.TcpSocket::connect("127.0.0.1", port)
|
|
defer sock.close()
|
|
let body_bytes = @encoding.string_to_utf8_bytes(body)
|
|
let sb = StringBuilder()
|
|
sb.write_string("\{http_method} \{path} HTTP/1.1\r\n")
|
|
sb.write_string("Host: \{host_hdr}\r\n")
|
|
for h in headers {
|
|
sb.write_string("\{h.0}: \{h.1}\r\n")
|
|
}
|
|
if body_bytes.length() > 0 {
|
|
sb.write_string("Content-Length: \{body_bytes.length()}\r\n")
|
|
}
|
|
sb.write_string("Connection: close\r\n\r\n")
|
|
if body.length() > 0 {
|
|
sb.write_string(body)
|
|
}
|
|
sock.write_all(@encoding.string_to_utf8_bytes(sb.to_string()))
|
|
let resp_bytes = pump_read_until_eof(sock, rs, max_ticks=1500)
|
|
@encoding.utf8_bytes_to_string(resp_bytes)
|
|
}
|
|
|
|
///|
|
|
test "2-router I2P DHT + BEP 9 Magnet + Streaming torrent download and HTTP API security" {
|
|
let seed_dir = "/tmp/moon_i2p_bt_seed"
|
|
let dl_dir = "/tmp/moon_i2p_bt_dl"
|
|
@ntcp2_net.mkdir_p(seed_dir)
|
|
@ntcp2_net.mkdir_p(dl_dir)
|
|
|
|
// Deterministic 40 KiB payload spanning 3 x 16 KiB pieces (16384 + 16384 + 8192)
|
|
let total_len = 40960
|
|
let raw_arr = FixedArray::make(total_len, b'\x00')
|
|
for i = 0; i < total_len; i = i + 1 {
|
|
raw_arr[i] = ((i * 17 + 5) % 251).to_byte()
|
|
}
|
|
let payload = Bytes::from_array(raw_arr)
|
|
@ntcp2_net.write_file_at(seed_dir + "/sample.bin", 0L, payload)
|
|
// Poison any prior download artifact so stale pieces cannot pass SHA-1
|
|
@ntcp2_net.write_file_at(
|
|
dl_dir + "/sample.bin",
|
|
0L,
|
|
Bytes::make(total_len, b'\xFF'),
|
|
)
|
|
|
|
let piece_hashes = @encoding.Writer::new(capacity=60)
|
|
piece_hashes.write_bytes(@crypto.sha1(payload[:16384].to_owned()))
|
|
piece_hashes.write_bytes(@crypto.sha1(payload[16384:32768].to_owned()))
|
|
piece_hashes.write_bytes(@crypto.sha1(payload[32768:].to_owned()))
|
|
let torrent_bytes = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(total_len.to_int64())),
|
|
(b"name", BBytes(b"sample.bin")),
|
|
(b"piece length", BInt(16384L)),
|
|
(b"pieces", BBytes(piece_hashes.to_bytes())),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
let meta = @bittorrent.parse_torrent(torrent_bytes)
|
|
let ih_hex = @encoding.hex_encode(meta.info_hash)
|
|
|
|
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_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_a.netdb.store_router_info(router_b.router_info)
|
|
let _ = router_b.netdb.store_router_info(router_a.router_info)
|
|
|
|
router_a.enable_torrent_engine(download_dir=seed_dir)
|
|
let seeded_ih = router_a.add_seed_torrent(torrent_bytes, seed_dir)
|
|
assert_eq(seeded_ih, ih_hex)
|
|
|
|
let a_hash = match router_a.torrent_session_hash() {
|
|
Some(h) => h
|
|
None => fail("router_a missing __torrent__ session hash")
|
|
}
|
|
let now_sec = @ntcp2_net.unix_time_sec()
|
|
let ls2_a = match router_a.netdb.get_lease_set2(a_hash, now_sec) {
|
|
Some(l) => l
|
|
None => fail("router_a missing __torrent__ LeaseSet2")
|
|
}
|
|
let _ = router_b.netdb.store_lease_set2(ls2_a, now_sec)
|
|
|
|
let api_port = router_b.start_torrent_api(
|
|
host="127.0.0.1",
|
|
port=0,
|
|
download_dir=dl_dir,
|
|
)
|
|
assert_true(api_port > 0)
|
|
let rs = [router_a, router_b]
|
|
|
|
// Security check 1: POST without X-Moon-I2P: 1 is rejected with 403
|
|
let unauth_resp = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/torrents/magnet",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json")],
|
|
"{\"uri\":\"magnet:?xt=urn:btih:\{ih_hex}\"}",
|
|
)
|
|
assert_true(unauth_resp.contains("403 Forbidden"))
|
|
|
|
// Security check 2: non-loopback Host header (DNS rebinding guard) is rejected with 403
|
|
let evil_host_resp = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"GET",
|
|
"/api/status",
|
|
"evil.example",
|
|
[],
|
|
"",
|
|
)
|
|
assert_true(evil_host_resp.contains("403 Forbidden"))
|
|
|
|
// Bootstrap Router B's I2P DHT onto Router A and add pure magnet URI (no trackers/peers)
|
|
let boot_resp = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/dht/bootstrap",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
|
"{\"b32\":\"\{router_a.torrent_b32_address()}\",\"port\":6881}",
|
|
)
|
|
assert_true(boot_resp.contains("200 OK"))
|
|
assert_true(boot_resp.contains("\"ok\":true"))
|
|
|
|
let mag_resp = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/torrents/magnet",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
|
"{\"uri\":\"magnet:?xt=urn:btih:\{ih_hex}&dn=sample.bin\"}",
|
|
)
|
|
assert_true(mag_resp.contains("200 OK"))
|
|
assert_true(mag_resp.contains(ih_hex))
|
|
|
|
// Step both routers until DHT discovery -> BEP 9 metadata -> full 3-piece download finishes
|
|
let mut completed = false
|
|
for iter = 0; iter < 60; iter = iter + 1 {
|
|
step_routers(rs, 25)
|
|
if iter % 4 == 0 {
|
|
let tlist = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"GET",
|
|
"/api/torrents",
|
|
"127.0.0.1:\{api_port}",
|
|
[],
|
|
"",
|
|
)
|
|
if tlist.contains("\"completed\":true") {
|
|
completed = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
assert_true(completed)
|
|
|
|
// Verify downloaded file matches original bytes bit-for-bit
|
|
let downloaded = @ntcp2_net.read_file_bytes(dl_dir + "/sample.bin")
|
|
assert_eq(downloaded, payload)
|
|
|
|
// Verify DHT routing table & stats recorded live exchanges
|
|
let status_json = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"GET",
|
|
"/api/status",
|
|
"127.0.0.1:\{api_port}",
|
|
[],
|
|
"",
|
|
)
|
|
assert_true(status_json.contains("\"completed_torrents\":1"))
|
|
assert_true(!status_json.contains("\"queries_sent\":0"))
|
|
assert_true(!status_json.contains("\"responses_recv\":0"))
|
|
assert_true(!status_json.contains("\"routing_table_nodes\":0"))
|
|
assert_true(!status_json.contains("\"peers_discovered\":0"))
|
|
|
|
let nodes_json = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"GET",
|
|
"/api/dht/nodes",
|
|
"127.0.0.1:\{api_port}",
|
|
[],
|
|
"",
|
|
)
|
|
assert_true(nodes_json.contains(router_a.torrent_b32_address()))
|
|
}
|
|
|
|
///|
|
|
test "POST /api/torrents/add accepts standard, URL-safe, and rejects invalid Base64" {
|
|
let dl_dir = "/tmp/moon_i2p_bt_b64_test"
|
|
@ntcp2_net.mkdir_p(dl_dir)
|
|
let r = @router.Router::start({
|
|
sam_host: "127.0.0.1",
|
|
sam_port: 0,
|
|
ntcp2_host: "127.0.0.1",
|
|
ntcp2_port: 0,
|
|
})
|
|
defer r.close()
|
|
let api_port = r.start_torrent_api(
|
|
host="127.0.0.1",
|
|
port=0,
|
|
download_dir=dl_dir,
|
|
)
|
|
let rs = [r]
|
|
|
|
let make_b64 = fn(tbytes : Bytes, to_std : Bool, strip_pad : Bool) -> String {
|
|
let i2p_b64 = @encoding.base64_encode(tbytes)
|
|
let sb = StringBuilder()
|
|
for ch in i2p_b64 {
|
|
if strip_pad && ch == '=' {
|
|
continue
|
|
}
|
|
if to_std {
|
|
match ch {
|
|
'-' => sb.write_char('+')
|
|
'~' => sb.write_char('/')
|
|
_ => sb.write_char(ch)
|
|
}
|
|
} else {
|
|
match ch {
|
|
'~' => sb.write_char('_')
|
|
_ => sb.write_char(ch)
|
|
}
|
|
}
|
|
}
|
|
sb.to_string()
|
|
}
|
|
|
|
let t1 = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(b"comment", BBytes(b"\xfb\xff\xbf\xfb\xff")),
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(16L)),
|
|
(b"name", BBytes(b"std-b64.bin")),
|
|
(b"piece length", BInt(16384L)),
|
|
(b"pieces", BBytes(Bytes::make(20, b'\xfb'))),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
let ih1 = @encoding.hex_encode(@bittorrent.parse_torrent(t1).info_hash)
|
|
let std_b64 = make_b64(t1, true, false)
|
|
assert_true(std_b64.contains("+"))
|
|
assert_true(std_b64.contains("/"))
|
|
let resp1 = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/torrents/add",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
|
"{\"torrent_b64\":\"\{std_b64}\"}",
|
|
)
|
|
assert_true(resp1.contains("200 OK"))
|
|
assert_true(resp1.contains(ih1))
|
|
|
|
// Unpadded URL-safe Base64 (ensure length % 3 != 0 so stripping '=' exercises padding recovery)
|
|
let mut t2 = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(b"comment", BBytes(b"\xfb\xff\xbf\xfb\xff")),
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(16L)),
|
|
(b"name", BBytes(b"url-b64-a.bin")),
|
|
(b"piece length", BInt(16384L)),
|
|
(b"pieces", BBytes(Bytes::make(20, b'\xff'))),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
if t2.length() % 3 == 0 {
|
|
t2 = @bittorrent.bencode_encode(
|
|
BDict([
|
|
(b"comment", BBytes(b"\xfb\xff\xbf\xfb\xff")),
|
|
(
|
|
b"info",
|
|
BDict([
|
|
(b"length", BInt(16L)),
|
|
(b"name", BBytes(b"url-b64-ab.bin")),
|
|
(b"piece length", BInt(16384L)),
|
|
(b"pieces", BBytes(Bytes::make(20, b'\xff'))),
|
|
]),
|
|
),
|
|
]),
|
|
)
|
|
}
|
|
let ih2 = @encoding.hex_encode(@bittorrent.parse_torrent(t2).info_hash)
|
|
let url_b64 = make_b64(t2, false, true)
|
|
assert_true(url_b64.contains("-") && url_b64.contains("_"))
|
|
assert_true(!url_b64.contains("="))
|
|
let resp2 = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/torrents/add",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
|
"{\"torrent_b64\":\"\{url_b64}\"}",
|
|
)
|
|
assert_true(resp2.contains("200 OK"))
|
|
assert_true(resp2.contains(ih2))
|
|
|
|
let bad_resp = test_http_api_request(
|
|
api_port,
|
|
rs,
|
|
"POST",
|
|
"/api/torrents/add",
|
|
"127.0.0.1:\{api_port}",
|
|
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
|
"{\"torrent_b64\":\"not*valid*b64!\"}",
|
|
)
|
|
assert_true(bad_resp.contains("400 Bad Request"))
|
|
assert_true(bad_resp.contains("invalid torrent_b64"))
|
|
}
|