feat: add I2P BitTorrent engine, DHT, and Firefox torrent extension
- Add pure-MoonBit bittorrent package: bencode codec, metainfo/magnet parsing with path traversal checks, peer wire protocol (BEP 10/9), and I2P Kademlia DHT with KRPC and 54-byte compact nodes - Add SHA-1 primitive in crypto for torrent info hashes - Add router torrent API and engine, enabled with --torrent-api (default 127.0.0.1:4445) and --download-dir - Add Firefox WebExtension torrent manager with native messaging host and packed .xpi - Add scripts to build a standalone Firefox + router AppImage and install the native host - Add just targets (proxy, appimage, extension), README docs, and ignore .cache/ and dist/
This commit is contained in:
1 parent
14eae00f5c
commit
fbe6818c4e
37 files changed
+8543
-9
No files matched your search
@@ -504,3 +504,220 @@ test "Proposal 157 Outbound tunnel replying via Inbound tunnel with OBEP ECIES G
|
||||
}
|
||||
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()))
|
||||
}
|
||||
Reference in new issue
Block a user