Skip to main content

geph5_broker_protocol/
exit.rs

1use std::{
2    collections::{BTreeMap, HashMap},
3    net::SocketAddr,
4    time::{Duration, SystemTime, UNIX_EPOCH},
5};
6
7use ed25519_dalek::VerifyingKey;
8use isocountry::CountryCode;
9use language_tags::LanguageTag;
10use serde::{Deserialize, Serialize};
11
12use crate::{AccountLevel, RouteDescriptor};
13
14#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
15/// This fully describes a particular exit.
16pub struct ExitDescriptor {
17    /// The listening port for the client-to-exit protocol
18    pub c2e_listen: SocketAddr,
19    /// The listening port for the bridge-to-exit protocol
20    pub b2e_listen: SocketAddr,
21    /// The country code
22    pub country: CountryCode,
23    /// The city code
24    pub city: String,
25    /// How loaded the server is
26    pub load: f32,
27    /// When does this descriptor expire?
28    pub expiry: u64,
29}
30
31#[derive(Serialize, Deserialize, Clone, Debug)]
32/// This fully describes all the available exits in the system.
33pub struct ExitList {
34    /// All the exits in the system
35    pub all_exits: Vec<(VerifyingKey, ExitDescriptor)>,
36    pub city_names: HashMap<String, HashMap<LanguageTag, String>>,
37}
38
39impl ExitList {
40    /// A convenience method to find the overall expiry time of the exit list.
41    pub fn expiry(&self) -> SystemTime {
42        UNIX_EPOCH
43            + Duration::from_secs(
44                self.all_exits
45                    .iter()
46                    .map(|exit| exit.1.expiry)
47                    .min()
48                    .unwrap_or_default(),
49            )
50    }
51}
52
53#[derive(Serialize, Deserialize, Clone, Debug)]
54/// This fully describes all the available exits in the system.
55pub struct NetStatus {
56    /// All the exits in the system.
57    pub exits: BTreeMap<String, (VerifyingKey, ExitDescriptor, ExitMetadata)>,
58}
59
60impl NetStatus {
61    /// A convenience method to find the overall expiry time of the NetStatus.
62    pub fn expiry(&self) -> SystemTime {
63        UNIX_EPOCH
64            + Duration::from_secs(
65                self.exits
66                    .values()
67                    .map(|exit| exit.1.expiry)
68                    .min()
69                    .unwrap_or_default(),
70            )
71    }
72}
73
74#[derive(Serialize, Deserialize, Clone, Debug)]
75pub struct ExitMetadata {
76    pub allowed_levels: Vec<AccountLevel>,
77    pub category: ExitCategory,
78}
79
80#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
81#[serde(rename_all = "snake_case")]
82pub enum ExitConstraint {
83    Auto,
84    Direct(String),
85    Hostname(String),
86    Country(CountryCode),
87    CountryCity(CountryCode, String),
88}
89
90#[derive(Serialize, Deserialize, Clone, Debug)]
91pub struct ExitRouteDescriptor {
92    pub exit_pubkey: VerifyingKey,
93    pub exit: ExitDescriptor,
94    pub route: RouteDescriptor,
95}
96
97#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
98#[serde(rename_all = "snake_case")]
99pub enum ExitCategory {
100    Core,
101    Streaming,
102}