feat: persist I2P torrent list to EDN state file across restarts
- Save active and paused torrents to torrents.edn on add, remove, pause, resume, and metadata fetch, then restore them on router startup. - Default path is $MOON_I2P_DATA_DIR, then $XDG_DATA_HOME, then ~/.local/share/moon-i2p-browser. Add --torrent-state <file> to override, or --no-torrent-state to disable. - Write the state file atomically (temp file, fsync, rename). A corrupt or unsupported file logs a warning and does not block startup. - Verify restored pieces incrementally in the background for large torrents; these show as state "checking" in /api/torrents. - Expose state_file in /api/status and the startup banner. - Add standard RFC 4648 Base64 encode/decode helpers, used for the info-b64 field. - Add the moonedn dependency and pass --torrent-state from the Android service.
This commit is contained in:
1 parent
b803e79199
commit
dd5b132b5a
17 files changed
+1105
-39
No files matched your search
@@ -852,3 +852,240 @@ test "POST /api/torrents/add accepts standard, URL-safe, and rejects invalid Bas
|
||||
assert_true(bad_resp.contains("400 Bad Request"))
|
||||
assert_true(bad_resp.contains("invalid torrent_b64"))
|
||||
}
|
||||
|
||||
///|
|
||||
test "torrent list persists and reloads from EDN state file across restarts" {
|
||||
let rand_hex = @encoding.hex_encode(@ntcp2_net.random_bytes(4))
|
||||
let base_dir = "/tmp/moon_i2p_edn_persist_test_\{rand_hex}"
|
||||
let dl_dir = "\{base_dir}/downloads"
|
||||
let state_file = "\{base_dir}/torrents.edn"
|
||||
@ntcp2_net.mkdir_p(dl_dir)
|
||||
|
||||
let make_single_torrent = fn(
|
||||
name : Bytes,
|
||||
payload : Bytes,
|
||||
) -> (Bytes, String) {
|
||||
let piece_len = 16384
|
||||
let hashes = @encoding.Writer::new()
|
||||
let mut off = 0
|
||||
while off < payload.length() {
|
||||
let end = if off + piece_len < payload.length() {
|
||||
off + piece_len
|
||||
} else {
|
||||
payload.length()
|
||||
}
|
||||
hashes.write_bytes(@crypto.sha1(payload[off:end].to_owned()))
|
||||
off = end
|
||||
}
|
||||
let tbytes = @bittorrent.bencode_encode(
|
||||
BDict([
|
||||
(b"announce", BBytes(b"http://tracker.insulaocculta.i2p/a")),
|
||||
(
|
||||
b"info",
|
||||
BDict([
|
||||
(b"length", BInt(payload.length().to_int64())),
|
||||
(b"name", BBytes(name)),
|
||||
(b"piece length", BInt(piece_len.to_int64())),
|
||||
(b"pieces", BBytes(hashes.to_bytes())),
|
||||
]),
|
||||
),
|
||||
]),
|
||||
)
|
||||
let meta = @bittorrent.parse_torrent(tbytes) catch {
|
||||
_ => abort("bad test torrent")
|
||||
}
|
||||
(tbytes, @encoding.hex_encode(meta.info_hash))
|
||||
}
|
||||
|
||||
let payload1 = Bytes::make(32768, b'\x5A')
|
||||
@ntcp2_net.write_file_at(dl_dir + "/complete.bin", 0L, payload1)
|
||||
let (t1_bytes, ih1) = make_single_torrent(b"complete.bin", payload1)
|
||||
let (t2_bytes, ih2) = make_single_torrent(
|
||||
b"paused.bin",
|
||||
Bytes::make(16384, b'\x33'),
|
||||
)
|
||||
let (t3_bytes, ih3) = make_single_torrent(
|
||||
b"removed.bin",
|
||||
Bytes::make(16384, b'\x44'),
|
||||
)
|
||||
let ih4 = "0123456789abcdef0123456789abcdef01234567"
|
||||
|
||||
let r1 = @router.Router::start({
|
||||
sam_host: "127.0.0.1",
|
||||
sam_port: 0,
|
||||
ntcp2_host: "127.0.0.1",
|
||||
ntcp2_port: 0,
|
||||
})
|
||||
let api1 = r1.start_torrent_api(
|
||||
host="127.0.0.1",
|
||||
port=0,
|
||||
download_dir=dl_dir,
|
||||
state_file~,
|
||||
)
|
||||
let rs1 = [r1]
|
||||
|
||||
for tb in [t1_bytes, t2_bytes, t3_bytes] {
|
||||
let b64 = @encoding.base64_std_encode(tb)
|
||||
let resp = test_http_api_request(
|
||||
api1,
|
||||
rs1,
|
||||
"POST",
|
||||
"/api/torrents/add",
|
||||
"127.0.0.1:\{api1}",
|
||||
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
||||
"{\"torrent_b64\":\"\{b64}\"}",
|
||||
)
|
||||
assert_true(resp.contains("200 OK"))
|
||||
}
|
||||
|
||||
let mag_resp = test_http_api_request(
|
||||
api1,
|
||||
rs1,
|
||||
"POST",
|
||||
"/api/torrents/magnet",
|
||||
"127.0.0.1:\{api1}",
|
||||
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
||||
"{\"uri\":\"magnet:?xt=urn:btih:\{ih4}&dn=pending-mag&tr=http://tracker.insulaocculta.i2p/a\"}",
|
||||
)
|
||||
assert_true(mag_resp.contains("200 OK"))
|
||||
assert_true(mag_resp.contains(ih4))
|
||||
|
||||
let pause_resp = test_http_api_request(
|
||||
api1,
|
||||
rs1,
|
||||
"POST",
|
||||
"/api/torrents/pause",
|
||||
"127.0.0.1:\{api1}",
|
||||
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
||||
"{\"info_hash\":\"\{ih2}\"}",
|
||||
)
|
||||
assert_true(pause_resp.contains("200 OK"))
|
||||
|
||||
let rm_resp = test_http_api_request(
|
||||
api1,
|
||||
rs1,
|
||||
"POST",
|
||||
"/api/torrents/remove",
|
||||
"127.0.0.1:\{api1}",
|
||||
[("Content-Type", "application/json"), ("X-Moon-I2P", "1")],
|
||||
"{\"info_hash\":\"\{ih3}\"}",
|
||||
)
|
||||
assert_true(rm_resp.contains("200 OK"))
|
||||
r1.close()
|
||||
|
||||
// Verify persisted EDN structure and contents on disk
|
||||
assert_true(@ntcp2_net.file_exists(state_file))
|
||||
let edn_text = @encoding.utf8_bytes_to_string(
|
||||
@ntcp2_net.read_file_bytes(state_file),
|
||||
)
|
||||
assert_true(!edn_text.contains(ih3))
|
||||
let limits : @edn.Limits = {
|
||||
input: 64 * 1024 * 1024,
|
||||
depth: 64,
|
||||
nodes: 1000000,
|
||||
token: 16 * 1024 * 1024,
|
||||
container: 100000,
|
||||
}
|
||||
let parsed = @edn.read(edn_text, limits~)
|
||||
match parsed {
|
||||
Mapping(root) => {
|
||||
let mut found_ver = false
|
||||
let mut count = -1
|
||||
for entry in root {
|
||||
match entry.0 {
|
||||
Keyword(k) if k == "version" =>
|
||||
match entry.1 {
|
||||
Integer(v) => {
|
||||
assert_eq(v, "1")
|
||||
found_ver = true
|
||||
}
|
||||
_ => fail("expected integer version")
|
||||
}
|
||||
Keyword(k) if k == "torrents" =>
|
||||
match entry.1 {
|
||||
Vector(items) => count = items.length()
|
||||
_ => fail("expected torrents vector")
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
assert_true(found_ver)
|
||||
assert_eq(count, 3)
|
||||
}
|
||||
_ => fail("expected EDN mapping at root")
|
||||
}
|
||||
|
||||
// Restart router 2 with the same state_file and verify full restoration
|
||||
let r2 = @router.Router::start({
|
||||
sam_host: "127.0.0.1",
|
||||
sam_port: 0,
|
||||
ntcp2_host: "127.0.0.1",
|
||||
ntcp2_port: 0,
|
||||
})
|
||||
defer r2.close()
|
||||
let api2 = r2.start_torrent_api(
|
||||
host="127.0.0.1",
|
||||
port=0,
|
||||
download_dir=dl_dir,
|
||||
state_file~,
|
||||
)
|
||||
let rs2 = [r2]
|
||||
|
||||
let status2 = test_http_api_request(
|
||||
api2,
|
||||
rs2,
|
||||
"GET",
|
||||
"/api/status",
|
||||
"127.0.0.1:\{api2}",
|
||||
[],
|
||||
"",
|
||||
)
|
||||
assert_true(status2.contains("\"torrent_count\":3"))
|
||||
assert_true(status2.contains("\"state_file\":\"\{state_file}\""))
|
||||
|
||||
let list2 = test_http_api_request(
|
||||
api2,
|
||||
rs2,
|
||||
"GET",
|
||||
"/api/torrents",
|
||||
"127.0.0.1:\{api2}",
|
||||
[],
|
||||
"",
|
||||
)
|
||||
assert_true(list2.contains(ih1))
|
||||
assert_true(list2.contains(ih2))
|
||||
assert_true(list2.contains(ih4))
|
||||
assert_true(!list2.contains(ih3))
|
||||
assert_true(list2.contains("\"downloaded_bytes\":32768"))
|
||||
assert_true(list2.contains("\"completed\":true"))
|
||||
assert_true(list2.contains("\"state\":\"paused\""))
|
||||
assert_true(list2.contains("\"state\":\"metadata\""))
|
||||
assert_true(list2.contains("\"name\":\"pending-mag\""))
|
||||
|
||||
// Verify corrupt EDN file does not crash startup
|
||||
let bad_state = "\{base_dir}/corrupt.edn"
|
||||
@ntcp2_net.write_file_atomic(bad_state, b"{broken edn")
|
||||
let r3 = @router.Router::start({
|
||||
sam_host: "127.0.0.1",
|
||||
sam_port: 0,
|
||||
ntcp2_host: "127.0.0.1",
|
||||
ntcp2_port: 0,
|
||||
})
|
||||
defer r3.close()
|
||||
let api3 = r3.start_torrent_api(
|
||||
host="127.0.0.1",
|
||||
port=0,
|
||||
download_dir=dl_dir,
|
||||
state_file=bad_state,
|
||||
)
|
||||
let status3 = test_http_api_request(
|
||||
api3,
|
||||
[r3],
|
||||
"GET",
|
||||
"/api/status",
|
||||
"127.0.0.1:\{api3}",
|
||||
[],
|
||||
"",
|
||||
)
|
||||
assert_true(status3.contains("\"torrent_count\":0"))
|
||||
}
|
||||
Reference in new issue
Block a user