Skip to content

Instantly share code, notes, and snippets.

@belohnung
Last active August 31, 2021 15:33
Show Gist options
  • Save belohnung/0d8ac13f13fd411aea09566dae8a0638 to your computer and use it in GitHub Desktop.
Save belohnung/0d8ac13f13fd411aea09566dae8a0638 to your computer and use it in GitHub Desktop.
Tool to ping MC:BE servers
use std::convert::TryInto;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
use std::thread;
const UNCONNECTED_PING: &'static [u8] = &[
0x01u8, // UNCONNECTED_PING ID
0x0, 0x0, 0x0, 0x0, 0x0, 0xD, 0x8, 0x8B, // ping-time
0x0, 0xFF, 0xFF, 0x0, 0xFE, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFD, 0xFD, 0x12, 0x34, 0x56,
0x78, // magic
];
fn main() {
let socket = UdpSocket::bind("0.0.0.0:0").expect("Could not bind to socket");
let server_address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(31, 172, 80, 69)), 19132);
socket.send_to(UNCONNECTED_PING, server_address);
loop {
let mut buf = [0u8; 1024];
let (length, address) = socket.recv_from(&mut buf).expect("Didn't receive data");
if length > 0 {
let buf = &mut buf[..length];
if buf[0] == 0x1c {
let mut pingtime = u64::from_be_bytes(buf[1..9].try_into().unwrap());
let mut strlength = u16::from_be_bytes(buf[33..35].try_into().unwrap());
let mut motd = String::from_utf8_lossy(&buf[35..]);
println!(
"IP:{:?} Pingtime : {:?} strlength: {:?}, motd: {:?}",
server_address, pingtime, strlength, motd
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment