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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use futures::future::{join, join_all};
use notify::Watcher as _;
use quinn::{Endpoint, SendStream, VarInt};
use std::collections::HashMap;
use std::fs::canonicalize;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::task::{self, JoinHandle};
use crate::cli_utils::configure_logging;
use crate::common::connection::quic::QuicReader;
use crate::common::constants::{LOCALHOST4, LOCALHOST6};
use crate::common::ip_display::IpDisplay;
use crate::common::message::UserState;
use crate::common::result::DynResult;
use super::bot::server_run_bots;
use super::options::ServerOptions;
use super::process::process;
use super::sockets::create_server_config;
use super::types::{Connections, Rooms};
use super::utils::send_room_update;
pub async fn main(module: &'static str, options: &ServerOptions) -> DynResult<()> {
configure_logging(module)?;
let streams = Arc::new(Mutex::new(HashMap::new()));
let rooms = Arc::new(Mutex::new(HashMap::new()));
let mut local_addrs: Vec<SocketAddr> = vec![];
let mut endpoints: Vec<Arc<Endpoint>> = vec![];
let futures = super::sockets::listen(options)
.await?
.into_iter()
.map(|endpoint| {
log::info!(
"Accepting connections on {} ({})",
endpoint
.local_addr()
.map(|addr| format!("{addr}"))
.unwrap_or_else(|err| format!("{err:?}")),
options.domain
);
if let Ok(addr) = endpoint.local_addr() {
local_addrs.push(addr);
}
let endpoint = Arc::new(endpoint);
endpoints.push(endpoint.clone());
handle_endpoint(endpoint, streams.clone(), rooms.clone())
})
.collect::<Vec<_>>();
let _watcher = if let Some(path) = options.private_pem.as_ref() {
endpoints.shrink_to_fit();
let path = canonicalize(path)?;
let path_clone = path.clone();
let options = options.clone();
// TODO: PollWatcher is bad, but it works even when removing the file
let mut watcher = notify::PollWatcher::new(
move |event: Result<notify::Event, _>| match event {
Ok(event) => {
if !event.paths.contains(&path_clone) {
return;
}
if !(event.kind.is_modify() || event.kind.is_create()) {
return;
}
log::debug!("Got modify notify events: {event:?}");
match create_server_config(&options) {
Ok(server_config) => {
for endpoint in &endpoints {
endpoint.set_server_config(Some(server_config.clone()));
log::debug!(
"Updated server config for endpoint: {:?}",
endpoint.local_addr()
);
}
}
Err(err) => {
log::error!("Error while trying to create server config: {err:?}")
}
}
}
Err(err) => log::error!("notify error: {err:?}"),
},
notify::Config::default()
.with_compare_contents(false)
.with_poll_interval(Duration::from_secs(100)), // certs do not need to change often
)?;
watcher.watch(&path, notify::RecursiveMode::NonRecursive)?;
log::debug!("watching {path:?}");
Some(watcher) // keep reference alive!
} else {
drop(endpoints);
None
};
let results = if let Some(addr) = local_addrs.into_iter().next() {
let ip = match addr.ip() {
IpAddr::V4(_) => LOCALHOST4,
IpAddr::V6(_) => LOCALHOST6,
};
join(server_run_bots(ip, options), join_all(futures))
.await
.1
} else {
join_all(futures).await
};
for result in results {
match result {
Ok(()) => (),
Err(err) => log::error!("Got error: {err:?}"),
}
}
Ok(())
}
async fn handle_endpoint(
endpoint: Arc<Endpoint>,
streams: Connections<IpDisplay, SendStream>,
rooms: Rooms<IpDisplay>,
) -> DynResult<()> {
while let Some(stream) = endpoint.accept().await {
let jh: JoinHandle<()> = match stream.accept() {
Ok(stream) => {
let peer: IpDisplay = stream.remote_address().into();
match stream.await {
Ok(connection) => match connection.open_bi().await {
Ok((writer, reader)) => {
let streams = streams.clone();
let rooms = rooms.clone();
task::spawn(async move {
{
let mut guard = streams.lock().await;
(*guard).insert(peer, writer);
};
match process(
&peer,
QuicReader::from(reader),
streams.clone(),
rooms.clone(),
)
.await
{
Ok(()) => (),
Err(err) => {
connection
.close(VarInt::from_u32(0), format!("{err}").as_ref());
log::warn!("Disconnected `{peer}`: {err}")
}
};
// CLEAN UP
let mut updated_rooms = vec![];
{
let mut guard = rooms.lock().await;
let mut empty_keys = Vec::new();
for (key, value) in guard.iter_mut() {
let mut room = value.lock().await;
let removed = room.users.remove(&peer);
if room.users.is_empty() {
if Arc::strong_count(value) > 1 {
log::error!("({key}: {value:?}) value has to high strong count");
}
empty_keys.push(*key);
} else if let Some(own) = removed.as_ref() {
if let Some(round) = room.round.as_ref() {
if round.users.contains(&own.id) {
// TODO: improve! Current behaviour is weird
// user of round left -> round broken -> set state of all to InRoom
room.round = None;
for (_, user) in room.users.iter_mut() {
user.state = UserState::InRoom;
}
}
}
updated_rooms.push(*key);
}
}
for key in empty_keys {
guard.remove(&key);
}
log::debug!("ROOMS: {:?}", *guard);
};
{
let mut guard = streams.lock().await;
(*guard).remove(&peer);
log::debug!("STREAMS: {:?}", guard.keys());
};
for room_id in updated_rooms {
if let Some(room) = rooms.lock().await.get(&room_id) {
if let Err(err) =
send_room_update(room_id, room, &streams).await
{
log::warn!("Failed to send room update: {err:?}");
}
}
}
})
}
Err(err) => {
log::trace!("Failed to accept bi: {err:?}");
continue;
}
},
Err(err) => {
log::trace!("Failed to await connecting: {err:?}");
continue;
}
}
}
Err(err) => {
log::trace!("Failed to get stream: {err}");
continue;
}
};
drop(jh);
}
Ok(())
}