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
+63
-19
@@ -3,27 +3,33 @@
|
||||
let i2p_b64_alphabet : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
||||
|
||||
///|
|
||||
fn b64_decode_char(c : Int32) -> Int32 raise EncodingError {
|
||||
/// Standard RFC 4648 Base64 alphabet: `A-Z`, `a-z`, `0-9`, `+`, `/` with `=` padding.
|
||||
let std_b64_alphabet : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||
|
||||
///|
|
||||
fn b64_decode_char_with_extras(
|
||||
c : Int32,
|
||||
c62 : Int32,
|
||||
c63 : Int32,
|
||||
label : String,
|
||||
) -> Int32 raise EncodingError {
|
||||
if c >= 65 && c <= 90 {
|
||||
c - 65
|
||||
} else if c >= 97 && c <= 122 {
|
||||
c - 97 + 26
|
||||
} else if c >= 48 && c <= 57 {
|
||||
c - 48 + 52
|
||||
} else if c == 45 {
|
||||
// '-'
|
||||
} else if c == c62 {
|
||||
62
|
||||
} else if c == 126 {
|
||||
// '~'
|
||||
} else if c == c63 {
|
||||
63
|
||||
} else {
|
||||
raise InvalidCharacter("invalid I2P Base64 character code: \{c}")
|
||||
raise InvalidCharacter("invalid \{label} Base64 character code: \{c}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Encodes bytes using the I2P Base64 alphabet (`-` and `~`, padded with `=`).
|
||||
pub fn base64_encode(data : Bytes) -> String {
|
||||
fn encode_b64_with_alphabet(data : Bytes, alphabet : Bytes) -> String {
|
||||
let len = data.length()
|
||||
if len == 0 {
|
||||
return ""
|
||||
@@ -42,15 +48,15 @@ pub fn base64_encode(data : Bytes) -> String {
|
||||
let c2 = ((b1 & 0x0F) << 2) | (b2 >> 6)
|
||||
let c3 = b2 & 0x3F
|
||||
|
||||
buf.write_char(i2p_b64_alphabet[c0].to_int().unsafe_to_char())
|
||||
buf.write_char(i2p_b64_alphabet[c1].to_int().unsafe_to_char())
|
||||
buf.write_char(alphabet[c0].to_int().unsafe_to_char())
|
||||
buf.write_char(alphabet[c1].to_int().unsafe_to_char())
|
||||
if has_b1 {
|
||||
buf.write_char(i2p_b64_alphabet[c2].to_int().unsafe_to_char())
|
||||
buf.write_char(alphabet[c2].to_int().unsafe_to_char())
|
||||
} else {
|
||||
buf.write_char('=')
|
||||
}
|
||||
if has_b2 {
|
||||
buf.write_char(i2p_b64_alphabet[c3].to_int().unsafe_to_char())
|
||||
buf.write_char(alphabet[c3].to_int().unsafe_to_char())
|
||||
} else {
|
||||
buf.write_char('=')
|
||||
}
|
||||
@@ -60,8 +66,12 @@ pub fn base64_encode(data : Bytes) -> String {
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes an I2P Base64 string (`-` and `~`, optional `=` padding) into `Bytes`.
|
||||
pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
fn decode_b64_with_extras(
|
||||
input : String,
|
||||
c62 : Int32,
|
||||
c63 : Int32,
|
||||
label : String,
|
||||
) -> Bytes raise EncodingError {
|
||||
let chars : Array[Int32] = []
|
||||
for ch in input {
|
||||
let c = ch.to_int()
|
||||
@@ -75,7 +85,9 @@ pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
return b""
|
||||
}
|
||||
if len % 4 != 0 {
|
||||
raise InvalidLength("I2P Base64 length must be a multiple of 4, got \{len}")
|
||||
raise InvalidLength(
|
||||
"\{label} Base64 length must be a multiple of 4, got \{len}",
|
||||
)
|
||||
}
|
||||
let out : Array[Byte] = Array(capacity=len / 4 * 3)
|
||||
let mut i = 0
|
||||
@@ -84,8 +96,8 @@ pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
let c1 = chars[i + 1]
|
||||
let c2 = chars[i + 2]
|
||||
let c3 = chars[i + 3]
|
||||
let v0 = b64_decode_char(c0)
|
||||
let v1 = b64_decode_char(c1)
|
||||
let v0 = b64_decode_char_with_extras(c0, c62, c63, label)
|
||||
let v1 = b64_decode_char_with_extras(c1, c62, c63, label)
|
||||
let pad2 = c2 == 61 // '='
|
||||
let pad3 = c3 == 61 // '='
|
||||
if pad2 && !pad3 {
|
||||
@@ -94,8 +106,16 @@ pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
if (pad2 || pad3) && i + 4 < len {
|
||||
raise InvalidCharacter("Base64 padding before end of input")
|
||||
}
|
||||
let v2 = if pad2 { 0 } else { b64_decode_char(c2) }
|
||||
let v3 = if pad3 { 0 } else { b64_decode_char(c3) }
|
||||
let v2 = if pad2 {
|
||||
0
|
||||
} else {
|
||||
b64_decode_char_with_extras(c2, c62, c63, label)
|
||||
}
|
||||
let v3 = if pad3 {
|
||||
0
|
||||
} else {
|
||||
b64_decode_char_with_extras(c3, c62, c63, label)
|
||||
}
|
||||
|
||||
out.push(((v0 << 2) | (v1 >> 4)).to_byte())
|
||||
if !pad2 {
|
||||
@@ -108,3 +128,27 @@ pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
}
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Encodes bytes using the I2P Base64 alphabet (`-` and `~`, padded with `=`).
|
||||
pub fn base64_encode(data : Bytes) -> String {
|
||||
encode_b64_with_alphabet(data, i2p_b64_alphabet)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes an I2P Base64 string (`-` and `~`, optional `=` padding) into `Bytes`.
|
||||
pub fn base64_decode(input : String) -> Bytes raise EncodingError {
|
||||
decode_b64_with_extras(input, 45, 126, "I2P")
|
||||
}
|
||||
|
||||
///|
|
||||
/// Encodes bytes using the standard RFC 4648 Base64 alphabet (`+` and `/`, padded with `=`).
|
||||
pub fn base64_std_encode(data : Bytes) -> String {
|
||||
encode_b64_with_alphabet(data, std_b64_alphabet)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes a standard RFC 4648 Base64 string (`+` and `/`, `=` padding) into `Bytes`.
|
||||
pub fn base64_std_decode(input : String) -> Bytes raise EncodingError {
|
||||
decode_b64_with_extras(input, 43, 47, "standard")
|
||||
}
|
||||
@@ -122,3 +122,37 @@ test "crc32, gzip dynamic Huffman inflate, deflate/gzip round-trip, I2CP mux, an
|
||||
assert_eq(zip_entries[1].1.length(), 940)
|
||||
assert_eq(@encoding.crc32(zip_entries[1].1), 0x51eab753U)
|
||||
}
|
||||
|
||||
///|
|
||||
test "standard RFC 4648 base64 uses + and / and round-trips" {
|
||||
let raw = b"\xfb\xff\xfe"
|
||||
let encoded = @encoding.base64_std_encode(raw)
|
||||
assert_eq(encoded, "+//+")
|
||||
assert_eq(@encoding.base64_std_decode(encoded), raw)
|
||||
|
||||
let cases : Array[(Bytes, String)] = [
|
||||
(b"", ""),
|
||||
(b"f", "Zg=="),
|
||||
(b"fo", "Zm8="),
|
||||
(b"foo", "Zm9v"),
|
||||
(b"foob", "Zm9vYg=="),
|
||||
(b"fooba", "Zm9vYmE="),
|
||||
(b"foobar", "Zm9vYmFy"),
|
||||
]
|
||||
for item in cases {
|
||||
let (bytes, expected) = item
|
||||
assert_eq(@encoding.base64_std_encode(bytes), expected)
|
||||
assert_eq(@encoding.base64_std_decode(expected), bytes)
|
||||
}
|
||||
assert_eq(@encoding.base64_std_decode(" +//+ \n"), raw)
|
||||
|
||||
// I2P alphabet '-' / '~' is rejected by standard Base64 decoder
|
||||
let mut bad_rejected = false
|
||||
let _ = @encoding.base64_std_decode("-~~-") catch {
|
||||
_ => {
|
||||
bad_rejected = true
|
||||
b""
|
||||
}
|
||||
}
|
||||
assert_true(bad_rejected)
|
||||
}
|
||||
@@ -14,6 +14,10 @@ pub fn base64_decode(String) -> Bytes raise EncodingError
|
||||
|
||||
pub fn base64_encode(Bytes) -> String
|
||||
|
||||
pub fn base64_std_decode(String) -> Bytes raise EncodingError
|
||||
|
||||
pub fn base64_std_encode(Bytes) -> String
|
||||
|
||||
pub fn crc32(Bytes) -> UInt32
|
||||
|
||||
pub fn deflate_raw(Bytes) -> Bytes
|
||||
|
||||
Reference in new issue
Block a user