readme and some changes
This commit is contained in:
parent
8d4a8ea0d3
commit
0710e32c63
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_mc_proto"
|
name = "rust_mc_proto"
|
||||||
description = "Minecraft packets protocol in pure rust"
|
description = "minecraft packets protocol in pure rust"
|
||||||
|
|
||||||
repository = "https://github.com/MeexReay/rust_mc_proto"
|
repository = "https://github.com/MeexReay/rust_mc_proto"
|
||||||
license-file = "LICENSE"
|
license-file = "LICENSE"
|
||||||
|
63
README.md
63
README.md
@ -1,2 +1,63 @@
|
|||||||
# rust_mc_proto
|
# rust_mc_proto
|
||||||
Minecraft packets protocol in pure rust
|
minecraft packets protocol in pure rust
|
||||||
|
supports compression (`MinecraftConnection::set_compression`)
|
||||||
|
|
||||||
|
all types of packets you can find on [wiki.vg](https://wiki.vg/)
|
||||||
|
|
||||||
|
|
||||||
|
## how to use it
|
||||||
|
|
||||||
|
for reference:
|
||||||
|
```rust
|
||||||
|
pub type MCConn<T> = MinecraftConnection<T>;
|
||||||
|
```
|
||||||
|
|
||||||
|
example how to get motd
|
||||||
|
```rust
|
||||||
|
use std::net::TcpStream;
|
||||||
|
use varint_rs::VarintWriter;
|
||||||
|
use rust_mc_proto::{Packet, ProtocolError, MCConn};
|
||||||
|
|
||||||
|
fn send_handshake(conn: &mut MCConn<TcpStream>,
|
||||||
|
protocol_version: u16,
|
||||||
|
server_address: &str,
|
||||||
|
server_port: u16,
|
||||||
|
next_state: u8) -> Result<(), ProtocolError> {
|
||||||
|
let mut packet = Packet::empty(0x00);
|
||||||
|
|
||||||
|
packet.write_u16_varint(protocol_version)?;
|
||||||
|
packet.write_string(server_address.to_string())?;
|
||||||
|
packet.write_unsigned_short(server_port)?;
|
||||||
|
packet.write_u8_varint(next_state)?;
|
||||||
|
|
||||||
|
conn.write_packet(&packet)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_status_request(conn: &mut MCConn<TcpStream>) -> Result<(), ProtocolError> {
|
||||||
|
let packet = Packet::empty(0x00);
|
||||||
|
conn.write_packet(&packet)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_status_response(conn: &mut MCConn<TcpStream>) -> Result<String, ProtocolError> {
|
||||||
|
let mut packet = conn.read_packet()?;
|
||||||
|
|
||||||
|
packet.read_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut conn = MCConn::connect("sloganmc.ru:25565").unwrap();
|
||||||
|
|
||||||
|
send_handshake(&mut conn, 765, "sloganmc.ru", 25565, 1).unwrap();
|
||||||
|
send_status_request(&mut conn).unwrap();
|
||||||
|
|
||||||
|
println!("{}", read_status_response(&mut conn).unwrap()); // prints servers motd in json
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
also you can get minecraft connection from any stream: `MinecraftConnection::from_stream`
|
||||||
|
|
||||||
|
I think this crate can be used for a server on rust idk -_-
|
36
src/lib.rs
36
src/lib.rs
@ -1,5 +1,5 @@
|
|||||||
use std::io::{Write, Read};
|
use std::io::{Write, Read};
|
||||||
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
|
use std::net::{TcpStream, ToSocketAddrs};
|
||||||
use bytebuffer::ByteBuffer;
|
use bytebuffer::ByteBuffer;
|
||||||
use varint_rs::{VarintWriter, VarintReader};
|
use varint_rs::{VarintWriter, VarintReader};
|
||||||
use flate2::{Compress, Compression, Decompress, FlushCompress, Status, FlushDecompress};
|
use flate2::{Compress, Compression, Decompress, FlushCompress, Status, FlushDecompress};
|
||||||
@ -109,15 +109,14 @@ impl Packet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Connection {
|
pub struct MinecraftConnection<T: Read + Write> {
|
||||||
pub stream: TcpStream,
|
pub stream: T,
|
||||||
pub addr: SocketAddr,
|
|
||||||
compress: bool,
|
compress: bool,
|
||||||
compress_threashold: usize
|
compress_threashold: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl MinecraftConnection<TcpStream> {
|
||||||
pub fn build(addr: &str) -> Result<Connection, ProtocolError> {
|
pub fn connect(addr: &str) -> Result<MinecraftConnection<TcpStream>, ProtocolError> {
|
||||||
let addr = match addr.to_socket_addrs() {
|
let addr = match addr.to_socket_addrs() {
|
||||||
Ok(mut i) => { match i.next() {
|
Ok(mut i) => { match i.next() {
|
||||||
Some(i) => { i },
|
Some(i) => { i },
|
||||||
@ -131,16 +130,21 @@ impl Connection {
|
|||||||
Err(_) => { return Err(ProtocolError::StreamConnectError) },
|
Err(_) => { return Err(ProtocolError::StreamConnectError) },
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Connection {
|
Ok(MinecraftConnection {
|
||||||
stream,
|
stream,
|
||||||
addr,
|
|
||||||
compress: false,
|
compress: false,
|
||||||
compress_threashold: 0
|
compress_threashold: 0
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(addr: &str) -> Connection {
|
impl<T: Read + Write> MinecraftConnection<T> {
|
||||||
Self::build(addr).unwrap()
|
pub fn new(stream: T) -> MinecraftConnection<T> {
|
||||||
|
MinecraftConnection {
|
||||||
|
stream,
|
||||||
|
compress: false,
|
||||||
|
compress_threashold: 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_compression(&mut self, threashold: usize) {
|
pub fn set_compression(&mut self, threashold: usize) {
|
||||||
@ -240,13 +244,9 @@ impl Connection {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self) {
|
|
||||||
self.stream.shutdown(std::net::Shutdown::Both).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VarintWriter for Connection {
|
impl<T: Read + Write> VarintWriter for MinecraftConnection<T> {
|
||||||
type Error = ProtocolError;
|
type Error = ProtocolError;
|
||||||
|
|
||||||
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
fn write(&mut self, byte: u8) -> Result<(), Self::Error> {
|
||||||
@ -257,7 +257,7 @@ impl VarintWriter for Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VarintReader for Connection {
|
impl<T: Read + Write> VarintReader for MinecraftConnection<T> {
|
||||||
type Error = ProtocolError;
|
type Error = ProtocolError;
|
||||||
|
|
||||||
fn read(&mut self) -> Result<u8, Self::Error> {
|
fn read(&mut self) -> Result<u8, Self::Error> {
|
||||||
@ -302,4 +302,6 @@ fn decompress_zlib(bytes: &[u8]) -> Result<Vec<u8>, ProtocolError> {
|
|||||||
},
|
},
|
||||||
Err(_) => Err(ProtocolError::ZlibError)
|
Err(_) => Err(ProtocolError::ZlibError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type MCConn<T> = MinecraftConnection<T>;
|
29
src/main.rs
29
src/main.rs
@ -1,37 +1,42 @@
|
|||||||
use rust_mc_proto::{Connection, Packet, ProtocolError};
|
use std::net::TcpStream;
|
||||||
use varint_rs::{VarintReader, VarintWriter};
|
use varint_rs::VarintWriter;
|
||||||
use std::io::Write;
|
use rust_mc_proto::{Packet, ProtocolError, MCConn};
|
||||||
|
|
||||||
fn send_handshake(conn: &mut Connection,
|
fn send_handshake(conn: &mut MCConn<TcpStream>,
|
||||||
protocol_version: u16,
|
protocol_version: u16,
|
||||||
server_address: &str,
|
server_address: &str,
|
||||||
server_port: u16,
|
server_port: u16,
|
||||||
next_state: u8) -> Result<(), ProtocolError> {
|
next_state: u8) -> Result<(), ProtocolError> {
|
||||||
let mut packet = Packet::empty(0x00);
|
let mut packet = Packet::empty(0x00);
|
||||||
|
|
||||||
packet.write_u16_varint(protocol_version)?;
|
packet.write_u16_varint(protocol_version)?;
|
||||||
packet.write_string(server_address.to_string())?;
|
packet.write_string(server_address.to_string())?;
|
||||||
packet.write_unsigned_short(server_port)?;
|
packet.write_unsigned_short(server_port)?;
|
||||||
packet.write_u8_varint(next_state)?;
|
packet.write_u8_varint(next_state)?;
|
||||||
|
|
||||||
packet.buffer.set_rpos(0);
|
conn.write_packet(&packet)?;
|
||||||
packet.buffer.set_wpos(0);
|
|
||||||
|
|
||||||
conn.write_packet(&packet)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_status_request(conn: &mut Connection) -> Result<(), ProtocolError> {
|
fn send_status_request(conn: &mut MCConn<TcpStream>) -> Result<(), ProtocolError> {
|
||||||
conn.write_packet(&Packet::empty(0x00))
|
let packet = Packet::empty(0x00);
|
||||||
|
conn.write_packet(&packet)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_status_response(conn: &mut Connection) -> Result<String, ProtocolError> {
|
fn read_status_response(conn: &mut MCConn<TcpStream>) -> Result<String, ProtocolError> {
|
||||||
let mut packet = conn.read_packet()?;
|
let mut packet = conn.read_packet()?;
|
||||||
|
|
||||||
packet.read_string()
|
packet.read_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut conn = Connection::new("localhost:25565");
|
let mut conn = MCConn::connect("sloganmc.ru:25565").unwrap();
|
||||||
|
|
||||||
send_handshake(&mut conn, 765, "localhost", 25565, 1).unwrap();
|
send_handshake(&mut conn, 765, "sloganmc.ru", 25565, 1).unwrap();
|
||||||
send_status_request(&mut conn).unwrap();
|
send_status_request(&mut conn).unwrap();
|
||||||
|
|
||||||
println!("{}", read_status_response(&mut conn).unwrap());
|
println!("{}", read_status_response(&mut conn).unwrap());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user