fastn_net/lib.rs
1//! # fastn-net
2//!
3//! Network utilities and P2P communication between fastn entities.
4//!
5//! This crate provides P2P networking capabilities for fastn entities using Iroh.
6//! Each fastn instance is called an "entity" in the P2P network, identified by
7//! its unique ID52 (a 52-character encoded Ed25519 public key).
8//!
9//! ## Example
10//!
11//! ```ignore
12//! use fastn_net::{global_iroh_endpoint, ping};
13//!
14//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
15//! // Get the global Iroh endpoint for this entity
16//! let endpoint = global_iroh_endpoint().await;
17//!
18//! // Connect to another entity (requires valid entity ID52)
19//! let entity_id = "entity_id52_here";
20//! let connection = /* establish connection to entity */;
21//!
22//! // Ping the connection
23//! ping(&connection).await?;
24//! # Ok(())
25//! # }
26//! ```
27//!
28//! ## Supported Protocols
29//!
30//! - [`Protocol::Ping`] - Test connectivity between entities
31//! - [`Protocol::Http`] - Proxy HTTP requests through entities
32//! - [`Protocol::Tcp`] - Tunnel TCP connections between entities
33//! - [`Protocol::Socks5`] - SOCKS5 proxy support
34
35extern crate self as fastn_net;
36
37pub mod dot_fastn;
38pub mod get_endpoint;
39mod get_stream;
40mod graceful;
41pub mod http;
42mod http_connection_manager;
43mod http_to_peer;
44mod peer_to_http;
45mod ping;
46pub mod protocol;
47mod secret;
48mod tcp;
49mod utils;
50mod utils_iroh;
51
52pub use get_endpoint::get_endpoint;
53pub use get_stream::{PeerStreamSenders, get_stream};
54pub use graceful::Graceful;
55pub use http::ProxyResult;
56pub use http_connection_manager::{HttpConnectionManager, HttpConnectionPool, HttpConnectionPools};
57pub use http_to_peer::{http_to_peer, http_to_peer_non_streaming};
58pub use peer_to_http::peer_to_http;
59pub use ping::{PONG, ping};
60pub use protocol::{APNS_IDENTITY, Protocol, ProtocolHeader};
61pub use secret::{
62    SECRET_KEY_FILE, generate_and_save_key, generate_secret_key, get_secret_key, read_or_create_key,
63};
64pub use tcp::{peer_to_tcp, pipe_tcp_stream_over_iroh, tcp_to_peer};
65pub use utils::mkdir;
66pub use utils_iroh::{
67    accept_bi, accept_bi_with, get_remote_id52, global_iroh_endpoint, next_json, next_string,
68};
69
70// Deprecated helper functions - use fastn_id52 directly
71pub use utils::{id52_to_public_key, public_key_to_id52};
72
73/// Map of entity IDs to their port and endpoint.
74///
75/// Stores tuples of (ID52 prefix, (port, Iroh endpoint)) for entity lookup.
76/// Uses a Vec instead of HashMap because:
77/// - We need prefix matching for shortened IDs in subdomains
78/// - The number of entities is typically small per instance
79/// - Linear search with prefix matching is fast enough
80///
81/// The ID52 strings may be truncated when used in DNS subdomains due to the
82/// 63-character limit, so prefix matching allows finding the correct entity.
83pub type IDMap = std::sync::Arc<tokio::sync::Mutex<Vec<(String, (u16, iroh::Endpoint))>>>;
84
85/// Acknowledgment message used in protocol handshakes.
86pub const ACK: &str = "ack";