Skip to content

Commit

Permalink
mostly works
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Dec 22, 2024
1 parent e90b0ef commit 339585f
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tools/antithesis-bot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ edition.workspace = true

[dependencies]
antithesis.workspace = true
bytes = "1.9.0"
dotenvy.workspace = true
envy.workspace = true
eyre.workspace = true
rand = "0.9.0-beta.1"
serde = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["full"] }
tracing-subscriber.workspace = true
tracing.workspace = true
valence_protocol.workspace = true

Expand Down
73 changes: 64 additions & 9 deletions tools/antithesis-bot/src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,70 @@
use tokio::net::TcpStream;
use bytes::BytesMut;
use eyre::{Context, eyre};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
use tracing::info;
use valence_protocol::{
Bounded, PacketDecoder, PacketEncoder, VarInt, WritePacket,
packets::handshaking::handshake_c2s::HandshakeNextState,
};

async fn launch(ip: String) -> eyre::Result<()> {
println!("Connecting to {}", ip);
/// 1.20.1
const PROTOCOL_VERSION: i32 = 763;

pub async fn launch(ip: String) -> eyre::Result<()> {
// Connect to the TCP server
let stream = TcpStream::connect(ip).await?;
println!("Connected successfully!");
let mut stream = TcpStream::connect(&ip)
.await
.wrap_err_with(|| format!("Failed to connect to {ip}"))?;

// Keep the connection alive
loop {
// TODO: Implement message handling
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let mut encoder = PacketEncoder::new();
let mut decoder = PacketDecoder::new();

// step 1: send a handshake packet
let packet = valence_protocol::packets::handshaking::HandshakeC2s {
protocol_version: VarInt(PROTOCOL_VERSION),
server_address: Bounded(&ip),
server_port: 0, // todo: probably does not matter
next_state: HandshakeNextState::Status,
};

encoder
.append_packet(&packet)
.map_err(|e| eyre!("failed to encode handshake packet: {e}"))?;

// status request
let packet = valence_protocol::packets::status::QueryRequestC2s;

encoder
.append_packet(&packet)
.map_err(|e| eyre!("failed to encode status request packet: {e}"))?;

stream
.write_all(&encoder.take())
.await
.wrap_err("failed to write handshake packet")?;

// wait for response
let mut buf = BytesMut::with_capacity(1024);

'outer: loop {
stream
.read_buf(&mut buf)
.await
.wrap_err("failed to read query response packet")?;

info!("read {} bytes", buf.len());

decoder.queue_bytes(buf.split());

if let Some(packet) = decoder
.try_next_packet()
.map_err(|e| eyre!("failed to decode packet: {e}"))?
{
tracing::info!("packet\n{packet:#?}");
break 'outer Ok(());
}
}
}
13 changes: 10 additions & 3 deletions tools/antithesis-bot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ mod bot;
#[derive(Deserialize, Debug)]
pub struct LaunchArguments {
ip: String,

#[serde(default = "default_bot_count")]
bot_count: u32,
}

pub fn bootstrap(args: LaunchArguments) {
const fn default_bot_count() -> u32 {
1
}

pub async fn start(args: LaunchArguments) -> eyre::Result<()> {
const UNUSUALLY_HIGH_BOT_THRESHOLD: u32 = 1_000;

tracing::info!("args = {args:?}");
Expand All @@ -21,6 +27,7 @@ pub fn bootstrap(args: LaunchArguments) {
tracing::warn!("bot_count {bot_count} is unusually high. This may cause issues.");
}

let sender: char = AntithesisRng.random();
let mut bot = antithesis::Bot::new(ip, bot_count);
bot::launch(ip).await?;

Ok(())
}
19 changes: 9 additions & 10 deletions tools/antithesis-bot/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use antithesis_bot::LaunchArguments;
use eyre::Result;
use serde::Deserialize;
use tracing::{info, warn};

#[derive(Deserialize)]
struct Args {
ip: String,
}

fn main() -> Result<()> {
#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::fmt::init();
// Load .env file first
if let Err(e) = dotenvy::dotenv() {
warn!("Failed to load .env file: {}", e);
}

// Deserialize environment variables into the struct
let args: Args = envy::from_env()?;
info!(?args.ip, "Using IP address");
let args: LaunchArguments = envy::from_env()?;

antithesis_bot::start(args).await?;

Ok(())
}
}

0 comments on commit 339585f

Please sign in to comment.