pub struct Endpoint { /* private fields */ }Expand description
Establish encrypted, direct connections over Internet Protocol with QUIC.
§Example
// Generate Ed25519 key which will be used to authenticate node.
let private_key = p2panda_core::PrivateKey::new();
// Use this iroh relay as a "home relay".
let relay_url = "https://my.relay.org".parse().expect("valid relay url");
// Initialise endpoint with custom network identifier.
let endpoint = Endpoint::builder(address_book)
.network_id([1; 32])
.private_key(private_key)
.relay_url(relay_url)
.spawn()
.await?;
// Other nodes can use this id now to establish a direct connection.
println!("my node id: {}", endpoint.node_id());§iroh
Most of the lower-level Internet Protocol networking is made possible by the work of iroh utilising well-established and known standards, like QUIC for transport, (self-certified) TLS 1.3 for transport encryption, QUIC Address Discovery (QAD) for STUN, TURN servers for relayed fallbacks.
§Network identifier
Use NetworkId to actively partition the network. The identifier serves as
a shared secret; nodes will not be able to establish connections if their identifiers differ.
§Custom Protocol Handlers
Register your own custom protocols using the Endpoint::accept method.
§Relays
Use Builder::relay_url to register one or more iroh relay urls which are required to aid
in establishing a direct connection.
§Resolving transport infos
To connect to any endpoint by it’s node id / public key we first need to resolve it to the associated addressing information (relay url, IPv4 and IPv6 addresses) before attempting to establish a direct connection.
Endpoint takes the AddressBook as a dependency which provides it with
the resolved transport information.
The address book itself is populated with resolved transport information by two services:
MdnsDiscovery: Resolve addresses of nearby devices on the local-area network.Discovery: Resolve addresses using random-walk strategy, exploring the network.
Implementations§
Source§impl Endpoint
impl Endpoint
pub fn builder(address_book: AddressBook) -> Builder
Sourcepub async fn endpoint(&self) -> Result<Endpoint, EndpointError>
pub async fn endpoint(&self) -> Result<Endpoint, EndpointError>
Return the internal iroh endpoint instance.
pub fn network_id(&self) -> NetworkId
pub fn node_id(&self) -> NodeId
Sourcepub async fn accept<P: ProtocolHandler>(
&self,
protocol_id: impl AsRef<[u8]>,
protocol_handler: P,
) -> Result<(), EndpointError>
pub async fn accept<P: ProtocolHandler>( &self, protocol_id: impl AsRef<[u8]>, protocol_handler: P, ) -> Result<(), EndpointError>
Register protocol handler for a given ALPN (protocol identifier).
Sourcepub async fn connect(
&self,
node_id: NodeId,
protocol_id: impl AsRef<[u8]>,
) -> Result<Connection, EndpointError>
pub async fn connect( &self, node_id: NodeId, protocol_id: impl AsRef<[u8]>, ) -> Result<Connection, EndpointError>
Starts a connection attempt to a remote iroh endpoint and returns a future which can be awaited for establishing the final connection.
The ALPN byte string, or application-level protocol identifier, is also required. The remote endpoint must support this alpn, otherwise the connection attempt will fail with an error.