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
@@ -613,3 +613,112 @@ int32_t moon_i2p_net_fetch_https(
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_mkdir_p(moonbit_bytes_t path_bytes, int32_t path_len) {
|
||||
if (path_len <= 0 || path_len >= 1024) {
|
||||
net_set_error("invalid directory path length");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
for (int32_t i = 1; i < path_len; i++) {
|
||||
if (path[i] == '/') {
|
||||
path[i] = '\0';
|
||||
if (mkdir(path, 0755) < 0 && errno != EEXIST) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
path[i] = '/';
|
||||
}
|
||||
}
|
||||
if (mkdir(path, 0755) < 0 && errno != EEXIST) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_write_file_at(
|
||||
moonbit_bytes_t path_bytes,
|
||||
int32_t path_len,
|
||||
int64_t offset,
|
||||
moonbit_bytes_t data,
|
||||
int32_t data_len) {
|
||||
if (path_len <= 0 || path_len >= 1024 || offset < 0 || data_len < 0) {
|
||||
net_set_error("invalid write_file_at arguments");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
int fd = open(path, O_CREAT | O_WRONLY, 0644);
|
||||
if (fd < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
int32_t wrote = 0;
|
||||
while (wrote < data_len) {
|
||||
ssize_t n = pwrite(fd, data + wrote, (size_t)(data_len - wrote), (off_t)(offset + wrote));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
net_set_error(strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
wrote += (int32_t)n;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_read_file_at(
|
||||
moonbit_bytes_t path_bytes,
|
||||
int32_t path_len,
|
||||
int64_t offset,
|
||||
moonbit_bytes_t dst,
|
||||
int32_t max_len) {
|
||||
if (path_len <= 0 || path_len >= 1024 || offset < 0 || max_len < 0) {
|
||||
net_set_error("invalid read_file_at arguments");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
int32_t total = 0;
|
||||
while (total < max_len) {
|
||||
ssize_t n = pread(fd, dst + total, (size_t)(max_len - total), (off_t)(offset + total));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
net_set_error(strerror(errno));
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
if (n == 0) break;
|
||||
total += (int32_t)n;
|
||||
}
|
||||
close(fd);
|
||||
return total;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_home_dir(moonbit_bytes_t dst, int32_t max_len) {
|
||||
const char *home = getenv("HOME");
|
||||
if (home == NULL || max_len <= 0) {
|
||||
return 0;
|
||||
}
|
||||
int32_t len = (int32_t)strlen(home);
|
||||
if (len > max_len) {
|
||||
len = max_len;
|
||||
}
|
||||
memcpy(dst, home, (size_t)len);
|
||||
return len;
|
||||
}
|
||||
@@ -140,6 +140,34 @@ extern "C" fn c_fetch_https(
|
||||
max_len : Int32,
|
||||
) -> Int32 = "moon_i2p_net_fetch_https"
|
||||
|
||||
///|
|
||||
#borrow(path)
|
||||
extern "C" fn c_mkdir_p(path : Bytes, path_len : Int32) -> Int32 = "moon_i2p_net_mkdir_p"
|
||||
|
||||
///|
|
||||
#borrow(path, data)
|
||||
extern "C" fn c_write_file_at(
|
||||
path : Bytes,
|
||||
path_len : Int32,
|
||||
offset : Int64,
|
||||
data : Bytes,
|
||||
data_len : Int32,
|
||||
) -> Int32 = "moon_i2p_net_write_file_at"
|
||||
|
||||
///|
|
||||
#borrow(path, dst)
|
||||
extern "C" fn c_read_file_at(
|
||||
path : Bytes,
|
||||
path_len : Int32,
|
||||
offset : Int64,
|
||||
dst : FixedArray[Byte],
|
||||
max_len : Int32,
|
||||
) -> Int32 = "moon_i2p_net_read_file_at"
|
||||
|
||||
///|
|
||||
#borrow(dst)
|
||||
extern "C" fn c_home_dir(dst : FixedArray[Byte], max_len : Int32) -> Int32 = "moon_i2p_net_home_dir"
|
||||
|
||||
///|
|
||||
fn last_os_error() -> String {
|
||||
let n = c_last_error_len()
|
||||
@@ -245,6 +273,60 @@ pub fn read_file_bytes(path : String) -> Bytes raise Ntcp2NetError {
|
||||
Bytes::from_array(buf)[:got].to_owned()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Creates directory `path` and any missing parent directories (`mkdir -p`).
|
||||
pub fn mkdir_p(path : String) -> Unit raise Ntcp2NetError {
|
||||
let pb = @encoding.string_to_utf8_bytes(path)
|
||||
if c_mkdir_p(pb, pb.length()) < 0 {
|
||||
raise IoError("mkdir_p(\{path}) failed: \{last_os_error()}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Writes `data` at byte `offset` into `path` (creating the file if missing).
|
||||
pub fn write_file_at(
|
||||
path : String,
|
||||
offset : Int64,
|
||||
data : Bytes,
|
||||
) -> Unit raise Ntcp2NetError {
|
||||
let pb = @encoding.string_to_utf8_bytes(path)
|
||||
if c_write_file_at(pb, pb.length(), offset, data, data.length()) < 0 {
|
||||
raise IoError("write_file_at(\{path}) failed: \{last_os_error()}")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Reads up to `len` bytes starting at byte `offset` from `path`.
|
||||
pub fn read_file_at(
|
||||
path : String,
|
||||
offset : Int64,
|
||||
len : Int32,
|
||||
) -> Bytes raise Ntcp2NetError {
|
||||
if len <= 0 {
|
||||
return b""
|
||||
}
|
||||
let pb = @encoding.string_to_utf8_bytes(path)
|
||||
let buf = FixedArray::make(len, b'\x00')
|
||||
let got = c_read_file_at(pb, pb.length(), offset, buf, len)
|
||||
if got < 0 {
|
||||
raise IoError("read_file_at(\{path}) failed: \{last_os_error()}")
|
||||
}
|
||||
Bytes::from_array(buf)[:got].to_owned()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Returns the `HOME` directory path, or empty string if unset.
|
||||
pub fn get_home_dir() -> String {
|
||||
let buf = FixedArray::make(1024, b'\x00')
|
||||
let n = c_home_dir(buf, 1024)
|
||||
if n <= 0 {
|
||||
return ""
|
||||
}
|
||||
@encoding.utf8_bytes_to_string(Bytes::from_array(buf)[:n].to_owned()) catch {
|
||||
_ => ""
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Native POSIX TCP socket wrapper with exact-length reads and line reading.
|
||||
pub struct TcpSocket {
|
||||
|
||||
Reference in new issue
Block a user