- 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/
252 lines
6.3 KiB
Plaintext
252 lines
6.3 KiB
Plaintext
// Generated using `moon info`, DON'T EDIT IT
|
|
package "experiments/moon_i2p/bittorrent"
|
|
|
|
import {
|
|
"experiments/moon_i2p/common",
|
|
"experiments/moon_i2p/encoding",
|
|
"moonbitlang/core/debug",
|
|
}
|
|
|
|
// Values
|
|
pub fn bdict_get(BVal, String) -> BVal?
|
|
|
|
pub fn bdict_get_bytes(BVal, String) -> Bytes?
|
|
|
|
pub fn bdict_get_int(BVal, String) -> Int64?
|
|
|
|
pub fn bdict_get_string(BVal, String) -> String?
|
|
|
|
pub fn bencode_decode(Bytes) -> BVal raise BtError
|
|
|
|
pub fn bencode_decode_prefix(Bytes, start? : Int32) -> (BVal, Int32) raise BtError
|
|
|
|
pub fn bencode_encode(BVal) -> Bytes
|
|
|
|
pub let bt_block_size : Int32
|
|
|
|
pub let bt_protocol_string : Bytes
|
|
|
|
pub fn build_http_announce_request(TrackerUrl, Bytes, Bytes, Int64, Int64, Int64, event? : String, local_dest_b64? : String) -> String
|
|
|
|
pub fn cmp_distance_20(Bytes, Bytes) -> Int32
|
|
|
|
pub fn decode_compact_nodes(Bytes, now_ms? : UInt64) -> Array[DhtNode] raise BtError
|
|
|
|
pub fn decode_ext_handshake(Bytes) -> RemoteExtensions raise BtError
|
|
|
|
pub fn decode_handshake(Bytes) -> (Bytes, Bytes, Bytes) raise BtError
|
|
|
|
pub fn decode_i2p_dht_ext(Bytes) -> (UInt32, UInt32)?
|
|
|
|
pub fn decode_i2p_pex(Bytes) -> Array[@common.Hash]
|
|
|
|
pub fn decode_krpc(Bytes) -> KrpcMessage raise BtError
|
|
|
|
pub fn decode_metadata_msg(Bytes) -> MetadataMsg raise BtError
|
|
|
|
pub fn encode_compact_nodes(Array[DhtNode]) -> Bytes
|
|
|
|
pub fn encode_ext_handshake(metadata_size? : Int32?) -> Bytes
|
|
|
|
pub fn encode_handshake(Bytes, Bytes) -> Bytes raise BtError
|
|
|
|
pub fn encode_i2p_dht_ext(UInt32, UInt32) -> Bytes
|
|
|
|
pub fn encode_i2p_pex(Array[@common.Hash]) -> Bytes
|
|
|
|
pub fn encode_krpc(KrpcMessage) -> Bytes
|
|
|
|
pub fn encode_metadata_data(Int32, Int32, Bytes) -> Bytes
|
|
|
|
pub fn encode_metadata_reject(Int32) -> Bytes
|
|
|
|
pub fn encode_metadata_request(Int32) -> Bytes
|
|
|
|
pub fn encode_peer_message(PeerMessage) -> Bytes
|
|
|
|
pub let ext_id_i2p_dht : Byte
|
|
|
|
pub let ext_id_pex : Byte
|
|
|
|
pub let ext_id_ut_metadata : Byte
|
|
|
|
pub fn extract_info_raw(Bytes) -> Bytes raise BtError
|
|
|
|
pub fn make_i2p_dht_node_id(@common.Hash, UInt32, Bytes) -> Bytes raise BtError
|
|
|
|
pub fn parse_http_tracker_response(Bytes) -> TrackerResponse raise BtError
|
|
|
|
pub fn parse_http_tracker_url(String) -> TrackerUrl?
|
|
|
|
pub fn parse_info_dict(Bytes, trackers? : Array[String]) -> Metainfo raise BtError
|
|
|
|
pub fn parse_magnet(String) -> MagnetLink raise BtError
|
|
|
|
pub fn parse_torrent(Bytes) -> Metainfo raise BtError
|
|
|
|
pub fn try_decode_peer_message(Array[Byte]) -> PeerMessage? raise BtError
|
|
|
|
pub fn url_encode_bytes(Bytes) -> String
|
|
|
|
pub fn verify_i2p_dht_node_id(Bytes, @common.Hash, UInt32) -> Bool
|
|
|
|
pub fn xor_distance_20(Bytes, Bytes) -> Bytes
|
|
|
|
// Errors
|
|
#derive(Eq, @debug.Debug)
|
|
pub suberror BtError {
|
|
InvalidBencode(String)
|
|
InvalidMetainfo(String)
|
|
InvalidMagnet(String)
|
|
InvalidWire(String)
|
|
InvalidKrpc(String)
|
|
Encoding(@encoding.EncodingError)
|
|
Common(@common.CommonError)
|
|
}
|
|
|
|
// Types and methods
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) enum BVal {
|
|
BInt(Int64)
|
|
BBytes(Bytes)
|
|
BList(Array[BVal])
|
|
BDict(Array[(Bytes, BVal)])
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct DhtNode {
|
|
node_id : Bytes
|
|
dest_hash : @common.Hash
|
|
port : UInt32
|
|
last_seen_ms : UInt64
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct DhtPendingTx {
|
|
tx_id : Bytes
|
|
kind : String
|
|
info_hash : Bytes?
|
|
dest_hash : @common.Hash
|
|
port : UInt32
|
|
sent_ms : UInt64
|
|
}
|
|
|
|
pub struct I2pDht {
|
|
local_id : Bytes
|
|
local_dest_hash : @common.Hash
|
|
query_port : UInt32
|
|
reply_port : UInt32
|
|
nodes : Array[DhtNode]
|
|
pending_txs : Array[DhtPendingTx]
|
|
mut next_tx_seq : UInt32
|
|
mut queries_sent : UInt64
|
|
mut responses_recv : UInt64
|
|
mut queries_recv : UInt64
|
|
mut peers_discovered : UInt64
|
|
// private fields
|
|
}
|
|
pub fn I2pDht::alloc_tx_id(Self) -> Bytes
|
|
pub fn I2pDht::closest_nodes(Self, Bytes, limit? : Int32) -> Array[DhtNode]
|
|
pub fn I2pDht::get_peers_local(Self, Bytes, UInt64) -> Array[@common.Hash]
|
|
pub fn I2pDht::make_token(Self, @common.Hash) -> Bytes
|
|
pub fn I2pDht::new(@common.Hash, Bytes, Bytes, query_port? : UInt32, reply_port? : UInt32) -> Self raise BtError
|
|
pub fn I2pDht::prune_expired_txs(Self, UInt64) -> Unit
|
|
pub fn I2pDht::record_peers_discovered(Self, UInt64) -> Unit
|
|
pub fn I2pDht::record_query_recv(Self) -> Unit
|
|
pub fn I2pDht::register_pending_tx(Self, Bytes, String, @common.Hash, UInt32, UInt64, info_hash? : Bytes?) -> Unit
|
|
pub fn I2pDht::store_peer(Self, Bytes, @common.Hash, UInt64) -> Unit
|
|
pub fn I2pDht::take_pending_tx(Self, Bytes) -> DhtPendingTx?
|
|
pub fn I2pDht::upsert_node(Self, Bytes, @common.Hash, UInt32, UInt64, verify_secure_id? : Bool) -> Bool
|
|
pub fn I2pDht::verify_token(Self, @common.Hash, Bytes) -> Bool
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) enum KrpcMessage {
|
|
Query(tx_id~ : Bytes, sender_id~ : Bytes, kind~ : KrpcQueryKind)
|
|
Response(tx_id~ : Bytes, sender_id~ : Bytes, nodes~ : Bytes, values~ : Array[Bytes], token~ : Bytes?)
|
|
ErrorMsg(tx_id~ : Bytes, code~ : Int64, msg~ : String)
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) enum KrpcQueryKind {
|
|
Ping
|
|
FindNode(target~ : Bytes)
|
|
GetPeers(info_hash~ : Bytes)
|
|
AnnouncePeer(info_hash~ : Bytes, port~ : UInt32, token~ : Bytes, implied_port~ : Bool)
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct MagnetLink {
|
|
info_hash : Bytes
|
|
display_name : String
|
|
trackers : Array[String]
|
|
dht_nodes : Array[(String, UInt32)]
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) enum MetadataMsg {
|
|
MetaRequest(Int32)
|
|
MetaData(Int32, Int32, Bytes)
|
|
MetaReject(Int32)
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct Metainfo {
|
|
info_hash : Bytes
|
|
name : String
|
|
piece_length : Int64
|
|
piece_hashes : Array[Bytes]
|
|
total_length : Int64
|
|
files : Array[TorrentFile]
|
|
trackers : Array[String]
|
|
raw_info : Bytes
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) enum PeerMessage {
|
|
KeepAlive
|
|
Choke
|
|
Unchoke
|
|
Interested
|
|
NotInterested
|
|
Have(UInt32)
|
|
Bitfield(Bytes)
|
|
Request(Int32, Int32, Int32)
|
|
Piece(Int32, Int32, Bytes)
|
|
Cancel(Int32, Int32, Int32)
|
|
Extended(Byte, Bytes)
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct RemoteExtensions {
|
|
mut ut_metadata_id : Byte?
|
|
mut i2p_pex_id : Byte?
|
|
mut i2p_dht_id : Byte?
|
|
mut metadata_size : Int32?
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct TorrentFile {
|
|
path : Array[String]
|
|
length : Int64
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct TrackerResponse {
|
|
interval : Int32
|
|
complete : Int32
|
|
incomplete : Int32
|
|
peers : Array[@common.Hash]
|
|
failure_reason : String?
|
|
}
|
|
|
|
#derive(Eq, @debug.Debug)
|
|
pub(all) struct TrackerUrl {
|
|
host : String
|
|
port : UInt32
|
|
request_target : String
|
|
}
|
|
|
|
// Type aliases
|
|
|
|
// Traits
|