184 lines
4.2 KiB
MoonBit
184 lines
4.2 KiB
MoonBit
///|
|
|
/// Tunnel pool management, multi-hop encryption/decryption, and transit relay state machine.
|
|
|
|
///|
|
|
/// Direction of a local tunnel.
|
|
#derive(Eq, Debug)
|
|
pub(all) enum TunnelDirection {
|
|
Inbound
|
|
Outbound
|
|
}
|
|
|
|
///|
|
|
/// A single hop along a tunnel path.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct TunnelHop {
|
|
router_hash : @common.Hash
|
|
receive_tunnel_id : UInt32
|
|
next_tunnel_id : UInt32
|
|
layer_key : Bytes // 32 bytes
|
|
iv_key : Bytes // 32 bytes
|
|
}
|
|
|
|
///|
|
|
/// An active local inbound or outbound tunnel.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Tunnel {
|
|
id : UInt32
|
|
direction : TunnelDirection
|
|
hops : Array[TunnelHop]
|
|
created_sec : UInt32
|
|
expires_sec : UInt32
|
|
}
|
|
|
|
///|
|
|
/// Outbound originator: pre-decrypts a cleartext 1024-byte cell through all hops
|
|
/// in reverse order so each hop's `tunnel_layer_encrypt` peels off a layer until
|
|
/// the OBEP sees the cleartext cell.
|
|
pub fn Tunnel::prepare_outbound_cell(
|
|
self : Tunnel,
|
|
cleartext_cell : Bytes,
|
|
) -> Bytes raise TunnelError {
|
|
let mut cell = cleartext_cell
|
|
for i = self.hops.length() - 1; i >= 0; i = i - 1 {
|
|
let hop = self.hops[i]
|
|
cell = tunnel_layer_decrypt(hop.layer_key, hop.iv_key, cell)
|
|
}
|
|
cell
|
|
}
|
|
|
|
///|
|
|
/// Inbound endpoint (originator): decrypts all hop layers in reverse order to
|
|
/// recover the cleartext 1024-byte cell encrypted along the inbound tunnel.
|
|
pub fn Tunnel::decrypt_inbound_cell(
|
|
self : Tunnel,
|
|
encrypted_cell : Bytes,
|
|
) -> Bytes raise TunnelError {
|
|
let mut cell = encrypted_cell
|
|
for i = self.hops.length() - 1; i >= 0; i = i - 1 {
|
|
let hop = self.hops[i]
|
|
cell = tunnel_layer_decrypt(hop.layer_key, hop.iv_key, cell)
|
|
}
|
|
cell
|
|
}
|
|
|
|
///|
|
|
/// Transit hop entry stored by a participating router to relay `TunnelData` messages.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct TransitHop {
|
|
receive_tunnel_id : UInt32
|
|
next_tunnel_id : UInt32
|
|
next_router : @common.Hash
|
|
layer_key : Bytes
|
|
iv_key : Bytes
|
|
is_obep : Bool
|
|
expires_sec : UInt32
|
|
}
|
|
|
|
///|
|
|
/// Manages local inbound/outbound tunnel pools and participating transit hops.
|
|
pub struct TunnelManager {
|
|
inbound : Array[Tunnel]
|
|
outbound : Array[Tunnel]
|
|
transit : Array[TransitHop]
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::new() -> TunnelManager {
|
|
{ inbound: [], outbound: [], transit: [], }
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::add_tunnel(self : TunnelManager, t : Tunnel) -> Unit {
|
|
match t.direction {
|
|
Inbound => self.inbound.push(t)
|
|
Outbound => self.outbound.push(t)
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::add_transit_hop(
|
|
self : TunnelManager,
|
|
hop : TransitHop,
|
|
) -> Unit {
|
|
self.transit.push(hop)
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::inbound_count(self : TunnelManager) -> Int32 {
|
|
self.inbound.length()
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::outbound_count(self : TunnelManager) -> Int32 {
|
|
self.outbound.length()
|
|
}
|
|
|
|
///|
|
|
pub fn TunnelManager::transit_count(self : TunnelManager) -> Int32 {
|
|
self.transit.length()
|
|
}
|
|
|
|
///|
|
|
/// Relays an incoming 1024-byte `TunnelData` cell for `receive_tunnel_id`.
|
|
/// Returns `Some((next_router, next_tunnel_id, encrypted_cell, is_obep))` if matched.
|
|
pub fn TunnelManager::relay_cell(
|
|
self : TunnelManager,
|
|
receive_tunnel_id : UInt32,
|
|
cell_1024 : Bytes,
|
|
) -> (@common.Hash, UInt32, Bytes, Bool)? raise TunnelError {
|
|
for hop in self.transit {
|
|
if hop.receive_tunnel_id == receive_tunnel_id {
|
|
let next_cell = tunnel_layer_encrypt(hop.layer_key, hop.iv_key, cell_1024)
|
|
return Some((hop.next_router, hop.next_tunnel_id, next_cell, hop.is_obep))
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
///|
|
|
/// Prunes expired tunnels and transit hops.
|
|
pub fn TunnelManager::prune_expired(
|
|
self : TunnelManager,
|
|
now_sec : UInt32,
|
|
) -> Int32 {
|
|
let mut removed = 0
|
|
let kept_in : Array[Tunnel] = []
|
|
for t in self.inbound {
|
|
if t.expires_sec > now_sec {
|
|
kept_in.push(t)
|
|
} else {
|
|
removed += 1
|
|
}
|
|
}
|
|
self.inbound.clear()
|
|
for t in kept_in {
|
|
self.inbound.push(t)
|
|
}
|
|
let kept_out : Array[Tunnel] = []
|
|
for t in self.outbound {
|
|
if t.expires_sec > now_sec {
|
|
kept_out.push(t)
|
|
} else {
|
|
removed += 1
|
|
}
|
|
}
|
|
self.outbound.clear()
|
|
for t in kept_out {
|
|
self.outbound.push(t)
|
|
}
|
|
let kept_tr : Array[TransitHop] = []
|
|
for h in self.transit {
|
|
if h.expires_sec > now_sec {
|
|
kept_tr.push(h)
|
|
} else {
|
|
removed += 1
|
|
}
|
|
}
|
|
self.transit.clear()
|
|
for h in kept_tr {
|
|
self.transit.push(h)
|
|
}
|
|
removed
|
|
}
|