Skip to content

Commit

Permalink
fix some more panics when the database is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-1 committed Mar 29, 2024
1 parent 70cec6e commit 1997988
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Also if you do intend on using any of the code here, please read the [license](L

It is assumed that you know the basics of server scanning. Otherwise, I recommend reading the [masscan readme](https://github.com/robertdavidgraham/masscan/blob/master/README.md) and [documentation](https://github.com/robertdavidgraham/masscan/blob/master/doc/masscan.8.markdown). Also be aware that matscan only supports Linux, but you probably shouldn't be running it at home anyways.

Rename `config.toml.example` to `config.toml` and fill in the fields.
Rename `example-config.toml` to `config.toml` and fill in the fields.

You'll also have to make a MongoDB database called `mcscanner` with two collections called `servers` and `bad_servers`. You should add a unique index for `addr+port` and a normal index for `timestamp` in the `servers` collection.

Expand Down
32 changes: 0 additions & 32 deletions config.toml.example

This file was deleted.

25 changes: 13 additions & 12 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,32 +262,33 @@ pub async fn collect_all_servers(
doc! {
"timestamp": {
// up to 30 days ago
"$gt": bson::DateTime::from(SystemTime::now() - std::time::Duration::from_secs(60 * 60 * 24 * 30)),
"$gt": bson::DateTime::from(SystemTime::now() - Duration::from_secs(60 * 60 * 24 * 30)),
}
}
}
CollectServersFilter::New => {
if let Some((cached, cached_time)) = &database.shared.lock().cached_all_servers_new {
// if it was more than 24 hours ago, download again
if cached_time.elapsed().as_secs() < 60 * 60 * 24 {
if cached_time.elapsed() < Duration::from_secs(60 * 60 * 24) {
return Ok(cached.clone());
}
}
// first 4 bytes are seconds since epoch
// other 12 are 0
let seconds_since_epoch = (SystemTime::now()
- std::time::Duration::from_secs(60 * 60 * 24 * 7))

// inserted in the past 7 days
let inserted_after_secs_since_epoch = (SystemTime::now()
- Duration::from_secs(60 * 60 * 24 * 7))
.duration_since(UNIX_EPOCH)?
.as_secs() as u32;

doc! {
"_id": {
// inserted in the past 7 days
"_id": {
// first 4 bytes are seconds since epoch
// other 12 are 0
"$gt": bson::oid::ObjectId::from_bytes([
(seconds_since_epoch >> 24) as u8,
(seconds_since_epoch >> 16) as u8,
(seconds_since_epoch >> 8) as u8,
seconds_since_epoch as u8,
(inserted_after_secs_since_epoch >> 24) as u8,
(inserted_after_secs_since_epoch >> 16) as u8,
(inserted_after_secs_since_epoch >> 8) as u8,
inserted_after_secs_since_epoch as u8,
0, 0, 0, 0, 0, 0, 0, 0
])
}
Expand Down
4 changes: 1 addition & 3 deletions src/modes/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ pub async fn get_addrs_and_protocol_versions(
"$lt": bson::DateTime::from(SystemTime::now() - Duration::from_secs(60 * 60 * 24 * 7)),
}
},
{
"fingerprint.activeMinecraft": { "$exists": false }
},
{ "fingerprint.activeMinecraft": { "$exists": false } },
]
};

Expand Down
1 change: 0 additions & 1 deletion src/modes/rescan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub async fn get_ranges(

let mut filter = doc! {
"timestamp": {
// up to 2 hours ago
"$gt": bson::DateTime::from(SystemTime::now() - Duration::from_secs(last_ping_ago_max_secs)),
"$lt": bson::DateTime::from(SystemTime::now() - Duration::from_secs(rescan_every_secs))
}
Expand Down
4 changes: 4 additions & 0 deletions src/modes/slash24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub async fn get_ranges(database: &Database) -> anyhow::Result<Vec<ScanRange>> {
let ranges: HashMap<(u8, u8, u8), ServerGroup> = to_ranges(&known_servers);
println!("converted into {} ranges", ranges.len());

if ranges.is_empty() {
return Ok(vec![]);
}

let mut rng = thread_rng();
let mut target_ranges = Vec::new();

Expand Down
4 changes: 4 additions & 0 deletions src/modes/slash24_few_ports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub async fn get_ranges(database: &Database) -> anyhow::Result<Vec<ScanRange>> {
let ranges: HashMap<(u8, u8, u8), ServerGroup> = to_ranges(&known_servers);
println!("converted into {} ranges", ranges.len());

if ranges.is_empty() {
return Ok(vec![]);
}

let mut rng = thread_rng();
let mut target_ranges = Vec::new();

Expand Down
4 changes: 4 additions & 0 deletions src/modes/slash24_few_ports_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub async fn get_ranges(database: &Database) -> anyhow::Result<Vec<ScanRange>> {
let ranges: HashMap<(u8, u8, u8), ServerGroup> = to_ranges(&known_servers);
println!("converted into {} ranges", ranges.len());

if ranges.is_empty() {
return Ok(vec![]);
}

let mut rng = thread_rng();
let mut target_ranges = Vec::new();

Expand Down
4 changes: 4 additions & 0 deletions src/modes/slash24_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub async fn get_ranges(database: &Database) -> anyhow::Result<Vec<ScanRange>> {
let ranges: HashMap<(u8, u8, u8), ServerGroup> = to_ranges(&known_servers);
println!("converted into {} ranges", ranges.len());

if ranges.is_empty() {
return Ok(vec![]);
}

let mut rng = thread_rng();
let mut target_ranges = Vec::new();

Expand Down
6 changes: 3 additions & 3 deletions src/scanner/protocols/minecraft_fingerprinting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub fn build_fingerprint_request(hostname: &str, port: u16, protocol_version: i3

// ServerboundHelloPacket was changed in 23w31a (1.20.2) so the uuid is no
// longer optional
if (protocol_version >= 764 && protocol_version < 0x40000000)
|| (protocol_version >= 1073741968)
{
// if (protocol_version >= 764 && protocol_version < 0x40000000)
// || (protocol_version >= 1073741968)
if matches!(protocol_version, 764..=0x40000000 | 1073741968..) {
#[rustfmt::skip]
full_buffer.extend_from_slice(&[
19, // length of following data
Expand Down

0 comments on commit 1997988

Please sign in to comment.