feat: persist I2P torrent list to EDN state file across restarts
Rolling Release / Test (MoonBit, Extension, Scripts) (push) Successful in 21s
Rolling Release / Build & Publish Rolling Release (push) Successful in 2m20s

- 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:
nandi committed 2026-11-29 02:46:23 -08:00
1 parent b803e79199
commit dd5b132b5a
17 files changed
+1105 -39

No files matched your search

+76
View File
@@ -37,6 +37,51 @@ fn split_host_port(spec : String) -> (String, Int32)? {
}
}
///|
fn trim_trailing_slash(dir : String) -> String {
if dir.length() > 1 && dir.has_suffix("/") {
dir[:dir.length() - 1].to_owned()
} else {
dir
}
}
///|
fn default_torrent_state_file(download_dir : String) -> String {
match @env.get_env_var("MOON_I2P_DATA_DIR") {
Some(d) => {
let t = d.trim().to_owned()
if t.length() > 0 {
return trim_trailing_slash(t) + "/torrents.edn"
}
}
None => ()
}
match @env.get_env_var("XDG_DATA_HOME") {
Some(d) => {
let t = d.trim().to_owned()
if t.length() > 0 {
return trim_trailing_slash(t) + "/moon-i2p-browser/torrents.edn"
}
}
None => ()
}
let home = match @env.get_env_var("HOME") {
Some(h) if h.trim().to_owned().length() > 0 => h.trim().to_owned()
_ => @ntcp2_net.get_home_dir().trim().to_owned()
}
if home.length() > 0 {
return trim_trailing_slash(home) +
"/.local/share/moon-i2p-browser/torrents.edn"
}
let dl = download_dir.trim().to_owned()
if dl.length() > 0 {
trim_trailing_slash(dl) + "/.moon-i2p-torrents.edn"
} else {
"./torrents.edn"
}
}
///|
fn print_usage() -> Unit {
println("moon-i2p native router daemon")
@@ -58,6 +103,10 @@ fn print_usage() -> Unit {
println(
" --download-dir <dir> Download folder for I2P torrents (default: ~/Downloads/moon-i2p)",
)
println(
" --torrent-state <file> EDN file to persist torrent list (default: $MOON_I2P_DATA_DIR/torrents.edn)",
)
println(" --no-torrent-state Disable EDN torrent list persistence")
println(
" --ntcp2-host <ip> NTCP2 advertise/bind IP (default: 127.0.0.1)",
)
@@ -101,6 +150,8 @@ fn main {
let mut torrent_api_host = "127.0.0.1"
let mut torrent_api_port = 4445
let mut download_dir = ""
let mut torrent_state = ""
let mut no_torrent_state = false
let mut ntcp2_host = "127.0.0.1"
let mut ntcp2_port = 0
let mut floodfill = false
@@ -185,6 +236,17 @@ fn main {
} else {
i += 1
}
"--torrent-state" =>
if i + 1 < args.length() {
torrent_state = args[i + 1]
i += 2
} else {
i += 1
}
"--no-torrent-state" => {
no_torrent_state = true
i += 1
}
"--ntcp2-port" =>
if i + 1 < args.length() {
match parse_port(args[i + 1]) {
@@ -280,6 +342,9 @@ fn main {
}
}
i += 1
} else if args[i].has_prefix("--torrent-state=") {
torrent_state = args[i][16:].to_owned()
i += 1
} else {
i += 1
}
@@ -310,11 +375,19 @@ fn main {
}
}
}
let effective_state_file = if no_torrent_state {
""
} else if torrent_state.trim().to_owned().length() > 0 {
torrent_state.trim().to_owned()
} else {
default_torrent_state_file(download_dir)
}
if torrent_api_enabled {
let _ = r.start_torrent_api(
host=torrent_api_host,
port=torrent_api_port,
download_dir~,
state_file=effective_state_file,
) catch {
_ => {
r.close()
@@ -391,6 +464,9 @@ fn main {
println(
" Torrent API: \{torrent_api_host}:\{r.torrent_api_port()} (b32=\{r.torrent_b32_address()})",
)
if effective_state_file.length() > 0 {
println(" Torrent State: \{effective_state_file}")
}
}
println(
" NetDB peers: \{r.netdb.router_count()} (floodfills=\{r.netdb.floodfill_count()})",