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

use chrono::naive::NaiveDateTime;

use iso_country::Country;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Action {
    Allow,
    Block,
}

impl Default for Action {
    fn default() -> Self {
        Self::Block
    }
}

#[derive(Serialize, Clone, Debug, PartialEq)]
pub enum Countries {
    #[serde(rename = "country")]
    AllowList(String),
    #[serde(rename = "not_country")]
    BlockList(String),
}

// TODO: as far as I know this only takes 2 letter lang codes, so maybe adding try build is for the
// best. This also allows us to switch the builder to store the list in a single internal Vec
// TODO: there's probably some resource that provides the 2 letter lang codes. Look around
// TODO: provide a `countries` method for specifying multiple at once
// TODO: does passing in an empty list of countries serialize correctly?
impl Countries {
    #[must_use]
    pub fn allow() -> CountriesBuilder {
        CountriesBuilder::new(Action::Allow)
    }

    #[must_use]
    pub fn block() -> CountriesBuilder {
        CountriesBuilder::new(Action::Block)
    }

    #[must_use]
    pub fn allowlist(countries: &[Country]) -> Self {
        Self::allow().countries(countries).build()
    }

    #[must_use]
    pub fn blocklist(countries: &[Country]) -> Self {
        Self::block().countries(countries).build()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        match self {
            Self::AllowList(countries) => countries.is_empty(),
            Self::BlockList(countries) => countries.is_empty(),
        }
    }
}

impl Default for Countries {
    fn default() -> Self {
        CountriesBuilder::default().build()
    }
}

impl From<CountriesBuilder> for Countries {
    fn from(builder: CountriesBuilder) -> Self {
        let CountriesBuilder { list, action } = builder;

        match action {
            Action::Allow => Self::AllowList(list.join(",")),
            Action::Block => Self::BlockList(list.join(",")),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct CountriesBuilder {
    list: Vec<String>,
    action: Action,
}

impl CountriesBuilder {
    #[must_use]
    fn new(action: Action) -> Self {
        Self {
            list: Vec::new(),
            action,
        }
    }

    #[must_use]
    pub fn country(mut self, country: Country) -> Self {
        self.list.push(country.to_string());
        self
    }

    #[must_use]
    pub fn countries(mut self, countries: &[Country]) -> Self {
        for country in countries {
            self = self.country(*country);
        }

        self
    }

    #[must_use]
    pub fn build(self) -> Countries {
        Countries::from(self)
    }
}

#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Level {
    Anonymous,
    Elite,
}

#[derive(Deserialize, Serialize, Clone, Copy, Debug, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
    Http,
    Socks4,
    Socks5,
}

// TODO: Is this needed? Can we just pull out "data" directly somehow?
// Note: Interal api only
#[doc(hidden)]
#[derive(Deserialize, Clone, Debug, PartialEq)]
pub struct Response {
    pub data: Vec<Proxy>,
}

#[derive(Deserialize, Clone, Debug, PartialEq)]
pub struct Proxy {
    // TODO: Combine this and the port number for a socketaddr? How to handle this
    pub ip: Ipv4Addr,
    // TODO: switch to non-zero u16
    pub port: u16,
    // TODO: I've seen "", how does this handle that
    pub country: Country,
    // #[serde(deserialize_with = "deserialize_date")]
    pub last_checked: NaiveDateTime,
    #[serde(rename = "proxy_level")]
    pub level: Level,
    #[serde(rename = "type")]
    pub protocol: Protocol,
    #[serde(rename = "speed")]
    // TODO: switch to duration (would be more explicit that it's minutes at least)
    pub time_to_connect: u8,
    #[serde(rename = "support")]
    pub supports: Supports,
}

// TODO: I've seen null, how does this handle that
#[derive(Deserialize, Clone, Copy, Debug, Default, PartialEq)]
pub struct Supports {
    pub https: bool,
    pub get: bool,
    pub post: bool,
    pub cookies: bool,
    pub referer: bool,
    pub forwards_user_agent: bool,
    pub connects_to_google: bool,
}