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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! Implementation of the [Query](https://wiki.vg/Query) protocol.

use bytes::{Buf, BufMut, Bytes, BytesMut};
use rand::random;
use std::collections::HashMap;
use std::time::Duration;
use tokio::io;
use tokio::net::UdpSocket;
use tokio::time::timeout;

use crate::errors::QueryProtocolError;

static QUERY_MAGIC: u16 = 0xfe_fd;
static SESSION_ID_MASK: u32 = 0x0f_0f_0f_0f;

/// A response from the server's basic query.
/// Taken from [wiki.vg](https://wiki.vg/Query#Response_2)
#[derive(Debug)]
pub struct BasicStatResponse {
    /// The "motd" - message shown in the server list by the client.
    pub motd: String,

    /// The server's game type.
    /// Vanilla servers hardcode this to "SMP".
    pub game_type: String,

    /// The server's world/map name.
    pub map: String,

    /// The current number of online players.
    pub num_players: usize,

    /// Maximum players online this server allows.
    pub max_players: usize,

    /// The port the serer is running on.
    pub host_port: u16,

    /// THe server's IP address.
    pub host_ip: String,
}

/// A response from the server's full query.
/// Taken from [wiki.vg](https://wiki.vg/Query#Response_3)
#[derive(Debug)]
pub struct FullStatResponse {
    /// The "motd" - message shown in the server list by the client.
    pub motd: String,

    /// The server's game type.
    /// Vanilla servers hardcode this to "SMP".
    pub game_type: String,

    /// The server's game ID.
    /// Vanilla servers hardcode this to "MINECRAFT".
    pub game_id: String,

    /// The server's game version.
    pub version: String,

    /// The plugins the server has installed.
    /// Vanilla servers return an empty string.
    /// Other server platforms may have their own format for this field.
    pub plugins: String,

    /// The server's world/map name.
    pub map: String,

    /// The current number of online players.
    pub num_players: usize,

    /// Maximum players online this server allows.
    pub max_players: usize,

    /// The port the server is running on.
    pub host_port: u16,

    /// The server's IP address.
    pub host_ip: String,

    /// THe current list of online players.
    pub players: Vec<String>,
}

/// Perform a basic stat query of the server per the [Query Protocol](https://wiki.vg/Query#Basic_Stat).
/// Note that the server must have `query-enabled=true` set in its properties to get a response.
/// The `query.port` property might also be different from `server.port`.
///
/// # Arguments
/// * `host` - the hostname/IP of thr server to query
/// * `port` - the port that the server's Query is running on
///
/// # Examples
/// ```
/// use mc_query::query;
/// use tokio::io::Result;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let res = query::stat_basic("localhost", 25565).await?;
///     println!("The server has {} players online out of {}", res.num_players, res.num_players);
///
///     Ok(())
/// }
/// ```
pub async fn stat_basic(host: &str, port: u16) -> io::Result<BasicStatResponse> {
    let socket = UdpSocket::bind("0.0.0.0:0").await?;
    socket.connect(format!("{host}:{port}")).await?;

    let (token, session) = handshake(&socket).await?;

    let mut bytes = BytesMut::new();
    bytes.put_u16(QUERY_MAGIC);
    bytes.put_u8(0); // packet type 0 - stat
    bytes.put_i32(session);
    bytes.put_i32(token);
    socket.send(&bytes).await?;

    let t = timeout(Duration::from_millis(250), recv_packet(&socket)).await;
    let mut res = match t {
        Ok(result) => result?,
        Err(_) => {
            // super unlucky time of challenge token expiring before we can use it
            // must retry handshake and request
            let (token, session) = handshake(&socket).await?;
            let mut bytes = BytesMut::new();
            bytes.put_u16(QUERY_MAGIC);
            bytes.put_u8(0); // packet type 0 - stat
            bytes.put_i32(session);
            bytes.put_i32(token);
            timeout(Duration::from_millis(250), recv_packet(&socket)).await??
        }
    };

    validate_packet(&mut res, 0, session)?;

    let motd = get_string(&mut res)?;
    let game_type = get_string(&mut res)?;
    let map = get_string(&mut res)?;
    let num_players = get_string(&mut res)?
        .parse()
        .map_err::<io::Error, _>(|_| QueryProtocolError::CannotParseInt.into())?;
    let max_players = get_string(&mut res)?
        .parse()
        .map_err::<io::Error, _>(|_| QueryProtocolError::CannotParseInt.into())?;

    let host_port = res.get_u16_le(); // shorts are little endian per protocol

    let host_ip = get_string(&mut res)?;

    Ok(BasicStatResponse {
        motd,
        game_type,
        map,
        num_players,
        max_players,
        host_port,
        host_ip,
    })
}

/// Perform a full stat query of the server per the [Query Protocol](https://wiki.vg/Query#Full_stat).
/// Note that the server must have `query-enabled=true` set in its properties to get a response.
/// The `query.port` property might also be different from `server.port`.
///
/// # Arguments
/// * `host` - the hostname/IP of thr server to query
/// * `port` - the port that the server's Query is running on
///
/// # Examples
/// ```
/// use mc_query::query;
/// use tokio::io::Result;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let res = query::stat_full("localhost", 25565).await?;
///     println!("The server has {} players online out of {}", res.num_players, res.num_players);
///
///     Ok(())
/// }
/// ```
pub async fn stat_full(host: &str, port: u16) -> io::Result<FullStatResponse> {
    let socket = UdpSocket::bind("0.0.0.0:0").await?;
    socket.connect(format!("{host}:{port}")).await?;

    let (token, session) = handshake(&socket).await?;

    let mut bytes = BytesMut::new();
    bytes.put_u16(QUERY_MAGIC);
    bytes.put_u8(0); // packet type 0 - stat
    bytes.put_i32(session);
    bytes.put_i32(token);
    bytes.put_u32(0); // 4 extra bytes required for full stat vs. basic
    socket.send(&bytes).await?;

    let t = timeout(Duration::from_millis(250), recv_packet(&socket)).await;
    let mut res = match t {
        Ok(result) => result?,
        Err(_) => {
            // super unlucky time of challenge token expiring before we can use it
            // must retry handshake and request
            let (token, session) = handshake(&socket).await?;
            let mut bytes = BytesMut::new();
            bytes.put_u16(QUERY_MAGIC);
            bytes.put_u8(0); // packet type 0 - stat
            bytes.put_i32(session);
            bytes.put_i32(token);
            bytes.put_u32(0); // 4 extra bytes required for full stat vs. basic
            timeout(Duration::from_millis(250), recv_packet(&socket)).await??
        }
    };
    validate_packet(&mut res, 0, session)?;

    // skip 11 meaningless padding bytes
    for _ in 0..11 {
        res.get_u8();
    }

    // K,V section
    let mut kv = HashMap::new();
    loop {
        let key = get_string(&mut res)?;
        if key.is_empty() {
            break;
        }
        let value = get_string(&mut res)?;
        kv.insert(key, value);
    }

    // excuse this horrendous code, I don't know of a better way
    let motd = kv
        .remove("hostname")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let game_type = kv
        .remove("gametype")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let game_id = kv
        .remove("game_id")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let version = kv
        .remove("version")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let plugins = kv
        .remove("plugins")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let map = kv
        .remove("map")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;
    let num_players = kv
        .remove("numplayers")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?
        .parse()
        .map_err(|_| QueryProtocolError::CannotParseInt)?;
    let max_players = kv
        .remove("maxplayers")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?
        .parse()
        .map_err(|_| QueryProtocolError::CannotParseInt)?;
    let host_port = kv
        .remove("hostport")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?
        .parse()
        .map_err(|_| QueryProtocolError::CannotParseInt)?;
    let host_ip = kv
        .remove("hostip")
        .ok_or(QueryProtocolError::InvalidKeyValueSection)?;

    // skip 10 meaningless padding bytes
    for _ in 0..10 {
        res.get_u8();
    }

    // players section
    let mut players = vec![];
    loop {
        let username = get_string(&mut res)?;
        if username.is_empty() {
            break;
        }
        players.push(username);
    }

    Ok(FullStatResponse {
        motd,
        game_type,
        game_id,
        version,
        plugins,
        map,
        num_players,
        max_players,
        host_port,
        host_ip,
        players,
    })
}

/// Perform a handshake request per https://wiki.vg/Query#Handshake
///
/// # Returns
/// A tuple `(challenge_token, session_id)` to be used in subsequent server interactions
async fn handshake(socket: &UdpSocket) -> io::Result<(i32, i32)> {
    // generate new token per interaction to avoid reset problems
    let session_id = (random::<u32>() & SESSION_ID_MASK) as i32;

    let mut req = BytesMut::with_capacity(7);
    req.put_u16(QUERY_MAGIC);
    req.put_u8(9); // packet type 9 - handshake
    req.put_i32(session_id);
    // no payload for handshake requests

    socket.send(&req).await?;

    let mut res = recv_packet(socket).await?;
    validate_packet(&mut res, 9, session_id)?;

    let token_str = get_string(&mut res)?;

    token_str
        .parse()
        .map(|t| (t, session_id))
        .map_err(|_| QueryProtocolError::CannotParseInt.into())
}

async fn recv_packet(socket: &UdpSocket) -> io::Result<Bytes> {
    let mut buf = [0u8; 65536];
    socket.recv(&mut buf).await?;

    Ok(Bytes::copy_from_slice(&buf))
}

fn validate_packet(packet: &mut Bytes, expected_type: u8, expected_session: i32) -> io::Result<()> {
    let recv_type = packet.get_u8();
    if recv_type != expected_type {
        return Err(QueryProtocolError::InvalidPacketType.into());
    }

    let recv_session = packet.get_i32();
    if recv_session != expected_session {
        return Err(QueryProtocolError::SessionIdMismatch.into());
    }

    Ok(())
}

fn get_string(bytes: &mut Bytes) -> io::Result<String> {
    let mut buf = vec![];
    loop {
        let byte = bytes.get_u8();
        if byte == 0 {
            break;
        }
        buf.push(byte);
    }

    String::from_utf8(buf).map_err(|_| QueryProtocolError::InvalidUtf8.into())
}

#[cfg(test)]
mod tests {
    use tokio::io;

    use super::{stat_basic, stat_full};

    #[tokio::test]
    async fn test_stat_basic() -> io::Result<()> {
        let response = stat_basic("localhost", 25565).await?;
        println!("{response:#?}");

        Ok(())
    }

    #[tokio::test]
    async fn test_stat_full() -> io::Result<()> {
        let response = stat_full("localhost", 25565).await?;
        println!("{response:#?}");

        Ok(())
    }
}