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;
|
||||
}
|
||||
Reference in new issue
Block a user