Updated maxminddb dependency, added location info to lookup

This commit is contained in:
Slatian 2023-02-24 00:42:15 +01:00
parent da13444a2e
commit 4881f76b5b
5 changed files with 55 additions and 17 deletions

15
Cargo.lock generated
View File

@ -714,6 +714,15 @@ version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
[[package]]
name = "ipnetwork"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4088d739b183546b239688ddbc79891831df421773df95e236daf7867866d355"
dependencies = [
"serde",
]
[[package]]
name = "is-terminal"
version = "0.4.3"
@ -822,11 +831,13 @@ checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40"
[[package]]
name = "maxminddb"
version = "0.17.3"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d13fa57adcc4f3aca91e511b3cdaa58ed8cbcbf97f20e342a11218c76e127f51"
checksum = "fe2ba61113f9f7a9f0e87c519682d39c43a6f3f79c2cc42c3ba3dda83b1fa334"
dependencies = [
"ipnetwork",
"log",
"memchr",
"serde",
]

View File

@ -19,4 +19,4 @@ tera = "1"
toml = "0.7"
tower = "*"
trust-dns-resolver = "0.22"
maxminddb = "0.17"
maxminddb = "0.23"

View File

@ -21,8 +21,8 @@ pub struct NamedLocation {
#[derive(serde::Deserialize, serde::Serialize, Default, Copy, Clone)]
pub struct LocationCoordinates {
latitude: f64,
logtitude: f64,
lat: f64,
lon: f64,
}
#[derive(serde::Deserialize, serde::Serialize, Default, Clone)]
@ -34,6 +34,8 @@ pub struct LocationResult {
subdivisions: Option<Vec<NamedLocation>>,
city: Option<NamedLocation>,
coordinates: Option<LocationCoordinates>,
accuracy: Option<u16>,
postal_code: Option<String>,
time_zone: Option<String>,
}
@ -76,7 +78,7 @@ names: &Option<BTreeMap<&str, &str>>,
}
}
pub fn geoip2_city_to_named_location(item: geoip2::model::City, languages: &Vec<&String>) -> NamedLocation {
pub fn geoip2_city_to_named_location(item: geoip2::city::City, languages: &Vec<&String>) -> NamedLocation {
NamedLocation {
iso_code: None,
geoname_id: item.geoname_id,
@ -84,7 +86,7 @@ pub fn geoip2_city_to_named_location(item: geoip2::model::City, languages: &Vec<
}
}
pub fn geoip2_continent_to_named_location(item: geoip2::model::Continent, languages: &Vec<&String>) -> NamedLocation {
pub fn geoip2_continent_to_named_location(item: geoip2::country::Continent, languages: &Vec<&String>) -> NamedLocation {
NamedLocation {
iso_code: item.code.map(ToString::to_string),
geoname_id: item.geoname_id,
@ -92,7 +94,7 @@ pub fn geoip2_continent_to_named_location(item: geoip2::model::Continent, langua
}
}
pub fn geoip2_country_to_named_location(item: geoip2::model::Country, languages: &Vec<&String>) -> NamedLocation {
pub fn geoip2_country_to_named_location(item: geoip2::country::Country, languages: &Vec<&String>) -> NamedLocation {
NamedLocation {
iso_code: item.iso_code.map(ToString::to_string),
geoname_id: item.geoname_id,
@ -100,7 +102,7 @@ pub fn geoip2_country_to_named_location(item: geoip2::model::Country, languages:
}
}
pub fn geoip2_represented_country_to_named_location(item: geoip2::model::RepresentedCountry, languages: &Vec<&String>) -> NamedLocation {
pub fn geoip2_represented_country_to_named_location(item: geoip2::country::RepresentedCountry, languages: &Vec<&String>) -> NamedLocation {
NamedLocation {
iso_code: item.iso_code.map(ToString::to_string),
geoname_id: item.geoname_id,
@ -108,7 +110,7 @@ pub fn geoip2_represented_country_to_named_location(item: geoip2::model::Represe
}
}
pub fn geoip2_subdivision_to_named_location(item: geoip2::model::Subdivision, languages: &Vec<&String>) -> NamedLocation {
pub fn geoip2_subdivision_to_named_location(item: geoip2::city::Subdivision, languages: &Vec<&String>) -> NamedLocation {
NamedLocation {
iso_code: item.iso_code.map(ToString::to_string),
geoname_id: item.geoname_id,
@ -169,6 +171,23 @@ impl QueryLocation for MMDBCarrier {
},
None => None,
},
coordinates: match &res.location {
Some(loc) => {
if loc.latitude.is_some() && loc.longitude.is_some() {
Some(LocationCoordinates {
lat: loc.latitude.unwrap_or(0.0),
lon: loc.longitude.unwrap_or(0.0),
})
} else {
None
}
},
None => None,
},
accuracy: match &res.location {
Some(loc) => loc.accuracy_radius,
None => None,
},
postal_code: match res.postal {
Some(p) => p.code.map(ToString::to_string),
None => None,

View File

@ -1,6 +1,6 @@
{% macro place_dl(place, label="", iso_code_prefix="") %}
{% if place %}
{% if label %}<dh>{{label}}</dh>{% endif %}
{% if label %}<dt>{{label}}</dt>{% endif %}
<dd>{{place.name}} {% if place.iso_code%}({% if iso_code_prefix %}{{iso_code_prefix}}-{% endif %}{{place.iso_code}}){% endif %}</dd>
{% endif %}
{% endmacro place_dl %}

View File

@ -13,16 +13,16 @@
<section>
<h2>Network Information</h2>
<dl>
<dh>Type of Address</dh>
<dt>Type of Address</dt>
<dd>{{r.ip_info.scope | title}} {{r.ip_info.cast | title}} IPv{% if r.ip_info.is_v6_address %}6{% else %}4{% endif %}</dd>
{% if r.hostname %}
<dh>Hostname</dh>
<dt>Hostname</dt>
<dd>{{ helper::dig(extra=extra, name=r.hostname) }}</dd>
{% endif %}
{% if r.asn %}
<dh><abbr="Autonomous System Number">ASN</abbr></dh>
<dt><abbr="Autonomous System Number">ASN</abbr></dt>
<dd>AS{{r.asn.asn}}</dd>
<dh>AS Name</dh>
<dt>AS Name</dt>
<dd>{{ r.asn.name }}</dd>
</dl>
{% endif %}
@ -47,13 +47,21 @@
{% endif %}
{{ helper::place_dl(place=r.location.city, label="City") }}
{% if r.location.postal_code %}
<dh>Postal Code</dh>
<dt>Postal Code</dt>
<dd>{{r.location.postal_code}}</dd>
{% endif %}
{% if r.location.time_zone %}
<dh>Timezone</dh>
<dt>Timezone</dt>
<dd>{{r.location.time_zone}}</dd>
{% endif %}
{% if r.location.accuracy %}
<dt>Accuaracy</dt>
<dd>~{{r.location.accuracy}}km</dd>
{% endif %}
{% if r.location.coordinates %}
<dt>Coordinates</dt>
<dd><a href="{{ links::map_link(lat=r.location.coordinates.lat, lon=r.location.coordinates.lon)}}">lat: {{r.location.coordinates.lat}}, lon: {{r.location.coordinates.lon}}</a></dd>
{% endif %}
</dl>
<!--We have to put that there to comply with maxminds licensing-->
<p><small>