From e4422fcb93af95fa2bd119b13160324f4f404c1d Mon Sep 17 00:00:00 2001 From: funtimes909 <68979259+Funtimes909@users.noreply.github.com> Date: Mon, 6 Apr 2026 12:30:40 +1200 Subject: [PATCH] Add last_seen field to database migration script --- migrations/20260327_init.sql | 3 ++- src/scanning/discovery.rs | 14 +++++++------- src/scanning/mod.rs | 31 ++++++++++++++++++++----------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/migrations/20260327_init.sql b/migrations/20260327_init.sql index 0b5ad6a..60b1552 100644 --- a/migrations/20260327_init.sql +++ b/migrations/20260327_init.sql @@ -1,7 +1,8 @@ create table if not exists favicons ( hash bytea primary key, data text not null, - first_seen timestamp without time zone not null default now () + first_seen timestamp without time zone not null default now (), + last_seen timestamp without time zone not null default now () ); create table if not exists countries ( diff --git a/src/scanning/discovery.rs b/src/scanning/discovery.rs index 78ae087..1d77222 100644 --- a/src/scanning/discovery.rs +++ b/src/scanning/discovery.rs @@ -6,8 +6,8 @@ use tokio::process::Command; use tracing::debug; use crate::CONFIG; -use crate::database::ServerUpdateOperation; use crate::database::Database; +use crate::database::ServerUpdateOperation; pub struct DiscoveryScanner { pub database: Database, @@ -34,12 +34,6 @@ impl DiscoveryScanner { while let Ok(Some(line)) = reader.next_line().await { let mut line = line.split_whitespace(); - // Address - let address = match line.nth(1).and_then(|a| Ipv4Addr::from_str(a).ok()) { - Some(address) => address, - _ => continue, - }; - // Port let port = match line .nth(3) @@ -52,6 +46,12 @@ impl DiscoveryScanner { None => continue, }; + // Address + let address = match line.nth(1).and_then(|a| Ipv4Addr::from_str(a).ok()) { + Some(address) => address, + None => continue, + }; + // Immediately ping any returned servers let completed_server = super::run(address, port).await; diff --git a/src/scanning/mod.rs b/src/scanning/mod.rs index a7a781c..a5801ee 100644 --- a/src/scanning/mod.rs +++ b/src/scanning/mod.rs @@ -1,5 +1,8 @@ -use std::net::{Ipv4Addr, SocketAddrV4}; use anyhow::bail; +use std::{ + net::{Ipv4Addr, SocketAddrV4}, + time::Duration, +}; use tokio::{net::TcpStream, task::JoinHandle}; use crate::protocol::{minecraft, response::MinecraftServer}; @@ -12,14 +15,20 @@ pub fn run(address: Ipv4Addr, port: u16) -> JoinHandle { - match serde_json::from_str::(&s) { - Ok(server) => Ok(server), - Err(e) => bail!("{e}"), - } - } - Err(e) => bail!("{e}") - } + return match tokio::time::timeout( + Duration::from_secs(4), + minecraft::simple_ping(&mut stream), + ) + .await + { + Ok(Ok(s)) => { + match serde_json::from_str::(&s) { + Ok(server) => Ok(server), + Err(e) => bail!("{e}"), + } + } + Ok(Err(e)) => bail!("{e}"), + Err(e) => bail!("{e}"), + }; }) -} \ No newline at end of file +}