aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/server_status_pinger.rs
blob: ae41ed51a82121822c1a25bccdb96dec588604d6 (plain)
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
use crate::{
    connection::Connection,
    packets::{
        handshake::client_intention_packet::ClientIntentionPacket,
        status::serverbound_status_request_packet::ServerboundStatusRequestPacket,
        ConnectionProtocol, Packet, PacketTrait,
    },
    resolver, ServerAddress,
};

pub async fn ping_server(address: &ServerAddress) -> Result<(), String> {
    let resolved_address = resolver::resolve_address(address).await?;

    let mut conn = Connection::new(&resolved_address).await?;

    println!("resolved_address {}", &resolved_address.ip);
    println!("writing intention packet {}", address.host);

    // send the client intention packet and switch to the status state
    conn.send_packet(
        ClientIntentionPacket {
            protocol_version: 757,
            hostname: address.host.clone(),
            port: address.port,
            intention: ConnectionProtocol::Status,
        }
        .get(),
    )
    .await;
    conn.switch_state(ConnectionProtocol::Status);

    // send the empty status request packet
    conn.send_packet(ServerboundStatusRequestPacket {}.get())
        .await;

    let packet = conn.read_packet().await.unwrap();

    match packet {
        Packet::ClientboundStatusResponsePacket(p) => {
            println!("{:?}", p);
            println!("{}", p.description.to_ansi(None));
        }
        _ => {
            println!("unexpected packet {:?}", packet);
        }
    }

    Ok(())

    // let data = mc_buf::read_varint(conn.stream);
    // println!("data {}", data);

    // // log what the server sends back
    // loop {
    //     if 0 == conn.stream.read_buf(&mut conn.buffer).await.unwrap() {
    //         // The remote closed the connection. For this to be a clean
    //         // shutdown, there should be no data in the read buffer. If
    //         // there is, this means that the peer closed the socket while
    //         // sending a frame.

    //         // log conn.buffer
    //         println!("{:?}", conn.buffer);
    //         if conn.buffer.is_empty() {
    //             println!("buffer is empty ok");
    //             return Ok(());
    //         } else {
    //             return Err("connection reset by peer".into());
    //         }
    //     }
    // }
}