1
//! RPC methods for access to the Geoip DB.
2

            
3
use std::sync::Arc;
4

            
5
use derive_deftly::Deftly;
6
use tor_rpcbase as rpc;
7

            
8
use super::session::ArtiRpcSession;
9

            
10
/// Return a Geoip database, if one is associated with this object.
11
///
12
/// This database is used to map IP addresses to a best-effort
13
/// guess of the country in which they are located.
14
///
15
/// If Arti was built without geoip support,
16
/// or if no geoip database is available,
17
/// returns an error.
18
///
19
/// ## Notes
20
///
21
/// Currently, if Arti was built with geoip support,
22
/// then there is necessarily a geoip database available,
23
/// since the database is linked into Arti at compile time.
24
/// This may change in the future.
25
///
26
/// This method returns the latest geoip database
27
/// at the time that it is called.
28
/// (As of Nov 2026, Arti has no ability at all to change the Geoip database
29
/// while it's running.)
30
#[derive(Debug, serde::Deserialize, serde::Serialize, Deftly)]
31
#[derive_deftly(rpc::DynMethod)]
32
#[deftly(rpc(method_name = "arti:get_geoip_db"))]
33
struct GetGeoipDb {}
34

            
35
impl rpc::RpcMethod for GetGeoipDb {
36
    type Output = rpc::SingleIdResponse;
37

            
38
    type Update = rpc::NoUpdates;
39
}
40

            
41
/// Implement [`GetGeoipDb`] on [`ArtiRpcSession`]
42
async fn get_geoip_db_on_session(
43
    _session: Arc<ArtiRpcSession>,
44
    _method: Box<GetGeoipDb>,
45
    ctx: Arc<dyn rpc::Context>,
46
) -> Result<rpc::SingleIdResponse, rpc::RpcError> {
47
    cfg_if::cfg_if! {
48
        if #[cfg(feature = "geoip")] {
49
            let db = tor_geoip::GeoipDb::new_embedded();
50
            let id = ctx.register_owned(db);
51
            Ok(id.into())
52
        } else {
53
            todo!()
54
        }
55
    }
56
}
57

            
58
rpc::static_rpc_invoke_fn! {
59
    get_geoip_db_on_session;
60
}