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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#![cfg(feature = "default")]
use crate::api::init_api_server;

pub use crate::miner::Connection;
use crate::BanManager;
pub use crate::MinerList;
use async_std::net::TcpListener;
use async_std::sync::Arc;
use async_std::task;
use futures::io::BufReader;
use futures::io::{AsyncBufReadExt, AsyncReadExt};
use futures::StreamExt;
use log::{info, warn};
use serde::Deserialize;
use std::time::Duration;
use stratum_types::traits::{DataProvider, StratumManager};
use stratum_types::Result;

//@todo turn this into the "server-builder syntax"
//on new
#[derive(Clone)]
pub struct StratumServer<SM: StratumManager> {
    config: ServerConfig,
    data_provider: Arc<SM::DataProvider>,
    auth_manager: Arc<SM::AuthManager>,
    block_validator: Arc<SM::BlockValidator>,
    connection_list: Arc<MinerList<SM>>,
    ban_manager: Arc<BanManager>,
}

//@todo put into builder
#[derive(Clone, Debug, Deserialize)]
pub struct ServerConfig {
    pub host: String,
    pub port: u16,
    pub max_connections: Option<usize>,
    pub proxy: bool,
    pub var_diff: bool,
    pub initial_difficulty: f64,
}

impl<SM> StratumServer<SM>
where
    SM: StratumManager + 'static,
{
    pub fn new(
        config: ServerConfig,
        data_provider: Arc<SM::DataProvider>,
        auth_manager: Arc<SM::AuthManager>,
        block_validator: Arc<SM::BlockValidator>,
    ) -> Self {
        let connection_list = Arc::new(MinerList::new());

        StratumServer {
            connection_list,
            config,
            data_provider,
            auth_manager,
            block_validator,
            ban_manager: Arc::new(BanManager::new()),
        }
    }

    //Initialize the server before we want to start accepting any connections.
    async fn init(&self) -> Result<()> {
        info!("Initializing...");
        self.init_data_provider().await?;
        info!("Data Provider Initialized");

        if cfg!(feature = "default") {
            init_api_server().await?;
            info!("API Server Initialized");
        }

        info!("Initialization Complete");
        Ok(())
    }

    async fn init_data_provider(&self) -> Result<()> {
        self.data_provider.init().await?;

        let data_provider = self.data_provider.clone();

        let connection_list = self.connection_list.clone();

        //Spawn the data provider polling.
        task::spawn(async move {
            //@todo could (should?) use inerval here instead. but it's still unstable.
            let mut rebroadcast_timer = 0;
            loop {
                // let mut interval = Interval::new(Duration::from_secs(2));
                task::sleep(Duration::from_secs(2)).await;
                if data_provider.poll_template().await.unwrap() {
                    connection_list.broadcast_new_job().await.unwrap();
                    rebroadcast_timer = 0;
                } else {
                    rebroadcast_timer += 2;
                }

                //@todo clean this up for everyone. Also we should do an update block policy. But
                //this does allow us to rebroadcast more often, and then we can build a more robust
                //update block policy tomorrow.
                if rebroadcast_timer >= 40 {
                    //Maybe rename this to broadcst old job, don't have to grab it from
                    //connection_list.
                    connection_list.broadcast_new_job().await.unwrap();
                    rebroadcast_timer = 0;
                }
            }
        });

        Ok(())
    }

    pub async fn start(&self) -> Result<()> {
        self.init().await?;

        let listening_host = format!("{}:{}", self.config.host, self.config.port);

        let listener = TcpListener::bind(&listening_host).await?;
        let mut incoming = listener.incoming();

        info!("Listening on {}", listening_host);

        while let Some(stream) = incoming.next().await {
            let stream = stream?;
            let mut addr = stream.peer_addr()?;
            let (rh, wh) = stream.split();
            let mut buffer_stream = BufReader::new(rh);
            //Wrap this in an if proxy
            // let addr = stream.peer_addr()?;
            let connection_list = self.connection_list.clone();
            let data_provider = self.data_provider.clone();
            let auth_manager = self.auth_manager.clone();
            let block_validator = self.block_validator.clone();
            let proxy = self.config.proxy;
            let port = self.config.port;
            let var_diff = self.config.var_diff;
            let initial_difficulty = self.config.initial_difficulty;
            let ban_manager = self.ban_manager.clone();

            //@todo wrap this in a function.
            if proxy {
                let mut buf = String::new();
                //@todo handle unwrap here.
                buffer_stream.read_line(&mut buf).await.unwrap();

                //Buf will be of the format "PROXY TCP4 92.118.161.17 172.20.42.228 55867 8080\r\n"
                //Trim the \r\n off
                let buf = buf.trim();
                //Might want to not be ascii whitespace and just normal here.
                // let pieces = buf.split_ascii_whitespace();

                let pieces: Vec<&str> = buf.split(' ').collect();

                let attempted_port: u16 = pieces[5].parse().unwrap();

                //Check that they were trying to connect to us.
                if attempted_port != port {
                    continue;
                }

                addr = format!("{}:{}", pieces[2], pieces[4]).parse().unwrap();
            }

            //If Proxy === true, then we don't get this information from the stream....
            //We need to read a newline, and then parse the proxy info from that. AND Then
            //create a new miner from that information.
            task::spawn(async move {
                if !ban_manager.check_banned(&addr).await {
                    let connection = Arc::new(Connection::new(
                        addr,
                        buffer_stream,
                        wh,
                        data_provider.clone(),
                        auth_manager.clone(),
                        block_validator.clone(),
                        var_diff,
                        initial_difficulty,
                    ));

                    connection_list
                        .add_miner(addr, connection.clone())
                        .await
                        .unwrap();

                    info!("Accepting stream from: {}", addr);

                    //Unused for now, but may be useful for logging or bans
                    let _result = connection.start().await;

                    info!("Closing stream from: {}", addr);

                    connection_list.remove_miner(addr).await.unwrap();

                    if connection.needs_ban().await {
                        ban_manager.add_ban(&addr).await;
                    }
                } else {
                    warn!(
                        "Banned connection attempting to connect: {}. Connected closed",
                        addr
                    );
                }
            });
        }
        Ok(())
    }
}