Skip to content

Commit bba9e19

Browse files
committed
format codebase
1 parent 3183b1a commit bba9e19

15 files changed

Lines changed: 105 additions & 83 deletions

File tree

migration/src/m20260406_000008_optimize_database_types.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,8 @@ impl MigrationTrait for Migration {
5555
// ── Step 1: Convert IP columns from TEXT to INET ─────────────────────
5656
println!("Converting IP columns from TEXT to INET...");
5757

58-
db.execute_unprepared(
59-
"ALTER TABLE servers ALTER COLUMN ip TYPE INET USING ip::INET",
60-
)
61-
.await?;
58+
db.execute_unprepared("ALTER TABLE servers ALTER COLUMN ip TYPE INET USING ip::INET")
59+
.await?;
6260
db.execute_unprepared(
6361
"ALTER TABLE server_players ALTER COLUMN ip TYPE INET USING ip::INET",
6462
)
@@ -257,10 +255,8 @@ impl MigrationTrait for Migration {
257255
"ALTER TABLE server_players ALTER COLUMN ip TYPE TEXT USING ip::TEXT",
258256
)
259257
.await?;
260-
db.execute_unprepared(
261-
"ALTER TABLE servers ALTER COLUMN ip TYPE TEXT USING ip::TEXT",
262-
)
263-
.await?;
258+
db.execute_unprepared("ALTER TABLE servers ALTER COLUMN ip TYPE TEXT USING ip::TEXT")
259+
.await?;
264260

265261
// Recreate FKs
266262
db.execute_unprepared(

migration/src/m20260408_000009_add_scaling_indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl MigrationTrait for Migration {
2828

2929
// Index for discovery range queries: get_ranges_to_scan joins
3030
// asn_ranges → asns and filters by category, then orders by last_scanned_at.
31-
// The category filter comes from the ASNs table, but indexing asn,
31+
// The category filter comes from the ASNs table, but indexing asn,
3232
// last_scanned_at helps the join and sort.
3333
db.execute_unprepared(
3434
"CREATE INDEX IF NOT EXISTS idx_asn_ranges_asn_lastscanned \

packages/api/src/handlers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
//! - GET / - Static dashboard (fallback to assets)
1717
1818
use axum::{
19+
Router,
1920
extract::{Path, Query, Request, State},
2021
http::{HeaderMap, StatusCode},
2122
middleware::{self, Next},
2223
response::{Json, Response},
2324
routing::{get, post},
24-
Router,
2525
};
2626
use serde::{Deserialize, Serialize};
2727
use std::sync::Arc;

packages/api/src/main.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
//! Web API and database management service for Minecraft server scanning.
44
//! This service can run independently of the scanner service.
55
6+
use migration::Migrator;
67
use nmcscan_shared::models::entities::{asns, servers};
78
use nmcscan_shared::repositories::{
89
ApiKeyRepository, AsnRepository, ServerRepository, StatsRepository,
910
};
1011
use nmcscan_shared::services::asn_fetcher::AsnFetcher;
1112
use nmcscan_shared::utils::exclude::{ExcludeList, ExcludeManager};
12-
use migration::Migrator;
13-
use sea_orm::{ColumnTrait, ConnectOptions, Database, EntityTrait, PaginatorTrait, QueryFilter, QuerySelect};
13+
use sea_orm::{
14+
ColumnTrait, ConnectOptions, Database, EntityTrait, PaginatorTrait, QueryFilter, QuerySelect,
15+
};
1416
use sea_orm_migration::MigratorTrait;
1517
use std::sync::Arc;
1618
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@@ -21,7 +23,11 @@ mod handlers;
2123

2224
/// NMCScan API Service arguments
2325
#[derive(Parser, Debug)]
24-
#[command(author, version, about = "NMCScan API Service - Web interface and database management")]
26+
#[command(
27+
author,
28+
version,
29+
about = "NMCScan API Service - Web interface and database management"
30+
)]
2531
struct Args {
2632
/// Log level (debug, info, warn, error)
2733
#[arg(short, long, env = "RUST_LOG", default_value = "info")]
@@ -104,7 +110,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
104110
tracing::warn!("Failed to parse exclude list: {}", e);
105111
ExcludeList::from_str("").unwrap()
106112
});
107-
tracing::info!("Loaded {} exclude networks (including honeypots)", exclude_list.len());
113+
tracing::info!(
114+
"Loaded {} exclude networks (including honeypots)",
115+
exclude_list.len()
116+
);
108117

109118
// 2. Initialize database and run migrations
110119
tracing::info!("Initializing database at {}...", args.database);
@@ -126,12 +135,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
126135
let asn_repo = Arc::new(AsnRepository::new((*db).clone()));
127136
let stats_repo = Arc::new(StatsRepository::new((*db).clone()));
128137
let api_key_repo = Arc::new(ApiKeyRepository::new((*db).clone()));
129-
let minecraft_account_repo = Arc::new(nmcscan_shared::repositories::MinecraftAccountRepository::new((*db).clone()));
138+
let minecraft_account_repo =
139+
Arc::new(nmcscan_shared::repositories::MinecraftAccountRepository::new((*db).clone()));
130140

131141
// 4. Initialize ASN fetcher and import/update ASN data
132142
tracing::info!("Initializing ASN fetcher...");
133143
let asn_fetcher = Arc::new(AsnFetcher::new(Arc::clone(&db), Arc::clone(&asn_repo)));
134-
144+
135145
// Full import if forced or if data is missing
136146
let range_count = asn_fetcher.asn_manager().read().await.range_count();
137147
let asn_count = asn_fetcher.asn_manager().read().await.asn_count();
@@ -162,10 +172,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
162172
.await
163173
.unwrap_or(0);
164174

165-
let total_count = asns::Entity::find()
166-
.count(&*db)
167-
.await
168-
.unwrap_or(1);
175+
let total_count = asns::Entity::find().count(&*db).await.unwrap_or(1);
169176

170177
let unknown_percentage = if total_count > 0 {
171178
(unknown_count as f64 / total_count as f64) * 100.0
@@ -183,7 +190,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
183190
let ipverse_map = asn_fetcher.fetch_ipverse_map().await;
184191
match asn_fetcher.recategorize_all_asns(&ipverse_map).await {
185192
Ok(updated) => {
186-
tracing::info!("Startup recategorization complete: {} ASNs reclassified", updated);
193+
tracing::info!(
194+
"Startup recategorization complete: {} ASNs reclassified",
195+
updated
196+
);
187197
}
188198
Err(e) => {
189199
tracing::error!("Startup recategorization failed: {}", e);
@@ -195,19 +205,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
195205
// 5. Backfill ASN data for existing servers
196206
{
197207
let db_clone = Arc::clone(&db);
198-
let servers_res: Result<Vec<servers::Model>, sea_orm::DbErr> =
199-
servers::Entity::find()
200-
.filter(servers::Column::Asn.is_null())
201-
.filter(servers::Column::Status.ne("ignored"))
202-
.limit(5000)
203-
.all(&*db_clone)
204-
.await;
208+
let servers_res: Result<Vec<servers::Model>, sea_orm::DbErr> = servers::Entity::find()
209+
.filter(servers::Column::Asn.is_null())
210+
.filter(servers::Column::Status.ne("ignored"))
211+
.limit(5000)
212+
.all(&*db_clone)
213+
.await;
205214
if let Ok(srvs) = servers_res {
206215
if !srvs.is_empty() {
207216
tracing::info!("Backfilling ASN data for {} servers...", srvs.len());
208217
// Note: Full backfill would require the ASN fetcher to look up each IP
209218
// This is a lighter version for the API service
210-
tracing::info!("Backfill skipped in API-only mode (scanner will handle on startup)");
219+
tracing::info!(
220+
"Backfill skipped in API-only mode (scanner will handle on startup)"
221+
);
211222
}
212223
}
213224
}
@@ -259,7 +270,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
259270
contact_email: args.contact_email.clone(),
260271
discord_link: args.discord_link.clone(),
261272
};
262-
273+
263274
let listen_addr = args.listen_addr.clone();
264275
let listen_addr_log = listen_addr.clone();
265276
let api_handle = tokio::spawn(async move {
@@ -268,7 +279,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
268279

269280
tracing::info!("✅ API Service started on {}", listen_addr_log);
270281
tracing::info!(" Database: connected");
271-
tracing::info!(" ASN Manager: initialized ({} ASNs, {} ranges)", asn_count, range_count);
282+
tracing::info!(
283+
" ASN Manager: initialized ({} ASNs, {} ranges)",
284+
asn_count,
285+
range_count
286+
);
272287
tracing::info!(" Scanner: NOT running (separate nmcscan-scanner service)");
273288
tracing::info!(" Login queue: NOT running (part of scanner service)");
274289

packages/scanner/src/login_queue.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
//! This gives the scanner time to do its initial SLP pass and populate version data.
99
1010
use std::net::SocketAddr;
11-
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
1211
use std::sync::Arc;
12+
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
1313
use tokio::sync::{Mutex, Semaphore};
1414
use tokio::time::{self, Duration};
1515

16-
use nmcscan_shared::network::login::{self, LoginObstacle, LoginResult, LATEST_PROTOCOL};
17-
use nmcscan_shared::repositories::ServerRepository;
1816
use chrono::Utc;
17+
use nmcscan_shared::network::login::{self, LATEST_PROTOCOL, LoginObstacle, LoginResult};
18+
use nmcscan_shared::repositories::ServerRepository;
1919
use sea_orm::prelude::IpNetwork;
2020

2121
/// Login queue statistics.
@@ -212,7 +212,11 @@ impl LoginQueue {
212212
// We've cycled through all servers — reset cursor and start over
213213
cursor_ip = None;
214214
cursor_port = None;
215-
match self.server_repo.get_online_servers_cursor(500, None, None).await {
215+
match self
216+
.server_repo
217+
.get_online_servers_cursor(500, None, None)
218+
.await
219+
{
216220
Ok(b) => current_batch = b,
217221
Err(e) => {
218222
tracing::error!("Failed to fetch online servers: {}", e);

packages/scanner/src/main.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
//! High-performance Minecraft server scanning service.
44
//! This service can run independently of the API service.
55
6+
mod login_queue;
67
mod scanner;
78
mod scanner_loop;
8-
mod login_queue;
99

10-
use nmcscan_shared::models::asn::AsnCategory;
1110
use crate::scanner::Scanner;
12-
use nmcscan_shared::repositories::{
13-
AsnRepository, ServerRepository, StatsRepository,
14-
};
11+
use migration::Migrator;
12+
use nmcscan_shared::models::asn::AsnCategory;
13+
use nmcscan_shared::repositories::{AsnRepository, ServerRepository, StatsRepository};
1514
use nmcscan_shared::services::asn_fetcher::AsnFetcher;
1615
use nmcscan_shared::services::scheduler::{Scheduler, ServerTarget};
1716
use nmcscan_shared::utils::exclude::{ExcludeList, ExcludeManager};
1817
use nmcscan_shared::utils::test_mode;
19-
use migration::Migrator;
2018
use sea_orm::{ConnectOptions, Database};
2119
use sea_orm_migration::MigratorTrait;
2220
use std::sync::Arc;
@@ -26,7 +24,11 @@ use clap::Parser;
2624

2725
/// NMCScan Scanner Service arguments
2826
#[derive(Parser, Debug)]
29-
#[command(author, version, about = "NMCScan Scanner Service - High-performance Minecraft server scanning")]
27+
#[command(
28+
author,
29+
version,
30+
about = "NMCScan Scanner Service - High-performance Minecraft server scanning"
31+
)]
3032
struct Args {
3133
/// Enable test mode (scan only known servers)
3234
#[arg(short, long, env = "TEST_MODE", default_value = "false")]
@@ -82,7 +84,6 @@ struct Args {
8284
force_asn_import: bool,
8385
}
8486

85-
8687
#[tokio::main]
8788
async fn main() -> Result<(), Box<dyn std::error::Error>> {
8889
let _ = dotenvy::dotenv();
@@ -191,22 +192,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
191192

192193
tracing::info!("Loading {} test servers...", test_servers.len());
193194
for (ip, port, _name, host) in &test_servers {
194-
let server_type: String = if *port == 19132 { "bedrock".to_string() } else { "java".to_string() };
195+
let server_type: String = if *port == 19132 {
196+
"bedrock".to_string()
197+
} else {
198+
"java".to_string()
199+
};
195200
let mut target = ServerTarget::new(ip.clone(), *port, server_type.clone());
196201
target.category = AsnCategory::Hosting;
197202
target.priority = 1;
198203
target.hostname = Some(host.clone());
199204

200205
let port_i16: i16 = (*port).try_into().unwrap_or(25565);
201-
let _ = server_repo.insert_server_if_new(ip, port_i16, &server_type).await;
206+
let _ = server_repo
207+
.insert_server_if_new(ip, port_i16, &server_type)
208+
.await;
202209
scheduler.add_server(target, false).await;
203210
}
204211
}
205212
// Non-test mode: queues start empty and fill naturally via background tasks
206213
// (try_refill_queues, fill_warm_queue_if_needed, fill_cold_queue_if_needed)
207214

208215
let (h, w, c, d) = scheduler.get_queue_sizes().await;
209-
tracing::info!("Scheduler queues: Hot={}, Warm={}, Cold={}, Discovery={}", h, w, c, d);
216+
tracing::info!(
217+
"Scheduler queues: Hot={}, Warm={}, Cold={}, Discovery={}",
218+
h,
219+
w,
220+
c,
221+
d
222+
);
210223

211224
let scheduler = Arc::new(scheduler);
212225
let scanner = Arc::new(scanner);

packages/scanner/src/scanner_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
use crate::scanner::Scanner;
2+
use nmcscan_shared::repositories::{ServerRepository, StatsRepository};
3+
use nmcscan_shared::services::scheduler::Scheduler;
14
use std::sync::Arc;
25
use std::sync::atomic::{AtomicU32, Ordering};
36
use tokio::sync::mpsc;
47
use tokio::time::{self, Duration};
5-
use crate::scanner::Scanner;
6-
use nmcscan_shared::repositories::{ServerRepository, StatsRepository};
7-
use nmcscan_shared::services::scheduler::Scheduler;
88

99
// Background scanner loop with tiered rate limiting and concurrency.
1010
pub async fn run_scanner_loop(

packages/shared/src/models/asn.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ impl AsnManager {
168168
"fbi",
169169
"cia",
170170
"nsa",
171-
172171
// Education & Healthcare
173172
"university",
174173
"college",
@@ -177,7 +176,6 @@ impl AsnManager {
177176
"hospital",
178177
"medical",
179178
"clinic",
180-
181179
// Critical Infrastructure
182180
"nuclear",
183181
"atomic",
@@ -186,7 +184,6 @@ impl AsnManager {
186184
"financial",
187185
"securities",
188186
"reserve",
189-
190187
// HONEYPOTS & SECURITY RESEARCH (CRITICAL - Must Never Scan)
191188
"honeypot",
192189
"honey pot",
@@ -239,19 +236,16 @@ impl AsnManager {
239236
"research project",
240237
"security scanner",
241238
"network monitor",
242-
243239
// Additional Scanner Organizations (specific names, NOT generic CDN/hosting)
244240
"sonar research",
245241
"opendns",
246242
"umbrella security",
247-
248243
// Research & Census Projects
249244
"isc",
250245
"internet systems consortium",
251246
"caida",
252247
"routeviews",
253248
"ripe ncc",
254-
255249
// IP Geolocation/Intelligence Services (often used for scanning)
256250
"maxmind",
257251
"ip2location",

packages/shared/src/network/login.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::io;
1919
use std::net::SocketAddr;
2020
use tokio::io::{AsyncReadExt, AsyncWriteExt};
2121
use tokio::net::TcpStream;
22-
use tokio::time::{timeout, Duration};
22+
use tokio::time::{Duration, timeout};
2323

2424
/// The username used for offline login attempts.
2525
const OFFLINE_USERNAME: &str = "NMCScan";
@@ -547,7 +547,9 @@ pub async fn attempt_login_smart(addr: SocketAddr, protocol_version: i32) -> Log
547547
if extracted_protocol != protocol_version {
548548
tracing::debug!(
549549
"Protocol mismatch detected for {}: extracted protocol {} from '{}', retrying...",
550-
addr, extracted_protocol, reason
550+
addr,
551+
extracted_protocol,
552+
reason
551553
);
552554
let retry_result = attempt_login(addr, extracted_protocol).await;
553555

packages/shared/src/network/slp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::net::SocketAddr;
1111
use thiserror::Error;
1212
use tokio::io::{AsyncReadExt, AsyncWriteExt};
1313
use tokio::net::TcpStream;
14-
use tokio::time::{timeout, Duration};
14+
use tokio::time::{Duration, timeout};
1515

1616
const MAX_PACKET_SIZE: usize = 256 * 1024; // 256KB limit for safety
1717

0 commit comments

Comments
 (0)