diff --git a/Cargo.lock b/Cargo.lock index 111b81b..379e5d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,185 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91e0c145792ef73a6ad36d27c75ac09f1832222a3c209689d90f534685ee5b7" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f09628afdcc538b57f3c6341e9c8e9970f18e4a481690a64974d7023bd33548b" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d012d2b9d65aca7f18f4d9878a045bc17899bba951561ba5ec3c2ba1eed9a061" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "mc-rcon" version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] diff --git a/Cargo.toml b/Cargo.toml index 6e5a485..198c8d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4.6.4", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index e7a11a9..924af4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,131 @@ -fn main() { - println!("Hello, world!"); +use std::{io::{Read, Write}, net::{TcpStream, ToSocketAddrs}}; +use clap::Parser; + +#[derive(Parser, Debug)] +struct Args { + #[arg(short, long)] + addr: String, + + #[arg(short, long)] + password: String, + + #[arg(short, long)] + cmd: String } + +#[derive(Clone, Copy, PartialEq)] +enum PacketType { + Login = 3, + Command = 2, + Response = 0 +} + +impl PacketType { + fn to_le_bytes(&self) -> [u8; 4] { + (*self as i32).to_le_bytes() + } +} + +impl TryFrom for PacketType { + type Error = (); + + fn try_from(value: i32) -> Result { + match value { + x if x == Self::Login as i32 => Ok(Self::Login), + x if x == Self::Command as i32 => Ok(Self::Command), + x if x == Self::Response as i32 => Ok(Self::Response), + _ => Err(()) + } + } +} + +struct RconClient { + stream: TcpStream, + next_id: i32 +} + +impl RconClient { + fn read_i32_le(&mut self) -> i32 { + let mut buf = [0u8; 4]; + self.stream.read_exact(&mut buf).unwrap(); + i32::from_le_bytes(buf) + } + + fn recv_packet(&mut self) -> (i32, PacketType, String) { + let length = self.read_i32_le(); + if length < 10 { + panic!("response length < 10"); + } + + let id = self.read_i32_le(); + let ptype = PacketType::try_from(self.read_i32_le()).unwrap(); + + let payload_len = (length - 10) as usize; + let mut payload_bytes = vec![0u8; payload_len]; + self.stream.read_exact(&mut payload_bytes).unwrap(); + + let mut nulls = [0u8; 2]; + self.stream.read_exact(&mut nulls).unwrap(); + + let payload = String::from_utf8_lossy(&payload_bytes).into_owned(); + (id, ptype, payload) + } + + fn send_packet(&mut self, ptype: PacketType, payload: &str) -> i32 { + let id = self.next_id; + self.next_id = self.next_id.wrapping_add(1); + + let payload_bytes = payload.as_bytes(); + let length = (payload_bytes.len() + 10) as i32; + + let mut packet = Vec::with_capacity(4 + length as usize); + packet.extend_from_slice(&length.to_le_bytes()); + packet.extend_from_slice(&id.to_le_bytes()); + packet.extend_from_slice(&ptype.to_le_bytes()); + packet.extend_from_slice(&payload_bytes); + packet.push(0); // terminate payload + packet.push(0); // padding + + self.stream.write_all(&packet).unwrap(); + id + } + + fn authenticate(&mut self, password: &str) { + self.send_packet(PacketType::Login, password); + + let (auth_id, _, _) = self.recv_packet(); + + if auth_id == -1 { + panic!("auth failed"); + } + } + + fn connect(addr: impl ToSocketAddrs, password: &str) -> Self { + let stream = TcpStream::connect(addr).unwrap(); + let mut client = RconClient { stream, next_id: 1 }; + client.authenticate(password); + client + } + + fn command(&mut self, cmd: &str) -> String { + let id = self.send_packet(PacketType::Command, cmd); + + let (resp_id, resp_type, payload) = self.recv_packet(); + if resp_type != PacketType::Response { + panic!("invalid response") + } + if resp_id != id { + panic!("invalid response") + } + + payload + } +} + +fn main() { + let args = Args::parse(); + + let mut client = RconClient::connect(args.addr, args.password.as_str()); + let resp = client.command(&args.cmd); + println!("{resp}"); +} \ No newline at end of file