From f0027aa097e6934b311b4f27e3ab1251e3afffbd Mon Sep 17 00:00:00 2001 From: Joel Ellis Date: Tue, 14 Feb 2023 23:31:17 +0000 Subject: [PATCH] Colorise the inspector output based on S2C or C2S (#242) ## Description I've used the popular `owo-colors` library to colourise the output of the `packet_inspector`. This makes it a lot easier to see who's sending what for debugging purposes. In future, I'd like to be able to filter out common, low-information packets like `SetPlayerPosition` and `KeepAlive` as well, but that's not in this PR. ![image](https://user-images.githubusercontent.com/28905212/218880818-69c0461c-bd23-45c4-9fd4-7c5868be4b67.png) ## Test Plan No tests - just eyeball :smile:. --- crates/packet_inspector/Cargo.toml | 1 + crates/packet_inspector/src/main.rs | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/packet_inspector/Cargo.toml b/crates/packet_inspector/Cargo.toml index c6bf521..96612f1 100644 --- a/crates/packet_inspector/Cargo.toml +++ b/crates/packet_inspector/Cargo.toml @@ -7,6 +7,7 @@ description = "A simple Minecraft proxy for inspecting packets." [dependencies] anyhow = "1" clap = { version = "4.0.30", features = ["derive"] } +owo-colors = "3.5.0" regex = "1.6.0" tokio = { version = "1", features = ["full"] } tracing-subscriber = "0.3.16" diff --git a/crates/packet_inspector/src/main.rs b/crates/packet_inspector/src/main.rs index 0446a94..4dbaa75 100644 --- a/crates/packet_inspector/src/main.rs +++ b/crates/packet_inspector/src/main.rs @@ -7,6 +7,7 @@ use std::sync::Arc; use anyhow::bail; use clap::Parser; +use owo_colors::OwoColorize; use regex::Regex; use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; @@ -58,6 +59,7 @@ struct State { read: OwnedReadHalf, write: OwnedWriteHalf, buf: String, + style: owo_colors::Style, } impl State { @@ -104,7 +106,7 @@ impl State { } } - println!("{}", self.buf); + println!("{}", self.buf.style(self.style)); Ok(pkt) } @@ -164,6 +166,7 @@ async fn handle_connection(client: TcpStream, cli: Arc) -> anyhow::Result<( read: server_read, write: client_write, buf: String::new(), + style: owo_colors::Style::new().purple(), }; let mut c2s = State { @@ -173,6 +176,7 @@ async fn handle_connection(client: TcpStream, cli: Arc) -> anyhow::Result<( read: client_read, write: server_write, buf: String::new(), + style: owo_colors::Style::new().green(), }; let handshake: Handshake = c2s.rw_packet().await?;