- 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.
155 lines
4.0 KiB
MoonBit
155 lines
4.0 KiB
MoonBit
///|
|
|
/// I2P custom Base64 alphabet: `A-Z`, `a-z`, `0-9`, `-`, `~` with `=` padding.
|
|
let i2p_b64_alphabet : Bytes = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-~"
|
|
|
|
///|
|
|
/// 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 == c62 {
|
|
62
|
|
} else if c == c63 {
|
|
63
|
|
} else {
|
|
raise InvalidCharacter("invalid \{label} Base64 character code: \{c}")
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn encode_b64_with_alphabet(data : Bytes, alphabet : Bytes) -> String {
|
|
let len = data.length()
|
|
if len == 0 {
|
|
return ""
|
|
}
|
|
let buf = StringBuilder(size_hint=(len + 2) / 3 * 4)
|
|
let mut i = 0
|
|
while i < len {
|
|
let b0 = data[i].to_int()
|
|
let has_b1 = i + 1 < len
|
|
let has_b2 = i + 2 < len
|
|
let b1 = if has_b1 { data[i + 1].to_int() } else { 0 }
|
|
let b2 = if has_b2 { data[i + 2].to_int() } else { 0 }
|
|
|
|
let c0 = b0 >> 2
|
|
let c1 = ((b0 & 0x03) << 4) | (b1 >> 4)
|
|
let c2 = ((b1 & 0x0F) << 2) | (b2 >> 6)
|
|
let c3 = b2 & 0x3F
|
|
|
|
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(alphabet[c2].to_int().unsafe_to_char())
|
|
} else {
|
|
buf.write_char('=')
|
|
}
|
|
if has_b2 {
|
|
buf.write_char(alphabet[c3].to_int().unsafe_to_char())
|
|
} else {
|
|
buf.write_char('=')
|
|
}
|
|
i += 3
|
|
}
|
|
buf.to_string()
|
|
}
|
|
|
|
///|
|
|
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()
|
|
if c == 32 || c == 9 || c == 10 || c == 13 {
|
|
continue
|
|
}
|
|
chars.push(c)
|
|
}
|
|
let len = chars.length()
|
|
if len == 0 {
|
|
return b""
|
|
}
|
|
if len % 4 != 0 {
|
|
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
|
|
while i < len {
|
|
let c0 = chars[i]
|
|
let c1 = chars[i + 1]
|
|
let c2 = chars[i + 2]
|
|
let c3 = chars[i + 3]
|
|
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 {
|
|
raise InvalidCharacter("invalid Base64 padding")
|
|
}
|
|
if (pad2 || pad3) && i + 4 < len {
|
|
raise InvalidCharacter("Base64 padding before end of input")
|
|
}
|
|
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 {
|
|
out.push((((v1 & 0x0F) << 4) | (v2 >> 2)).to_byte())
|
|
}
|
|
if !pad3 {
|
|
out.push((((v2 & 0x03) << 6) | v3).to_byte())
|
|
}
|
|
i += 4
|
|
}
|
|
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")
|
|
}
|