Colorise the inspector output based on S2C or C2S (#242)

<!-- Please make sure that your PR is aligned with the guidelines in
CONTRIBUTING.md to the best of your ability. -->
<!-- Good PRs have tests! Make sure you have sufficient test coverage.
-->

## Description

<!-- Describe the changes you've made. You may include any justification
you want here. -->
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 😄.
This commit is contained in:
Joel Ellis 2023-02-14 23:31:17 +00:00 committed by GitHub
parent 909f7d3909
commit f0027aa097
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View file

@ -7,6 +7,7 @@ description = "A simple Minecraft proxy for inspecting packets."
[dependencies] [dependencies]
anyhow = "1" anyhow = "1"
clap = { version = "4.0.30", features = ["derive"] } clap = { version = "4.0.30", features = ["derive"] }
owo-colors = "3.5.0"
regex = "1.6.0" regex = "1.6.0"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tracing-subscriber = "0.3.16" tracing-subscriber = "0.3.16"

View file

@ -7,6 +7,7 @@ use std::sync::Arc;
use anyhow::bail; use anyhow::bail;
use clap::Parser; use clap::Parser;
use owo_colors::OwoColorize;
use regex::Regex; use regex::Regex;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; use tokio::net::tcp::{OwnedReadHalf, OwnedWriteHalf};
@ -58,6 +59,7 @@ struct State {
read: OwnedReadHalf, read: OwnedReadHalf,
write: OwnedWriteHalf, write: OwnedWriteHalf,
buf: String, buf: String,
style: owo_colors::Style,
} }
impl State { impl State {
@ -104,7 +106,7 @@ impl State {
} }
} }
println!("{}", self.buf); println!("{}", self.buf.style(self.style));
Ok(pkt) Ok(pkt)
} }
@ -164,6 +166,7 @@ async fn handle_connection(client: TcpStream, cli: Arc<Cli>) -> anyhow::Result<(
read: server_read, read: server_read,
write: client_write, write: client_write,
buf: String::new(), buf: String::new(),
style: owo_colors::Style::new().purple(),
}; };
let mut c2s = State { let mut c2s = State {
@ -173,6 +176,7 @@ async fn handle_connection(client: TcpStream, cli: Arc<Cli>) -> anyhow::Result<(
read: client_read, read: client_read,
write: server_write, write: server_write,
buf: String::new(), buf: String::new(),
style: owo_colors::Style::new().green(),
}; };
let handshake: Handshake = c2s.rw_packet().await?; let handshake: Handshake = c2s.rw_packet().await?;