stevenarella/protocol/src/protocol/mod.rs

1023 lines
29 KiB
Rust
Raw Normal View History

2016-03-16 14:25:35 -04:00
// Copyright 2016 Matthew Collins
2015-09-17 11:21:56 -04:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2015-09-07 16:11:00 -04:00
#![allow(dead_code)]
2015-09-17 11:04:25 -04:00
use aes::Aes128;
use cfb8::Cfb8;
use cfb8::stream_cipher::{NewStreamCipher, StreamCipher};
2015-09-17 11:04:25 -04:00
use serde_json;
Replace hyper with reqwest (#7) An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well * Begin updating to hyper 0.12.11 https://github.com/iceiix/steven/issues/4#issuecomment-425759778 * Use type variables for hyper::Client * Fix setting header syntax, Content-Type: application/json, 17->13 * Parse strings into URLs with url.parse::<hyper::Uri>().unwrap() https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25 * Use hyper::Request::post() then client.request() since client.post() removed * wait() on the ResponseFuture to get the Result * try! to unwrap the Result * status() is now a method * Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile * Replace send() with wait() on ResponseFuture * Parse HeaderValue to u64 * Slices implement std::io::Read trait * Read into_bytes() instead of read_to_end() * Disable boxed logger for now to workaround 'expected function, found macro' * Remove unnecessary mutability, warnings * Hack to parse twice to avoid double move * Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper * Start converting to reqwest: add Protocol::Error and reqwest::Error conversion * Use reqwest, replacing hyper, in protocol * Convert resources to use reqwest instead of hyper * Convert skin download to reqwest, instead of hyper * Remove hyper * Revert unnecessary variable name change req/body to reduce diff * Revert unnecessary whitespace change to reduce diff, align indentation on . * Fix authenticating to server, wrong method and join URL * Update Cargo.lock
2018-10-27 20:03:34 -04:00
use reqwest;
2015-09-07 16:11:00 -04:00
2015-09-10 06:49:41 -04:00
pub mod mojang;
2015-09-12 15:31:26 -04:00
use nbt;
2015-09-10 06:49:41 -04:00
use format;
2015-09-12 15:31:26 -04:00
use std::fmt;
2015-09-08 07:57:24 -04:00
use std::default;
2015-09-07 16:11:00 -04:00
use std::net::TcpStream;
use std::io;
use std::io::{Write, Read};
use std::convert;
2015-09-17 11:04:25 -04:00
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
use flate2::read::{ZlibDecoder, ZlibEncoder};
use flate2::Compression;
use std::time::{Instant, Duration};
use shared::Position;
2015-09-25 09:00:49 -04:00
2016-12-09 09:32:02 -05:00
pub const SUPPORTED_PROTOCOL: i32 = 315;
2015-09-07 16:11:00 -04:00
2015-09-07 16:11:00 -04:00
/// Helper macro for defining packets
#[macro_export]
macro_rules! state_packets {
($($state:ident $stateName:ident {
$($dir:ident $dirName:ident {
2015-09-12 15:31:26 -04:00
$(
$(#[$attr:meta])*
packet $name:ident {
$($(#[$fattr:meta])*field $field:ident: $field_type:ty = $(when ($cond:expr))*, )+
}
)*
2015-09-07 16:11:00 -04:00
})+
})+) => {
use protocol::*;
use std::io;
2015-09-12 15:31:26 -04:00
#[derive(Debug)]
2015-09-07 16:11:00 -04:00
pub enum Packet {
$(
$(
$(
$name($state::$dir::$name),
)*
)+
)+
}
$(
pub mod $state {
2015-09-07 16:11:00 -04:00
$(
pub mod $dir {
2015-09-10 06:49:41 -04:00
#![allow(unused_imports)]
2015-09-07 16:11:00 -04:00
use protocol::*;
use std::io;
2015-09-10 06:49:41 -04:00
use format;
2015-09-12 15:31:26 -04:00
use nbt;
use types;
use item;
use shared::Position;
2015-09-07 16:11:00 -04:00
pub mod internal_ids {
create_ids!(i32, $($name),*);
}
2015-09-07 16:11:00 -04:00
$(
2015-09-12 15:31:26 -04:00
#[derive(Default, Debug)]
$(#[$attr])* pub struct $name {
$($(#[$fattr])* pub $field: $field_type),+,
2015-09-07 16:11:00 -04:00
}
impl PacketType for $name {
fn packet_id(&self) -> i32 { internal_ids::$name }
2015-09-07 16:11:00 -04:00
fn write<W: io::Write>(self, buf: &mut W) -> Result<(), Error> {
2015-09-07 16:11:00 -04:00
$(
2015-09-10 06:49:41 -04:00
if true $(&& ($cond(&self)))* {
self.$field.write_to(buf)?;
2015-09-10 06:49:41 -04:00
}
2015-09-07 16:11:00 -04:00
)+
Result::Ok(())
}
}
)*
}
)+
}
)+
/// Returns the packet for the given state, direction and id after parsing the fields
/// from the buffer.
pub fn packet_by_id<R: io::Read>(state: State, dir: Direction, id: i32, mut buf: &mut R) -> Result<Option<Packet>, Error> {
2015-09-07 16:11:00 -04:00
match state {
$(
State::$stateName => {
match dir {
$(
Direction::$dirName => {
match id {
$(
self::$state::$dir::internal_ids::$name => {
2015-09-10 06:49:41 -04:00
use self::$state::$dir::$name;
let mut packet : $name = $name::default();
2015-09-08 07:57:24 -04:00
$(
2015-09-10 06:49:41 -04:00
if true $(&& ($cond(&packet)))* {
packet.$field = Serializable::read_from(&mut buf)?;
2015-09-10 06:49:41 -04:00
}
2015-09-08 07:57:24 -04:00
)+
Result::Ok(Option::Some(Packet::$name(packet)))
},
2015-09-07 16:11:00 -04:00
)*
2015-09-08 07:57:24 -04:00
_ => Result::Ok(Option::None)
2015-09-07 16:11:00 -04:00
}
}
)+
}
}
)+
}
}
}
}
pub mod packet;
2015-09-25 09:00:49 -04:00
pub trait Serializable: Sized {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error>;
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error>;
2015-09-08 07:57:24 -04:00
}
2015-09-12 15:31:26 -04:00
impl Serializable for Vec<u8> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Vec<u8>, Error> {
2015-09-12 15:31:26 -04:00
let mut v = Vec::new();
buf.read_to_end(&mut v)?;
2015-09-12 15:31:26 -04:00
Ok(v)
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_all(&self[..]).map_err(|v| v.into())
2015-09-12 15:31:26 -04:00
}
}
impl Serializable for Option<nbt::NamedTag>{
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<nbt::NamedTag>, Error> {
let ty = buf.read_u8()?;
2015-09-12 15:31:26 -04:00
if ty == 0 {
Result::Ok(None)
} else {
let name = nbt::read_string(buf)?;
let tag = nbt::Tag::read_from(buf)?;
2015-09-12 15:31:26 -04:00
Result::Ok(Some(nbt::NamedTag(name, tag)))
}
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-12 15:31:26 -04:00
match *self {
Some(ref val) => {
buf.write_u8(10)?;
nbt::write_string(buf, &val.0)?;
val.1.write_to(buf)?;
2015-09-12 15:31:26 -04:00
}
None => buf.write_u8(0)?,
2015-09-12 15:31:26 -04:00
}
Result::Ok(())
}
}
2015-09-10 06:49:41 -04:00
impl <T> Serializable for Option<T> where T : Serializable {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Option<T>, Error> {
Result::Ok(Some(T::read_from(buf)?))
2015-09-10 06:49:41 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-10 06:49:41 -04:00
if self.is_some() {
self.as_ref().unwrap().write_to(buf)?;
2015-09-10 06:49:41 -04:00
}
Result::Ok(())
}
}
2015-09-08 07:57:24 -04:00
impl Serializable for String {
fn read_from<R: io::Read>(buf: &mut R) -> Result<String, Error> {
2016-12-09 09:32:02 -05:00
let len = VarInt::read_from(buf)?.0;
debug_assert!(len >= 0, "Negative string length: {}", len);
debug_assert!(len <= 65536, "String length too big: {}", len);
2015-09-08 07:57:24 -04:00
let mut ret = String::new();
buf.take(len as u64).read_to_string(&mut ret)?;
2015-09-08 07:57:24 -04:00
Result::Ok(ret)
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-08 07:57:24 -04:00
let bytes = self.as_bytes();
VarInt(bytes.len() as i32).write_to(buf)?;
buf.write_all(bytes)?;
2015-09-08 07:57:24 -04:00
Result::Ok(())
}
}
2015-09-10 06:49:41 -04:00
impl Serializable for format::Component {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
let len = VarInt::read_from(buf)?.0;
2015-09-10 06:49:41 -04:00
let mut ret = String::new();
buf.take(len as u64).read_to_string(&mut ret)?;
2015-10-07 14:36:59 -04:00
let val: serde_json::Value = serde_json::from_str(&ret[..]).unwrap();
2015-09-10 06:49:41 -04:00
Result::Ok(Self::from_value(&val))
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-10 06:49:41 -04:00
let val = serde_json::to_string(&self.to_value()).unwrap();
let bytes = val.as_bytes();
VarInt(bytes.len() as i32).write_to(buf)?;
buf.write_all(bytes)?;
2015-09-10 06:49:41 -04:00
Result::Ok(())
}
}
impl Serializable for () {
fn read_from<R: io::Read>(_: &mut R) -> Result<(), Error> {
2015-09-10 06:49:41 -04:00
Result::Ok(())
}
2016-04-16 16:43:41 -04:00
fn write_to<W: io::Write>(&self, _: &mut W) -> Result<(), Error> {
2015-09-10 06:49:41 -04:00
Result::Ok(())
}
}
impl Serializable for bool {
fn read_from<R: io::Read>(buf: &mut R) -> Result<bool, Error> {
Result::Ok(buf.read_u8()? != 0)
2015-09-08 07:57:24 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_u8(if *self {
2015-10-07 14:36:59 -04:00
1
} else {
0
})?;
2015-09-08 07:57:24 -04:00
Result::Ok(())
}
}
2015-09-12 15:31:26 -04:00
impl Serializable for i8 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<i8, Error> {
Result::Ok(buf.read_i8()?)
2015-09-12 15:31:26 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_i8(*self)?;
2015-09-12 15:31:26 -04:00
Result::Ok(())
}
}
2015-09-10 06:49:41 -04:00
impl Serializable for i16 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<i16, Error> {
Result::Ok(buf.read_i16::<BigEndian>()?)
2015-09-10 06:49:41 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_i16::<BigEndian>(*self)?;
2015-09-10 06:49:41 -04:00
Result::Ok(())
}
2015-09-08 07:57:24 -04:00
}
impl Serializable for i32 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<i32, Error> {
Result::Ok(buf.read_i32::<BigEndian>()?)
2015-09-08 07:57:24 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_i32::<BigEndian>(*self)?;
2015-09-08 07:57:24 -04:00
Result::Ok(())
}
}
impl Serializable for i64 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<i64, Error> {
Result::Ok(buf.read_i64::<BigEndian>()?)
2015-09-08 07:57:24 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_i64::<BigEndian>(*self)?;
2015-09-08 07:57:24 -04:00
Result::Ok(())
}
}
2015-09-10 06:49:41 -04:00
impl Serializable for u8 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<u8, Error> {
Result::Ok(buf.read_u8()?)
2015-09-10 06:49:41 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_u8(*self)?;
2015-09-10 06:49:41 -04:00
Result::Ok(())
}
}
2015-09-08 07:57:24 -04:00
impl Serializable for u16 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<u16, Error> {
Result::Ok(buf.read_u16::<BigEndian>()?)
2015-09-08 07:57:24 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_u16::<BigEndian>(*self)?;
2015-09-08 07:57:24 -04:00
Result::Ok(())
}
}
2016-03-21 10:05:13 -04:00
impl Serializable for u64 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<u64, Error> {
Result::Ok(buf.read_u64::<BigEndian>()?)
2016-03-21 10:05:13 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_u64::<BigEndian>(*self)?;
2016-03-21 10:05:13 -04:00
Result::Ok(())
}
}
2015-09-12 15:31:26 -04:00
impl Serializable for f32 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<f32, Error> {
Result::Ok(buf.read_f32::<BigEndian>()?)
2015-09-12 15:31:26 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_f32::<BigEndian>(*self)?;
2015-09-12 15:31:26 -04:00
Result::Ok(())
}
}
impl Serializable for f64 {
fn read_from<R: io::Read>(buf: &mut R) -> Result<f64, Error> {
Result::Ok(buf.read_f64::<BigEndian>()?)
2015-09-12 15:31:26 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_f64::<BigEndian>(*self)?;
2015-09-12 15:31:26 -04:00
Result::Ok(())
}
}
2016-04-05 13:50:53 -04:00
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
2015-09-12 15:31:26 -04:00
pub struct UUID(u64, u64);
impl UUID {
pub fn from_str(s: &str) -> UUID {
use hex;
// TODO: Panics aren't the best idea here
if s.len() != 36 {
panic!("Invalid UUID format");
}
let mut parts = hex::decode(&s[..8]).unwrap();
parts.extend_from_slice(&hex::decode(&s[9..13]).unwrap());
parts.extend_from_slice(&hex::decode(&s[14..18]).unwrap());
parts.extend_from_slice(&hex::decode(&s[19..23]).unwrap());
parts.extend_from_slice(&hex::decode(&s[24..36]).unwrap());
let mut high = 0u64;
let mut low = 0u64;
for i in 0 .. 8 {
high |= (parts[i] as u64) << (56 - i*8);
low |= (parts[i + 8] as u64) << (56 - i*8);
}
UUID(high, low)
}
}
2015-09-12 15:31:26 -04:00
impl Default for UUID {
2015-10-07 14:36:59 -04:00
fn default() -> Self {
UUID(0, 0)
}
2015-09-12 15:31:26 -04:00
}
impl Serializable for UUID {
fn read_from<R: io::Read>(buf: &mut R) -> Result<UUID, Error> {
Result::Ok(UUID(buf.read_u64::<BigEndian>()?,
buf.read_u64::<BigEndian>()?))
2015-09-12 15:31:26 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
buf.write_u64::<BigEndian>(self.0)?;
buf.write_u64::<BigEndian>(self.1)?;
2015-09-12 15:31:26 -04:00
Result::Ok(())
}
}
pub trait Lengthable : Serializable + Copy + Default {
fn into(self) -> usize;
fn from(_: usize) -> Self;
2015-09-12 15:31:26 -04:00
}
2015-09-10 06:49:41 -04:00
pub struct LenPrefixed<L: Lengthable, V> {
len: L,
2015-10-07 14:36:59 -04:00
pub data: Vec<V>,
2015-09-10 06:49:41 -04:00
}
impl <L: Lengthable, V: Default> LenPrefixed<L, V> {
2016-03-21 06:55:31 -04:00
pub fn new(data: Vec<V>) -> LenPrefixed<L, V> {
2015-09-29 15:09:36 -04:00
LenPrefixed {
2015-09-10 06:49:41 -04:00
len: Default::default(),
data: data,
}
}
}
impl <L: Lengthable, V: Serializable> Serializable for LenPrefixed<L, V> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixed<L, V>, Error> {
let len_data: L = Serializable::read_from(buf)?;
2015-10-07 14:36:59 -04:00
let len: usize = len_data.into();
let mut data: Vec<V> = Vec::with_capacity(len);
for _ in 0..len {
data.push(Serializable::read_from(buf)?);
2015-09-10 06:49:41 -04:00
}
2015-10-07 14:36:59 -04:00
Result::Ok(LenPrefixed {
len: len_data,
data: data,
})
2015-09-10 06:49:41 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-10-07 14:36:59 -04:00
let len_data: L = L::from(self.data.len());
len_data.write_to(buf)?;
2015-09-29 15:09:36 -04:00
let data = &self.data;
2015-09-10 06:49:41 -04:00
for val in data {
val.write_to(buf)?;
2015-09-10 06:49:41 -04:00
}
Result::Ok(())
}
}
2015-09-12 15:31:26 -04:00
2015-09-10 06:49:41 -04:00
impl <L: Lengthable, V: Default> Default for LenPrefixed<L, V> {
fn default() -> Self {
LenPrefixed {
len: default::Default::default(),
2015-10-07 14:36:59 -04:00
data: default::Default::default(),
2015-09-10 06:49:41 -04:00
}
}
}
2015-09-12 15:31:26 -04:00
impl <L: Lengthable, V: fmt::Debug> fmt::Debug for LenPrefixed<L, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.data.fmt(f)
}
}
// Optimization
pub struct LenPrefixedBytes<L: Lengthable> {
len: L,
2015-10-07 14:36:59 -04:00
pub data: Vec<u8>,
2015-09-12 15:31:26 -04:00
}
impl <L: Lengthable> LenPrefixedBytes<L> {
2016-03-21 06:55:31 -04:00
pub fn new(data: Vec<u8>) -> LenPrefixedBytes<L> {
2015-09-29 15:09:36 -04:00
LenPrefixedBytes {
2015-09-12 15:31:26 -04:00
len: Default::default(),
data: data,
}
}
}
impl <L: Lengthable> Serializable for LenPrefixedBytes<L> {
fn read_from<R: io::Read>(buf: &mut R) -> Result<LenPrefixedBytes<L>, Error> {
let len_data: L = Serializable::read_from(buf)?;
2015-10-07 14:36:59 -04:00
let len: usize = len_data.into();
let mut data: Vec<u8> = Vec::with_capacity(len);
buf.take(len as u64).read_to_end(&mut data)?;
2015-10-07 14:36:59 -04:00
Result::Ok(LenPrefixedBytes {
len: len_data,
data: data,
})
2015-09-12 15:31:26 -04:00
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-10-07 14:36:59 -04:00
let len_data: L = L::from(self.data.len());
len_data.write_to(buf)?;
buf.write_all(&self.data[..])?;
2015-09-12 15:31:26 -04:00
Result::Ok(())
}
}
impl <L: Lengthable> Default for LenPrefixedBytes<L> {
fn default() -> Self {
LenPrefixedBytes {
len: default::Default::default(),
2015-10-07 14:36:59 -04:00
data: default::Default::default(),
2015-09-12 15:31:26 -04:00
}
}
}
impl <L: Lengthable> fmt::Debug for LenPrefixedBytes<L> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.data.fmt(f)
}
}
impl Lengthable for i16 {
fn into(self) -> usize {
self as usize
}
fn from(u: usize) -> i16 {
u as i16
}
}
impl Lengthable for i32 {
fn into(self) -> usize {
self as usize
}
fn from(u: usize) -> i32 {
u as i32
}
}
2016-04-02 20:26:31 -04:00
/// `VarInt` have a variable size (between 1 and 5 bytes) when encoded based
2015-09-07 16:11:00 -04:00
/// on the size of the number
2015-09-10 06:49:41 -04:00
#[derive(Clone, Copy)]
2015-09-12 15:31:26 -04:00
pub struct VarInt(pub i32);
2015-09-08 07:57:24 -04:00
2015-09-12 15:31:26 -04:00
impl Lengthable for VarInt {
fn into(self) -> usize {
self.0 as usize
}
fn from(u: usize) -> VarInt {
VarInt(u as i32)
}
}
2015-09-10 06:49:41 -04:00
2015-09-08 07:57:24 -04:00
impl Serializable for VarInt {
2016-04-02 20:26:31 -04:00
/// Decodes a `VarInt` from the Reader
fn read_from<R: io::Read>(buf: &mut R) -> Result<VarInt, Error> {
2015-09-08 07:57:24 -04:00
const PART : u32 = 0x7F;
let mut size = 0;
let mut val = 0u32;
loop {
let b = buf.read_u8()? as u32;
2015-09-08 07:57:24 -04:00
val |= (b & PART) << (size * 7);
2015-10-07 14:36:59 -04:00
size += 1;
2015-09-08 07:57:24 -04:00
if size > 5 {
return Result::Err(Error::Err("VarInt too big".to_owned()));
2015-09-08 07:57:24 -04:00
}
if (b & 0x80) == 0 {
break
}
2015-09-07 16:11:00 -04:00
}
2015-09-08 07:57:24 -04:00
Result::Ok(VarInt(val as i32))
2015-09-07 16:11:00 -04:00
}
2016-04-02 20:26:31 -04:00
/// Encodes a `VarInt` into the Writer
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-08 07:57:24 -04:00
const PART : u32 = 0x7F;
let mut val = self.0 as u32;
loop {
if (val & !PART) == 0 {
buf.write_u8(val as u8)?;
2015-09-08 07:57:24 -04:00
return Result::Ok(());
}
buf.write_u8(((val & PART) | 0x80) as u8)?;
2015-09-08 07:57:24 -04:00
val >>= 7;
2015-09-07 16:11:00 -04:00
}
}
2015-09-08 07:57:24 -04:00
}
2015-09-07 16:11:00 -04:00
2015-09-08 07:57:24 -04:00
impl default::Default for VarInt {
2015-10-07 14:36:59 -04:00
fn default() -> VarInt {
VarInt(0)
}
2015-09-07 16:11:00 -04:00
}
2015-09-12 15:31:26 -04:00
impl fmt::Debug for VarInt {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
2016-04-02 20:26:31 -04:00
/// `VarLong` have a variable size (between 1 and 10 bytes) when encoded based
2015-09-12 15:31:26 -04:00
/// on the size of the number
#[derive(Clone, Copy)]
pub struct VarLong(pub i64);
impl Lengthable for VarLong {
2015-09-10 06:49:41 -04:00
fn into(self) -> usize {
self.0 as usize
}
2015-09-12 15:31:26 -04:00
fn from(u: usize) -> VarLong {
VarLong(u as i64)
}
2015-09-10 06:49:41 -04:00
}
2015-09-12 15:31:26 -04:00
impl Serializable for VarLong {
2016-04-02 20:26:31 -04:00
/// Decodes a `VarLong` from the Reader
fn read_from<R: io::Read>(buf: &mut R) -> Result<VarLong, Error> {
2015-09-12 15:31:26 -04:00
const PART : u64 = 0x7F;
let mut size = 0;
let mut val = 0u64;
loop {
let b = buf.read_u8()? as u64;
2015-09-12 15:31:26 -04:00
val |= (b & PART) << (size * 7);
2015-10-07 14:36:59 -04:00
size += 1;
2015-09-12 15:31:26 -04:00
if size > 10 {
return Result::Err(Error::Err("VarLong too big".to_owned()));
2015-09-12 15:31:26 -04:00
}
if (b & 0x80) == 0 {
break
}
}
Result::Ok(VarLong(val as i64))
}
2016-04-02 20:26:31 -04:00
/// Encodes a `VarLong` into the Writer
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
2015-09-12 15:31:26 -04:00
const PART : u64 = 0x7F;
let mut val = self.0 as u64;
loop {
if (val & !PART) == 0 {
buf.write_u8(val as u8)?;
2015-09-12 15:31:26 -04:00
return Result::Ok(());
}
buf.write_u8(((val & PART) | 0x80) as u8)?;
2015-09-12 15:31:26 -04:00
val >>= 7;
}
}
}
impl default::Default for VarLong {
2015-10-07 14:36:59 -04:00
fn default() -> VarLong {
VarLong(0)
}
2015-09-12 15:31:26 -04:00
}
impl fmt::Debug for VarLong {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
2015-09-10 06:49:41 -04:00
}
}
impl Serializable for Position {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Position, Error> {
let pos = buf.read_u64::<BigEndian>()?;
Ok(Position::new(
((pos as i64) >> 38) as i32,
(((pos as i64) >> 26) & 0xFFF) as i32,
((pos as i64) << 38 >> 38) as i32
))
}
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {
let pos = (((self.x as u64) & 0x3FFFFFF) << 38)
| (((self.y as u64) & 0xFFF) << 26)
| ((self.z as u64) & 0x3FFFFFF);
buf.write_u64::<BigEndian>(pos)?;
Result::Ok(())
}
}
2015-09-07 16:11:00 -04:00
/// Direction is used to define whether packets are going to the
/// server or the client.
2015-09-10 06:49:41 -04:00
#[derive(Clone, Copy)]
2015-09-07 16:11:00 -04:00
pub enum Direction {
Serverbound,
2015-10-07 14:36:59 -04:00
Clientbound,
2015-09-07 16:11:00 -04:00
}
/// The protocol has multiple 'sub-protocols' or states which control which
/// packet an id points to.
#[derive(Clone, Copy)]
pub enum State {
Handshaking,
Play,
Status,
2015-10-07 14:36:59 -04:00
Login,
2015-09-07 16:11:00 -04:00
}
/// Return for any protocol related error.
#[derive(Debug)]
pub enum Error {
Err(String),
2016-03-20 08:04:02 -04:00
Disconnect(format::Component),
2015-09-07 16:11:00 -04:00
IOError(io::Error),
Json(serde_json::Error),
Replace hyper with reqwest (#7) An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well * Begin updating to hyper 0.12.11 https://github.com/iceiix/steven/issues/4#issuecomment-425759778 * Use type variables for hyper::Client * Fix setting header syntax, Content-Type: application/json, 17->13 * Parse strings into URLs with url.parse::<hyper::Uri>().unwrap() https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25 * Use hyper::Request::post() then client.request() since client.post() removed * wait() on the ResponseFuture to get the Result * try! to unwrap the Result * status() is now a method * Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile * Replace send() with wait() on ResponseFuture * Parse HeaderValue to u64 * Slices implement std::io::Read trait * Read into_bytes() instead of read_to_end() * Disable boxed logger for now to workaround 'expected function, found macro' * Remove unnecessary mutability, warnings * Hack to parse twice to avoid double move * Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper * Start converting to reqwest: add Protocol::Error and reqwest::Error conversion * Use reqwest, replacing hyper, in protocol * Convert resources to use reqwest instead of hyper * Convert skin download to reqwest, instead of hyper * Remove hyper * Revert unnecessary variable name change req/body to reduce diff * Revert unnecessary whitespace change to reduce diff, align indentation on . * Fix authenticating to server, wrong method and join URL * Update Cargo.lock
2018-10-27 20:03:34 -04:00
Reqwest(reqwest::Error),
2015-09-07 16:11:00 -04:00
}
impl convert::From<io::Error> for Error {
2015-10-07 14:36:59 -04:00
fn from(e: io::Error) -> Error {
2015-09-07 16:11:00 -04:00
Error::IOError(e)
}
}
impl convert::From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Error {
Error::Json(e)
}
}
Replace hyper with reqwest (#7) An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well * Begin updating to hyper 0.12.11 https://github.com/iceiix/steven/issues/4#issuecomment-425759778 * Use type variables for hyper::Client * Fix setting header syntax, Content-Type: application/json, 17->13 * Parse strings into URLs with url.parse::<hyper::Uri>().unwrap() https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25 * Use hyper::Request::post() then client.request() since client.post() removed * wait() on the ResponseFuture to get the Result * try! to unwrap the Result * status() is now a method * Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile * Replace send() with wait() on ResponseFuture * Parse HeaderValue to u64 * Slices implement std::io::Read trait * Read into_bytes() instead of read_to_end() * Disable boxed logger for now to workaround 'expected function, found macro' * Remove unnecessary mutability, warnings * Hack to parse twice to avoid double move * Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper * Start converting to reqwest: add Protocol::Error and reqwest::Error conversion * Use reqwest, replacing hyper, in protocol * Convert resources to use reqwest instead of hyper * Convert skin download to reqwest, instead of hyper * Remove hyper * Revert unnecessary variable name change req/body to reduce diff * Revert unnecessary whitespace change to reduce diff, align indentation on . * Fix authenticating to server, wrong method and join URL * Update Cargo.lock
2018-10-27 20:03:34 -04:00
impl convert::From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Error {
Error::Reqwest(e)
}
}
2015-09-07 16:11:00 -04:00
impl ::std::error::Error for Error {
fn description(&self) -> &str {
2015-09-29 15:09:36 -04:00
match *self {
Error::Err(ref val) => &val[..],
2016-03-20 08:04:02 -04:00
Error::Disconnect(_) => "Disconnect",
2015-09-29 15:09:36 -04:00
Error::IOError(ref e) => e.description(),
Error::Json(ref e) => e.description(),
Replace hyper with reqwest (#7) An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well * Begin updating to hyper 0.12.11 https://github.com/iceiix/steven/issues/4#issuecomment-425759778 * Use type variables for hyper::Client * Fix setting header syntax, Content-Type: application/json, 17->13 * Parse strings into URLs with url.parse::<hyper::Uri>().unwrap() https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25 * Use hyper::Request::post() then client.request() since client.post() removed * wait() on the ResponseFuture to get the Result * try! to unwrap the Result * status() is now a method * Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile * Replace send() with wait() on ResponseFuture * Parse HeaderValue to u64 * Slices implement std::io::Read trait * Read into_bytes() instead of read_to_end() * Disable boxed logger for now to workaround 'expected function, found macro' * Remove unnecessary mutability, warnings * Hack to parse twice to avoid double move * Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper * Start converting to reqwest: add Protocol::Error and reqwest::Error conversion * Use reqwest, replacing hyper, in protocol * Convert resources to use reqwest instead of hyper * Convert skin download to reqwest, instead of hyper * Remove hyper * Revert unnecessary variable name change req/body to reduce diff * Revert unnecessary whitespace change to reduce diff, align indentation on . * Fix authenticating to server, wrong method and join URL * Update Cargo.lock
2018-10-27 20:03:34 -04:00
Error::Reqwest(ref e) => e.description(),
2015-09-07 16:11:00 -04:00
}
}
}
impl ::std::fmt::Display for Error {
2015-10-07 14:36:59 -04:00
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
2015-09-29 15:09:36 -04:00
match *self {
Error::Err(ref val) => write!(f, "protocol error: {}", val),
2016-03-20 08:04:02 -04:00
Error::Disconnect(ref val) => write!(f, "{}", val),
2015-10-07 14:36:59 -04:00
Error::IOError(ref e) => e.fmt(f),
Error::Json(ref e) => e.fmt(f),
Replace hyper with reqwest (#7) An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well * Begin updating to hyper 0.12.11 https://github.com/iceiix/steven/issues/4#issuecomment-425759778 * Use type variables for hyper::Client * Fix setting header syntax, Content-Type: application/json, 17->13 * Parse strings into URLs with url.parse::<hyper::Uri>().unwrap() https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25 * Use hyper::Request::post() then client.request() since client.post() removed * wait() on the ResponseFuture to get the Result * try! to unwrap the Result * status() is now a method * Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile * Replace send() with wait() on ResponseFuture * Parse HeaderValue to u64 * Slices implement std::io::Read trait * Read into_bytes() instead of read_to_end() * Disable boxed logger for now to workaround 'expected function, found macro' * Remove unnecessary mutability, warnings * Hack to parse twice to avoid double move * Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper * Start converting to reqwest: add Protocol::Error and reqwest::Error conversion * Use reqwest, replacing hyper, in protocol * Convert resources to use reqwest instead of hyper * Convert skin download to reqwest, instead of hyper * Remove hyper * Revert unnecessary variable name change req/body to reduce diff * Revert unnecessary whitespace change to reduce diff, align indentation on . * Fix authenticating to server, wrong method and join URL * Update Cargo.lock
2018-10-27 20:03:34 -04:00
Error::Reqwest(ref e) => e.fmt(f),
2015-09-07 16:11:00 -04:00
}
}
}
type Aes128Cfb = Cfb8<Aes128>;
2015-09-07 16:11:00 -04:00
pub struct Conn {
stream: TcpStream,
2016-03-20 08:04:02 -04:00
pub host: String,
pub port: u16,
2015-09-07 16:11:00 -04:00
direction: Direction,
2016-03-20 08:04:02 -04:00
pub state: State,
2015-09-10 06:49:41 -04:00
cipher: Option<Aes128Cfb>,
2015-09-10 06:49:41 -04:00
compression_threshold: i32,
2015-10-07 14:36:59 -04:00
compression_read: Option<ZlibDecoder<io::Cursor<Vec<u8>>>>,
2015-09-10 06:49:41 -04:00
compression_write: Option<ZlibEncoder<io::Cursor<Vec<u8>>>>,
2015-09-07 16:11:00 -04:00
}
impl Conn {
2015-10-07 14:36:59 -04:00
pub fn new(target: &str) -> Result<Conn, Error> {
2015-09-07 16:11:00 -04:00
// TODO SRV record support
2016-04-02 20:26:31 -04:00
let mut parts = target.split(':').collect::<Vec<&str>>();
2015-09-25 09:00:49 -04:00
let address = if parts.len() == 1 {
parts.push("25565");
format!("{}:25565", parts[0])
} else {
format!("{}:{}", parts[0], parts[1])
2015-09-07 16:11:00 -04:00
};
let stream = TcpStream::connect(&*address)?;
2015-09-07 16:11:00 -04:00
Result::Ok(Conn {
stream: stream,
host: parts[0].to_owned(),
port: parts[1].parse().unwrap(),
direction: Direction::Serverbound,
state: State::Handshaking,
2015-09-10 06:49:41 -04:00
cipher: Option::None,
compression_threshold: -1,
compression_read: Option::None,
compression_write: Option::None,
2015-09-07 16:11:00 -04:00
})
}
pub fn write_packet<T: PacketType>(&mut self, packet: T) -> Result<(), Error> {
let mut buf = Vec::new();
VarInt(packet.packet_id()).write_to(&mut buf)?;
packet.write(&mut buf)?;
2015-09-10 06:49:41 -04:00
2015-10-07 14:36:59 -04:00
let mut extra = if self.compression_threshold >= 0 {
1
} else {
0
};
2015-09-10 06:49:41 -04:00
if self.compression_threshold >= 0 && buf.len() as i32 > self.compression_threshold {
if self.compression_write.is_none() {
self.compression_write = Some(ZlibEncoder::new(io::Cursor::new(Vec::new()), Compression::default()));
}
2015-09-10 06:49:41 -04:00
extra = 0;
let uncompressed_size = buf.len();
let mut new = Vec::new();
VarInt(uncompressed_size as i32).write_to(&mut new)?;
let write = self.compression_write.as_mut().unwrap();
2015-09-10 06:49:41 -04:00
write.reset(io::Cursor::new(buf));
write.read_to_end(&mut new)?;
2015-09-10 06:49:41 -04:00
buf = new;
}
VarInt(buf.len() as i32 + extra).write_to(self)?;
2015-09-10 06:49:41 -04:00
if self.compression_threshold >= 0 && extra == 1 {
VarInt(0).write_to(self)?;
2015-09-10 06:49:41 -04:00
}
self.write_all(&buf)?;
2015-09-07 16:11:00 -04:00
Result::Ok(())
}
pub fn read_packet(&mut self) -> Result<packet::Packet, Error> {
let len = VarInt::read_from(self)?.0 as usize;
let mut ibuf = vec![0; len];
self.read_exact(&mut ibuf)?;
2015-09-07 16:11:00 -04:00
let mut buf = io::Cursor::new(ibuf);
2015-09-10 06:49:41 -04:00
if self.compression_threshold >= 0 {
if self.compression_read.is_none() {
self.compression_read = Some(ZlibDecoder::new(io::Cursor::new(Vec::new())));
}
let uncompressed_size = VarInt::read_from(&mut buf)?.0;
2015-09-10 06:49:41 -04:00
if uncompressed_size != 0 {
let mut new = Vec::with_capacity(uncompressed_size as usize);
{
let reader = self.compression_read.as_mut().unwrap();
2015-09-10 06:49:41 -04:00
reader.reset(buf);
reader.read_to_end(&mut new)?;
2015-09-10 06:49:41 -04:00
}
buf = io::Cursor::new(new);
}
}
let id = VarInt::read_from(&mut buf)?.0;
2015-09-07 16:11:00 -04:00
let dir = match self.direction {
Direction::Clientbound => Direction::Serverbound,
Direction::Serverbound => Direction::Clientbound,
};
let packet = packet::packet_by_id(self.state, dir, id, &mut buf)?;
2015-09-07 16:11:00 -04:00
match packet {
Some(val) => {
let pos = buf.position() as usize;
let ibuf = buf.into_inner();
2015-09-12 15:31:26 -04:00
if ibuf.len() != pos {
2015-10-07 14:36:59 -04:00
return Result::Err(Error::Err(format!("Failed to read all of packet 0x{:X}, \
had {} bytes left",
id,
ibuf.len() - pos)))
2015-09-07 16:11:00 -04:00
}
Result::Ok(val)
2015-10-07 14:36:59 -04:00
}
None => Result::Err(Error::Err("missing packet".to_owned())),
2015-09-10 06:49:41 -04:00
}
}
pub fn enable_encyption(&mut self, key: &[u8], _decrypt: bool) {
let cipher = Aes128Cfb::new_var(key, key).unwrap();
self.cipher = Option::Some(cipher);
2015-09-10 06:49:41 -04:00
}
pub fn set_compresssion(&mut self, threshold: i32) {
2015-09-10 06:49:41 -04:00
self.compression_threshold = threshold;
}
2015-09-25 09:00:49 -04:00
pub fn do_status(mut self) -> Result<(Status, Duration), Error> {
2015-09-25 09:00:49 -04:00
use serde_json::Value;
use self::packet::status::serverbound::*;
use self::packet::handshake::serverbound::Handshake;
2015-09-25 09:00:49 -04:00
use self::packet::Packet;
let host = self.host.clone();
let port = self.port;
self.write_packet(Handshake {
2015-09-25 09:00:49 -04:00
protocol_version: VarInt(SUPPORTED_PROTOCOL),
host: host,
port: port,
next: VarInt(1),
})?;
2015-09-25 09:00:49 -04:00
self.state = State::Status;
2015-10-07 14:36:59 -04:00
self.write_packet(StatusRequest { empty: () })?;
2015-09-25 09:00:49 -04:00
let status = if let Packet::StatusResponse(res) = self.read_packet()? {
2015-09-25 09:00:49 -04:00
res.status
} else {
return Err(Error::Err("Wrong packet".to_owned()));
};
let start = Instant::now();
self.write_packet(StatusPing { ping: 42 })?;
2015-09-25 09:00:49 -04:00
if let Packet::StatusPong(_) = self.read_packet()? {
2015-09-25 09:00:49 -04:00
} else {
return Err(Error::Err("Wrong packet".to_owned()));
};
let ping = start.elapsed();
2015-09-25 09:00:49 -04:00
let val: Value = match serde_json::from_str(&status) {
Ok(val) => val,
Err(_) => return Err(Error::Err("Json parse error".to_owned())),
};
let invalid_status = || Error::Err("Invalid status".to_owned());
let version = val.get("version").ok_or(invalid_status())?;
let players = val.get("players").ok_or(invalid_status())?;
2015-09-25 09:00:49 -04:00
Ok((Status {
version: StatusVersion {
name: version.get("name").and_then(Value::as_str).ok_or(invalid_status())?
2015-10-07 14:36:59 -04:00
.to_owned(),
protocol: version.get("protocol")
2015-10-07 14:36:59 -04:00
.and_then(Value::as_i64)
.ok_or(invalid_status())? as i32,
2015-09-25 09:00:49 -04:00
},
players: StatusPlayers {
max: players.get("max")
2015-10-07 14:36:59 -04:00
.and_then(Value::as_i64)
.ok_or(invalid_status())? as i32,
online: players.get("online")
2015-10-07 14:36:59 -04:00
.and_then(Value::as_i64)
.ok_or(invalid_status())? as i32,
2015-10-07 14:36:59 -04:00
sample: Vec::new(), /* TODO */
2015-09-25 09:00:49 -04:00
},
description: format::Component::from_value(val.get("description")
.ok_or(invalid_status())?),
favicon: val.get("favicon").and_then(Value::as_str).map(|v| v.to_owned()),
2015-10-07 14:36:59 -04:00
},
ping))
2015-09-25 09:00:49 -04:00
}
}
#[derive(Debug)]
pub struct Status {
pub version: StatusVersion,
pub players: StatusPlayers,
pub description: format::Component,
pub favicon: Option<String>,
}
#[derive(Debug)]
pub struct StatusVersion {
pub name: String,
pub protocol: i32,
}
#[derive(Debug)]
pub struct StatusPlayers {
pub max: i32,
pub online: i32,
pub sample: Vec<StatusPlayer>,
}
#[derive(Debug)]
pub struct StatusPlayer {
name: String,
id: String,
2015-09-10 06:49:41 -04:00
}
impl Read for Conn {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.cipher.as_mut() {
Option::None => self.stream.read(buf),
Option::Some(cipher) => {
let ret = self.stream.read(buf)?;
cipher.decrypt(&mut buf[..ret]);
2015-09-10 06:49:41 -04:00
Ok(ret)
2015-10-07 14:36:59 -04:00
}
2015-09-10 06:49:41 -04:00
}
}
}
impl Write for Conn {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.cipher.as_mut() {
Option::None => self.stream.write(buf),
Option::Some(cipher) => {
// TODO: avoid copying, but trait requires non-mutable buf
let mut data = vec![0; buf.len()];
for i in 0..buf.len() {
data[i] = buf[i];
}
cipher.encrypt(&mut data);
self.stream.write_all(&data)?;
2015-09-10 06:49:41 -04:00
Ok(buf.len())
2015-10-07 14:36:59 -04:00
}
2015-09-10 06:49:41 -04:00
}
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}
impl Clone for Conn {
fn clone(&self) -> Self {
Conn {
stream: self.stream.try_clone().unwrap(),
host: self.host.clone(),
port: self.port,
direction: self.direction,
state: self.state,
cipher: Option::None,
compression_threshold: self.compression_threshold,
compression_read: Option::None,
compression_write: Option::None,
2015-09-07 16:11:00 -04:00
}
}
}
pub trait PacketType {
2015-09-07 16:11:00 -04:00
fn packet_id(&self) -> i32;
fn write<W: io::Write>(self, buf: &mut W) -> Result<(), Error>;
2015-09-07 16:11:00 -04:00
}