tower_ipfilter/
lib.rs

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
use std::net::IpAddr;

pub mod types;
mod compress;
mod extract;
mod body;
pub mod geo_filter;
pub mod ip_filter;
pub mod network_filter_service;
pub mod connection_info_service;



pub trait IpServiceTrait: Send + Sync {
    async fn add_ip(&self, ip: IpAddr, reason: String, date: String);
    fn is_ip_blocked(&self, ip: &IpAddr) -> impl std::future::Future<Output = bool> + Send;
}
#[cfg(test)]
mod tests {
    use dashmap::DashMap;
    use geo_filter::GeoIpv4Filter;
    use ipnetwork::{IpNetwork, Ipv4Network};
    use types::{CountryLocation};

    use super::*;
    use std::net::{IpAddr, Ipv4Addr};
    use std::str::FromStr;

    fn create_test_geo_ip_service() -> GeoIpv4Filter {
        let ip_networks = DashMap::new();

        // Add some test data
        ip_networks.insert(Ipv4Network::from_str("192.168.0.0/16").unwrap(), CountryLocation {
            geoname_id: 1,
            locale_code: "EN".to_string(),
            continent_code: "EU".to_string(),
            continent_name: "Europe".to_string(),
            country_iso_code: Some("GB".to_string()),
            country_name: Some("United Kingdom".to_string()),
            is_in_european_union: false,
        });
        ip_networks.insert(Ipv4Network::from_str("10.0.0.0/8").unwrap(), CountryLocation {
            geoname_id: 2,
            locale_code: "EN".to_string(),
            continent_code: "NA".to_string(),
            continent_name: "North America".to_string(),
            country_iso_code: Some("US".to_string()),
            country_name: Some("United States".to_string()),
            is_in_european_union: false,
        });
        ip_networks.insert(Ipv4Network::from_str("172.16.0.0/12").unwrap(),  CountryLocation {
            geoname_id: 3,
            locale_code: "FR".to_string(),
            continent_code: "EU".to_string(),
            continent_name: "Europe".to_string(),
            country_iso_code: Some("FR".to_string()),
            country_name: Some("France".to_string()),
            is_in_european_union: true,
        });
        //ip_networks.insert(Ipv4Network::from_str("2001:db8::/32").unwrap(), CountryLocation {
        //    geoname_id: 4,
        //    locale_code: "JA".to_string(),
        //    continent_code: "AS".to_string(),
        //    continent_name: "Asia".to_string(),
        //    country_iso_code: Some("JP".to_string()),
        //    country_name: Some("Japan".to_string()),
        //    is_in_european_union: false,
        //});


        GeoIpv4Filter {
            networks: ip_networks,
            addresses: DashMap::new(),
            countries: DashMap::new(),
            mode: Default::default(),
        }
    }

    #[tokio::test]
    async fn test_get_country_for_ip() {
        let service = create_test_geo_ip_service();

        // Test IPv4 addresses
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("192.168.1.1").unwrap()).await.unwrap().country_name,
            Some("United Kingdom".to_string())
        );
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("10.0.0.1").unwrap()).await.unwrap().country_name,
            Some("United States".to_string())
        );
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("172.16.0.1").unwrap()).await.unwrap().country_name,
            Some("France".to_string())
        );

        // Test IPv6 address
        //assert_eq!(
        //    service.get_country_for_ip(&Ipv4Addr::from_str("2001:db8::1").unwrap()).await.unwrap().country_name,
        //    Some("Japan".to_string())
        //);
//
        //// Test IP address not in any network
        //assert_eq!(
        //    service.get_country_for_ip(&Ipv4Addr::from_str("8.8.8.8").unwrap()).await,
        //    None
        //);
    }

    #[tokio::test]
    async fn test_get_country_for_ip_edge_cases() {
        let service = create_test_geo_ip_service();

        // Test edge of network
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("192.168.255.255").unwrap()).await.unwrap().country_name,
            Some("United Kingdom".to_string())
        );

        // Test start of network
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("10.0.0.0").unwrap()).await.unwrap().country_name,
            Some("United States".to_string())
        );

        // Test end of network
        assert_eq!(
            service.get_country_for_ip(&Ipv4Addr::from_str("10.255.255.255").unwrap()).await.unwrap().country_name,
            Some("United States".to_string())
        );
    }

    #[tokio::test]
    
    async fn test_blocklist() {
        let service = create_test_geo_ip_service();

        // Set up blocklist
        service.set_countries(vec!["United States".to_string(), "France".to_string()]);

        // Test blocked countries
        assert!(service.is_country_blocked("United States").await);
        assert!(service.is_country_blocked("France").await);
        assert!(!service.is_country_blocked("United Kingdom").await);
        assert!(!service.is_country_blocked("Japan").await);

        // Test blocked IPs
        assert!(service.is_ip_blocked(&Ipv4Addr::from_str("10.0.0.1").unwrap()).await); // US
        assert!(service.is_ip_blocked(&Ipv4Addr::from_str("172.16.0.1").unwrap()).await); // France
        assert!(!service.is_ip_blocked(&Ipv4Addr::from_str("192.168.1.1").unwrap()).await); // UK
        //assert!(!service.is_ip_blocked(&Ipv4Addr::from_str("2001:db8::1").unwrap()).await); // Japan

        // Test IP not in any network
        assert!(!service.is_ip_blocked(&Ipv4Addr::from_str("8.8.8.8").unwrap()).await);

        // Update blocklist
        service.set_countries(vec!["Japan".to_string()]);

        // Test updated blocklist
        assert!(!service.is_ip_blocked(&Ipv4Addr::from_str("10.0.0.1").unwrap()).await); // US
        //assert!(service.is_ip_blocked(&Ipv4Addr::from_str("2001:db8::1").unwrap()).await); // Japan
    }
}