#bittorrent #p2p #distributed #networking #torrent #send-message #tokio

vincenzo

一个为文森佐客户端提供动力的 BitTorrent 协议库

2 个版本

0.0.3 2023 年 11 月 11 日
0.0.2 2023 年 11 月 11 日

#1964 in 网络编程

MIT 许可证

340KB
6K SLoC

image

Vincenzo

Vincenzo 是一个用于构建基于 BitTorrent 协议的程序的库。

此包提供了整个协议的构建块,因此用户可以用它构建任何东西:库、二进制文件,甚至新的用户界面。

功能

  • BitTorrent V1 协议
  • 多平台
  • 支持磁力链接
  • 使用 tokio 的异步 I/O
  • 与追踪器的 UDP 连接
  • 与 UI 分离的守护进程

示例

要下载种子文件,我们只需运行守护进程并向其发送消息。

    use vincenzo::daemon::Daemon;
    use vincenzo::daemon::DaemonMsg;
    use vincenzo::magnet::Magnet;
    use tokio::spawn;
    use tokio::sync::oneshot;

    #[tokio::main]
    async fn main() {
        let download_dir = "/home/gabriel/Downloads".to_string();

        let mut daemon = Daemon::new(download_dir);
        let tx = daemon.ctx.tx.clone();

        spawn(async move {
            daemon.run().await.unwrap();
        });

        let magnet = Magnet::new("magnet:?xt=urn:btih:ab6ad7ff24b5ed3a61352a1f1a7811a8c3cc6dde&dn=archlinux-2023.09.01-x86_64.iso").unwrap();

        // identifier of the torrent
        let info_hash = magnet.parse_xt();

        tx.send(DaemonMsg::NewTorrent(magnet)).await.unwrap();

        // get information about the torrent download
        let (otx, orx) = oneshot::channel();

        tx.send(DaemonMsg::RequestTorrentState(info_hash, otx)).await.unwrap();
        let torrent_state = orx.await.unwrap();

        // TorrentState {
        //     name: "torrent name",
        //     download_rate: 999999,
        //     ...
        // }
    }

支持的 BEPs

依赖项

~11–24MB
~298K SLoC