nordselect 1.4.4

Select the ideal NordVPN server
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! The filters module consists of the Filter trait (used to implement filters) and several common inplementations of it.

use super::{Protocol, Server, ServerCategory};
use std::collections::HashSet;
use std::iter::FromIterator;

/// Way to reduce the amount of available servers.
pub trait Filter {
    /// Returns whether this server fullfills the needs of the Filter. When false, the given server
    /// should be removed from the set.
    fn filter(&self, _: &Server) -> bool;
}

/// Filter to only use servers from one specific country.
///
/// # Example
///
/// ```
/// use nordselect::Servers;
/// use nordselect::filters::CountryFilter;
///
/// let mut data = Servers::dummy_data();
/// data.filter(&CountryFilter::from("BE"));
///
/// assert_eq!(data.perfect_server().unwrap().flag, "BE");
/// ```
pub struct CountryFilter {
    /// The country on which to filter, noted according to
    /// [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
    country: String,
}

/// Ways to construct a CountryFilter.
impl CountryFilter {
    /// Creates a CountryFilter from the given country. The countrycode should be an
    /// [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code.
    #[deprecated(
        since = "1.0.0",
        note = "Inefficient, use the From-trait implementation instead"
    )]
    pub fn from_code(countrycode: String) -> CountryFilter {
        CountryFilter {
            country: countrycode.to_ascii_uppercase(),
        }
    }
}

impl Filter for CountryFilter {
    fn filter(&self, server: &Server) -> bool {
        self.country == server.flag
    }
}

impl<'a> From<&'a str> for CountryFilter {
    fn from(countrycode: &str) -> CountryFilter {
        CountryFilter {
            country: countrycode.to_ascii_uppercase(),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum Region {
    /// The [European Union](https://en.wikipedia.org/wiki/European_Union), consisting of 27 countries.
    ///
    /// Because of the Brexit, the United Kingdom is not included in this region
    EuropeanUnion,
    /// The European Economic Area, consisting of the European Union, Norway, Lichtenstein and Iceland.
    EuropeanEconomicArea,
    /// The Benelux consists of Belgium, The Netherlands and Luxembourgh
    Benelux,
    /// [5 eyes programme countries](https://en.wikipedia.org/wiki/Five_Eyes)
    FiveEyes,
    /// [6 eyes programme countries.](https://en.wikipedia.org/wiki/Five_Eyes#Other_international_cooperatives)
    SixEyes,
    /// [9 eyes programme countries.](https://en.wikipedia.org/wiki/Five_Eyes#Other_international_cooperatives)
    NineEyes,
    /// [14 eyes programme countries.](https://en.wikipedia.org/wiki/Five_Eyes#Other_international_cooperatives)
    FourteenEyes,
}

impl Region {
    /// Tries to create a Region from a string slice. Returns a Region if there's one represented
    /// by your str slice. Returns None otherwise.
    ///
    /// The provided str slice should be **uppercase**!
    pub fn from_str(region_short: &str) -> Option<Region> {
        match region_short {
            "EU" | "ЕЮ" => Some(Region::EuropeanUnion),
            "EEA" => Some(Region::EuropeanEconomicArea),
            "BENELUX" => Some(Region::Benelux),
            "5E" => Some(Region::FiveEyes),
            "6E" => Some(Region::SixEyes),
            "9E" => Some(Region::NineEyes),
            "14E" => Some(Region::FourteenEyes),
            _ => None,
        }
    }

    /// Returns all possible region codes with their respective meanings in human readable form.
    /// Useful to provide lists to your users to choose from.
    ///
    /// Using a value from index 0 of the tuple will guaranteed give a Some when calling `[from_str](#method_from_str)`
    pub fn from_str_options() -> [(&'static str, &'static str); 8] {
        [
            ("EU", "The European Union"),
            ("ЕЮ", "The European Union (Cyrillic notation)"),
            ("EEA", "The European Economic Area"),
            ("BENELUX", "Countries of the Benelux"),
            ("5E", "Countries involved in the Five Eyes programme."),
            ("6E", "Countries involved in the Six Eyes programme."),
            ("9E", "Countries involved in the Nine Eyes programme."),
            ("14E", "Countries involved in the Fourteen Eyes programme."),
        ]
    }

    /// Returns the main short notation for a given Region.
    pub fn short(&self) -> &'static str {
        match self {
            Region::EuropeanUnion => "EU",
            Region::EuropeanEconomicArea => "EEA",
            Region::Benelux => "BENELUX",
            Region::FiveEyes => "5E",
            Region::SixEyes => "6E",
            Region::NineEyes => "9E",
            Region::FourteenEyes => "14E",
        }
    }

    pub fn countries(&self) -> Vec<&str> {
        match self {
            Region::EuropeanEconomicArea => vec![
                "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
                "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE", "NO",
                "LI", "IS",
            ],
            Region::EuropeanUnion => vec![
                "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
                "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
            ],
            Region::Benelux => vec!["BE", "LU", "NL"],
            Region::FiveEyes => vec!["AU", "CA", "NZ", "GB", "US"],
            Region::SixEyes => vec!["AU", "CA", "FR", "NZ", "GB", "US"],
            Region::NineEyes => vec!["AU", "CA", "DK", "FR", "NL", "NO", "NZ", "GB", "US"],
            Region::FourteenEyes => vec![
                "AU", "BE", "CA", "DE", "DK", "ES", "FR", "IT", "NL", "NO", "NZ", "GB", "SE", "US",
            ],
        }
    }
}

/// Filter that keeps servers from any of the provided countries.
///
/// This struct can be build from your own list of countries, or it can be used with one of the
/// provided regions. To see the available regions, use [CountriesFilter::available_regions()](#method.available_regions)
///
/// # Examples
/// ```
/// use nordselect::Servers;
/// use nordselect::filters::CountriesFilter;
///
/// let mut data = Servers::dummy_data();
///
/// // Countries of the European Union.
/// data.filter(&CountriesFilter::from_region("EU").unwrap());
///
/// // The country will be one of the EU.
/// assert!(
///     CountriesFilter::region_countries("EU").unwrap()
///         .contains(&data.perfect_server().unwrap().flag.as_ref()));
/// ```
pub struct CountriesFilter {
    /// Countries which are allowed.
    countries: HashSet<String>,
}

/// Region operations
impl CountriesFilter {
    /// Builds a CountriesFilter from one of the provided regions. Regions should be given in the
    /// [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) format, but can be
    /// uppercase or lowercase.
    ///
    /// When calling this with one of the `[available_regions](method.available_regions)` will
    /// always return `Some(CountriesFilter)`.
    #[deprecated(
        since = "1.1.0",
        note = "Use the Region object instead. It has more regions and works better."
    )]
    #[allow(deprecated)]
    pub fn from_region(region: &str) -> Option<CountriesFilter> {
        match region.to_lowercase().as_ref() {
            "eu" | "ею" => Some(CountriesFilter {
                countries: HashSet::from_iter(
                    Self::region_countries("EU")
                        .unwrap()
                        .iter()
                        .map(|s| String::from(*s)),
                ),
            }),
            _ => None,
        }
    }

    /// Returns regions that can be used.
    ///
    /// When calling [from_region](method.from_region) with one of the values in the returned slice
    /// should always give a `Some`-value.
    #[deprecated(
        since = "1.1.0",
        note = "Use the Region object instead. It has more regions and works better."
    )]
    pub fn available_regions() -> &'static [&'static str] {
        &["EU", "ЕЮ"]
    }

    /// Returns the countries that are represented by the given region. Regions should be in
    /// [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) format.
    #[deprecated(
        since = "1.1.0",
        note = "Use the Region object instead. It has more regions and works better."
    )]
    pub fn region_countries(region: &str) -> Option<&'static [&'static str]> {
        match region {
            "EU" | "ЕЮ" => Some(&[
                "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
                "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
            ]),
            _ => None,
        }
    }
}

impl From<Region> for CountriesFilter {
    fn from(region: Region) -> CountriesFilter {
        CountriesFilter {
            countries: HashSet::from_iter(
                region
                    .countries()
                    .into_iter()
                    .map(String::from),
            ),
        }
    }
}

impl From<HashSet<String>> for CountriesFilter {
    fn from(countries: HashSet<String>) -> CountriesFilter {
        CountriesFilter { countries }
    }
}

impl Filter for CountriesFilter {
    fn filter(&self, server: &Server) -> bool {
        self.countries.contains(&server.flag)
    }
}

/// Filter that keeps only servers that accept a specific protocol.
///
/// # Example
///
/// ```
/// use nordselect::Servers;
/// use nordselect::Protocol;
/// use nordselect::filters::ProtocolFilter;
/// let mut data = Servers::dummy_data();
///
/// // Filter on the TCP protocol
/// data.filter(&ProtocolFilter::from(Protocol::Tcp));
///
/// assert!(data.perfect_server().is_some());
/// ```
pub struct ProtocolFilter {
    /// The protocol that should be filtered against.
    protocol: Protocol,
}

impl From<Protocol> for ProtocolFilter {
    fn from(protocol: Protocol) -> ProtocolFilter {
        ProtocolFilter { protocol }
    }
}

impl Filter for ProtocolFilter {
    fn filter(&self, server: &Server) -> bool {
        match self.protocol {
            Protocol::Tcp => server.features.openvpn_tcp,
            Protocol::Udp => server.features.openvpn_udp,
            Protocol::Pptp => server.features.pptp,
            Protocol::L2tp => server.features.l2tp,
            Protocol::OpenVPNXTcp => server.features.openvpn_tcp,
            Protocol::OpenVPNXUdp => server.features.openvpn_udp,
            Protocol::Socks => server.features.socks,
            Protocol::CyberSecProxy => server.features.proxy_cybersec,
            Protocol::SslProxy => server.features.proxy_ssl,
            Protocol::CyberSecSslProxy => server.features.proxy_ssl_cybersec,
            Protocol::Proxy => server.features.proxy,
            Protocol::WireGuardUdp => server.features.wireguard_udp,
        }
    }
}

/// Filter that keeps servers with less or equal load compared to a provided value.
///
/// # Example
///
/// ```
/// use nordselect::Servers;
/// use nordselect::filters::LoadFilter;
/// let mut data = Servers::dummy_data();
///
/// // Filter on 10% load or less.
/// data.filter(&LoadFilter::from(10));
///
/// assert!(data.perfect_server().is_some());
/// ```
pub struct LoadFilter {
    /// The maximal allowed load.
    load: u8,
}

impl From<u8> for LoadFilter {
    fn from(load: u8) -> LoadFilter {
        LoadFilter { load }
    }
}

impl Filter for LoadFilter {
    fn filter(&self, server: &Server) -> bool {
        server.load.cmp(&self.load) != std::cmp::Ordering::Greater
    }
}

/// Filter that contains multiple Filter instances. This could be more efficient, as only servers
/// fullfilling all requirements are kept.
///
/// Logically, this should be viewed as an AND-gate, as every `Filter` should allow the server to
/// be kept.
pub struct CombinedFilter {
    // The actual filters
    filters: Vec<Box<dyn Filter>>,
}

/// Ways to construct `CombinedFilters`.
impl CombinedFilter {
    /// Builds a new `CombinedFilter`.
    pub fn new() -> CombinedFilter {
        CombinedFilter {
            filters: Vec::new(),
        }
    }

    /// Builds a new `CombinedFilter` with the given capacity.
    pub fn with_capacity(capacity: usize) -> CombinedFilter {
        CombinedFilter {
            filters: Vec::with_capacity(capacity),
        }
    }
}

impl From<Vec<Box<dyn Filter>>> for CombinedFilter {
    fn from(filters: Vec<Box<dyn Filter>>) -> CombinedFilter {
        CombinedFilter { filters }
    }
}

impl CombinedFilter {
    /// Adds a new filter
    pub fn add_filter(&mut self, filter: Box<dyn Filter>) {
        self.filters.push(filter);
    }
}

impl Filter for CombinedFilter {
    fn filter(&self, server: &Server) -> bool {
        self.filters
            .iter()
            // Sorry for the confusing line of Rust code.
            .any(|filter| filter.filter(server))
    }
}

/// Filter the Servers using a given category.
///
/// # Example
///
/// ```
/// use nordselect::{Servers, ServerCategory};
/// use nordselect::filters::CategoryFilter;
/// let mut data = Servers::dummy_data();
///
/// // Filter on Standard servers.
/// data.filter(&CategoryFilter::from(ServerCategory::Standard));
///
/// assert!(data.perfect_server().is_some());
/// ```
pub struct CategoryFilter {
    category: ServerCategory,
}

impl From<ServerCategory> for CategoryFilter {
    fn from(category: ServerCategory) -> CategoryFilter {
        CategoryFilter { category }
    }
}

impl Filter for CategoryFilter {
    fn filter(&self, server: &Server) -> bool {
        server.categories.contains(&self.category)
    }
}

/// Filter that negates the results of a given filter.
///
/// # Example
///
/// ```
/// use nordselect::Servers;
/// use nordselect::filters::{CountryFilter, NegatingFilter};
///
/// let mut data = Servers::dummy_data();
/// data.filter(&NegatingFilter::new(CountryFilter::from("BE")));
///
/// assert_ne!(data.perfect_server().unwrap().flag, "BE");
/// ```
pub struct NegatingFilter(Box<dyn Filter>);

impl NegatingFilter {
    pub fn new(filter: impl Filter + 'static) -> Self {
        Self(Box::new(filter))
    }
}

impl From<Box<dyn Filter + 'static>> for NegatingFilter {
    fn from(filter: Box<dyn Filter + 'static>) -> Self {
        Self(filter)
    }
}

impl Filter for NegatingFilter {
    fn filter(&self, server: &Server) -> bool {
        !self.0.filter(server)
    }
}

#[cfg(test)]
mod tests {
    use super::super::Servers;
    use super::*;

    #[test]
    #[allow(deprecated)]
    fn country_filter_simple_legacy() {
        let mut data = Servers::dummy_data();

        data.filter(&CountryFilter::from_code("sg".to_string()));

        let server_opt = data.perfect_server();

        assert!(server_opt.is_some());
        assert_eq!(server_opt.unwrap().flag, "SG");
    }

    #[test]
    #[allow(deprecated)]
    fn country_filter_advanced_legacy() {
        let mut data = Servers::dummy_data();

        data.filter(&CountryFilter::from_code("Sg".to_string()));

        let server_opt = data.perfect_server();

        assert!(server_opt.is_some());
        assert_eq!(server_opt.unwrap().flag, "SG");
    }

    #[test]
    fn country_filter_simple() {
        let mut data = Servers::dummy_data();

        data.filter(&CountryFilter::from("sg"));

        let server_opt = data.perfect_server();

        assert!(server_opt.is_some());
        assert_eq!(server_opt.unwrap().flag, "SG");
    }

    #[test]
    fn country_filter_advanced() {
        let mut data = Servers::dummy_data();

        data.filter(&CountryFilter::from("Sg"));

        let server_opt = data.perfect_server();

        assert!(server_opt.is_some());
        assert_eq!(server_opt.unwrap().flag, "SG");
    }

    #[test]
    #[allow(deprecated)]

    fn countries_filter_regions_give_some() {
        for region in CountriesFilter::available_regions() {
            assert!(CountriesFilter::from_region(region).is_some());
        }
    }

    #[test]
    fn countries_filter_empty() {
        let mut data = Servers::dummy_data();

        data.filter(&CountriesFilter::from(HashSet::with_capacity(0)));

        let server_opt = data.perfect_server();

        assert_eq!(server_opt, None);
    }

    #[test]
    fn countries_filter_simple() {
        let mut data = Servers::dummy_data();
        let vec = vec!["AE", "AL", "AR"];

        data.filter(&CountriesFilter::from(HashSet::from_iter(
            vec.iter().map(|x| x.to_string()),
        )));

        let server_opt = data.perfect_server();

        assert!(server_opt.is_some());
        assert!(vec.contains(&server_opt.unwrap().flag.as_str()));
    }

    #[test]
    fn valid_regions() {
        assert_eq!(
            Region::from_str("EU").unwrap().countries(),
            vec![
                "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
                "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
            ]
        );
        assert_eq!(
            Region::from_str("ЕЮ").unwrap().countries(),
            vec![
                "AT", "BE", "BG", "HR", "CY", "CZ", "DK", "EE", "FI", "FR", "DE", "GR", "HU", "IE",
                "IT", "LV", "LT", "LU", "MT", "NL", "PL", "PT", "RO", "SK", "SI", "ES", "SE",
            ]
        );
        assert_eq!(
            Region::from_str("5E").unwrap().countries(),
            vec!["AU", "CA", "NZ", "GB", "US"]
        );
        assert_eq!(
            Region::from_str("6E").unwrap().countries(),
            vec!["AU", "CA", "FR", "NZ", "GB", "US"]
        );
        assert_eq!(
            Region::from_str("9E").unwrap().countries(),
            vec!["AU", "CA", "DK", "FR", "NL", "NO", "NZ", "GB", "US"]
        );
        assert_eq!(
            Region::from_str("14E").unwrap().countries(),
            vec![
                "AU", "BE", "CA", "DE", "DK", "ES", "FR", "IT", "NL", "NO", "NZ", "GB", "SE", "US",
            ],
        );

        // Make sure we do not forget a region
        for (region, _) in Region::from_str_options().iter() {
            assert!(Region::from_str(region).is_some());
        }
    }

    #[test]
    fn invalid_regions() {
        assert_eq!(Region::from_str("blablabla"), None);
        assert_eq!(Region::from_str(""), None);
        assert_eq!(Region::from_str("idk"), None);
        assert_eq!(Region::from_str("test"), None);
        assert_eq!(Region::from_str("12e"), None);
        assert_eq!(Region::from_str("15e"), None);
    }
}