completely refactor codebase

+two-words domain generation algorithm
main
Skye 2 years ago
parent f46577c355
commit 6cefc1a7c2
Signed by: me
GPG Key ID: 0104BC05F41B77B8

24
Cargo.lock generated

@ -257,17 +257,26 @@ dependencies = [
"async-trait", "async-trait",
"async-tungstenite", "async-tungstenite",
"clap", "clap",
"e4mc-common",
"env_logger", "env_logger",
"futures", "futures",
"futures-util", "futures-util",
"lazy_static", "lazy_static",
"log", "log",
"serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
] ]
[[package]]
name = "e4mc-common"
version = "0.1.0"
dependencies = [
"async-tungstenite",
"serde",
"serde_json",
]
[[package]] [[package]]
name = "e4mc-server" name = "e4mc-server"
version = "0.1.0" version = "0.1.0"
@ -275,12 +284,12 @@ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
"async-tungstenite", "async-tungstenite",
"e4mc-common",
"env_logger", "env_logger",
"futures", "futures",
"lazy_static", "lazy_static",
"log", "log",
"nanoid", "rand",
"serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
"tokio", "tokio",
@ -582,15 +591,6 @@ dependencies = [
"windows-sys 0.45.0", "windows-sys 0.45.0",
] ]
[[package]]
name = "nanoid"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ffa00dec017b5b1a8b7cf5e2c008bfda1aa7e0697ac1508b491fdf2622fb4d8"
dependencies = [
"rand",
]
[[package]] [[package]]
name = "num_cpus" name = "num_cpus"
version = "1.15.0" version = "1.15.0"

@ -3,4 +3,5 @@
members = [ members = [
"server", "server",
"client", "client",
"common"
] ]

@ -17,7 +17,7 @@ futures = "0.3.28"
futures-util = { version = "0.3.28", features = ["io"] } futures-util = { version = "0.3.28", features = ["io"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.17" log = "0.4.17"
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95" serde_json = "1.0.95"
thiserror = "1.0.40" thiserror = "1.0.40"
tokio = { version = "1.27.0", features = ["rt-multi-thread", "sync", "macros", "net", "io-util"] } tokio = { version = "1.27.0", features = ["rt-multi-thread", "sync", "macros", "net", "io-util"] }
e4mc-common = { path = "../common"}

@ -1,186 +1,162 @@
use std::{collections::HashMap, net::SocketAddr}; use std::collections::HashMap;
use anyhow::anyhow;
use async_tungstenite::tokio::connect_async;
use async_tungstenite::tungstenite::Message; use async_tungstenite::tungstenite::Message;
use async_tungstenite::{tokio::connect_async, WebSocketStream};
use clap::Parser;
use e4mc_common::ClientboundControlMessage;
use futures::stream::{SplitSink, SplitStream};
use futures::{AsyncRead, AsyncWrite};
use futures_util::{SinkExt, StreamExt}; use futures_util::{SinkExt, StreamExt};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{error, info, trace}; use log::{error, info, trace, warn};
use serde::{Deserialize, Serialize}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use tokio::net::tcp::{WriteHalf, ReadHalf};
use tokio::{ use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{
tcp::{OwnedReadHalf, OwnedWriteHalf},
TcpStream,
},
sync::mpsc::{UnboundedReceiver, UnboundedSender}, sync::mpsc::{UnboundedReceiver, UnboundedSender},
sync::RwLock, sync::RwLock,
task,
}; };
enum ChannelHandlerMessage {
Data(Vec<u8>),
Shutdown,
}
lazy_static! { lazy_static! {
static ref CHANNEL_MAP: RwLock<HashMap<u8, UnboundedSender<ChannelHandlerMessage>>> = static ref CHANNEL_MAP: RwLock<HashMap<u8, UnboundedSender<ChannelTaskMessage>>> =
RwLock::new(HashMap::new()); RwLock::new(HashMap::new());
} }
/// Simple program to greet a person
#[derive(Parser, Debug)]
struct Args {
/// Port of Minecraft server
#[arg(short, long, default_value_t = 25565)]
port: u16,
/// Address of e4mc-server instance
#[arg(short, long)]
server: Option<String>,
}
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let _ = env_logger::try_init(); let _ = env_logger::try_init();
let args = Args::parse(); let args = Args::parse();
let server = args.server.unwrap_or("wss://e4mc.skyevg.systems".to_string()); let server = args
.server
.unwrap_or("wss://e4mc.skyevg.systems".to_string());
let (ws_conn, _) = connect_async(server).await?; let (ws_conn, _) = connect_async(server).await?;
let (mut send, mut recv) = ws_conn.split(); let (send, recv) = ws_conn.split();
let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let sender = &*Box::leak(Box::new(sender)); // the task::spawn below requires 'static and this is main tokio::select! {
tokio::try_join!( res = handle_send(send, receiver) => {
async move { if let Err(e) = res {
while let Some(Ok(message)) = recv.next().await { error!("Error in send loop: {}", e);
match message {
Message::Text(message) => {
info!("{}", message);
let message: ClientboundControlMessage = serde_json::from_str(&message)?;
match message {
ClientboundControlMessage::DomainAssigned(domain) => {
println!("Domain assigned: {}", domain);
}
ClientboundControlMessage::ChannelOpen(id, _) => {
match TcpStream::connect(("127.0.0.1", args.port)).await {
Ok(conn) => {
let (send, recv) = tokio::sync::mpsc::unbounded_channel();
CHANNEL_MAP.write().await.insert(id, send);
task::spawn(async move {
if let Err(e) =
handle_channel(id, sender.clone(), conn, recv).await
{
error!("Error handling channel: {}", e);
}
CHANNEL_MAP.write().await.remove(&id);
info!("Sending close request for channel: {}", id);
sender
.send(Message::Text(
serde_json::to_string(
&ServerboundControlMessage::ChannelClosed(
id,
),
)
.unwrap(),
))
.unwrap();
});
}
Err(e) => {
error!("Error creating channel: {}", e);
sender
.send(Message::Text(
serde_json::to_string(
&ServerboundControlMessage::ChannelClosed(
id,
),
)
.unwrap(),
))
.unwrap();
}
};
}
ClientboundControlMessage::ChannelClosed(id) => {
let mut channel_map = CHANNEL_MAP.write().await;
if let Some(send) = channel_map.remove(&id) {
if let Err(e) = send.send(ChannelHandlerMessage::Shutdown) {
error!("Error closing channel: {}", e);
}
}
}
}
}
Message::Binary(buf) => {
trace!("recv: {:?}", buf);
if let Some(send) = CHANNEL_MAP.write().await.get_mut(&buf[0]) {
if let Err(e) = send.send(ChannelHandlerMessage::Data(buf[1..].to_vec())) {
error!("Error transferring data: {}", e);
}
}
}
_ => {}
}
} }
Ok(()) as anyhow::Result<()>
}, },
async move { res = handle_recv(recv, sender, args.port) => {
while let Some(message) = receiver.recv().await { if let Err(e) = res {
send.send(message.clone()).await?; error!("Error in recv loop: {}", e);
} }
Ok(()) as anyhow::Result<()>
} }
)?; }
Ok(()) Ok(())
} }
async fn handle_channel( async fn handle_send<T: AsyncRead + AsyncWrite + Unpin>(
id: u8, mut send: SplitSink<WebSocketStream<T>, Message>,
sender: UnboundedSender<Message>, mut receiver: UnboundedReceiver<Message>,
mut conn: TcpStream,
mut recv: UnboundedReceiver<ChannelHandlerMessage>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let (mut read, mut write) = conn.split(); while let Some(message) = receiver.recv().await {
tokio::select!(_ = async move { send.send(message).await?;
let mut buf = [0u8; 1024]; }
loop {
let ready = read.ready(tokio::io::Interest::READABLE).await?;
if ready.is_read_closed() {
return Ok(())
}
let len = read.read(&mut buf).await?;
if len == 0 {
continue;
}
let mut packet = Vec::from(&buf[..len]);
packet.insert(0, id);
trace!("send: {:?}", packet);
sender.send(Message::Binary(packet))?;
}
Ok(()) as anyhow::Result<()>
} => {},
_ = async move {
while let Some(message) = recv.recv().await {
match message {
ChannelHandlerMessage::Data(buf) => write.write_all(&buf).await?,
ChannelHandlerMessage::Shutdown => return Ok(()),
}
}
Ok(()) as anyhow::Result<()>
} => {});
conn.shutdown().await?;
Ok(()) Ok(())
} }
use clap::Parser; #[derive(Debug, Clone, PartialEq, Eq)]
enum ChannelTaskMessage {
Close,
Data(Vec<u8>),
}
/// Simple program to greet a person async fn handle_recv<T: AsyncRead + AsyncWrite + Unpin>(
#[derive(Parser, Debug)] mut recv: SplitStream<WebSocketStream<T>>,
struct Args { sender: UnboundedSender<Message>,
/// Port of Minecraft server
#[arg(short, long, default_value_t = 25565)]
port: u16, port: u16,
) -> anyhow::Result<()> {
while let Some(message) = recv.next().await {
let message = message?;
match message {
Message::Text(message) => {
info!("{}", message);
let message: ClientboundControlMessage = serde_json::from_str(&message)?;
match message {
ClientboundControlMessage::DomainAssigned(domain) => {
println!("Domain assigned: {}", domain);
}
ClientboundControlMessage::ChannelOpen(channel, _) => {
let conn = TcpStream::connect(("127.0.0.1", port)).await?;
let (send, recv) = tokio::sync::mpsc::unbounded_channel();
CHANNEL_MAP.write().await.insert(channel, send);
tokio::spawn(handle_channel(conn, sender.clone(), channel, recv));
}
ClientboundControlMessage::ChannelClosed(channel) => {
if let Some(sender) = CHANNEL_MAP.write().await.remove(&channel) {
sender.send(ChannelTaskMessage::Close)?;
}
}
}
}
Message::Binary(buf) => {
let channel = buf[0];
if let Some(sender) = CHANNEL_MAP.read().await.get(&channel) {
sender.send(ChannelTaskMessage::Data(buf[1..].to_vec()))?;
} else {
warn!("Message sent to unknown Channel");
}
},
_ => continue,
}
}
Ok(())
}
/// Address of e4mc-server instance async fn handle_channel(mut stream: TcpStream, sender: UnboundedSender<Message>, channel: u8, recv: UnboundedReceiver<ChannelTaskMessage>) {
#[arg(short, long)] let (read, write) = stream.split();
server: Option<String>, tokio::select! {
res = handle_channel_send(write, recv) => {
if let Err(e) = res {
error!("Error in send loop: {}", e);
}
},
res = handle_channel_recv(read, sender, channel) => {
if let Err(e) = res {
error!("Error in recv loop: {}", e);
}
}
}
CHANNEL_MAP.write().await.remove(&channel);
} }
#[derive(Deserialize)] async fn handle_channel_send(mut write: WriteHalf<'_>, mut recv: UnboundedReceiver<ChannelTaskMessage>) -> anyhow::Result<()> {
enum ClientboundControlMessage { while let Some(message) = recv.recv().await {
DomainAssigned(String), match message {
ChannelOpen(u8, SocketAddr), ChannelTaskMessage::Close => return Ok(()),
ChannelClosed(u8), ChannelTaskMessage::Data(buf) => write.write_all(&buf).await?,
}
}
Ok(())
} }
#[derive(Serialize)] async fn handle_channel_recv(mut read: ReadHalf<'_>, sender: UnboundedSender<Message>, channel: u8) -> anyhow::Result<()> {
enum ServerboundControlMessage { let mut buf = [0u8; 1024];
ChannelClosed(u8), loop {
let ready = read.ready(tokio::io::Interest::READABLE).await?;
if ready.is_read_closed() {
return Ok(());
}
let len = read.read(&mut buf).await?;
if len == 0 {
continue;
}
let mut packet = Vec::from(&buf[..len]);
packet.insert(0, channel);
trace!("send: {:?}", packet);
sender.send(Message::Binary(packet))?;
}
} }

@ -1,233 +0,0 @@
use async_std::io::{ReadExt, WriteExt};
use async_trait::async_trait;
use log::{info, error};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum NettyReadError {
#[error("{0}")]
IoError(std::io::Error),
#[error("Was not a netty packet, but a Legacy ServerListPing")]
LegacyServerListPing,
}
impl From<std::io::Error> for NettyReadError {
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}
impl From<std::io::ErrorKind> for NettyReadError {
fn from(value: std::io::ErrorKind) -> Self {
Self::IoError(value.into())
}
}
#[async_trait]
pub trait ReadExtNetty: ReadExt + Unpin {
async fn read_bool(&mut self) -> Result<bool, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
match buf[0] {
0 => Ok(false),
1 => Ok(true),
_ => Err(std::io::ErrorKind::InvalidData.into()),
}
}
async fn read_byte(&mut self) -> Result<i8, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
Ok(i8::from_be_bytes(buf))
}
async fn read_unsigned_byte(&mut self) -> Result<u8, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
Ok(buf[0])
}
async fn read_short(&mut self) -> Result<i16, NettyReadError> {
let mut buf = [0u8; 2];
self.read_exact(&mut buf).await?;
Ok(i16::from_be_bytes(buf))
}
async fn read_unsigned_short(&mut self) -> Result<u16, NettyReadError> {
let mut buf = [0u8; 2];
self.read_exact(&mut buf).await?;
Ok(u16::from_be_bytes(buf))
}
async fn read_int(&mut self) -> Result<i32, NettyReadError> {
let mut buf = [0u8; 4];
self.read_exact(&mut buf).await?;
Ok(i32::from_be_bytes(buf))
}
async fn read_long(&mut self) -> Result<i64, NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
Ok(i64::from_be_bytes(buf))
}
async fn read_float(&mut self) -> Result<f32, NettyReadError> {
let mut buf = [0u8; 4];
self.read_exact(&mut buf).await?;
Ok(f32::from_be_bytes(buf))
}
async fn read_double(&mut self) -> Result<f64, NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
Ok(f64::from_be_bytes(buf))
}
async fn read_string(&mut self) -> Result<String, NettyReadError> {
let len = self.read_varint().await?;
let mut buf = vec![0u8; len as usize];
self.read_exact(&mut buf).await?;
String::from_utf8(buf).map_err(|_| std::io::ErrorKind::InvalidData.into())
}
async fn read_varint(&mut self) -> Result<i32, NettyReadError> {
let mut res = 0i32;
for i in 0..5 {
let part = self.read_unsigned_byte().await?;
res |= (part as i32 & 0x7F) << (7 * i);
if part & 0x80 == 0 {
return Ok(res);
}
}
error!("Varint is invalid");
Err(std::io::ErrorKind::InvalidData.into())
}
// async fn read_varint(&mut self) -> Result<i32, NettyReadError> {
// let mut value = 0i32;
// let mut buf = [0u8; 1];
// let mut pos = 0u8;
// loop {
// self.read_exact(&mut buf).await?;
// println!("{}", buf[0]);
// value |= ((buf[0] & 0b01111111) << pos) as i32;
// if (buf[0] & 0b10000000) == 0 {
// break;
// };
// pos += 7;
// if pos >= 32 {
// return Err(std::io::ErrorKind::InvalidData.into());
// };
// }
// Ok(value)
// }
async fn read_varlong(&mut self) -> Result<i64, NettyReadError> {
let mut value = 0i64;
let mut buf = [0u8; 1];
let mut position = 0u8;
loop {
self.read_exact(&mut buf).await?;
value |= ((buf[0] & 0b01111111) << position) as i64;
if (buf[0] & 0b10000000) == 0 {
break;
};
position += 7;
if position >= 64 {
return Err(std::io::ErrorKind::InvalidData.into());
};
}
Ok(value)
}
async fn read_vec3(&mut self) -> Result<(i32, i16, i32), NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
let packed = u64::from_be_bytes(buf);
let x: i32 = ((packed & 0xffffffc000000000) >> 38) as _;
let y: i16 = (packed & 0xfff) as _;
let z: i32 = ((packed & 0x3ffffff000) >> 12) as _;
Ok((
match x {
i32::MIN..=0x1ffffff => x,
0x2000000.. => x - 0x2000000,
},
match y {
i16::MIN..=0x7ff => y,
0x800.. => y - 0x800,
},
match z {
i32::MIN..=0x1ffffff => z,
0x2000000.. => z - 0x2000000,
},
))
}
async fn read_uuid(&mut self) -> Result<u128, NettyReadError> {
let mut buf = [0u8; 16];
self.read_exact(&mut buf).await?;
Ok(u128::from_be_bytes(buf))
}
async fn read_packet(&mut self) -> Result<Vec<u8>, NettyReadError> {
let len = self.read_varint().await?;
let mut buf = vec![0u8; len as usize];
if len == 254 {
let mut temp = [0u8];
self.read_exact(&mut temp).await?;
if temp[0] == 0xFA {
// FE 01 FA: Legacy ServerListPing
return Err(NettyReadError::LegacyServerListPing);
}
buf[0] = temp[0];
self.read_exact(&mut buf[1..]).await?;
} else {
self.read_exact(&mut buf).await?;
}
Ok(buf)
}
// fn read_packet_compressed(&mut self) -> Result<Vec<u8>, NettyReadError> {
// let len = self.read_varint()?;
// let len_decompressed = self.read_varint()?;
// let mut buf = vec![0u8; len as usize];
// self.read_exact(&mut buf)?;
// if len_decompressed == 0 {
// return Ok(buf);
// }
// let mut buf_decompressed = vec![0u8; len_decompressed as usize];
// if flate2::Decompress::new(true)
// .decompress(&buf, &mut buf_decompressed, flate2::FlushDecompress::Finish)
// .is_err()
// {
// return Err(std::io::ErrorKind::InvalidData.into());
// };
// Ok(buf_decompressed)
// }
}
impl<T: ReadExt + Unpin> ReadExtNetty for T {}
#[async_trait]
pub trait WriteExtNetty: WriteExt + Unpin {
async fn write_varint(&mut self, mut val: i32) -> std::io::Result<()> {
for _ in 0..5 {
if val & !0x7F == 0 {
self.write_all(&[val as u8]).await?;
return Ok(());
}
self.write_all(&[(val & 0x7F | 0x80) as u8]).await?;
val >>= 7;
}
Err(std::io::ErrorKind::InvalidData.into())
}
async fn write_string(&mut self, s: &str) -> std::io::Result<()> {
self.write_varint(s.len() as i32).await?;
self.write_all(s.as_bytes()).await?;
Ok(())
}
}
impl<T: WriteExt + Unpin> WriteExtNetty for T {}

@ -0,0 +1,11 @@
[package]
name = "e4mc-common"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
async-tungstenite = "0.20.0"

@ -0,0 +1,27 @@
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum ClientboundControlMessage {
DomainAssigned(String),
ChannelOpen(u8, SocketAddr),
ChannelClosed(u8),
}
impl From<ClientboundControlMessage> for async_tungstenite::tungstenite::Message {
fn from(val: ClientboundControlMessage) -> Self {
async_tungstenite::tungstenite::Message::Text(serde_json::to_string(&val).unwrap())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum ServerboundControlMessage {
ChannelClosed(u8),
}
impl From<ServerboundControlMessage> for async_tungstenite::tungstenite::Message {
fn from(val: ServerboundControlMessage) -> Self {
async_tungstenite::tungstenite::Message::Text(serde_json::to_string(&val).unwrap())
}
}

@ -15,8 +15,8 @@ env_logger = "0.10.0"
futures = "0.3.28" futures = "0.3.28"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.17" log = "0.4.17"
nanoid = "0.4.0"
serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95" serde_json = "1.0.95"
thiserror = "1.0.40" thiserror = "1.0.40"
tokio = { version = "1.27.0", features = ["rt-multi-thread", "sync", "macros", "net", "io-util"] } tokio = { version = "1.27.0", features = ["rt-multi-thread", "sync", "macros", "net", "io-util"] }
e4mc-common = { path = "../common"}
rand = "0.8.5"

@ -1,24 +1,29 @@
use std::collections::{HashMap}; use std::collections::HashMap;
use std::env; use std::env;
use std::net::SocketAddr; use std::net::SocketAddr;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use futures::{SinkExt, StreamExt}; use async_tungstenite::tokio::TokioAdapter;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::RwLock;
use tokio::task;
use async_tungstenite::tungstenite::Message; use async_tungstenite::tungstenite::Message;
use async_tungstenite::WebSocketStream;
use e4mc_common::{ClientboundControlMessage, ServerboundControlMessage};
use futures::stream::{SplitSink, SplitStream};
use futures::{Sink, SinkExt, Stream, StreamExt};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use log::{error, info}; use log::{error, info, trace, warn};
use nanoid::nanoid;
use netty::ReadExtNetty; use netty::ReadExtNetty;
use serde::{Deserialize, Serialize}; use rand::seq::SliceRandom;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::tcp::{ReadHalf, WriteHalf};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::{Mutex, RwLock};
use tokio::task;
use crate::netty::{NettyReadError, WriteExtNetty}; use crate::netty::{NettyReadError, WriteExtNetty};
mod netty; mod netty;
mod wordlist;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct Handshake { struct Handshake {
@ -28,7 +33,7 @@ struct Handshake {
next_state: HandshakeType, next_state: HandshakeType,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
#[repr(i32)] #[repr(i32)]
enum HandshakeType { enum HandshakeType {
Status = 1, Status = 1,
@ -43,7 +48,7 @@ impl Handshake {
} else { } else {
let protocol_version = packet.read_varint().await?; let protocol_version = packet.read_varint().await?;
let server_address = packet.read_string().await?; let server_address = packet.read_string().await?;
let server_port = packet.read_unsigned_short().await?; let server_port = packet.read_u16().await?;
let next_state = match packet.read_varint().await? { let next_state = match packet.read_varint().await? {
1 => HandshakeType::Status, 1 => HandshakeType::Status,
2 => HandshakeType::Login, 2 => HandshakeType::Login,
@ -57,10 +62,38 @@ impl Handshake {
}) })
} }
} }
async fn send(&self, mut writer: impl AsyncWriteExt + Unpin + Send) -> tokio::io::Result<()> {
let mut buf = vec![];
buf.write_varint(0).await?;
buf.write_varint(self.protocol_version).await?;
buf.write_string(&self.server_address).await?;
buf.write_all(&self.server_port.to_be_bytes()).await?;
buf.write_varint(self.next_state as i32).await?;
writer.write_varint(buf.len() as i32).await?;
writer.write_all(&buf).await?;
Ok(())
}
}
#[derive(Debug)]
enum WebsocketHandlerMessage {
ChannelOpen {
id_callback: tokio::sync::oneshot::Sender<Option<u8>>,
backchannel: UnboundedSender<MinecraftHandlerMessage>,
addr: SocketAddr,
},
ChannelClose(u8),
Data(Vec<u8>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum MinecraftHandlerMessage {
ChannelClose,
Data(Vec<u8>),
} }
lazy_static! { lazy_static! {
static ref EXPOSER_MAP: RwLock<HashMap<String, UnboundedSender<(Handshake, TcpStream, SocketAddr)>>> = static ref ROUTING_MAP: RwLock<HashMap<String, UnboundedSender<WebsocketHandlerMessage>>> =
RwLock::new(HashMap::new()); RwLock::new(HashMap::new());
static ref BASE_DOMAIN: String = env::var("BASE_DOMAIN").expect("BASE_DOMAIN missing"); static ref BASE_DOMAIN: String = env::var("BASE_DOMAIN").expect("BASE_DOMAIN missing");
} }
@ -77,7 +110,7 @@ async fn main() -> Result<()> {
info!("WebSocket Listening on: {}", ws_bind_addr); info!("WebSocket Listening on: {}", ws_bind_addr);
while let Ok((stream, _)) = listener.accept().await { while let Ok((stream, _)) = listener.accept().await {
task::spawn(async { task::spawn(async {
if let Err(e) = accept_ws_connection(stream).await { if let Err(e) = accept_websocket_connection(stream).await {
error!("Error handling WebSocket connection: {}", e); error!("Error handling WebSocket connection: {}", e);
} }
}); });
@ -89,7 +122,7 @@ async fn main() -> Result<()> {
info!("Minecraft Listening on: {}", mc_bind_addr); info!("Minecraft Listening on: {}", mc_bind_addr);
while let Ok((stream, _)) = listener.accept().await { while let Ok((stream, _)) = listener.accept().await {
task::spawn(async { task::spawn(async {
if let Err(e) = accept_mc_connection(stream).await { if let Err(e) = accept_minecraft_connection(stream).await {
error!("Error handling Minecraft connection: {}", e); error!("Error handling Minecraft connection: {}", e);
} }
}); });
@ -101,254 +134,256 @@ async fn main() -> Result<()> {
Ok(()) Ok(())
} }
#[derive(Serialize)] struct RoutingHandle {
enum ClientboundControlMessage { receiver: UnboundedReceiver<WebsocketHandlerMessage>,
DomainAssigned(String), domain: String,
ChannelOpen(u8, SocketAddr),
ChannelClosed(u8),
} }
#[derive(Deserialize)] impl RoutingHandle {
enum ServerboundControlMessage {
ChannelClosed(u8),
}
struct ExposerMapHandle(String, UnboundedReceiver<(Handshake, TcpStream, SocketAddr)>);
impl ExposerMapHandle {
async fn new(domain: String) -> Self { async fn new(domain: String) -> Self {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
EXPOSER_MAP.write().await.insert(domain.clone(), sender); ROUTING_MAP.write().await.insert(domain.clone(), sender);
Self(domain, receiver) Self { receiver, domain }
} }
async fn recv(&mut self) -> Option<(Handshake, TcpStream, SocketAddr)> { async fn recv(&mut self) -> Option<WebsocketHandlerMessage> {
self.1.recv().await self.receiver.recv().await
} }
} }
impl Drop for ExposerMapHandle { impl Drop for RoutingHandle {
fn drop(&mut self) { fn drop(&mut self) {
let domain = self.0.clone(); let domain = self.domain.clone();
task::spawn(async move { tokio::spawn(async move {
EXPOSER_MAP.write().await.remove(&domain); ROUTING_MAP.write().await.remove(&domain);
}); });
} }
} }
struct ChannelHandle( async fn accept_websocket_connection(stream: TcpStream) -> Result<()> {
u8,
UnboundedReceiver<MinecraftConnectionMessage>,
UnboundedSender<WebsocketConnectionMessage>,
);
impl ChannelHandle {
async fn new(
id: u8,
addr: SocketAddr,
send: UnboundedSender<WebsocketConnectionMessage>,
) -> Result<Self> {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
send.send(WebsocketConnectionMessage::Open(id, addr, sender))
.map_err(|e| anyhow::anyhow!("{}", e))?;
Ok(Self(id, receiver, send))
}
async fn recv(&mut self) -> Option<MinecraftConnectionMessage> {
self.1.recv().await
}
}
impl Drop for ChannelHandle {
fn drop(&mut self) {
self.2.send(WebsocketConnectionMessage::Close(self.0)).map_err(|e| anyhow::anyhow!("{}", e)).unwrap();
}
}
enum MinecraftConnectionMessage {
Data(Vec<u8>),
Close,
}
enum WebsocketConnectionMessage {
Data(Vec<u8>),
Close(u8),
Open(u8, SocketAddr, UnboundedSender<MinecraftConnectionMessage>),
}
async fn accept_ws_connection(stream: TcpStream) -> Result<()> {
let addr = stream.peer_addr()?; let addr = stream.peer_addr()?;
info!("WebSocker Peer address: {}", addr); info!("WebSocket Peer address: {}", addr);
let mut ws_stream = async_tungstenite::tokio::accept_async(stream).await?; let mut ws_stream = async_tungstenite::tokio::accept_async(stream).await?;
info!("New WebSocket connection: {}", addr); info!("New WebSocket connection: {}", addr);
let mut domain = get_random_domain(); let domain = get_random_domain().await;
let exposer_map = EXPOSER_MAP.read().await;
while exposer_map.contains_key(&domain) {
domain = get_random_domain();
}
let domain = domain;
info!("Connection {} was assigned domain {}", addr, domain);
ws_stream ws_stream
.send(Message::Text(serde_json::to_string( .send(ClientboundControlMessage::DomainAssigned(domain.clone()).into())
&ClientboundControlMessage::DomainAssigned(domain.clone()),
)?))
.await?; .await?;
drop(exposer_map); let handle = RoutingHandle::new(domain.clone()).await;
let mut exposer_handle = ExposerMapHandle::new(domain.clone()).await; let channel_table = Mutex::new(HashMap::new());
let channel_map = &RwLock::new(HashMap::new()); let (mut write, mut read) = ws_stream.split();
let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel(); tokio::select! {
let (mut send, mut recv) = ws_stream.split(); res = handle_websocket_send(&mut write, &channel_table, handle) => {
if let Err(e) = res {
error!("Error on WebSocket send loop: {}", e);
}
},
res = handle_websocket_recv(&mut read, &channel_table) => {
if let Err(e) = res {
error!("Error on WebSocket recv loop: {}", e);
}
}
}
futures::try_join!( let _ = write.reunite(read)?.close(None).await; // if this errors, why bother?
async move {
while let Some(message) = receiver.recv().await { Ok(())
match message { }
WebsocketConnectionMessage::Data(buf) => {
send.send(Message::Binary(buf)).await? async fn handle_websocket_send(
} write: &mut SplitSink<WebSocketStream<TokioAdapter<TcpStream>>, Message>,
WebsocketConnectionMessage::Close(id) => { channel_table: &Mutex<HashMap<u8, UnboundedSender<MinecraftHandlerMessage>>>,
send.send(Message::Text(serde_json::to_string( mut handle: RoutingHandle,
&ClientboundControlMessage::ChannelClosed(id), ) -> Result<()> {
)?)) while let Some(data) = handle.recv().await {
.await?; match data {
channel_map.write().await.remove(&id); WebsocketHandlerMessage::ChannelOpen {
} id_callback,
WebsocketConnectionMessage::Open(id, addr, channel) => { backchannel,
send.send(Message::Text(serde_json::to_string( addr,
&ClientboundControlMessage::ChannelOpen(id, addr), } => {
)?)) let mut table = channel_table.lock().await;
let channel = get_available_channel(table.keys().copied().collect());
if id_callback.send(channel).is_err() {
warn!("ID callback died before we could send the assigned ID");
} else if let Some(channel) = channel {
table.insert(channel, backchannel);
write
.send(ClientboundControlMessage::ChannelOpen(channel, addr).into())
.await?; .await?;
channel_map.write().await.insert(id, channel);
}
} }
} }
Ok(()) as Result<()> WebsocketHandlerMessage::ChannelClose(channel) => {
}, write
async move { .send(ClientboundControlMessage::ChannelClosed(channel).into())
while let Some(Ok(message)) = recv.next().await { .await?;
match message { channel_table.lock().await.remove(&channel);
Message::Text(message) => {
let message: ServerboundControlMessage = serde_json::from_str(&message)?;
match message {
ServerboundControlMessage::ChannelClosed(id) => {
if let Some(channel) = channel_map.write().await.remove(&id) {
info!("Closing channel id: {}", id);
channel.send(MinecraftConnectionMessage::Close).map_err(|e| anyhow::anyhow!("{}", e))?;
}
}
}
}
Message::Binary(buf) => {
if let Some(channel) = channel_map.read().await.get(&buf[0]) {
channel
.send(MinecraftConnectionMessage::Data(buf[1..].to_vec())).map_err(|e| anyhow::anyhow!("{}", e))?;
}
}
_ => {}
}
} }
Ok(()) as Result<()> WebsocketHandlerMessage::Data(buf) => {
}, write.send(Message::Binary(buf)).await?;
async move {
while let Some((handshake, mc_stream, addr)) = exposer_handle.recv().await {
if let Some(channel_id) =
get_available_channel(channel_map.read().await.keys().copied().collect())
{
task::spawn(handle_sent_connection(
handshake,
mc_stream,
addr,
sender.clone(),
channel_id,
));
}
} }
Ok(()) as Result<()>
} }
)?; }
Ok(()) Ok(())
} }
async fn handle_sent_connection( async fn handle_websocket_recv(
handshake: Handshake, read: &mut SplitStream<WebSocketStream<TokioAdapter<TcpStream>>>,
mut mc_stream: TcpStream, channel_table: &Mutex<HashMap<u8, UnboundedSender<MinecraftHandlerMessage>>>,
addr: SocketAddr,
send: UnboundedSender<WebsocketConnectionMessage>,
channel_id: u8,
) -> Result<()> { ) -> Result<()> {
let mut handle = ChannelHandle::new(channel_id, addr, send.clone()).await?; while let Some(message) = read.next().await {
let mut buf = vec![]; if let Err(async_tungstenite::tungstenite::Error::ConnectionClosed) = message {
buf.write_varint(0).await?; info!("Connection closed normally");
buf.write_varint(handshake.protocol_version).await?; return Ok(());
buf.write_string(&handshake.server_address).await?; }
buf.write_all(&handshake.server_port.to_be_bytes()).await?; let message = message?;
buf.write_varint(handshake.next_state as i32).await?; match message {
let mut len_buf = vec![channel_id]; Message::Text(message) => {
len_buf.write_varint(buf.len() as i32).await?; let message: ServerboundControlMessage = serde_json::from_str(&message)?;
len_buf.append(&mut buf);
send.send(WebsocketConnectionMessage::Data(len_buf)).map_err(|e| anyhow::anyhow!("{}", e))?;
let (mut read, mut write) = mc_stream.split();
tokio::select!(_ =
#[allow(unreachable_code)]
async move {
let mut buf = [0u8; 1024];
loop {
read.readable().await?;
let len = read.read(&mut buf).await?;
if len == 0 {
continue;
}
let mut packet = Vec::from(&buf[..len]);
packet.insert(0, channel_id);
send.send(WebsocketConnectionMessage::Data(packet)).map_err(|e| anyhow::anyhow!("{}", e))?;
}
Ok(()) as anyhow::Result<()>
} => {},
_ = async move {
while let Some(message) = handle.recv().await {
match message { match message {
MinecraftConnectionMessage::Data(buf) => { ServerboundControlMessage::ChannelClosed(channel) => {
write.write_all(&buf).await?; if let Some(sender) = channel_table.lock().await.remove(&channel) {
} sender.send(MinecraftHandlerMessage::ChannelClose)?;
MinecraftConnectionMessage::Close => { }
return Ok(())
} }
} }
} }
Ok(()) as anyhow::Result<()> Message::Binary(buf) => {
} => {} let channel = buf[0];
); if let Some(sender) = channel_table.lock().await.get(&channel) {
mc_stream.shutdown().await?; sender.send(MinecraftHandlerMessage::Data(buf[1..].to_vec()))?;
}
}
_ => {}
}
}
Ok(()) Ok(())
} }
async fn accept_mc_connection(mut stream: TcpStream) -> Result<()> { struct ChannelHandle {
receiver: UnboundedReceiver<MinecraftHandlerMessage>,
sender: UnboundedSender<WebsocketHandlerMessage>,
channel: u8,
}
impl ChannelHandle {
async fn new(
sender: &UnboundedSender<WebsocketHandlerMessage>,
addr: SocketAddr,
) -> Result<Self> {
let (mc_sender, mc_receiver) = tokio::sync::mpsc::unbounded_channel();
let (id_sender, id_receiver) = tokio::sync::oneshot::channel();
sender.send(WebsocketHandlerMessage::ChannelOpen {
id_callback: id_sender,
backchannel: mc_sender,
addr,
})?;
if let Some(channel) = id_receiver.await? {
Ok(Self {
receiver: mc_receiver,
sender: sender.clone(),
channel,
})
} else {
Err(anyhow!("Websocket handler couldn't give channel"))
}
}
fn send(&self, mut buf: Vec<u8>) -> Result<()> {
buf.insert(0, self.channel);
self.sender.send(WebsocketHandlerMessage::Data(buf))?;
Ok(())
}
async fn recv(&mut self) -> Option<MinecraftHandlerMessage> {
self.receiver.recv().await
}
fn split(&mut self) -> (ChannelHandleSend, ChannelHandleRecv) {
(
ChannelHandleSend {
sender: &mut self.sender,
channel: self.channel,
},
ChannelHandleRecv {
receiver: &mut self.receiver,
},
)
}
}
struct ChannelHandleSend<'a> {
sender: &'a mut UnboundedSender<WebsocketHandlerMessage>,
channel: u8,
}
impl ChannelHandleSend<'_> {
fn send(&self, mut buf: Vec<u8>) -> Result<()> {
buf.insert(0, self.channel);
self.sender.send(WebsocketHandlerMessage::Data(buf))?;
Ok(())
}
}
struct ChannelHandleRecv<'a> {
receiver: &'a mut UnboundedReceiver<MinecraftHandlerMessage>,
}
impl ChannelHandleRecv<'_> {
async fn recv(&mut self) -> Option<MinecraftHandlerMessage> {
self.receiver.recv().await
}
}
impl Drop for ChannelHandle {
fn drop(&mut self) {
self.sender
.send(WebsocketHandlerMessage::ChannelClose(self.channel))
.unwrap();
}
}
async fn accept_minecraft_connection(mut stream: TcpStream) -> Result<()> {
let addr = stream.peer_addr()?; let addr = stream.peer_addr()?;
info!("New Minecraft connection: {}", addr); info!("Minecraft Peer address: {}", addr);
let packet = stream.read_packet().await; let packet = stream.read_packet().await;
if let Err(NettyReadError::LegacyServerListPing) = packet { if let Err(NettyReadError::LegacyServerListPing) = packet {
stream stream
.write_all(include_bytes!("./legacy_serverlistping_response.bin")) .write_all(include_bytes!("legacy_serverlistping_response.bin"))
.await?; .await?;
return Ok(()); return Ok(());
} }
let handshake = Handshake::new(&packet?).await?;
let packet = packet?; if let Some(sender) = ROUTING_MAP.read().await.get(&handshake.server_address) {
let mut handle = ChannelHandle::new(sender, addr).await?;
let handshake = Handshake::new(&packet).await?; let mut buf = vec![];
if let Some(sender) = EXPOSER_MAP.read().await.get(&handshake.server_address) { handshake.send(&mut buf).await?;
sender.send((handshake, stream, addr)).map_err(|e| anyhow::anyhow!("{}", e))?; handle.send(buf)?;
let (send, recv) = handle.split();
let (read, write) = stream.split();
tokio::select! {
res = handle_minecraft_send(write, recv) => {
if let Err(e) = res {
error!("Error on Minecraft send loop: {}", e);
}
},
res = handle_minecraft_recv(read, send) => {
if let Err(e) = res {
error!("Error on Minecraft recv loop: {}", e);
}
}
}
stream.shutdown().await?;
} else { } else {
match handshake.next_state { match handshake.next_state {
HandshakeType::Status => { HandshakeType::Status => {
@ -373,18 +408,53 @@ async fn accept_mc_connection(mut stream: TcpStream) -> Result<()> {
Ok(()) Ok(())
} }
const ID_ALPHABET: [char; 36] = [ async fn handle_minecraft_send(
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', mut write: WriteHalf<'_>,
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', mut recv: ChannelHandleRecv<'_>,
]; ) -> Result<()> {
const ID_LENGTH: usize = 8; while let Some(data) = recv.recv().await {
match data {
MinecraftHandlerMessage::ChannelClose => return Ok(()),
MinecraftHandlerMessage::Data(buf) => write.write_all(&buf).await?,
}
}
Ok(())
}
async fn handle_minecraft_recv(mut read: ReadHalf<'_>, send: ChannelHandleSend<'_>) -> Result<()> {
let mut buf = [0u8; 1024];
loop {
let ready = read.ready(tokio::io::Interest::READABLE).await?;
if ready.is_read_closed() {
return Ok(());
}
let len = read.read(&mut buf).await?;
if len == 0 {
continue;
}
let packet = Vec::from(&buf[..len]);
trace!("send: {:?}", packet);
send.send(packet)?;
}
}
fn get_random_domain() -> String { async fn get_random_domain() -> String {
format!( let mut domain = format!(
"{}.{}", "{}-{}.{}",
nanoid!(ID_LENGTH, &ID_ALPHABET), wordlist::ID_WORDS.choose(&mut rand::thread_rng()).unwrap(),
wordlist::ID_WORDS.choose(&mut rand::thread_rng()).unwrap(),
BASE_DOMAIN.as_str() BASE_DOMAIN.as_str()
) );
let map = ROUTING_MAP.read().await;
while map.contains_key(&domain) {
domain = format!(
"{}-{}.{}",
wordlist::ID_WORDS.choose(&mut rand::thread_rng()).unwrap(),
wordlist::ID_WORDS.choose(&mut rand::thread_rng()).unwrap(),
BASE_DOMAIN.as_str()
);
}
domain
} }
fn get_available_channel(used: Vec<u8>) -> Option<u8> { fn get_available_channel(used: Vec<u8>) -> Option<u8> {

@ -1,7 +1,7 @@
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use async_trait::async_trait; use async_trait::async_trait;
use log::{error}; use log::error;
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -26,64 +26,6 @@ impl From<std::io::ErrorKind> for NettyReadError {
#[async_trait] #[async_trait]
pub trait ReadExtNetty: AsyncReadExt + Unpin { pub trait ReadExtNetty: AsyncReadExt + Unpin {
async fn read_bool(&mut self) -> Result<bool, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
match buf[0] {
0 => Ok(false),
1 => Ok(true),
_ => Err(std::io::ErrorKind::InvalidData.into()),
}
}
async fn read_byte(&mut self) -> Result<i8, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
Ok(i8::from_be_bytes(buf))
}
async fn read_unsigned_byte(&mut self) -> Result<u8, NettyReadError> {
let mut buf = [0u8];
self.read_exact(&mut buf).await?;
Ok(buf[0])
}
async fn read_short(&mut self) -> Result<i16, NettyReadError> {
let mut buf = [0u8; 2];
self.read_exact(&mut buf).await?;
Ok(i16::from_be_bytes(buf))
}
async fn read_unsigned_short(&mut self) -> Result<u16, NettyReadError> {
let mut buf = [0u8; 2];
self.read_exact(&mut buf).await?;
Ok(u16::from_be_bytes(buf))
}
async fn read_int(&mut self) -> Result<i32, NettyReadError> {
let mut buf = [0u8; 4];
self.read_exact(&mut buf).await?;
Ok(i32::from_be_bytes(buf))
}
async fn read_long(&mut self) -> Result<i64, NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
Ok(i64::from_be_bytes(buf))
}
async fn read_float(&mut self) -> Result<f32, NettyReadError> {
let mut buf = [0u8; 4];
self.read_exact(&mut buf).await?;
Ok(f32::from_be_bytes(buf))
}
async fn read_double(&mut self) -> Result<f64, NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
Ok(f64::from_be_bytes(buf))
}
async fn read_string(&mut self) -> Result<String, NettyReadError> { async fn read_string(&mut self) -> Result<String, NettyReadError> {
let len = self.read_varint().await?; let len = self.read_varint().await?;
let mut buf = vec![0u8; len as usize]; let mut buf = vec![0u8; len as usize];
@ -91,11 +33,10 @@ pub trait ReadExtNetty: AsyncReadExt + Unpin {
String::from_utf8(buf).map_err(|_| std::io::ErrorKind::InvalidData.into()) String::from_utf8(buf).map_err(|_| std::io::ErrorKind::InvalidData.into())
} }
async fn read_varint(&mut self) -> Result<i32, NettyReadError> { async fn read_varint(&mut self) -> Result<i32, NettyReadError> {
let mut res = 0i32; let mut res = 0i32;
for i in 0..5 { for i in 0..5 {
let part = self.read_unsigned_byte().await?; let part = self.read_u8().await?;
res |= (part as i32 & 0x7F) << (7 * i); res |= (part as i32 & 0x7F) << (7 * i);
if part & 0x80 == 0 { if part & 0x80 == 0 {
return Ok(res); return Ok(res);
@ -105,72 +46,6 @@ pub trait ReadExtNetty: AsyncReadExt + Unpin {
Err(std::io::ErrorKind::InvalidData.into()) Err(std::io::ErrorKind::InvalidData.into())
} }
// async fn read_varint(&mut self) -> Result<i32, NettyReadError> {
// let mut value = 0i32;
// let mut buf = [0u8; 1];
// let mut pos = 0u8;
// loop {
// self.read_exact(&mut buf).await?;
// println!("{}", buf[0]);
// value |= ((buf[0] & 0b01111111) << pos) as i32;
// if (buf[0] & 0b10000000) == 0 {
// break;
// };
// pos += 7;
// if pos >= 32 {
// return Err(std::io::ErrorKind::InvalidData.into());
// };
// }
// Ok(value)
// }
async fn read_varlong(&mut self) -> Result<i64, NettyReadError> {
let mut value = 0i64;
let mut buf = [0u8; 1];
let mut position = 0u8;
loop {
self.read_exact(&mut buf).await?;
value |= ((buf[0] & 0b01111111) << position) as i64;
if (buf[0] & 0b10000000) == 0 {
break;
};
position += 7;
if position >= 64 {
return Err(std::io::ErrorKind::InvalidData.into());
};
}
Ok(value)
}
async fn read_vec3(&mut self) -> Result<(i32, i16, i32), NettyReadError> {
let mut buf = [0u8; 8];
self.read_exact(&mut buf).await?;
let packed = u64::from_be_bytes(buf);
let x: i32 = ((packed & 0xffffffc000000000) >> 38) as _;
let y: i16 = (packed & 0xfff) as _;
let z: i32 = ((packed & 0x3ffffff000) >> 12) as _;
Ok((
match x {
i32::MIN..=0x1ffffff => x,
0x2000000.. => x - 0x2000000,
},
match y {
i16::MIN..=0x7ff => y,
0x800.. => y - 0x800,
},
match z {
i32::MIN..=0x1ffffff => z,
0x2000000.. => z - 0x2000000,
},
))
}
async fn read_uuid(&mut self) -> Result<u128, NettyReadError> {
let mut buf = [0u8; 16];
self.read_exact(&mut buf).await?;
Ok(u128::from_be_bytes(buf))
}
async fn read_packet(&mut self) -> Result<Vec<u8>, NettyReadError> { async fn read_packet(&mut self) -> Result<Vec<u8>, NettyReadError> {
let len = self.read_varint().await?; let len = self.read_varint().await?;
let mut buf = vec![0u8; len as usize]; let mut buf = vec![0u8; len as usize];

@ -0,0 +1,213 @@
pub const ID_WORDS: [&str; 2048] = [
"abandon", "ability", "able", "about", "above", "absent", "absorb", "abstract", "absurd",
"abuse", "access", "accident", "account", "accuse", "achieve", "acid", "acoustic", "acquire",
"across", "act", "action", "actor", "actress", "actual", "adapt", "add", "addict", "address",
"adjust", "admit", "adult", "advance", "advice", "aerobic", "affair", "afford", "afraid",
"again", "age", "agent", "agree", "ahead", "aim", "air", "airport", "aisle", "alarm", "album",
"alcohol", "alert", "alien", "all", "alley", "allow", "almost", "alone", "alpha", "already",
"also", "alter", "always", "amateur", "amazing", "among", "amount", "amused", "analyst",
"anchor", "ancient", "anger", "angle", "angry", "animal", "ankle", "announce", "annual",
"another", "answer", "antenna", "antique", "anxiety", "any", "apart", "apology", "appear",
"apple", "approve", "april", "arch", "arctic", "area", "arena", "argue", "arm", "armed",
"armor", "army", "around", "arrange", "arrest", "arrive", "arrow", "art", "artefact", "artist",
"artwork", "ask", "aspect", "assault", "asset", "assist", "assume", "asthma", "athlete",
"atom", "attack", "attend", "attitude", "attract", "auction", "audit", "august", "aunt",
"author", "auto", "autumn", "average", "avocado", "avoid", "awake", "aware", "away", "awesome",
"awful", "awkward", "axis", "baby", "bachelor", "bacon", "badge", "bag", "balance", "balcony",
"ball", "bamboo", "banana", "banner", "bar", "barely", "bargain", "barrel", "base", "basic",
"basket", "battle", "beach", "bean", "beauty", "because", "become", "beef", "before", "begin",
"behave", "behind", "believe", "below", "belt", "bench", "benefit", "best", "betray", "better",
"between", "beyond", "bicycle", "bid", "bike", "bind", "biology", "bird", "birth", "bitter",
"black", "blade", "blame", "blanket", "blast", "bleak", "bless", "blind", "blood", "blossom",
"blouse", "blue", "blur", "blush", "board", "boat", "body", "boil", "bomb", "bone", "bonus",
"book", "boost", "border", "boring", "borrow", "boss", "bottom", "bounce", "box", "boy",
"bracket", "brain", "brand", "brass", "brave", "bread", "breeze", "brick", "bridge", "brief",
"bright", "bring", "brisk", "broccoli", "broken", "bronze", "broom", "brother", "brown",
"brush", "bubble", "buddy", "budget", "buffalo", "build", "bulb", "bulk", "bullet", "bundle",
"bunker", "burden", "burger", "burst", "bus", "business", "busy", "butter", "buyer", "buzz",
"cabbage", "cabin", "cable", "cactus", "cage", "cake", "call", "calm", "camera", "camp", "can",
"canal", "cancel", "candy", "cannon", "canoe", "canvas", "canyon", "capable", "capital",
"captain", "car", "carbon", "card", "cargo", "carpet", "carry", "cart", "case", "cash",
"casino", "castle", "casual", "cat", "catalog", "catch", "category", "cattle", "caught",
"cause", "caution", "cave", "ceiling", "celery", "cement", "census", "century", "cereal",
"certain", "chair", "chalk", "champion", "change", "chaos", "chapter", "charge", "chase",
"chat", "cheap", "check", "cheese", "chef", "cherry", "chest", "chicken", "chief", "child",
"chimney", "choice", "choose", "chronic", "chuckle", "chunk", "churn", "cigar", "cinnamon",
"circle", "citizen", "city", "civil", "claim", "clap", "clarify", "claw", "clay", "clean",
"clerk", "clever", "click", "client", "cliff", "climb", "clinic", "clip", "clock", "clog",
"close", "cloth", "cloud", "clown", "club", "clump", "cluster", "clutch", "coach", "coast",
"coconut", "code", "coffee", "coil", "coin", "collect", "color", "column", "combine", "come",
"comfort", "comic", "common", "company", "concert", "conduct", "confirm", "congress",
"connect", "consider", "control", "convince", "cook", "cool", "copper", "copy", "coral",
"core", "corn", "correct", "cost", "cotton", "couch", "country", "couple", "course", "cousin",
"cover", "coyote", "crack", "cradle", "craft", "cram", "crane", "crash", "crater", "crawl",
"crazy", "cream", "credit", "creek", "crew", "cricket", "crime", "crisp", "critic", "crop",
"cross", "crouch", "crowd", "crucial", "cruel", "cruise", "crumble", "crunch", "crush", "cry",
"crystal", "cube", "culture", "cup", "cupboard", "curious", "current", "curtain", "curve",
"cushion", "custom", "cute", "cycle", "dad", "damage", "damp", "dance", "danger", "daring",
"dash", "daughter", "dawn", "day", "deal", "debate", "debris", "decade", "december", "decide",
"decline", "decorate", "decrease", "deer", "defense", "define", "defy", "degree", "delay",
"deliver", "demand", "demise", "denial", "dentist", "deny", "depart", "depend", "deposit",
"depth", "deputy", "derive", "describe", "desert", "design", "desk", "despair", "destroy",
"detail", "detect", "develop", "device", "devote", "diagram", "dial", "diamond", "diary",
"dice", "diesel", "diet", "differ", "digital", "dignity", "dilemma", "dinner", "dinosaur",
"direct", "dirt", "disagree", "discover", "disease", "dish", "dismiss", "disorder", "display",
"distance", "divert", "divide", "divorce", "dizzy", "doctor", "document", "dog", "doll",
"dolphin", "domain", "donate", "donkey", "donor", "door", "dose", "double", "dove", "draft",
"dragon", "drama", "drastic", "draw", "dream", "dress", "drift", "drill", "drink", "drip",
"drive", "drop", "drum", "dry", "duck", "dumb", "dune", "during", "dust", "dutch", "duty",
"dwarf", "dynamic", "eager", "eagle", "early", "earn", "earth", "easily", "east", "easy",
"echo", "ecology", "economy", "edge", "edit", "educate", "effort", "egg", "eight", "either",
"elbow", "elder", "electric", "elegant", "element", "elephant", "elevator", "elite", "else",
"embark", "embody", "embrace", "emerge", "emotion", "employ", "empower", "empty", "enable",
"enact", "end", "endless", "endorse", "enemy", "energy", "enforce", "engage", "engine",
"enhance", "enjoy", "enlist", "enough", "enrich", "enroll", "ensure", "enter", "entire",
"entry", "envelope", "episode", "equal", "equip", "era", "erase", "erode", "erosion", "error",
"erupt", "escape", "essay", "essence", "estate", "eternal", "ethics", "evidence", "evil",
"evoke", "evolve", "exact", "example", "excess", "exchange", "excite", "exclude", "excuse",
"execute", "exercise", "exhaust", "exhibit", "exile", "exist", "exit", "exotic", "expand",
"expect", "expire", "explain", "expose", "express", "extend", "extra", "eye", "eyebrow",
"fabric", "face", "faculty", "fade", "faint", "faith", "fall", "false", "fame", "family",
"famous", "fan", "fancy", "fantasy", "farm", "fashion", "fat", "fatal", "father", "fatigue",
"fault", "favorite", "feature", "february", "federal", "fee", "feed", "feel", "female",
"fence", "festival", "fetch", "fever", "few", "fiber", "fiction", "field", "figure", "file",
"film", "filter", "final", "find", "fine", "finger", "finish", "fire", "firm", "first",
"fiscal", "fish", "fit", "fitness", "fix", "flag", "flame", "flash", "flat", "flavor", "flee",
"flight", "flip", "float", "flock", "floor", "flower", "fluid", "flush", "fly", "foam",
"focus", "fog", "foil", "fold", "follow", "food", "foot", "force", "forest", "forget", "fork",
"fortune", "forum", "forward", "fossil", "foster", "found", "fox", "fragile", "frame",
"frequent", "fresh", "friend", "fringe", "frog", "front", "frost", "frown", "frozen", "fruit",
"fuel", "fun", "funny", "furnace", "fury", "future", "gadget", "gain", "galaxy", "gallery",
"game", "gap", "garage", "garbage", "garden", "garlic", "garment", "gas", "gasp", "gate",
"gather", "gauge", "gaze", "general", "genius", "genre", "gentle", "genuine", "gesture",
"ghost", "giant", "gift", "giggle", "ginger", "giraffe", "girl", "give", "glad", "glance",
"glare", "glass", "glide", "glimpse", "globe", "gloom", "glory", "glove", "glow", "glue",
"goat", "goddess", "gold", "good", "goose", "gorilla", "gospel", "gossip", "govern", "gown",
"grab", "grace", "grain", "grant", "grape", "grass", "gravity", "great", "green", "grid",
"grief", "grit", "grocery", "group", "grow", "grunt", "guard", "guess", "guide", "guilt",
"guitar", "gun", "gym", "habit", "hair", "half", "hammer", "hamster", "hand", "happy",
"harbor", "hard", "harsh", "harvest", "hat", "have", "hawk", "hazard", "head", "health",
"heart", "heavy", "hedgehog", "height", "hello", "helmet", "help", "hen", "hero", "hidden",
"high", "hill", "hint", "hip", "hire", "history", "hobby", "hockey", "hold", "hole", "holiday",
"hollow", "home", "honey", "hood", "hope", "horn", "horror", "horse", "hospital", "host",
"hotel", "hour", "hover", "hub", "huge", "human", "humble", "humor", "hundred", "hungry",
"hunt", "hurdle", "hurry", "hurt", "husband", "hybrid", "ice", "icon", "idea", "identify",
"idle", "ignore", "ill", "illegal", "illness", "image", "imitate", "immense", "immune",
"impact", "impose", "improve", "impulse", "inch", "include", "income", "increase", "index",
"indicate", "indoor", "industry", "infant", "inflict", "inform", "inhale", "inherit",
"initial", "inject", "injury", "inmate", "inner", "innocent", "input", "inquiry", "insane",
"insect", "inside", "inspire", "install", "intact", "interest", "into", "invest", "invite",
"involve", "iron", "island", "isolate", "issue", "item", "ivory", "jacket", "jaguar", "jar",
"jazz", "jealous", "jeans", "jelly", "jewel", "job", "join", "joke", "journey", "joy", "judge",
"juice", "jump", "jungle", "junior", "junk", "just", "kangaroo", "keen", "keep", "ketchup",
"key", "kick", "kid", "kidney", "kind", "kingdom", "kiss", "kit", "kitchen", "kite", "kitten",
"kiwi", "knee", "knife", "knock", "know", "lab", "label", "labor", "ladder", "lady", "lake",
"lamp", "language", "laptop", "large", "later", "latin", "laugh", "laundry", "lava", "law",
"lawn", "lawsuit", "layer", "lazy", "leader", "leaf", "learn", "leave", "lecture", "left",
"leg", "legal", "legend", "leisure", "lemon", "lend", "length", "lens", "leopard", "lesson",
"letter", "level", "liar", "liberty", "library", "license", "life", "lift", "light", "like",
"limb", "limit", "link", "lion", "liquid", "list", "little", "live", "lizard", "load", "loan",
"lobster", "local", "lock", "logic", "lonely", "long", "loop", "lottery", "loud", "lounge",
"love", "loyal", "lucky", "luggage", "lumber", "lunar", "lunch", "luxury", "lyrics", "machine",
"mad", "magic", "magnet", "maid", "mail", "main", "major", "make", "mammal", "man", "manage",
"mandate", "mango", "mansion", "manual", "maple", "marble", "march", "margin", "marine",
"market", "marriage", "mask", "mass", "master", "match", "material", "math", "matrix",
"matter", "maximum", "maze", "meadow", "mean", "measure", "meat", "mechanic", "medal", "media",
"melody", "melt", "member", "memory", "mention", "menu", "mercy", "merge", "merit", "merry",
"mesh", "message", "metal", "method", "middle", "midnight", "milk", "million", "mimic", "mind",
"minimum", "minor", "minute", "miracle", "mirror", "misery", "miss", "mistake", "mix", "mixed",
"mixture", "mobile", "model", "modify", "mom", "moment", "monitor", "monkey", "monster",
"month", "moon", "moral", "more", "morning", "mosquito", "mother", "motion", "motor",
"mountain", "mouse", "move", "movie", "much", "muffin", "mule", "multiply", "muscle", "museum",
"mushroom", "music", "must", "mutual", "myself", "mystery", "myth", "naive", "name", "napkin",
"narrow", "nasty", "nation", "nature", "near", "neck", "need", "negative", "neglect",
"neither", "nephew", "nerve", "nest", "net", "network", "neutral", "never", "news", "next",
"nice", "night", "noble", "noise", "nominee", "noodle", "normal", "north", "nose", "notable",
"note", "nothing", "notice", "novel", "now", "nuclear", "number", "nurse", "nut", "oak",
"obey", "object", "oblige", "obscure", "observe", "obtain", "obvious", "occur", "ocean",
"october", "odor", "off", "offer", "office", "often", "oil", "okay", "old", "olive", "olympic",
"omit", "once", "one", "onion", "online", "only", "open", "opera", "opinion", "oppose",
"option", "orange", "orbit", "orchard", "order", "ordinary", "organ", "orient", "original",
"orphan", "ostrich", "other", "outdoor", "outer", "output", "outside", "oval", "oven", "over",
"own", "owner", "oxygen", "oyster", "ozone", "pact", "paddle", "page", "pair", "palace",
"palm", "panda", "panel", "panic", "panther", "paper", "parade", "parent", "park", "parrot",
"party", "pass", "patch", "path", "patient", "patrol", "pattern", "pause", "pave", "payment",
"peace", "peanut", "pear", "peasant", "pelican", "pen", "penalty", "pencil", "people",
"pepper", "perfect", "permit", "person", "pet", "phone", "photo", "phrase", "physical",
"piano", "picnic", "picture", "piece", "pig", "pigeon", "pill", "pilot", "pink", "pioneer",
"pipe", "pistol", "pitch", "pizza", "place", "planet", "plastic", "plate", "play", "please",
"pledge", "pluck", "plug", "plunge", "poem", "poet", "point", "polar", "pole", "police",
"pond", "pony", "pool", "popular", "portion", "position", "possible", "post", "potato",
"pottery", "poverty", "powder", "power", "practice", "praise", "predict", "prefer", "prepare",
"present", "pretty", "prevent", "price", "pride", "primary", "print", "priority", "prison",
"private", "prize", "problem", "process", "produce", "profit", "program", "project", "promote",
"proof", "property", "prosper", "protect", "proud", "provide", "public", "pudding", "pull",
"pulp", "pulse", "pumpkin", "punch", "pupil", "puppy", "purchase", "purity", "purpose",
"purse", "push", "put", "puzzle", "pyramid", "quality", "quantum", "quarter", "question",
"quick", "quit", "quiz", "quote", "rabbit", "raccoon", "race", "rack", "radar", "radio",
"rail", "rain", "raise", "rally", "ramp", "ranch", "random", "range", "rapid", "rare", "rate",
"rather", "raven", "raw", "razor", "ready", "real", "reason", "rebel", "rebuild", "recall",
"receive", "recipe", "record", "recycle", "reduce", "reflect", "reform", "refuse", "region",
"regret", "regular", "reject", "relax", "release", "relief", "rely", "remain", "remember",
"remind", "remove", "render", "renew", "rent", "reopen", "repair", "repeat", "replace",
"report", "require", "rescue", "resemble", "resist", "resource", "response", "result",
"retire", "retreat", "return", "reunion", "reveal", "review", "reward", "rhythm", "rib",
"ribbon", "rice", "rich", "ride", "ridge", "rifle", "right", "rigid", "ring", "riot", "ripple",
"risk", "ritual", "rival", "river", "road", "roast", "robot", "robust", "rocket", "romance",
"roof", "rookie", "room", "rose", "rotate", "rough", "round", "route", "royal", "rubber",
"rude", "rug", "rule", "run", "runway", "rural", "sad", "saddle", "sadness", "safe", "sail",
"salad", "salmon", "salon", "salt", "salute", "same", "sample", "sand", "satisfy", "satoshi",
"sauce", "sausage", "save", "say", "scale", "scan", "scare", "scatter", "scene", "scheme",
"school", "science", "scissors", "scorpion", "scout", "scrap", "screen", "script", "scrub",
"sea", "search", "season", "seat", "second", "secret", "section", "security", "seed", "seek",
"segment", "select", "sell", "seminar", "senior", "sense", "sentence", "series", "service",
"session", "settle", "setup", "seven", "shadow", "shaft", "shallow", "share", "shed", "shell",
"sheriff", "shield", "shift", "shine", "ship", "shiver", "shock", "shoe", "shoot", "shop",
"short", "shoulder", "shove", "shrimp", "shrug", "shuffle", "shy", "sibling", "sick", "side",
"siege", "sight", "sign", "silent", "silk", "silly", "silver", "similar", "simple", "since",
"sing", "siren", "sister", "situate", "six", "size", "skate", "sketch", "ski", "skill", "skin",
"skirt", "skull", "slab", "slam", "sleep", "slender", "slice", "slide", "slight", "slim",
"slogan", "slot", "slow", "slush", "small", "smart", "smile", "smoke", "smooth", "snack",
"snake", "snap", "sniff", "snow", "soap", "soccer", "social", "sock", "soda", "soft", "solar",
"soldier", "solid", "solution", "solve", "someone", "song", "soon", "sorry", "sort", "soul",
"sound", "soup", "source", "south", "space", "spare", "spatial", "spawn", "speak", "special",
"speed", "spell", "spend", "sphere", "spice", "spider", "spike", "spin", "spirit", "split",
"spoil", "sponsor", "spoon", "sport", "spot", "spray", "spread", "spring", "spy", "square",
"squeeze", "squirrel", "stable", "stadium", "staff", "stage", "stairs", "stamp", "stand",
"start", "state", "stay", "steak", "steel", "stem", "step", "stereo", "stick", "still",
"sting", "stock", "stomach", "stone", "stool", "story", "stove", "strategy", "street",
"strike", "strong", "struggle", "student", "stuff", "stumble", "style", "subject", "submit",
"subway", "success", "such", "sudden", "suffer", "sugar", "suggest", "suit", "summer", "sun",
"sunny", "sunset", "super", "supply", "supreme", "sure", "surface", "surge", "surprise",
"surround", "survey", "suspect", "sustain", "swallow", "swamp", "swap", "swarm", "swear",
"sweet", "swift", "swim", "swing", "switch", "sword", "symbol", "symptom", "syrup", "system",
"table", "tackle", "tag", "tail", "talent", "talk", "tank", "tape", "target", "task", "taste",
"tattoo", "taxi", "teach", "team", "tell", "ten", "tenant", "tennis", "tent", "term", "test",
"text", "thank", "that", "theme", "then", "theory", "there", "they", "thing", "this",
"thought", "three", "thrive", "throw", "thumb", "thunder", "ticket", "tide", "tiger", "tilt",
"timber", "time", "tiny", "tip", "tired", "tissue", "title", "toast", "tobacco", "today",
"toddler", "toe", "together", "toilet", "token", "tomato", "tomorrow", "tone", "tongue",
"tonight", "tool", "tooth", "top", "topic", "topple", "torch", "tornado", "tortoise", "toss",
"total", "tourist", "toward", "tower", "town", "toy", "track", "trade", "traffic", "tragic",
"train", "transfer", "trap", "trash", "travel", "tray", "treat", "tree", "trend", "trial",
"tribe", "trick", "trigger", "trim", "trip", "trophy", "trouble", "truck", "true", "truly",
"trumpet", "trust", "truth", "try", "tube", "tuition", "tumble", "tuna", "tunnel", "turkey",
"turn", "turtle", "twelve", "twenty", "twice", "twin", "twist", "two", "type", "typical",
"ugly", "umbrella", "unable", "unaware", "uncle", "uncover", "under", "undo", "unfair",
"unfold", "unhappy", "uniform", "unique", "unit", "universe", "unknown", "unlock", "until",
"unusual", "unveil", "update", "upgrade", "uphold", "upon", "upper", "upset", "urban", "urge",
"usage", "use", "used", "useful", "useless", "usual", "utility", "vacant", "vacuum", "vague",
"valid", "valley", "valve", "van", "vanish", "vapor", "various", "vast", "vault", "vehicle",
"velvet", "vendor", "venture", "venue", "verb", "verify", "version", "very", "vessel",
"veteran", "viable", "vibrant", "vicious", "victory", "video", "view", "village", "vintage",
"violin", "virtual", "virus", "visa", "visit", "visual", "vital", "vivid", "vocal", "voice",
"void", "volcano", "volume", "vote", "voyage", "wage", "wagon", "wait", "walk", "wall",
"walnut", "want", "warfare", "warm", "warrior", "wash", "wasp", "waste", "water", "wave",
"way", "wealth", "weapon", "wear", "weasel", "weather", "web", "wedding", "weekend", "weird",
"welcome", "west", "wet", "whale", "what", "wheat", "wheel", "when", "where", "whip",
"whisper", "wide", "width", "wife", "wild", "will", "win", "window", "wine", "wing", "wink",
"winner", "winter", "wire", "wisdom", "wise", "wish", "witness", "wolf", "woman", "wonder",
"wood", "wool", "word", "work", "world", "worry", "worth", "wrap", "wreck", "wrestle", "wrist",
"write", "wrong", "yard", "year", "yellow", "you", "young", "youth", "zebra", "zero", "zone",
"zoo",
];
Loading…
Cancel
Save