Skip to content

Commit ca9b906

Browse files
authored
Merge pull request #46 from dimacurrentai/maxminddb
Added `step08` with `maxminddb`.
2 parents 8c088ef + b67cbc1 commit ca9b906

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

Dockerfile.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ FROM alpine AS build
44
# Intentionally not installing `cargo`, etc. But it does need `clang`.
55
RUN apk add rustup clang musl
66

7+
# PLACEHOLDER_BUILD
8+
79
# Init local rust via `rustup`, including static targets builder.
810
RUN rustup-init -y -t "$(uname -m)-unknown-linux-musl" --no-modify-path
911
RUN mv /root/.cargo/bin/* /usr/local/bin/
@@ -23,4 +25,5 @@ RUN cargo test --release -- --test-threads=1
2325
# The resulting container with the static binary only.
2426
FROM scratch
2527
COPY --from=build /binary /binary
28+
# PLACEHOLDER_RUN
2629
ENTRYPOINT ["/binary"]

step08_maxminddb/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Dockerfile
2+
GeoLite2-*.mmdb

step08_maxminddb/code/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "maxmindb"
3+
edition = "2021"
4+
5+
[dependencies]
6+
maxminddb = "0.21.0"
7+
tokio = { version = "1.0", features = ["full"] }
8+
trust-dns-resolver = "0.23.2"

step08_maxminddb/code/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use maxminddb::Reader;
2+
use std::net::IpAddr;
3+
use std::path::Path;
4+
use trust_dns_resolver::config::{NameServerConfig, Protocol, ResolverConfig, ResolverOpts};
5+
use trust_dns_resolver::TokioAsyncResolver;
6+
7+
const OPENDNS_SERVER: &str = "208.67.222.222:53";
8+
const OPENDNS_MYIP_DOMAIN: &str = "myip.opendns.com.";
9+
const GEOIP_DB_PATH: &str = "GeoLite2-Country.mmdb";
10+
11+
#[tokio::main]
12+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
13+
if let Ok(ip) = get_self_ip().await {
14+
println!("Public IP: {}", ip);
15+
if let Err(e) = map_ip_to_geo(ip) {
16+
println!("Error mapping IP to geo: {}", e);
17+
}
18+
} else {
19+
println!("Failed to get your IP address");
20+
}
21+
Ok(())
22+
}
23+
24+
async fn get_self_ip() -> Result<IpAddr, Box<dyn std::error::Error>> {
25+
let mut config = ResolverConfig::new();
26+
config.add_name_server(NameServerConfig {
27+
socket_addr: OPENDNS_SERVER.parse()?,
28+
protocol: Protocol::Udp,
29+
tls_dns_name: None,
30+
trust_negative_responses: false,
31+
bind_addr: None,
32+
});
33+
let resolver = TokioAsyncResolver::tokio(config, ResolverOpts::default());
34+
let response = resolver.lookup_ip(OPENDNS_MYIP_DOMAIN).await?;
35+
response.iter().next().ok_or_else(|| "No IP found".into())
36+
}
37+
38+
fn map_ip_to_geo(ip: IpAddr) -> Result<(), Box<dyn std::error::Error>> {
39+
let db_path = Path::new(GEOIP_DB_PATH);
40+
if !db_path.exists() {
41+
return Err(format!("GeoIP database file not found: {}", GEOIP_DB_PATH).into());
42+
}
43+
44+
let reader = Reader::open_readfile(GEOIP_DB_PATH)?;
45+
let result: maxminddb::geoip2::City = reader.lookup(ip)?;
46+
47+
println!("{:#?}", result);
48+
49+
let iso = result.country.unwrap().iso_code;
50+
println!("{iso:?}");
51+
Ok(())
52+
}

step08_maxminddb/run.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
cat ../Dockerfile.template | grep -B 999999 '^# PLACEHOLDER_BUILD$' >Dockerfile
6+
7+
echo 'RUN apk add wget' >>Dockerfile
8+
echo 'RUN wget https://github.com/P3TERX/GeoLite.mmdb/raw/download/GeoLite2-Country.mmdb' >>Dockerfile
9+
10+
cat ../Dockerfile.template | grep -A 999999 '^# PLACEHOLDER_BUILD$' | grep -B 999999 '^# PLACEHOLDER_RUN$' >>Dockerfile
11+
12+
echo 'COPY --from=build /GeoLite2-Country.mmdb /' >>Dockerfile
13+
14+
cat ../Dockerfile.template | grep -A 999999 '^# PLACEHOLDER_RUN$' ../Dockerfile.template >>Dockerfile
15+
16+
docker build . -t demo
17+
docker run --rm -t demo

0 commit comments

Comments
 (0)