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
use core::{convert::Infallible, fmt, str::FromStr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use serde::{de, Deserialize, Deserializer};
use crate::{proxy_type::ProxyType, usage_type::UsageType};
#[derive(Deserialize, Debug, Clone)]
pub struct Record {
#[serde(deserialize_with = "ip_deserialize")]
pub ip_from: IpAddr,
#[serde(deserialize_with = "ip_deserialize")]
pub ip_to: IpAddr,
pub proxy_type: Option<ProxyType>,
#[serde(with = "serde_field_with::to_and_from_string")]
pub country_code: RecordValue,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub country_name: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub region_name: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub city_name: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub isp: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub domain: Option<RecordValue>,
pub usage_type: Option<UsageType>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub asn: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub as_name: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub last_seen: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub threat: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub provider: Option<RecordValue>,
#[serde(default, with = "serde_field_with::to_and_from_string_option")]
pub residential: Option<RecordValue>,
}
fn ip_deserialize<'de, D>(deserializer: D) -> Result<IpAddr, D::Error>
where
D: Deserializer<'de>,
{
let s = Box::<str>::deserialize(deserializer)?;
if let Ok(v) = s.parse::<u32>() {
Ok(Ipv4Addr::from(v).into())
} else if let Ok(v) = s.parse::<u128>() {
Ok(Ipv6Addr::from(v).into())
} else if let Ok(v) = s.parse::<Ipv4Addr>() {
Ok(v.into())
} else if let Ok(v) = s.parse::<Ipv6Addr>() {
Ok(v.into())
} else {
Err(de::Error::custom(""))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum RecordValue {
String(Box<str>),
Usize(usize),
Unknown,
}
impl Default for RecordValue {
fn default() -> Self {
Self::Unknown
}
}
impl FromStr for RecordValue {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "-" {
Ok(Self::Unknown)
} else if let Ok(v) = s.parse::<usize>() {
Ok(Self::Usize(v))
} else {
Ok(Self::String(s.into()))
}
}
}
impl fmt::Display for RecordValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RecordValue::String(s) => write!(f, "{}", s),
RecordValue::Usize(v) => write!(f, "{}", v),
RecordValue::Unknown => write!(f, "-"),
}
}
}
impl RecordValue {
pub fn is_unknown(&self) -> bool {
matches!(self, Self::Unknown)
}
}
impl Record {
pub(crate) fn with_empty(ip_from: IpAddr, ip_to: IpAddr) -> Self {
Self {
ip_from,
ip_to,
proxy_type: Default::default(),
country_code: Default::default(),
country_name: Default::default(),
region_name: Default::default(),
city_name: Default::default(),
isp: Default::default(),
domain: Default::default(),
usage_type: Default::default(),
asn: Default::default(),
as_name: Default::default(),
last_seen: Default::default(),
threat: Default::default(),
residential: Default::default(),
provider: Default::default(),
}
}
}
impl
TryFrom<(
IpAddr,
IpAddr,
ip2location_bin_format::record_field::RecordFieldContents,
)> for Record
{
type Error = Box<str>;
fn try_from(
(ip_from, ip_to, record_field_contents): (
IpAddr,
IpAddr,
ip2location_bin_format::record_field::RecordFieldContents,
),
) -> Result<Self, Self::Error> {
use ip2location_bin_format::record_field::RecordFieldContent;
let mut record = Record::with_empty(ip_from, ip_to);
for record_field_content in record_field_contents.iter() {
match record_field_content {
RecordFieldContent::COUNTRY(_, v, v_name) => {
record.country_code = v.parse().expect("unreachable");
record.country_name = Some(v_name.parse().expect("unreachable"));
}
RecordFieldContent::REGION(_, v) => {
record.region_name = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::CITY(_, v) => {
record.city_name = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::LATITUDE(_) => {
return Err("Unknown field LATITUDE".into());
}
RecordFieldContent::LONGITUDE(_) => {
return Err("Unknown field LONGITUDE".into());
}
RecordFieldContent::ZIPCODE(_, _) => {
return Err("Unknown field ZIPCODE".into());
}
RecordFieldContent::TIMEZONE(_, _) => {
return Err("Unknown field TIMEZONE".into());
}
RecordFieldContent::PROXYTYPE(_, v) => {
let v = v
.parse::<ProxyType>()
.map_err(|err| Box::<str>::from(err.to_string()))?;
record.proxy_type = Some(v);
}
RecordFieldContent::ISP(_, v) => {
record.isp = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::DOMAIN(_, v) => {
record.domain = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::USAGETYPE(_, v) => {
let v = v
.parse::<UsageType>()
.map_err(|err| Box::<str>::from(err.to_string()))?;
record.usage_type = Some(v);
}
RecordFieldContent::ASN(_, v) => {
record.asn = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::AS(_, v) => {
record.as_name = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::LASTSEEN(_, v) => {
record.last_seen = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::THREAT(_, v) => {
record.threat = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::RESIDENTIAL(_, v) => {
record.residential = Some(v.parse().expect("unreachable"));
}
RecordFieldContent::PROVIDER(_, v) => {
record.provider = Some(v.parse().expect("unreachable"));
}
}
}
Ok(record)
}
}
#[derive(Debug, Clone, Copy)]
pub enum RecordField {
ProxyType,
CountryCodeAndName,
RegionName,
CityName,
Isp,
Domain,
UsageType,
Asn,
AsName,
LastSeen,
Threat,
Provider,
Residential,
}
impl From<&RecordField> for ip2location_bin_format::record_field::RecordField {
fn from(x: &RecordField) -> Self {
match x {
RecordField::ProxyType => Self::PROXYTYPE,
RecordField::CountryCodeAndName => Self::COUNTRY,
RecordField::RegionName => Self::REGION,
RecordField::CityName => Self::CITY,
RecordField::Isp => Self::ISP,
RecordField::Domain => Self::DOMAIN,
RecordField::UsageType => Self::USAGETYPE,
RecordField::Asn => Self::ASN,
RecordField::AsName => Self::AS,
RecordField::LastSeen => Self::LASTSEEN,
RecordField::Threat => Self::THREAT,
RecordField::Provider => Self::PROVIDER,
RecordField::Residential => Self::RESIDENTIAL,
}
}
}