Added RCON support
This commit is contained in:
1 parent
50dcbfeb6d
commit
414cf3190c
12 files changed
+386
-16
No files matched your search
@@ -0,0 +1,220 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::{self, Read},
|
||||
};
|
||||
|
||||
use mio::{
|
||||
net::{TcpListener, TcpStream},
|
||||
Events, Interest, Poll, Token,
|
||||
};
|
||||
use packet::{Packet, PacketError, PacketType};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{commands::handle_command, config::RCONConfig};
|
||||
|
||||
mod packet;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum RCONError {
|
||||
#[error("authentication failed")]
|
||||
Auth,
|
||||
#[error("command exceeds the maximum length")]
|
||||
CommandTooLong,
|
||||
#[error("{}", _0)]
|
||||
Io(io::Error),
|
||||
}
|
||||
|
||||
const SERVER: Token = Token(0);
|
||||
|
||||
pub struct RCONServer {}
|
||||
|
||||
impl RCONServer {
|
||||
pub async fn new(config: &RCONConfig) -> Result<Self, io::Error> {
|
||||
assert!(config.enabled, "RCON is not enabled");
|
||||
let addr = format!("{}:{}", config.ip, config.port)
|
||||
.parse()
|
||||
.expect("Failed to parse RCON address");
|
||||
let mut poll = Poll::new().unwrap();
|
||||
let mut listener = TcpListener::bind(addr).unwrap();
|
||||
|
||||
poll.registry()
|
||||
.register(&mut listener, SERVER, Interest::READABLE)
|
||||
.unwrap();
|
||||
|
||||
let mut unique_token = Token(SERVER.0 + 1);
|
||||
|
||||
let mut events = Events::with_capacity(128);
|
||||
|
||||
let mut connections: HashMap<Token, RCONClient> = HashMap::new();
|
||||
|
||||
let password = config.password.clone();
|
||||
|
||||
loop {
|
||||
poll.poll(&mut events, None).unwrap();
|
||||
|
||||
for event in events.iter() {
|
||||
match event.token() {
|
||||
SERVER => loop {
|
||||
// Received an event for the TCP server socket, which
|
||||
// indicates we can accept an connection.
|
||||
let (mut connection, address) = match listener.accept() {
|
||||
Ok((connection, address)) => (connection, address),
|
||||
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
|
||||
// If we get a `WouldBlock` error we know our
|
||||
// listener has no more incoming connections queued,
|
||||
// so we can return to polling and wait for some
|
||||
// more.
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
// If it was any other kind of error, something went
|
||||
// wrong and we terminate with an error.
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
log::info!("Accepted connection from: {}", address);
|
||||
|
||||
let token = Self::next(&mut unique_token);
|
||||
poll.registry()
|
||||
.register(
|
||||
&mut connection,
|
||||
token,
|
||||
Interest::READABLE.add(Interest::WRITABLE),
|
||||
)
|
||||
.unwrap();
|
||||
connections.insert(token, RCONClient::new(connection));
|
||||
},
|
||||
|
||||
token => {
|
||||
let done = if let Some(client) = connections.get_mut(&token) {
|
||||
client.handle(&password).await
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if done {
|
||||
if let Some(mut client) = connections.remove(&token) {
|
||||
poll.registry().deregister(&mut client.connection)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn next(current: &mut Token) -> Token {
|
||||
let next = current.0;
|
||||
current.0 += 1;
|
||||
Token(next)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RCONClient {
|
||||
connection: TcpStream,
|
||||
logged_in: bool,
|
||||
incoming: Vec<u8>,
|
||||
closed: bool,
|
||||
}
|
||||
|
||||
impl RCONClient {
|
||||
pub fn new(connection: TcpStream) -> Self {
|
||||
Self {
|
||||
connection,
|
||||
logged_in: false,
|
||||
incoming: Vec::new(),
|
||||
closed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle(&mut self, password: &str) -> bool {
|
||||
if !self.closed {
|
||||
loop {
|
||||
match self.read_bytes() {
|
||||
// Stream closed, so we can't reply, so we just close everything.
|
||||
Ok(true) => return true,
|
||||
Ok(false) => {}
|
||||
Err(e) if e.kind() == io::ErrorKind::WouldBlock => break,
|
||||
Err(e) => {
|
||||
log::error!("could not read packet: {e}");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// If we get a close here, we might have a reply, which we still want to write.
|
||||
match self.poll(password).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
log::error!("rcon error: {e}");
|
||||
self.closed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
self.closed
|
||||
}
|
||||
|
||||
async fn poll(&mut self, password: &str) -> Result<(), PacketError> {
|
||||
loop {
|
||||
let packet = match self.receive_packet().await? {
|
||||
Some(p) => p,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
match packet.get_type() {
|
||||
PacketType::Auth => {
|
||||
let body = packet.get_body();
|
||||
if !body.is_empty() && packet.get_body() == password {
|
||||
self.send(&mut Packet::new(
|
||||
packet.get_id(),
|
||||
PacketType::AuthResponse,
|
||||
"".into(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
log::info!("RCON Client logged in successfully");
|
||||
self.logged_in = true;
|
||||
} else {
|
||||
log::warn!("RCON Client has tried wrong password");
|
||||
self.send(&mut Packet::new(-1, PacketType::AuthResponse, "".into()))
|
||||
.await
|
||||
.unwrap();
|
||||
return Err(PacketError::WrongPassword);
|
||||
}
|
||||
}
|
||||
PacketType::ExecCommand => {
|
||||
if self.logged_in {
|
||||
let mut output = Vec::new();
|
||||
handle_command(
|
||||
&mut crate::commands::CommandSender::Rcon(&mut output),
|
||||
packet.get_body(),
|
||||
);
|
||||
for line in output {
|
||||
self.send(&mut Packet::new(packet.get_id(), PacketType::Output, line))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
PacketType::Output => todo!(),
|
||||
PacketType::AuthResponse => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_bytes(&mut self) -> io::Result<bool> {
|
||||
let mut buf = [0; 1460];
|
||||
let n = self.connection.read(&mut buf)?;
|
||||
if n == 0 {
|
||||
return Ok(true);
|
||||
}
|
||||
self.incoming.extend_from_slice(&buf[..n]);
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
async fn send(&mut self, packet: &mut Packet) -> io::Result<()> {
|
||||
packet.send_packet(&mut self.connection).await
|
||||
}
|
||||
|
||||
async fn receive_packet(&mut self) -> Result<Option<Packet>, PacketError> {
|
||||
Packet::deserialize(&mut self.incoming).await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
use std::io::{self, BufRead, Cursor, Write};
|
||||
|
||||
use bytes::{BufMut, BytesMut};
|
||||
use mio::net::TcpStream;
|
||||
use thiserror::Error;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum PacketType {
|
||||
Auth,
|
||||
AuthResponse,
|
||||
ExecCommand,
|
||||
Output,
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum PacketError {
|
||||
#[error("invalid length")]
|
||||
InvalidLength,
|
||||
#[error("expected terminating NUL byte")]
|
||||
NoNullTermination,
|
||||
#[error("wrong password")]
|
||||
WrongPassword,
|
||||
}
|
||||
|
||||
impl PacketType {
|
||||
fn to_i32(self) -> i32 {
|
||||
match self {
|
||||
PacketType::Auth => 3,
|
||||
PacketType::AuthResponse => 2,
|
||||
PacketType::ExecCommand => 2,
|
||||
PacketType::Output => 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_i32(n: i32) -> PacketType {
|
||||
match n {
|
||||
3 => PacketType::Auth,
|
||||
2 => PacketType::ExecCommand,
|
||||
_ => PacketType::Output,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Packet {
|
||||
id: i32,
|
||||
ptype: PacketType,
|
||||
body: String,
|
||||
}
|
||||
|
||||
impl Packet {
|
||||
pub fn new(id: i32, ptype: PacketType, body: String) -> Packet {
|
||||
Packet { id, ptype, body }
|
||||
}
|
||||
|
||||
pub async fn send_packet(&mut self, connection: &mut TcpStream) -> io::Result<()> {
|
||||
// let len = outgoing.len() as u64;
|
||||
let mut buf = BytesMut::new();
|
||||
// 10 is for 4 bytes ty, 4 bytes id, and 2 terminating nul bytes.
|
||||
buf.put_i32_le(10 + self.get_body().len() as i32);
|
||||
buf.put_i32_le(self.id);
|
||||
buf.put_i32_le(self.get_type().to_i32());
|
||||
let bytes = self.get_body().as_bytes();
|
||||
buf.put_slice(bytes);
|
||||
buf.put_u8(0);
|
||||
buf.put_u8(0);
|
||||
connection.write(&buf).unwrap();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn deserialize(incoming: &mut Vec<u8>) -> Result<Option<Packet>, PacketError> {
|
||||
if incoming.len() < 4 {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut buf = Cursor::new(&incoming);
|
||||
let len = buf.read_i32_le().await.unwrap() + 4;
|
||||
if !(0..=1460).contains(&len) {
|
||||
return Err(PacketError::InvalidLength);
|
||||
}
|
||||
let id = buf.read_i32_le().await.unwrap();
|
||||
let ty = buf.read_i32_le().await.unwrap();
|
||||
let mut payload = vec![];
|
||||
let _ = buf.read_until(b'\0', &mut payload).unwrap();
|
||||
payload.pop();
|
||||
if buf.read_u8().await.unwrap() != 0 {
|
||||
return Err(PacketError::NoNullTermination);
|
||||
}
|
||||
if buf.position() != len as u64 {
|
||||
return Err(PacketError::InvalidLength);
|
||||
}
|
||||
incoming.drain(0..len as usize);
|
||||
|
||||
let packet = Packet {
|
||||
id,
|
||||
ptype: PacketType::from_i32(ty),
|
||||
body: String::from_utf8(payload).unwrap(),
|
||||
};
|
||||
|
||||
Ok(Some(packet))
|
||||
}
|
||||
pub fn get_body(&self) -> &str {
|
||||
&self.body
|
||||
}
|
||||
|
||||
pub fn get_type(&self) -> PacketType {
|
||||
self.ptype
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> i32 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user