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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#[allow(deprecated)] use std::hash::SipHasher;
use std::{
    hash::{Hash, Hasher},
    io::{self, ErrorKind},
    net::SocketAddr,
    sync::{Arc, Mutex},
    time::{Duration, Instant},
};

use protocol::Parcel;
use rand::{thread_rng, Rng};
use tokio::{net::UdpSocket, sync::watch};
use tracing::{debug, error, trace};

use crate::{
    proto::{
        Init, QueryServerRequest, QueryServerResponse, RawQueryServerRequest,
        RawQueryServerResponse, ServerInfo, MAX_REQUEST_SIZE, MAX_RESPONSE_SIZE, VELOREN_HEADER,
        VERSION,
    },
    ratelimit::{RateLimiter, ReducedIpAddr},
};

const SECRET_REGEN_INTERNVAL: Duration = Duration::from_secs(300);

pub struct QueryServer {
    addr: SocketAddr,
    server_info: watch::Receiver<ServerInfo>,
    settings: protocol::Settings,
    ratelimit: RateLimiter,
}

#[derive(Default, Clone, Copy, Debug)]
pub struct Metrics {
    pub received_packets: u32,
    pub dropped_packets: u32,
    pub invalid_packets: u32,
    pub proccessing_errors: u32,
    pub info_requests: u32,
    pub init_requests: u32,
    pub sent_responses: u32,
    pub failed_responses: u32,
    pub timed_out_responses: u32,
    pub ratelimited: u32,
}

impl QueryServer {
    pub fn new(addr: SocketAddr, server_info: watch::Receiver<ServerInfo>, ratelimit: u16) -> Self {
        Self {
            addr,
            server_info,
            ratelimit: RateLimiter::new(ratelimit),
            settings: Default::default(),
        }
    }

    /// This produces TRACE level logs for any packet received on the assigned
    /// port. To prevent potentially unfettered log spam, disable the TRACE
    /// level for this crate (when outside of debugging contexts).
    ///
    /// NOTE: TRACE and DEBUG levels are disabled by default for this crate when
    /// using `veloren-common-frontend`.
    pub async fn run(&mut self, metrics: Arc<Mutex<Metrics>>) -> Result<(), tokio::io::Error> {
        let mut socket = UdpSocket::bind(self.addr).await?;

        let gen_secret = || {
            let mut rng = thread_rng();
            (rng.gen::<u64>(), rng.gen::<u64>())
        };
        let mut secrets = gen_secret();
        let mut last_secret_refresh = Instant::now();

        let mut buf = Box::new([0; MAX_REQUEST_SIZE]);
        loop {
            let (len, remote_addr) = match socket.recv_from(&mut *buf).await {
                Ok(v) => v,
                Err(e) if e.kind() == ErrorKind::NotConnected => {
                    error!(
                        ?e,
                        "Query server connection was closed, re-binding to socket..."
                    );
                    socket = UdpSocket::bind(self.addr).await?;
                    continue;
                },
                err => {
                    debug!(?err, "Error while receiving from query server socket");
                    continue;
                },
            };

            let mut new_metrics = Metrics {
                received_packets: 1,
                ..Default::default()
            };

            let raw_msg_buf = &buf[..len];
            let msg_buf = if Self::validate_datagram(raw_msg_buf) {
                // Require 2 extra bytes for version (currently unused)
                &raw_msg_buf[2..(raw_msg_buf.len() - VELOREN_HEADER.len())]
            } else {
                new_metrics.dropped_packets += 1;
                continue;
            };

            self.process_datagram(msg_buf, remote_addr, secrets, &mut new_metrics, &socket)
                .await;

            // Update metrics at the end of eath packet
            if let Ok(mut metrics) = metrics.lock() {
                *metrics += new_metrics;
            }

            {
                let now = Instant::now();
                if now.duration_since(last_secret_refresh) > SECRET_REGEN_INTERNVAL {
                    last_secret_refresh = now;
                    secrets = gen_secret();
                }

                self.ratelimit.maintain(now);
            }
        }
    }

    // Header must be discarded after this validation passes
    fn validate_datagram(data: &[u8]) -> bool {
        let len = data.len();
        // Require 2 extra bytes for version (currently unused)
        if len < MAX_RESPONSE_SIZE.max(VELOREN_HEADER.len() + 2) {
            trace!(?len, "Datagram too short");
            false
        } else if len > MAX_REQUEST_SIZE {
            trace!(?len, "Datagram too large");
            false
        } else if data[(len - VELOREN_HEADER.len())..] != VELOREN_HEADER {
            trace!(?len, "Datagram header invalid");
            false
        // TODO: Allow lower versions once proper versioning is added.
        } else if u16::from_ne_bytes(data[..2].try_into().unwrap()) != VERSION {
            trace!(
                "Datagram has invalid version {:?}, current {VERSION:?}",
                &data[..2]
            );
            false
        } else {
            true
        }
    }

    async fn process_datagram(
        &mut self,
        datagram: &[u8],
        remote: SocketAddr,
        secrets: (u64, u64),
        metrics: &mut Metrics,
        socket: &UdpSocket,
    ) {
        let Ok(RawQueryServerRequest {
            p: client_p,
            request,
        }) =
            <RawQueryServerRequest as Parcel>::read(&mut io::Cursor::new(datagram), &self.settings)
        else {
            metrics.invalid_packets += 1;
            return;
        };

        trace!(?request, "Received packet");

        #[allow(deprecated)]
        let real_p = {
            // Use SipHash-2-4 to compute the `p` value from a server specific
            // secret and the client's address.
            //
            // This is used to verify that packets are from an entity that can
            // receive packets at the given address.
            //
            // Only use the first 64 bits from Ipv6 addresses since the latter
            // 64 bits can change very frequently (as much as for every
            // request).
            let mut hasher = SipHasher::new_with_keys(secrets.0, secrets.1);
            ReducedIpAddr::from(remote.ip()).hash(&mut hasher);
            hasher.finish()
        };

        if real_p != client_p {
            Self::send_response(
                RawQueryServerResponse::Init(Init {
                    p: real_p,
                    max_supported_version: VERSION,
                }),
                remote,
                socket,
                metrics,
            )
            .await;

            return;
        }

        if !self.ratelimit.can_request(remote.ip().into()) {
            trace!("Ratelimited request");
            metrics.ratelimited += 1;
            return;
        }

        match request {
            QueryServerRequest::Init => {
                metrics.init_requests += 1;
                Self::send_response(
                    RawQueryServerResponse::Init(Init {
                        p: real_p,
                        max_supported_version: VERSION,
                    }),
                    remote,
                    socket,
                    metrics,
                )
                .await;
            },
            QueryServerRequest::ServerInfo => {
                metrics.info_requests += 1;
                let server_info = *self.server_info.borrow();
                Self::send_response(
                    RawQueryServerResponse::Response(QueryServerResponse::ServerInfo(server_info)),
                    remote,
                    socket,
                    metrics,
                )
                .await;
            },
        }
    }

    async fn send_response(
        response: RawQueryServerResponse,
        addr: SocketAddr,
        socket: &UdpSocket,
        metrics: &mut Metrics,
    ) {
        // TODO: Once more versions are added, send the packet in the same version as
        // the request here.
        match <RawQueryServerResponse as Parcel>::raw_bytes(&response, &Default::default()) {
            Ok(data) => {
                if data.len() > MAX_RESPONSE_SIZE {
                    error!(
                        ?MAX_RESPONSE_SIZE,
                        "Attempted to send a response larger than the maximum allowed size (size: \
                         {}, response: {response:?})",
                        data.len()
                    );
                    #[cfg(debug_assertions)]
                    panic!(
                        "Attempted to send a response larger than the maximum allowed size (size: \
                         {}, max: {}, response: {response:?})",
                        data.len(),
                        MAX_RESPONSE_SIZE
                    );
                }

                match socket.send_to(&data, addr).await {
                    Ok(_) => {
                        metrics.sent_responses += 1;
                    },
                    Err(err) => {
                        metrics.failed_responses += 1;
                        debug!(?err, "Failed to send query server response");
                    },
                }
            },
            Err(error) => {
                trace!(?error, "Failed to serialize response");
                #[cfg(debug_assertions)]
                panic!("Serializing response failed: {error:?} ({response:?})");
            },
        }
    }
}

impl std::ops::AddAssign for Metrics {
    fn add_assign(
        &mut self,
        Self {
            received_packets,
            dropped_packets,
            invalid_packets,
            proccessing_errors,
            info_requests,
            init_requests,
            sent_responses,
            failed_responses,
            timed_out_responses,
            ratelimited,
        }: Self,
    ) {
        self.received_packets += received_packets;
        self.dropped_packets += dropped_packets;
        self.invalid_packets += invalid_packets;
        self.proccessing_errors += proccessing_errors;
        self.info_requests += info_requests;
        self.init_requests += init_requests;
        self.sent_responses += sent_responses;
        self.failed_responses += failed_responses;
        self.timed_out_responses += timed_out_responses;
        self.ratelimited += ratelimited;
    }
}

impl Metrics {
    /// Resets all metrics to 0 and returns previous ones
    ///
    /// Used by the consumer of the metrics.
    pub fn reset(&mut self) -> Self { std::mem::take(self) }
}