1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::{net::IpAddr, sync::Arc, time::Duration};

//@todo wrap this in a Mutex<Arc. Then when a new config is refreshed, everyone can just get it
//from a clone.
#[derive(Default, Clone)]
pub struct ConfigManager {
    config: Arc<Config>,
}

impl ConfigManager {
    pub(crate) fn new(config: Config) -> Self {
        Self {
            config: Arc::new(config),
        }
    }

    pub(crate) fn current_config(&self) -> Arc<Config> {
        self.config.clone()
    }

    // Helpers
    pub(crate) fn proxy_protocol(&self) -> bool {
        self.config.connection.proxy_protocol
    }

    pub(crate) fn default_ban_duration(&self) -> Duration {
        self.config.bans.default_ban_duration
    }
}

#[derive(Clone, Debug, Default)]
pub struct Config {
    pub(crate) connection: ConnectionConfig,
    pub(crate) difficulty: DifficultyConfig,
    pub(crate) bans: BanManagerConfig,
}

#[derive(Clone, Debug)]
pub struct BanManagerConfig {
    pub(crate) default_ban_duration: Duration,
    pub(crate) _ban_score_allowed: u64,
    pub(crate) _whitelisted_ips: Vec<IpAddr>,
    pub(crate) _perma_ban_starting_list: Vec<IpAddr>,
}

impl Default for BanManagerConfig {
    fn default() -> Self {
        BanManagerConfig {
            default_ban_duration: Duration::from_secs(3600),
            _ban_score_allowed: 100,
            _whitelisted_ips: Vec::new(),
            _perma_ban_starting_list: Vec::new(),
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct ConnectionConfig {
    pub(crate) proxy_protocol: bool,
    pub(crate) max_connections: Option<usize>,
}

#[derive(Clone, Debug, Default)]
pub struct DifficultyConfig {
    pub(crate) initial_difficulty: u64,
    pub(crate) var_diff: bool,
    pub(crate) minimum_difficulty: u64,
    pub(crate) maximum_difficulty: u64,
    //Seconds
    pub(crate) retarget_time: u64,
    //Seconds
    pub(crate) target_time: u64,
    //@todo see if we use this.
    pub(crate) variance_percent: f64,
}

// #[cfg(feature = "upstream")]
// #[derive(Clone, Debug, Default)]
// pub struct UpstreamConfig {
//     pub(crate) enabled: bool,
//     pub(crate) url: String,
// }