Skip to content

Commit

Permalink
feat: better logging, and allow ban manager to come from config. Rele…
Browse files Browse the repository at this point in the history
…ase v5.7.4
  • Loading branch information
kilpatty committed Feb 5, 2024
1 parent 4e2a710 commit 08759f5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stratum-server"
version = "5.7.3"
version = "5.7.4"
authors = ["Sean Kilgarriff [email protected]"]
rust-version = "1.67.1"
edition = "2021"
Expand Down
18 changes: 16 additions & 2 deletions server/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
config::{ConnectionConfig, DifficultyConfig},
config::{BanManagerConfig, ConnectionConfig, DifficultyConfig},
id_manager::IDManager,
router::Router,
types::ReadyIndicator,
Expand Down Expand Up @@ -27,6 +27,7 @@ pub struct StratumServerBuilder<State, CState> {
pub ready_indicator: ReadyIndicator,
pub shutdown_message: Option<Buffer>,
pub cancel_token: Option<CancellationToken>,
pub ban_manager_enabled: bool,
}

impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync + 'static>
Expand Down Expand Up @@ -62,6 +63,7 @@ impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync
// },
shutdown_message: None,
cancel_token: None,
ban_manager_enabled: false,
}
}

Expand Down Expand Up @@ -163,16 +165,28 @@ impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync
self
}

#[must_use]
pub fn with_ban_manager(mut self, enabled: bool) -> Self {
self.ban_manager_enabled = enabled;
self
}

pub async fn build(self) -> Result<StratumServer<State, CState>> {
let ban_manager_config = BanManagerConfig {
enabled: self.ban_manager_enabled,
..Default::default()
};

let config = Config {
connection: self.connection_config,
difficulty: self.var_diff_config,
bans: crate::config::BanManagerConfig::default(),
bans: ban_manager_config,
};

let config_manager = ConfigManager::new(config);

let listener = TcpListener::bind(format!("{}:{}", self.host, self.port)).await?;

//This will fail if unable to find a local port.
let listen_address = listener.local_addr()?;
let listener = TcpListenerStream::new(listener);
Expand Down
6 changes: 6 additions & 0 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ impl ConfigManager {
pub(crate) fn difficulty_config(&self) -> &DifficultyConfig {
&self.config.difficulty
}

pub(crate) fn ban_manager_enabled(&self) -> bool {
self.config.bans.enabled
}
}

#[derive(Clone, Debug, Default)]
Expand All @@ -42,6 +46,7 @@ pub struct Config {

#[derive(Clone, Debug)]
pub struct BanManagerConfig {
pub(crate) enabled: bool,
pub(crate) default_ban_duration: Duration,
pub(crate) _ban_score_allowed: u64,
pub(crate) _whitelisted_ips: Vec<IpAddr>,
Expand All @@ -51,6 +56,7 @@ pub struct BanManagerConfig {
impl Default for BanManagerConfig {
fn default() -> Self {
BanManagerConfig {
enabled: false,
default_ban_duration: Duration::from_secs(3600),
_ban_score_allowed: 100,
_whitelisted_ips: Vec::new(),
Expand Down
1 change: 1 addition & 0 deletions server/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<State: Clone + Send + Sync + 'static, CState: Clone + Send + Sync + 'static
// );
// } else {
tracing::debug!(
ip = connection.ip().to_string(),
"Calling method: {} for connection: {}",
value.method(),
connection.id()
Expand Down
10 changes: 10 additions & 0 deletions server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use extended_primitives::Buffer;
use parking_lot::{Mutex, RwLock};
use serde::Serialize;
use std::{
net::SocketAddr,
sync::Arc,
time::{Duration, Instant, SystemTime},
};
Expand Down Expand Up @@ -78,9 +79,11 @@ pub struct Session<State> {
difficulty_settings: Arc<RwLock<DifficultySettings>>,
}

//@todo I want to get IP here and return it
struct Inner<State> {
pub id: ConnectionID,
pub session_id: SessionID,
pub ip: SocketAddr,
pub state: State,
}

Expand All @@ -100,6 +103,7 @@ impl<State: Clone> Session<State> {
pub fn new(
id: ConnectionID,
session_id: SessionID,
ip: SocketAddr,
sender: UnboundedSender<SendInformation>,
config_manager: ConfigManager,
cancel_token: CancellationToken,
Expand All @@ -118,6 +122,7 @@ impl<State: Clone> Session<State> {
let inner = Inner {
id,
session_id,
ip,
state,
};

Expand Down Expand Up @@ -376,6 +381,11 @@ impl<State: Clone> Session<State> {
self.shared.lock().last_active = Instant::now();
}

#[must_use]
pub fn ip(&self) -> SocketAddr {
self.inner.ip
}

//@todo Use internal URLs if they exist otherwise use the default URL?
//Figure out how to do this gracefully.
// pub(crate) fn graceful_shutdown(&self) {
Expand Down
7 changes: 4 additions & 3 deletions server/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync
self.connection.address
};

//@todo this seems dangerous on Global Accelerator As they all use the same address ->
//Let's think about if we use this by default or not.
// self.ban_manager.check_banned(address)?;
if self.config_manager.ban_manager_enabled() {
self.ban_manager.check_banned(address)?;
}

let (mut reader, tx, handle) = self.connection.init();

Expand All @@ -57,6 +57,7 @@ impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync
let session = Session::new(
self.id.clone(),
session_id,
address,
tx,
self.config_manager.clone(),
session_cancel_token.clone(),
Expand Down

0 comments on commit 08759f5

Please sign in to comment.