Skip to content

Commit

Permalink
Document types in {mullvad,talpid}-types
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Apr 30, 2020
1 parent ffc2c7f commit f27f3c7
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 18 deletions.
7 changes: 4 additions & 3 deletions mullvad-types/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};

pub type AccountToken = String;

/// Account expiration info returned by the API via `/v1/me`
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand All @@ -13,8 +14,8 @@ pub struct AccountData {
pub expiry: DateTime<Utc>,
}

/// Data-structure that's returned from successfuly invocation of the mullvad API's
/// `submit_voucher(account, voucher)` RPC
/// Data structure that's returned from successful invocation of the mullvad API's
/// `/v1/submit-voucher` RPC
#[derive(serde::Deserialize, serde::Serialize, Debug)]
pub struct VoucherSubmission {
/// Amount of time added to the account
Expand All @@ -23,7 +24,7 @@ pub struct VoucherSubmission {
pub new_expiry: DateTime<Utc>,
}

/// Mapping of mullvad-api errors
/// Mapping of mullvad-api errors to enum variants
#[derive(err_derive::Error, Debug)]
pub enum VoucherError {
/// Error code -400
Expand Down
2 changes: 2 additions & 0 deletions mullvad-types/src/auth_failed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use lazy_static::lazy_static;
use regex::Regex;
use std::fmt;

/// Used in the CLI to represent a failure by the tunnel
/// to authenticate with a remote server.
#[derive(Debug)]
pub struct AuthFailed {
reason: AuthFailedInner,
Expand Down
1 change: 1 addition & 0 deletions mullvad-types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub type CountryCode = String;
pub type CityCode = String;
pub type Hostname = String;

/// Describes the physical location of a [`crate::relay_list::Relay`] as returned by the API.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
pub country: String,
Expand Down
16 changes: 16 additions & 0 deletions mullvad-types/src/relay_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub trait Match<T> {
fn matches(&self, other: &T) -> bool;
}

/// Limits the set of [`crate::relay_list::Relay`]s that a `RelaySelector` may select.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(target_os = "android", derive(FromJava, IntoJava))]
Expand Down Expand Up @@ -66,6 +67,8 @@ impl<T: fmt::Debug + Clone + Eq + PartialEq> Match<T> for Constraint<T> {
}
}

/// Specifies a specific endpoint or [`RelayConstraints`] to use when `mullvad-daemon` selects a
/// relay.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(target_os = "android", derive(IntoJava))]
Expand Down Expand Up @@ -127,6 +130,7 @@ impl RelaySettings {
}
}

/// Limits the set of [`crate::relay_list::Relay`]s that a `RelaySelector` may select.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(not(target_os = "android"), derive(Default))]
#[cfg_attr(target_os = "android", derive(IntoJava))]
Expand Down Expand Up @@ -199,6 +203,8 @@ impl fmt::Display for RelayConstraints {
}


/// Limits the set of [`crate::relay_list::Relay`]s used by a `RelaySelector` based on
/// location.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
#[cfg_attr(target_os = "android", derive(FromJava, IntoJava))]
Expand All @@ -224,6 +230,7 @@ impl fmt::Display for LocationConstraint {
}
}

/// Used in [`RelayConstraints`] to limit relay selection based on protocol.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub enum TunnelProtocol {
#[serde(rename = "wireguard")]
Expand All @@ -241,6 +248,7 @@ impl fmt::Display for TunnelProtocol {
}
}

/// Deprecated. Contains protocol-specific constraints for relay selection.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub enum TunnelConstraints {
#[serde(rename = "openvpn")]
Expand Down Expand Up @@ -282,6 +290,7 @@ impl Match<WireguardEndpointData> for TunnelConstraints {
}
}

/// [`Constraint`]s applicable to OpenVPN relay servers.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct OpenVpnConstraints {
pub port: Constraint<u16>,
Expand All @@ -308,6 +317,7 @@ impl Match<OpenVpnEndpointData> for OpenVpnConstraints {
}
}

/// [`Constraint`]s applicable to WireGuard relay servers.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]
pub struct WireguardConstraints {
pub port: Constraint<u16>,
Expand Down Expand Up @@ -335,6 +345,8 @@ impl Match<WireguardEndpointData> for WireguardConstraints {
}


/// Specifies a specific endpoint or [`BridgeConstraints`] to use when `mullvad-daemon` selects a
/// bridge server.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum BridgeSettings {
Expand All @@ -344,6 +356,7 @@ pub enum BridgeSettings {
}


/// Limits the set of bridge servers to use in `mullvad-daemon`.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub struct BridgeConstraints {
Expand All @@ -359,6 +372,7 @@ impl fmt::Display for BridgeConstraints {
}
}

/// Setting indicating whether to connect to a bridge server, or to handle it automatically.
#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum BridgeState {
Expand Down Expand Up @@ -387,6 +401,7 @@ pub struct InternalBridgeConstraints {
pub transport_protocol: Constraint<TransportProtocol>,
}

/// Used to update the [`RelaySettings`] used in `mullvad-daemon`.
#[derive(Debug, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(FromJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand Down Expand Up @@ -422,6 +437,7 @@ impl RelaySettingsUpdate {
}
}

/// Used in [`RelaySettings`] to change relay constraints in the daemon.
#[derive(Debug, Default, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(FromJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand Down
10 changes: 10 additions & 0 deletions mullvad-types/src/relay_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use talpid_types::net::{
};


/// Stores a list of relays for each country obtained from the API using `RelayListProxy`.
/// This can also be passed to frontends.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand All @@ -30,6 +32,7 @@ impl RelayList {
}
}

/// A list of [`RelayListCity`]s within a country. Used by [`RelayList`].
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand All @@ -39,6 +42,7 @@ pub struct RelayListCountry {
pub cities: Vec<RelayListCity>,
}

/// A list of [`Relay`]s within a city. Used by [`RelayListCountry`].
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand All @@ -52,6 +56,7 @@ pub struct RelayListCity {
pub relays: Vec<Relay>,
}

/// Stores information for a relay returned by the API at `v1/relays`. Parsed in `RelayListProxy`.
#[derive(Debug, Clone, Deserialize, Serialize)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand Down Expand Up @@ -79,6 +84,7 @@ pub struct Relay {
pub location: Option<Location>,
}

/// Provides protocol-specific information about a [`Relay`].
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
#[serde(default)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
Expand All @@ -100,6 +106,7 @@ impl RelayTunnels {
}
}

/// Information about an OpenVPN endpoint at a [`Relay`].
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct OpenVpnEndpointData {
pub port: u16,
Expand All @@ -118,6 +125,7 @@ impl fmt::Display for OpenVpnEndpointData {
}
}

/// Information about a WireGuard endpoint at a [`Relay`].
#[derive(Clone, Eq, PartialEq, Hash, Deserialize, Serialize, Debug)]
#[cfg_attr(target_os = "android", derive(IntoJava))]
#[cfg_attr(target_os = "android", jnix(package = "net.mullvad.mullvadvpn.model"))]
Expand Down Expand Up @@ -149,6 +157,7 @@ impl fmt::Display for WireguardEndpointData {
}
}

/// Used by `RelayListProxy` to store bridge servers for a [`Relay`].
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
#[serde(default)]
pub struct RelayBridges {
Expand All @@ -165,6 +174,7 @@ impl RelayBridges {
}
}

/// Data needed to connect using Shadowsocks to a [`Relay`].
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct ShadowsocksEndpointData {
pub port: u16,
Expand Down
1 change: 1 addition & 0 deletions talpid-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod net;
pub mod tunnel;


/// Used to generate string representations of error chains.
pub trait ErrorExt {
/// Creates a string representation of the entire error chain.
fn display_chain(&self) -> String;
Expand Down
10 changes: 7 additions & 3 deletions talpid-types/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl From<openvpn::TunnelParameters> for TunnelParameters {
}
}

/// Type of the tunnel
/// The tunnel protocol used by a [`TunnelEndpoint`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename = "tunnel_type")]
pub enum TunnelType {
Expand Down Expand Up @@ -85,7 +85,6 @@ impl fmt::Display for TunnelType {
pub struct TunnelEndpoint {
#[serde(flatten)]
pub endpoint: Endpoint,
/// Type of the tunnel
#[cfg_attr(target_os = "android", jnix(skip))]
pub tunnel_type: TunnelType,
#[cfg_attr(target_os = "android", jnix(skip))]
Expand Down Expand Up @@ -167,6 +166,9 @@ impl fmt::Display for TransportProtocol {
}
}


/// Returned when `TransportProtocol::from_str` fails to convert a string into a
/// [`TransportProtocol`] object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransportProtocolParseError;

Expand All @@ -184,7 +186,9 @@ pub struct GenericTunnelOptions {
pub enable_ipv6: bool,
}

/// Returns a vector of IP networks representing all of the internet.
/// Returns a vector of IP networks representing all of the internet, 0.0.0.0/0.
/// This may be used in [`crate::net::wireguard::PeerConfig`] to route all traffic
/// to the tunnel interface.
pub fn all_of_the_internet() -> Vec<ipnetwork::IpNetwork> {
vec![
"0.0.0.0/0".parse().expect("Failed to parse ipv6 network"),
Expand Down
16 changes: 12 additions & 4 deletions talpid-types/src/net/openvpn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::net::{
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

/// Information needed by `OpenVpnMonitor` to establish a tunnel connection.
/// See [`crate::net::TunnelParameters`].
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct TunnelParameters {
pub config: ConnectionConfig,
Expand All @@ -13,6 +15,7 @@ pub struct TunnelParameters {
pub proxy: Option<ProxySettings>,
}

/// Connection configuration used by [`TunnelParameters`].
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct ConnectionConfig {
pub endpoint: Endpoint,
Expand All @@ -30,24 +33,23 @@ impl ConnectionConfig {
}
}

/// TunnelOptions contains options for an openvpn tunnel that should be applied
/// `TunnelOptions` contains options for an OpenVPN tunnel that should be applied
/// irrespective of the relay parameters - i.e. have nothing to do with the particular
/// OpenVPN server, but do affect the connection.
/// Stored in [`TunnelParameters`].
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub struct TunnelOptions {
/// Optional argument for openvpn to try and limit TCP packet size,
/// as discussed [here](https://openvpn.net/archive/openvpn-users/2003-11/msg00154.html)
pub mssfix: Option<u16>,
}

/// Proxy server options to be used by `OpenVpnMonitor` when starting a tunnel.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ProxySettings {
/// Generic proxy running independently on localhost.
Local(LocalProxySettings),
/// Generic proxy running on remote host.
Remote(RemoteProxySettings),
/// Bundled Shadowsocks proxy.
Shadowsocks(ShadowsocksProxySettings),
}

Expand All @@ -71,6 +73,7 @@ impl ProxySettings {
}
}

/// Options for a generic proxy running on localhost.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct LocalProxySettings {
pub port: u16,
Expand All @@ -86,6 +89,7 @@ impl LocalProxySettings {
}
}

/// Options for a generic proxy running on remote host.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct RemoteProxySettings {
pub address: SocketAddr,
Expand All @@ -107,6 +111,7 @@ pub struct ProxyAuth {
pub password: String,
}

/// Options for a bundled Shadowsocks proxy.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Deserialize, Serialize)]
pub struct ShadowsocksProxySettings {
pub peer: SocketAddr,
Expand All @@ -124,6 +129,8 @@ impl ShadowsocksProxySettings {
}
}

/// List of ciphers usable by a Shadowsocks proxy.
/// Cf. [`ShadowsocksProxySettings::cipher`].
pub static SHADOWSOCKS_CIPHERS: &[&str] = &[
// Stream ciphers.
"aes-128-cfb",
Expand All @@ -148,6 +155,7 @@ pub static SHADOWSOCKS_CIPHERS: &[&str] = &[
"aes-256-pmac-siv",
];

/// Checks whether the proxy settings to be used by `OpenVpnMonitor` are valid.
pub fn validate_proxy_settings(proxy: &ProxySettings) -> Result<(), String> {
match proxy {
ProxySettings::Local(local) => {
Expand Down
4 changes: 1 addition & 3 deletions talpid-types/src/net/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::fmt;
#[serde(rename_all = "snake_case")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProxyType {
/// Shadowsocks
Shadowsocks,
/// Custom bridge
Custom,
}

Expand All @@ -23,7 +21,7 @@ impl fmt::Display for ProxyType {
}


/// Bridge endpoint, broadcast as part of TunnelEndpoint
/// Bridge endpoint, broadcast as part of a [`crate::net::TunnelEndpoint`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ProxyEndpoint {
#[serde(flatten)]
Expand Down
Loading

0 comments on commit f27f3c7

Please sign in to comment.