#peer #hole #tcp #server #connect #punch #connection

gday_hole_punch

使用TCP打洞安全地连接到对等方

3个不稳定版本

0.2.1 2024年7月12日
0.2.0 2024年7月8日
0.1.1 2024年6月7日

#1971 in 网络编程

Download history 148/week @ 2024-06-03 13/week @ 2024-06-10 255/week @ 2024-07-08 7/week @ 2024-07-15 40/week @ 2024-07-22

每月302次下载
用于gday

MIT授权

58KB
811

注意:此crate仍在早期开发阶段,因此预计会有破坏性变更。

gday_hole_punch

Crates.io Version docs.rs

让位于NAT(网络地址转换)之后的对等方尝试建立直接的认证TCP连接。使用TCP打洞和辅助gday_server来完成此操作。

参见文档

被使用

  • gday - 发送文件的命令行工具。

依赖


lib.rs:

注意:此crate仍在早期开发阶段,因此预计会有破坏性变更。

让两个位于NAT(网络地址转换)之后的对等方尝试建立直接的认证TCP连接。使用TCP打洞和辅助gday_server来完成此操作。此库由gday,一个发送文件的命令行工具,使用。

示例

#
let servers = server_connector::DEFAULT_SERVERS;
let timeout = std::time::Duration::from_secs(5);
let room_code = 123;
let shared_secret = 456;

//////// Peer 1 ////////

// Connect to a random server in the default server list
let (mut server_connection, server_id) = server_connector::connect_to_random_server(
    servers,
    timeout
)?;

// PeerCode useful for giving rendezvous info to peer
let peer_code = PeerCode { server_id, room_code, shared_secret };
let code_to_share = peer_code.to_string();

// Create a room in the server, and get my contact from it
let (contact_sharer, my_contact) = ContactSharer::create_room(
    &mut server_connection,
    room_code
)?;

// Wait for the server to send the peer's contact
let peer_contact = contact_sharer.get_peer_contact()?;

// Use TCP hole-punching to connect to the peer,
// verify their identity with the shared_secret,
// and get a cryptographically-secure shared key
let (tcp_stream, strong_key) = try_connect_to_peer(
    my_contact.local,
    peer_contact,
    &shared_secret.to_be_bytes(),
    timeout
)?;

//////// Peer 2 (on a different computer) ////////

let peer_code = PeerCode::from_str(&code_to_share)?;

// Connect to the same server as Peer 1
let mut server_connection = server_connector::connect_to_server_id(
    servers,
    peer_code.server_id,
    timeout
)?;

// Join the same room in the server, and get my local contact
let (contact_sharer, my_contact) = ContactSharer::join_room(
    &mut server_connection,
    peer_code.room_code
)?;

let peer_contact = contact_sharer.get_peer_contact()?;

let (tcp_stream, strong_key) = try_connect_to_peer(
    my_contact.local,
    peer_contact,
    &peer_code.shared_secret.to_be_bytes(),
    timeout
)?;

依赖

~14–25MB
~453K SLoC