-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e90b0ef
commit 339585f
Showing
5 changed files
with
87 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |