Site: the big clouds are machines, not people
A Windows installation 'in Cheyenne' right after a release is Microsoft's scanner running the new program in Azure. The DB-IP Lite ASN database (fetched like the city one) tells the networks of Microsoft, Amazon, Google, Oracle, Alibaba, Tencent and IBM apart: their launches are not counted or announced, their downloads and visits count as crawlers. Small hostings are left alone: people's VPNs run on them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
7bf4d58cbf
commit
75692ede4d
4 changed files with 60 additions and 6 deletions
|
|
@ -18,7 +18,8 @@ COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
|
|||
RUN mkdir -p /app/data
|
||||
# Starts as root only to take ownership of the mounted data/, then runs as "site".
|
||||
ENV PORT=8080 STATIC_DIR=/app/static SITE_DIR=/app/site DB_PATH=/app/data/downloads.sqlite \
|
||||
GEOIP_DB=/app/data/dbip-city-ipv4.mmdb,/app/data/dbip-city-ipv6.mmdb
|
||||
GEOIP_DB=/app/data/dbip-city-ipv4.mmdb,/app/data/dbip-city-ipv6.mmdb \
|
||||
ASN_DB=/app/data/dbip-asn.mmdb
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=60s --timeout=5s --start-period=10m CMD curl -fsS http://127.0.0.1:8080/healthz || exit 1
|
||||
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
||||
|
|
|
|||
|
|
@ -75,7 +75,9 @@ instead of GitHub: the answer is GitHub's latest release (the fields the program
|
|||
in the `launches` table — one row per installation and local day: the random id the
|
||||
program made up (32 hex digits), its version, platform (`windows`, `macos`, `deb`, `rpm`,
|
||||
`arch`, `aur`, `appimage`, `flatpak`, `tarball`, `other`, `android`, `android-noengine`) and
|
||||
the country of the address (the address is not kept). The local network is not counted,
|
||||
the country of the address (the address is not kept). The local network and the big clouds (Microsoft, Amazon, Google, Oracle, Alibaba,
|
||||
Tencent, IBM — by the DB-IP Lite ASN database: security scanners run new programs there)
|
||||
are not counted,
|
||||
nor more than 5 new installations a day from one address. `/stats`
|
||||
shows installations that ran today, in 7 and 30 days, in all, and new ones in 7 days, by
|
||||
platform, and the versions of the last 7 days. A program that cannot reach the site asks
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#!/bin/sh
|
||||
# Fetches the DB-IP Lite City geolocation databases (CC BY 4.0, MaxMind DB format, via the
|
||||
# ip-location-db package on unpkg) when they are missing or older than 35 days, then
|
||||
# Fetches the DB-IP Lite City geolocation databases and the DB-IP Lite ASN database (CC BY
|
||||
# 4.0, MaxMind DB format, via the ip-location-db packages on unpkg) when they are missing
|
||||
# or older than 35 days, then
|
||||
# starts the server as the unprivileged user "site". Without them the site works and
|
||||
# downloads are stored without a country.
|
||||
set -u
|
||||
|
|
@ -22,4 +23,15 @@ for v in ipv4 ipv6; do
|
|||
fi
|
||||
fi
|
||||
done
|
||||
# The network owners (ASN): the big clouds are told apart from people.
|
||||
f=/app/data/dbip-asn.mmdb
|
||||
if [ ! -s "$f" ] || [ -n "$(find "$f" -mtime +35 2>/dev/null)" ]; then
|
||||
echo "downloading ${f##*/}"
|
||||
if curl -fsSL --retry 3 --max-time 600 -o "$f.part" "${ASN_URL:-https://unpkg.com/@ip-location-db/dbip-asn-mmdb}/dbip-asn.mmdb"; then
|
||||
mv "$f.part" "$f"
|
||||
else
|
||||
rm -f "$f.part"
|
||||
echo "network database download failed; keeping what is there"
|
||||
fi
|
||||
fi
|
||||
exec chesspenguin-site
|
||||
|
|
|
|||
|
|
@ -69,6 +69,8 @@ struct Config {
|
|||
/// Geolocation databases (MaxMind DB format): DB-IP Lite City, either the official
|
||||
/// file or the IPv4/IPv6 pair of ip-location-db; comma-separated.
|
||||
geoip_paths: Vec<PathBuf>,
|
||||
/// The network owner (ASN) database: DB-IP Lite ASN, to tell the big clouds apart.
|
||||
asn_path: PathBuf,
|
||||
repo: String,
|
||||
stats_user: String,
|
||||
stats_password: String,
|
||||
|
|
@ -85,6 +87,7 @@ impl Config {
|
|||
site_dir: var("SITE_DIR", "site").into(),
|
||||
site_url: var("SITE_URL", "http://localhost:8080"),
|
||||
db_path: var("DB_PATH", "data/downloads.sqlite").into(),
|
||||
asn_path: var("ASN_DB", "data/dbip-asn.mmdb").into(),
|
||||
geoip_paths: var("GEOIP_DB", "data/dbip-city-ipv4.mmdb,data/dbip-city-ipv6.mmdb")
|
||||
.split(',')
|
||||
.map(|s| PathBuf::from(s.trim()))
|
||||
|
|
@ -122,6 +125,7 @@ struct AppState {
|
|||
site: site::Site,
|
||||
db: Mutex<rusqlite::Connection>,
|
||||
geoip: Vec<maxminddb::Reader<Vec<u8>>>,
|
||||
asn: Option<maxminddb::Reader<Vec<u8>>>,
|
||||
http: reqwest::Client,
|
||||
/// The same without following redirects: to learn where GitHub sends a file.
|
||||
direct: reqwest::Client,
|
||||
|
|
@ -208,6 +212,13 @@ async fn main() {
|
|||
if geoip.is_empty() {
|
||||
eprintln!("geolocation off: downloads are stored without a country");
|
||||
}
|
||||
let asn = match maxminddb::Reader::open_readfile(&config.asn_path) {
|
||||
Ok(r) => Some(r),
|
||||
Err(e) => {
|
||||
eprintln!("network database {} not used: {e}; clouds are not told apart", config.asn_path.display());
|
||||
None
|
||||
}
|
||||
};
|
||||
if config.stats_password.is_empty() {
|
||||
eprintln!("STATS_PASSWORD is empty: /stats is disabled");
|
||||
}
|
||||
|
|
@ -219,6 +230,7 @@ async fn main() {
|
|||
config,
|
||||
db: Mutex::new(db),
|
||||
geoip,
|
||||
asn,
|
||||
http: reqwest::Client::builder()
|
||||
.user_agent("chesspenguin-site")
|
||||
.timeout(Duration::from_secs(15))
|
||||
|
|
@ -370,7 +382,7 @@ fn client(state: &AppState, peer: SocketAddr, headers: &HeaderMap) -> Client {
|
|||
"owner"
|
||||
} else if is_local(ip) {
|
||||
"local"
|
||||
} else if looks_like_bot(&ua, lang.as_deref()) {
|
||||
} else if looks_like_bot(&ua, lang.as_deref()) || in_cloud(state, ip) {
|
||||
"bot"
|
||||
} else {
|
||||
"human"
|
||||
|
|
@ -619,7 +631,8 @@ async fn api_update(
|
|||
.filter(|p| LAUNCH_PLATFORMS.contains(&p.as_str()));
|
||||
if let (Some(install), Some(version), Some(platform)) = (install, version, platform) {
|
||||
let ip = client_ip(&state.config, peer, &headers);
|
||||
if !is_local(ip) {
|
||||
// Neither the local network nor a cloud: a program a scanner runs is no user.
|
||||
if !is_local(ip) && !in_cloud(&state, ip) {
|
||||
let (country, region, city) = locate(&state, ip);
|
||||
let day = chrono::Local::now().format("%Y-%m-%d").to_string();
|
||||
// A few new installations a day per address (a family, a classroom); more is
|
||||
|
|
@ -837,6 +850,32 @@ fn client_ip(config: &Config, peer: SocketAddr, headers: &HeaderMap) -> IpAddr {
|
|||
/// both the GeoIP2-style records of the official DB-IP file (`country.iso_code`,
|
||||
/// `subdivisions[0].names.en`, `city.names.en`) and the flat ones of ip-location-db
|
||||
/// (`country_code`, `state1`, `city`).
|
||||
/// The big clouds (their networks, ASN): an address in them is a machine — a security
|
||||
/// scanner running a new program (Microsoft's, in Cheyenne), a crawler — not a person.
|
||||
/// Small hostings are not here: people's VPNs run on them.
|
||||
const CLOUDS: &[u32] = &[
|
||||
8075, 8068, 8069, 8070, 8071, 8072, 8073, 8074, 12076, // Microsoft (Azure)
|
||||
16509, 14618, 8987, // Amazon (AWS)
|
||||
15169, 396982, 19527, 36040, 36492, 43515, // Google (Cloud)
|
||||
31898, 792, // Oracle
|
||||
45102, 37963, 24429, // Alibaba
|
||||
132203, 45090, // Tencent
|
||||
36351, // IBM (SoftLayer)
|
||||
];
|
||||
|
||||
/// The address is in a big cloud (see [`CLOUDS`]).
|
||||
fn in_cloud(state: &AppState, ip: IpAddr) -> bool {
|
||||
let Some(reader) = &state.asn else {
|
||||
return false;
|
||||
};
|
||||
reader
|
||||
.lookup(ip)
|
||||
.ok()
|
||||
.and_then(|r| r.decode::<serde_json::Value>().ok().flatten())
|
||||
.and_then(|r| r["autonomous_system_number"].as_u64())
|
||||
.is_some_and(|n| CLOUDS.contains(&(n as u32)))
|
||||
}
|
||||
|
||||
fn locate(state: &AppState, ip: IpAddr) -> (Option<String>, Option<String>, Option<String>) {
|
||||
let record = state.geoip.iter().find_map(|reader| {
|
||||
reader
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue