summaryrefslogtreecommitdiffhomepage
path: root/mullvad_daemon/src/ipc_api.rs
blob: 84932a0b8fe789a9c14ff4b6cfcaee08db264207 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use jsonrpc_core::Error;
use jsonrpc_core::futures::BoxFuture;
use jsonrpc_macros::pubsub;
use jsonrpc_pubsub::SubscriptionId;

use std::collections::HashMap;
use std::net::IpAddr;

pub type AccountToken = String;
pub type CountryCode = String;

build_rpc_trait! {
    pub trait IpcApi {
        type Metadata;

        /// Fetches and returns metadata about an account. Returns an error on non-existing
        /// accounts.
        #[rpc(name = "get_account_data")]
        fn get_account_data(&self, AccountToken) -> Result<AccountData, Error>;

        /// Returns available countries.
        #[rpc(name = "get_countries")]
        fn get_countries(&self) -> Result<HashMap<CountryCode, String>, Error>;

        /// Set which account to connect with
        #[rpc(name = "set_account")]
        fn set_account(&self, AccountToken) -> Result<(), Error>;

        /// Set which country to connect to
        #[rpc(name = "set_country")]
        fn set_country(&self, CountryCode) -> Result<(), Error>;

        /// Set if the backend should automatically establish a tunnel on start or not.
        #[rpc(name = "set_autoconnect")]
        fn set_autoconnect(&self, bool) -> Result<(), Error>;

        /// Try to connect if disconnected, or do nothing if already connecting/connected.
        #[rpc(name = "connect")]
        fn connect(&self) -> Result<(), Error>;

        /// Disconnect the VPN tunnel if it is connecting/connected. Does nothing if already
        /// disconnected.
        #[rpc(name = "disconnect")]
        fn disconnect(&self) -> Result<(), Error>;

        /// Returns the current security state of the Mullvad client. Changes to this state will
        /// be announced to subscribers of `event`.
        #[rpc(name = "get_state")]
        fn get_state(&self) -> Result<SecurityState, Error>;

        /// Returns the current public IP of this computer.
        #[rpc(name = "get_ip")]
        fn get_ip(&self) -> Result<IpAddr, Error>;

        /// Performs a geoIP lookup and returns the current location as perceived by the public
        /// internet.
        #[rpc(name = "get_location")]
        fn get_location(&self) -> Result<Location, Error>;

        #[pubsub(name = "event")] {
            /// Subscribes to the `event` notifications.
            #[rpc(name = "event_subscribe")]
            fn subscribe(&self, Self::Metadata, pubsub::Subscriber<String>);

            /// Unsubscribes from the `event` notifications.
            #[rpc(name = "event_unsubscribe")]
            fn unsubscribe(&self, SubscriptionId) -> BoxFuture<bool, Error>;
        }
    }
}

#[derive(Serialize)]
pub struct AccountData {
    pub paid_until: String,
}

#[derive(Serialize)]
pub struct Location {
    pub latlong: [f64; 2],
    pub country: String,
    pub city: String,
}

#[derive(Serialize)]
pub enum SecurityState {
    Unsecured,
    Securing,
    Secured,
    Unsecuring,
}