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
#![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;
#[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>,
}
#[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()),
}
}
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();
task::spawn(async move {
loop {
task::sleep(Duration::from_secs(2)).await;
if data_provider.poll_template().await.unwrap() {
connection_list.broadcast_new_job().await.unwrap();
}
}
});
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);
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();
if proxy {
let mut buf = String::new();
buffer_stream.read_line(&mut buf).await.unwrap();
let buf = buf.trim();
let pieces: Vec<&str> = buf.split(' ').collect();
let attempted_port: u16 = pieces[5].parse().unwrap();
if attempted_port != port {
continue;
}
addr = format!("{}:{}", pieces[2], pieces[4]).parse().unwrap();
}
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);
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(())
}
}