Skip to content

Commit 8a90161

Browse files
committed
cleanup
1 parent 2f59c49 commit 8a90161

File tree

8 files changed

+54
-75
lines changed

8 files changed

+54
-75
lines changed

src/database/collect_servers.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ impl CacheItem {
2828
let cache_duration = TimeDelta::hours(24);
2929

3030
// first time
31-
let Some(last_updated) = self.last_updated else {
32-
return None;
33-
};
31+
let last_updated = self.last_updated?;
3432
// cache expired
3533
if last_updated.elapsed().as_secs_f64() > cache_duration.as_seconds_f64() {
3634
return None;
@@ -123,7 +121,7 @@ impl Database {
123121
let ip = Ipv4Addr::from_bits(row.get::<i32, _>(0) as u32);
124122
let port = row.get::<i16, _>(1) as u16;
125123

126-
servers.push(SocketAddrV4::new(Ipv4Addr::from(ip), port as u16));
124+
servers.push(SocketAddrV4::new(ip, port));
127125

128126
if servers.len() % 10000 == 0 {
129127
info!("Collected {} servers", servers.len());

src/database/migrate_mongo_to_postgres.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,8 @@ pub async fn do_migration(mongodb_uri: &str, postgres_uri: &str) {
6363
.map(String::from);
6464
let version_protocol = version.and_then(|v| v.get_i32("protocol")).ok();
6565

66-
let online_players = players
67-
.as_ref()
68-
.ok()
69-
.and_then(|v| v.get_i32("online").ok())
70-
.map(|v| v as i32);
71-
let max_players = players
72-
.as_ref()
73-
.ok()
74-
.and_then(|v| v.get_i32("max").ok())
75-
.map(|v| v as i32);
66+
let online_players = players.as_ref().ok().and_then(|v| v.get_i32("online").ok());
67+
let max_players = players.as_ref().ok().and_then(|v| v.get_i32("max").ok());
7668

7769
let is_online_mode = doc.get_bool("onlineMode").ok();
7870
let mut player_sample = Vec::new();
@@ -201,7 +193,7 @@ pub async fn do_migration(mongodb_uri: &str, postgres_uri: &str) {
201193
});
202194
tasks.push_back(task);
203195

204-
while tasks.len() >= 1 {
196+
while tasks.len() >= 100 {
205197
tasks.pop_front().unwrap().await.unwrap();
206198
}
207199
}

src/database/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Database {
4343
pub async fn connect(postgres_uri: &str) -> eyre::Result<Self> {
4444
let pool = sqlx::postgres::PgPoolOptions::new()
4545
.max_connections(100)
46-
.connect(&postgres_uri)
46+
.connect(postgres_uri)
4747
.await?;
4848
sqlx::migrate!().run(&pool).await?;
4949

@@ -130,7 +130,7 @@ impl Database {
130130
const ALLOWED_PLAYER_LIMIT: i64 = 1000;
131131
const KEEP_PLAYER_COUNT: i64 = 500;
132132

133-
assert!(ALLOWED_PLAYER_LIMIT > KEEP_PLAYER_COUNT);
133+
const _: () = assert!(ALLOWED_PLAYER_LIMIT > KEEP_PLAYER_COUNT);
134134

135135
let mut rows = sqlx::query(
136136
"

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async fn main() -> eyre::Result<()> {
4040
dotenv().ok();
4141
println!("dotenv");
4242

43-
let args = env::args().into_iter().collect::<Box<[String]>>();
43+
let args = env::args().collect::<Box<[String]>>();
4444
if args.get(1) == Some(&"mongodb-migrate".to_string()) {
4545
let mongodb_uri = args
4646
.get(2)
@@ -49,7 +49,7 @@ async fn main() -> eyre::Result<()> {
4949
.get(3)
5050
.expect("postgres uri must be the third argument");
5151

52-
migrate_mongo_to_postgres::do_migration(&mongodb_uri, &postgres_uri).await;
52+
migrate_mongo_to_postgres::do_migration(mongodb_uri, postgres_uri).await;
5353
println!("Done.");
5454
return Ok(());
5555
}

src/net/tcp.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,11 @@ impl StatelessTcpReadHalf {
362362
packet.to_vec()
363363
};
364364

365-
if let Some(ipv4) = Ipv4Packet::new(&payload_for_ipv4) {
366-
if let Some(tcp) = process_ipv4(&ipv4) {
367-
if self.source_port.contains(tcp.destination) {
368-
return Some((ipv4.from_packet(), tcp));
369-
}
370-
}
365+
if let Some(ipv4) = Ipv4Packet::new(&payload_for_ipv4)
366+
&& let Some(tcp) = process_ipv4(&ipv4)
367+
&& self.source_port.contains(tcp.destination)
368+
{
369+
return Some((ipv4.from_packet(), tcp));
371370
}
372371
}
373372
Err(_) => return None,

src/processing/minecraft/mod.rs

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,10 @@ pub fn parse_ping_response_json(d: &[u8]) -> eyre::Result<PingResponse> {
144144
.unwrap_or_default();
145145

146146
let version = v.get("version");
147-
let version_name = version
148-
.get_str("name")
149-
.map(|s| sanitize_text_for_postgres(s));
147+
let version_name = version.get_str("name").map(sanitize_text_for_postgres);
150148
let version_protocol = version.get_i32("protocol");
151149

152-
let favicon = v.get_str("favicon").map(|s| sanitize_text_for_postgres(s));
150+
let favicon = v.get_str("favicon").map(sanitize_text_for_postgres);
153151
// filter out bad favicons
154152
let favicon = favicon.filter(|f| f.starts_with("data:image/png;base64,"));
155153
let favicon_hash = favicon.as_ref().map(|s| make_favicon_hash(s));
@@ -178,8 +176,7 @@ pub fn parse_ping_response_json(d: &[u8]) -> eyre::Result<PingResponse> {
178176
.map(|a| {
179177
a.iter()
180178
.filter_map(|v| {
181-
let Some(name) = v.get_str("name").map(|s| sanitize_text_for_postgres(s))
182-
else {
179+
let Some(name) = v.get_str("name").map(sanitize_text_for_postgres) else {
183180
// name is required
184181
is_fake_sample = true;
185182
return None;
@@ -224,18 +221,14 @@ pub fn parse_ping_response_json(d: &[u8]) -> eyre::Result<PingResponse> {
224221
let forge_data = v.get("forgeData");
225222
let forgedata_fml_network_version = forge_data.get_i32("fmlNetworkVersion");
226223
let mod_info = v.get("modinfo");
227-
let modinfo_type = mod_info
228-
.get_str("type")
229-
.map(|s| sanitize_text_for_postgres(s));
224+
let modinfo_type = mod_info.get_str("type").map(sanitize_text_for_postgres);
230225
let is_modded = v.get_bool("isModded");
231226
let modpack_data = v.get("modpackData");
232227
let modpackdata_project_id = modpack_data.get_i32("projectID");
233-
let modpackdata_name = modpack_data
234-
.get_str("name")
235-
.map(|s| sanitize_text_for_postgres(s));
228+
let modpackdata_name = modpack_data.get_str("name").map(sanitize_text_for_postgres);
236229
let modpackdata_version = modpack_data
237230
.get_str("version")
238-
.map(|s| sanitize_text_for_postgres(s));
231+
.map(sanitize_text_for_postgres);
239232

240233
Ok(PingResponse {
241234
description_json,
@@ -276,25 +269,25 @@ pub async fn insert_server_to_db(
276269
{
277270
let mut shared = db.shared.lock();
278271
let ips_with_same_hash = shared.ip_to_hash_and_ports.get_mut(target.ip());
279-
if let Some((data, previously_checked_ports)) = ips_with_same_hash {
280-
if !previously_checked_ports.contains(&target.port()) {
281-
if let Some(count) = &mut data.count {
282-
let this_server_hash = make_ping_response_hash(r)?;
283-
284-
if this_server_hash == data.hash {
285-
*count += 1;
286-
previously_checked_ports.insert(target.port());
287-
288-
if *count >= 100 {
289-
// too many servers with the same hash... add to bad ips!
290-
println!("found a new bad ip: {} :(", target.ip());
291-
// we call add_to_ips_with_aliased_servers later
292-
is_aliased_server = true;
293-
}
294-
} else {
295-
// this server has a different hash than the other servers with the same IP
296-
data.count = None;
272+
if let Some((data, previously_checked_ports)) = ips_with_same_hash
273+
&& !previously_checked_ports.contains(&target.port())
274+
{
275+
if let Some(count) = &mut data.count {
276+
let this_server_hash = make_ping_response_hash(r)?;
277+
278+
if this_server_hash == data.hash {
279+
*count += 1;
280+
previously_checked_ports.insert(target.port());
281+
282+
if *count >= 100 {
283+
// too many servers with the same hash... add to bad ips!
284+
println!("found a new bad ip: {} :(", target.ip());
285+
// we call add_to_ips_with_aliased_servers later
286+
is_aliased_server = true;
297287
}
288+
} else {
289+
// this server has a different hash than the other servers with the same IP
290+
data.count = None;
298291
}
299292
}
300293
} else {
@@ -314,7 +307,7 @@ pub async fn insert_server_to_db(
314307

315308
if is_aliased_server {
316309
let db = db.clone();
317-
let target = target.clone();
310+
let target = *target;
318311
tokio::spawn(async move {
319312
let _ = db
320313
// for now, assume 25565 is the only allowed port. might change this in the future.

src/processing/minecraft/passive_fingerprint.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,11 @@ pub fn generate_passive_fingerprint(data: &str) -> eyre::Result<PassiveMinecraft
8585
field_order = Some(field_order_string);
8686
}
8787

88-
if let Some(players) = data.get("players").and_then(|s| s.as_object()) {
89-
if let Some(sample) = players.get("sample").and_then(|s| s.as_array()) {
90-
if sample.is_empty() {
91-
empty_sample = true;
92-
}
93-
}
88+
if let Some(players) = data.get("players").and_then(|s| s.as_object())
89+
&& let Some(sample) = players.get("sample").and_then(|s| s.as_array())
90+
&& sample.is_empty()
91+
{
92+
empty_sample = true;
9493
}
9594
}
9695

src/strategies/rescan.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ pub enum Sort {
1717
pub async fn get_ranges(database: &Database, opts: &RescanConfig) -> eyre::Result<Vec<ScanRange>> {
1818
let mut ranges = Vec::new();
1919

20-
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(
20+
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new(format!(
2121
"
22-
SELECT ip, port FROM servers
23-
WHERE
24-
last_pinged > NOW() - INTERVAL '$1 seconds'
25-
AND last_pinged < NOW() - INTERVAL '$1 seconds'
26-
",
27-
);
22+
SELECT ip, port FROM servers
23+
WHERE
24+
last_pinged > NOW() - INTERVAL '{} seconds'
25+
AND last_pinged < NOW() - INTERVAL '{} seconds'
26+
",
27+
opts.last_ping_ago_max_secs as i64, opts.rescan_every_secs as i64
28+
));
2829

2930
if !opts.filter_sql.is_empty() {
3031
// this could result in sql injection, but the config is considered to be
@@ -57,10 +58,7 @@ pub async fn get_ranges(database: &Database, opts: &RescanConfig) -> eyre::Resul
5758

5859
let sql = qb.into_sql();
5960
debug!("Doing rescan query with SQL: {sql}");
60-
let mut rows = sqlx::query(&sql)
61-
.bind(opts.last_ping_ago_max_secs as i64)
62-
.bind(opts.rescan_every_secs as i64)
63-
.fetch(&database.pool);
61+
let mut rows = sqlx::query(&sql).fetch(&database.pool);
6462

6563
while let Some(Ok(row)) = rows.next().await {
6664
let ip = Ipv4Addr::from_bits(row.get::<i32, _>(0) as u32);
@@ -85,7 +83,7 @@ pub async fn get_ranges(database: &Database, opts: &RescanConfig) -> eyre::Resul
8583
continue;
8684
}
8785

88-
ranges.push(ScanRange::single(ip, port as u16));
86+
ranges.push(ScanRange::single(ip, port));
8987
if ranges.len() % 1000 == 0 {
9088
println!("{} ips", ranges.len());
9189
}

0 commit comments

Comments
 (0)