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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Providers are `actix-web-location`'s abstraction to allow multiple ways of determining location.

use crate::{domain::LocationBuilder, Error, Location};
use async_trait::async_trait;

#[cfg(feature = "maxmind")]
pub use maxmind::MaxMindProvider;

#[cfg(feature = "actix-web-v3")]
use actix_web_3::HttpRequest;

#[cfg(feature = "actix-web-v4")]
use actix_web_4::HttpRequest;

/// An object that can be queried to convert [`HttpRequest`] into locations.
///
/// Use [`macro@async_trait`] when implementing.
#[async_trait(?Send)]
pub trait Provider: Send + Sync {
    /// Provide a name of the provider for use in diagnostics.
    fn name(&self) -> &str;

    /// Derive a location from a request's metadata.
    async fn get_location(&self, request: &HttpRequest) -> Result<Option<Location>, Error>;

    /// Can this provider produce locations with country information?
    fn expect_country(&self) -> bool {
        true
    }

    /// Can this provider produce locations with region information?
    fn expect_region(&self) -> bool {
        true
    }

    /// Can this provider produce locations with city information?
    fn expect_city(&self) -> bool {
        true
    }
}

/// A "dummy" provider that returns None for all fields.
pub struct FallbackProvider {
    fallback: Location,
}

impl FallbackProvider {
    /// Create a fallback provider.
    ///
    /// The passed location builder will be modified to include the provider name.
    pub fn new(fallback_builder: LocationBuilder) -> Self {
        Self {
            fallback: fallback_builder
                .provider("fallback".to_string())
                .finish()
                .expect("Location construction bug"),
        }
    }
}

#[async_trait(?Send)]
impl Provider for FallbackProvider {
    fn name(&self) -> &str {
        "fallback"
    }

    async fn get_location(&self, _request: &HttpRequest) -> Result<Option<Location>, Error> {
        Ok(Some(self.fallback.clone()))
    }
}

#[cfg(feature = "maxmind")]
mod maxmind {
    use std::{
        net::{IpAddr, SocketAddr},
        path::Path,
        sync::Arc,
    };

    use crate::domain::LocationBuilder;

    use super::{Error, Location, Provider};
    use anyhow::anyhow;
    use async_trait::async_trait;
    use lazy_static::lazy_static;
    use maxminddb::geoip2::City;

    #[cfg(feature = "actix-web-v3")]
    use actix_web_3::{http::HeaderName, HttpRequest};

    #[cfg(feature = "actix-web-v4")]
    use actix_web_4::{http::header::HeaderName, HttpRequest};

    lazy_static! {
        static ref X_FORWARDED_FOR: HeaderName = HeaderName::from_static("x-forwarded-for");
    }

    /// A provider that uses a MaxMind GeoIP database to derive location from a the IP a request was sent from.
    #[derive(Clone)]
    pub struct MaxMindProvider {
        mmdb: Arc<maxminddb::Reader<Vec<u8>>>,
    }

    impl MaxMindProvider {
        /// Read a file from the given path into memory, and use it to construct a location provider.
        pub fn from_path(path: &Path) -> Result<Self, Error> {
            Ok(Self {
                mmdb: maxminddb::Reader::open_readfile(path)
                    .map_err(|e| Error::Setup(anyhow!("{}", e)))
                    .map(Arc::new)?,
            })
        }
    }

    #[async_trait(?Send)]
    impl Provider for MaxMindProvider {
        fn name(&self) -> &str {
            "maxmind"
        }

        async fn get_location(&self, request: &HttpRequest) -> Result<Option<Location>, Error> {
            let header = request.headers().get(&*X_FORWARDED_FOR);

            let addr = if let Some(header) = header {
                // Expect a typical X-Forwarded-For where the first address is
                // the client's, the front ends should ensure this
                let value = header
                    .to_str()
                    .map_err(|e| Error::Http(e.into()))?
                    .split(',')
                    .next()
                    .unwrap_or_default()
                    .trim();
                let parsed = value
                    .parse::<IpAddr>()
                    // Fallback to parsing as SocketAddr for when a port
                    // number's included
                    .or_else(|_| value.parse::<SocketAddr>().map(|socket| socket.ip()))
                    .map_err(|e| Error::Http(e.into()))?;
                Some(parsed)
            } else {
                None
            };

            addr.map(|addr| {
                let city = self
                    .mmdb
                    .lookup::<City>(addr)
                    .map_err(|err| Error::Provider(err.into()))?;
                let builder: LocationBuilder = (city, "en").into();
                builder
                    .provider("maxmind".to_string())
                    .finish()
                    .map_err(|_| Error::Provider(anyhow::anyhow!("Bug while building location")))
            })
            .transpose()
        }
    }
}

#[cfg(test)]
pub(crate) mod tests {
    #[cfg(not(feature = "actix-web-v4"))]
    use actix_web_3::test::TestRequest;
    #[cfg(feature = "actix-web-v4")]
    use actix_web_4::test::TestRequest;

    use super::FallbackProvider;
    use crate::{Location, Provider};

    #[actix_rt::test]
    async fn fallback_works_empty() {
        let provider = FallbackProvider::new(Location::build());
        let request = TestRequest::default().to_http_request();
        let location = provider
            .get_location(&request)
            .await
            .expect("Could not get location")
            .expect("Location was none");
        assert_eq!(
            location,
            Location {
                country: None,
                region: None,
                city: None,
                dma: None,
                provider: "fallback".to_string()
            }
        )
    }

    #[actix_rt::test]
    async fn fallback_works_full() {
        let provider = FallbackProvider::new(
            Location::build()
                .country("CA".to_string())
                .region("BC".to_string())
                .city("Burnaby".to_string()),
        );
        let request = TestRequest::default().to_http_request();
        let location = provider
            .get_location(&request)
            .await
            .expect("Could not get location")
            .expect("Location was none");
        assert_eq!(
            location,
            Location {
                country: Some("CA".to_string()),
                region: Some("BC".to_string()),
                city: Some("Burnaby".to_string()),
                dma: None,
                provider: "fallback".to_string()
            }
        )
    }

    #[cfg(feature = "maxmind")]
    pub(crate) mod maxmind {
        use std::path::PathBuf;

        use crate::{providers::MaxMindProvider, Error, Location, Provider};

        #[cfg(not(feature = "actix-web-v4"))]
        use actix_web_3::test::TestRequest;
        #[cfg(feature = "actix-web-v4")]
        use actix_web_4::test::TestRequest;

        pub(crate) const MMDB_LOC: &str = "./GeoLite2-City-Test.mmdb";
        pub(crate) const TEST_ADDR_1: &str = "216.160.83.56";
        pub(crate) const TEST_ADDR_2: &str = "127.0.0.1";
        pub(crate) const TEST_ADDR_3: &str = "216.160.83.56, 127.0.0.1, 10.0.0.1";
        pub(crate) const TEST_ADDR_4: &str = "216.160.83.56:31337, 127.0.0.1";

        /// Return the expected location for [TEST_ADDR_1]
        fn test_location() -> Location {
            Location::build()
                .country("US".to_string())
                .region("WA".to_string())
                .city("Milton".to_string())
                .dma(819)
                .provider("maxmind".to_string())
                .finish()
                .expect("bug when creating location")
        }

        #[actix_rt::test]
        async fn known_ip() {
            let provider = MaxMindProvider::from_path(&PathBuf::from(MMDB_LOC))
                .expect("could not make maxmind client");

            #[cfg(not(feature = "actix-web-v4"))]
            let request = TestRequest::default()
                .header("X-Forwarded-For", TEST_ADDR_1)
                .to_http_request();
            #[cfg(feature = "actix-web-v4")]
            let request = TestRequest::default()
                .insert_header(("X-Forwarded-For", TEST_ADDR_1))
                .to_http_request();

            let location = provider
                .get_location(&request)
                .await
                .expect("could not get location")
                .expect("location was none");
            assert_eq!(location, test_location());
        }

        #[actix_rt::test]
        async fn unknown_ip() {
            let provider = MaxMindProvider::from_path(&PathBuf::from(MMDB_LOC))
                .expect("could not make maxmind client");

            #[cfg(not(feature = "actix-web-v4"))]
            let request = TestRequest::default()
                .header("X-Forwarded-For", TEST_ADDR_2)
                .to_http_request();
            #[cfg(feature = "actix-web-v4")]
            let request = TestRequest::default()
                .insert_header(("X-Forwarded-For", TEST_ADDR_2))
                .to_http_request();

            let location = provider.get_location(&request).await;
            assert!(matches!(location, Err(Error::Provider(_))));
        }

        #[actix_rt::test]
        async fn with_proxy_ips() {
            let provider = MaxMindProvider::from_path(&PathBuf::from(MMDB_LOC))
                .expect("could not make maxmind client");

            #[cfg(not(feature = "actix-web-v4"))]
            let request = TestRequest::default()
                .header("X-Forwarded-For", TEST_ADDR_3)
                .to_http_request();
            #[cfg(feature = "actix-web-v4")]
            let request = TestRequest::default()
                .insert_header(("X-Forwarded-For", TEST_ADDR_3))
                .to_http_request();

            let location = provider
                .get_location(&request)
                .await
                .expect("could not get location")
                .expect("location was none");
            assert_eq!(location, test_location());
        }

        #[actix_rt::test]
        async fn with_port() {
            let provider = MaxMindProvider::from_path(&PathBuf::from(MMDB_LOC))
                .expect("could not make maxmind client");

            #[cfg(not(feature = "actix-web-v4"))]
            let request = TestRequest::default()
                .header("X-Forwarded-For", TEST_ADDR_4)
                .to_http_request();
            #[cfg(feature = "actix-web-v4")]
            let request = TestRequest::default()
                .insert_header(("X-Forwarded-For", TEST_ADDR_4))
                .to_http_request();

            let location = provider
                .get_location(&request)
                .await
                .expect("could not get location")
                .expect("location was none");
            assert_eq!(location, test_location());
        }

        #[test]
        fn expected_info() {
            let provider = MaxMindProvider::from_path(&PathBuf::from(MMDB_LOC))
                .expect("could not make maxmind client");
            assert!(provider.expect_country());
            assert!(provider.expect_region());
            assert!(provider.expect_city());
        }
    }
}