Skip to content

Commit

Permalink
we do a bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrecknt committed Feb 21, 2024
1 parent e692d0f commit 93619a8
Show file tree
Hide file tree
Showing 26 changed files with 192 additions and 504 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Snowstorm.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ secret = "some random key"
enabled = true
interface_name = "eth0"
task_size_sanity_limit = 1000000
mode_duration = 300
push_to_db = true

[bot]
token = "abcdefghijklmnopqrstuvwxyz.abcdef.ghijklmnopqrstuvwxyzabcdefghijklmnopqr"
Expand Down
41 changes: 41 additions & 0 deletions crates/common/src/network_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ impl SocketAddrV4Range {
ranges.remove(*index);
}
}

pub fn count_addresses(&self) -> u64 {
let ip_count = 1 + u32::from(*self.end.ip()) - u32::from(*self.start.ip());
let port_count = 1 as u64 + self.end.port() as u64 - self.start.port() as u64;
ip_count as u64 * port_count
}

pub fn random(&self, index: u64) -> SocketAddrV4 {
let start_port = self.start.port();
let start_ip = u32::from(*self.start.ip());

let port_count = 1 as u64 + self.end.port() as u64 - self.start.port() as u64;
let ip = (index / port_count as u64) as u32;
let port = (index % port_count as u64) as u16;
SocketAddrV4::new((start_ip + ip).into(), start_port + port)
}
}

impl PartialOrd for SocketAddrV4Range {
Expand Down Expand Up @@ -114,3 +130,28 @@ impl From<(Ipv4AddrRange, u16)> for SocketAddrV4Range {
(ip, port, port).into()
}
}

pub trait RangesExt {
fn count_addresses(&self) -> u64;
fn get_addr_at(&self, index: u64) -> SocketAddrV4;
}
impl RangesExt for Vec<SocketAddrV4Range> {
fn count_addresses(&self) -> u64 {
self.iter().map(|range| range.count_addresses()).sum()
}

fn get_addr_at(&self, index: u64) -> SocketAddrV4 {
let mut cursor = 0;
let mut cursor_total = 0;
while let Some(range) = self.get(cursor) {
let range_size = range.count_addresses();
if cursor_total + range_size <= index {
cursor_total += range_size;
cursor += 1;
continue;
}
return range.random(index - cursor_total);
}
panic!(":(")
}
}
6 changes: 5 additions & 1 deletion crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ pub struct ScannerConfig {
#[default = false]
pub enabled: bool,
pub interface_name: String,
pub task_size_sanity_limit: usize,
pub task_size_sanity_limit: u64,
pub mode_duration: u64,
#[serde(default = "_true")]
pub push_to_db: bool,
}

#[derive(Deserialize, SmartDefault)]
Expand Down Expand Up @@ -94,6 +97,7 @@ pub struct OauthDiscordConfig {
pub redirect_uri: String,
pub client_id: String,
pub client_secret: String,
pub guild_id: String,
}

#[derive(Deserialize, SmartDefault)]
Expand Down
18 changes: 9 additions & 9 deletions crates/io/src/database.rs → crates/io/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use crate::ScannerState;
use database::{player::PlayerInfo, server::PingResult};
use std::{
collections::BTreeSet,
net::Ipv4Addr,
net::{Ipv4Addr, SocketAddrV4},
sync::{mpsc::Sender, Arc},
};
use tokio::sync::Mutex;

pub struct DatabaseScanner {
pub state: Arc<Mutex<ScannerState>>,
pub sender: Sender<(PingResult, Vec<PlayerInfo>)>,
pub data: BTreeSet<(Ipv4Addr, u16)>,
pub data: BTreeSet<SocketAddrV4>,
}

impl DatabaseScanner {
Expand All @@ -29,7 +29,7 @@ impl DatabaseScanner {
.records()
.map(|item| {
let item = item.unwrap();
(
SocketAddrV4::new(
Ipv4Addr::from(item[0].parse::<u32>().unwrap()),
item[1].parse::<u16>().unwrap(),
)
Expand All @@ -45,21 +45,21 @@ impl DatabaseScanner {
}

impl Io for DatabaseScanner {
async fn ping(&mut self, addr: Ipv4Addr, port: u16) -> eyre::Result<()> {
if self.data.contains(&(addr, port)) {
async fn ping(&mut self, addr: SocketAddrV4) -> eyre::Result<()> {
if self.data.contains(&addr) {
self.state.lock().await.discovered += 1;
self.sender
.send((PingResult::none(addr, port), vec![]))
.send((PingResult::none(*addr.ip(), addr.port()), vec![]))
.expect("Unable to send ping result");
}
Ok(())
}

async fn legacy_ping(&mut self, addr: Ipv4Addr, port: u16) -> eyre::Result<()> {
if self.data.contains(&(addr, port)) {
async fn legacy_ping(&mut self, addr: SocketAddrV4) -> eyre::Result<()> {
if self.data.contains(&addr) {
self.state.lock().await.discovered += 1;
self.sender
.send((PingResult::none(addr, port), vec![]))
.send((PingResult::none(*addr.ip(), addr.port()), vec![]))
.expect("Unable to send ping result");
}
Ok(())
Expand Down
16 changes: 5 additions & 11 deletions crates/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::{
hash::{DefaultHasher, Hash, Hasher},
net::SocketAddrV4,
};
use std::hash::{DefaultHasher, Hash, Hasher};

pub mod database;
pub mod modes;
pub mod network;
pub mod pnet;
pub mod proxy;
Expand All @@ -16,24 +12,22 @@ lazy_static::lazy_static! {
pub trait Io {
fn ping(
&mut self,
addr: std::net::Ipv4Addr,
port: u16,
addr: std::net::SocketAddrV4,
) -> impl std::future::Future<Output = eyre::Result<()>> + Send;

fn legacy_ping(
&mut self,
addr: std::net::Ipv4Addr,
port: u16,
addr: std::net::SocketAddrV4,
) -> impl std::future::Future<Output = eyre::Result<()>> + Send;
}

pub fn cookie(address: &SocketAddrV4, seed: u64) -> u32 {
pub fn cookie(address: &std::net::SocketAddrV4, seed: u64) -> u32 {
let mut hasher = DefaultHasher::new();
(*address.ip(), address.port(), seed).hash(&mut hasher);
hasher.finish() as u32
}

#[derive(Default)]
pub struct ScannerState {
pub discovered: u32,
pub discovered: u64,
}
13 changes: 0 additions & 13 deletions crates/io/src/modes/all_ports.rs

This file was deleted.

6 changes: 0 additions & 6 deletions crates/io/src/modes/auto/mod.rs

This file was deleted.

17 changes: 0 additions & 17 deletions crates/io/src/modes/discovery.rs

This file was deleted.

19 changes: 0 additions & 19 deletions crates/io/src/modes/discovery_top.rs

This file was deleted.

78 changes: 0 additions & 78 deletions crates/io/src/modes/mod.rs

This file was deleted.

27 changes: 0 additions & 27 deletions crates/io/src/modes/range.rs

This file was deleted.

30 changes: 0 additions & 30 deletions crates/io/src/modes/range_top.rs

This file was deleted.

Loading

0 comments on commit 93619a8

Please sign in to comment.