Skip to content

Commit

Permalink
99% of this doesn't work but it's been forever since I last did anyth…
Browse files Browse the repository at this point in the history
…ing :/
  • Loading branch information
Shrecknt committed Aug 7, 2024
1 parent 3681d73 commit f6f3462
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bunger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
config = { workspace = true }
database = { workspace = true }
ram_server = { workspace = true }
flate2 = { workspace = true }
serde = { workspace = true }
Expand Down
42 changes: 20 additions & 22 deletions crates/bunger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use database::server_joins::JoinResult;
use flate2::read::ZlibDecoder;
use std::{
io::{Cursor, Read},
Expand All @@ -12,19 +13,8 @@ use varint::{AsyncVarint, SyncVarintWrite};

pub mod varint;

#[derive(Debug, Default)]
pub struct JoinData {
pub error: Option<String>,
}

impl JoinData {
pub fn new() -> Self {
Self::default()
}
}

pub async fn join(addr: SocketAddrV4, version: i32) -> JoinData {
let mut join_data = JoinData::new();
pub async fn join(addr: SocketAddrV4, version: i32, server_id: i64) -> JoinResult {
let mut join_data = JoinResult::none(server_id);

let res = join_internal(addr, version, &mut join_data).await;
if let Err(err) = res {
Expand All @@ -37,9 +27,10 @@ pub async fn join(addr: SocketAddrV4, version: i32) -> JoinData {
async fn join_internal(
addr: SocketAddrV4,
version: i32,
join_data: &mut JoinData,
_join_data: &mut JoinResult,
) -> eyre::Result<()> {
let mut stream = TcpStream::connect(addr).await?;
let compression = &mut None;

let mut packet = Vec::new();
packet.write_varint(0x00)?;
Expand All @@ -49,7 +40,7 @@ async fn join_internal(
packet.write_all(addr).await?;
packet.write_u16(42069).await?;
packet.write_varint(2)?;
write_packet(&mut stream, &packet, false).await?;
write_packet(&mut stream, &packet, compression).await?;

let mut packet = Vec::new();
packet.write_varint(0x00)?;
Expand All @@ -61,9 +52,9 @@ async fn join_internal(
let player_uuid = uuid!("36d4d63f-7268-4879-a57f-122e9df006c2").as_bytes();
packet.write_all(player_uuid).await?;
}
write_packet(&mut stream, &packet, false).await?;
write_packet(&mut stream, &packet, compression).await?;

let packet = read_packet(&mut stream, false).await?;
let packet = read_packet(&mut stream, compression).await?;
println!("got packet = {packet:?}");
match packet.0 {
0 => {
Expand All @@ -83,10 +74,14 @@ async fn join_internal(
Ok(())
}

async fn write_packet(stream: &mut TcpStream, packet: &[u8], compressed: bool) -> eyre::Result<()> {
async fn write_packet(
stream: &mut TcpStream,
packet: &[u8],
compression: &mut Option<i32>,
) -> eyre::Result<()> {
let mut to_send = Vec::new();

if compressed {
if let Some(_compression) = compression {
todo!()
} else {
to_send.write_varint(packet.len() as i32)?;
Expand All @@ -99,12 +94,15 @@ async fn write_packet(stream: &mut TcpStream, packet: &[u8], compressed: bool) -
Ok(())
}

async fn read_packet(stream: &mut TcpStream, compressed: bool) -> eyre::Result<(i32, Vec<u8>)> {
async fn read_packet(
stream: &mut TcpStream,
compression: &mut Option<i32>,
) -> eyre::Result<(i32, Vec<u8>)> {
println!("a");
let packet_length = stream.read_varint().await?;

if compressed {
let (data_length_size, data_length) = stream.read_varint_len().await?;
if let Some(_compression) = compression {
let (_data_length_size, data_length) = stream.read_varint_len().await?;
if data_length != 0 {
let mut buf = Vec::with_capacity(data_length as usize);
stream.read_exact(&mut buf).await?;
Expand Down
12 changes: 11 additions & 1 deletion crates/bunger/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
use std::{net::SocketAddrV4, str::FromStr};

use database::{server::PingResult, DatabaseConnection};

#[tokio::main]
async fn main() -> eyre::Result<()> {
let addr = SocketAddrV4::from_str("127.0.0.1:25569").unwrap();

let db = DatabaseConnection::new().await.unwrap();
let server_id = PingResult::from_ip_port(addr.ip(), addr.port(), &db.pool)
.await
.map(|res| res.id.unwrap())
.unwrap_or(0);

let mut server =
ram_server::run_server("1.8.9", 25569, false).expect("unable to start server :<");

let data = bunger::join(SocketAddrV4::from_str("127.0.0.1:25569").unwrap(), 47).await;
let data = bunger::join(addr, 47, server_id).await;

server.kill().expect("Unable to kill child process");

Expand Down
16 changes: 1 addition & 15 deletions crates/common/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub struct StatelessTcpWriteHalf {

mtu: usize,

#[cfg(not(feature = "benchmark"))]
socket: RawSocket,

pub fingerprint: Fingerprint,
Expand All @@ -94,7 +93,6 @@ pub struct StatelessTcpReadHalf {
source_port: SourcePort,

// tx: Box<dyn DataLinkSender>,
#[cfg(not(feature = "benchmark"))]
rx: Box<dyn DataLinkReceiver>,
}

Expand All @@ -115,7 +113,6 @@ impl StatelessTcp {
};

// Create a channel to receive on
#[cfg(not(feature = "benchmark"))]
let (_tx, rx) = match datalink::channel(
&interface,
Config {
Expand All @@ -135,14 +132,10 @@ impl StatelessTcp {
IpAddr::V6(_) => panic!("ipv6 not supported"),
};

#[cfg(not(feature = "benchmark"))]
let mut socket = RawSocket::new(&interface.name).unwrap();

let interface_mac = interface.mac;

#[cfg(feature = "benchmark")]
let mut mtu = 1000; // idk
#[cfg(not(feature = "benchmark"))]
let mut mtu = socket.interface_mtu().unwrap();
if interface_mac.is_some() {
mtu += ETH_HEADER_LEN;
Expand All @@ -158,7 +151,6 @@ impl StatelessTcp {
interface_mac,
mtu,

#[cfg(not(feature = "benchmark"))]
socket,

template_syn_packet: TemplatePacket::new(TemplatePacketRepr {
Expand All @@ -183,7 +175,6 @@ impl StatelessTcp {
read: StatelessTcpReadHalf {
source_port,
interface_mac,
#[cfg(not(feature = "benchmark"))]
rx,
},
write: write_half,
Expand Down Expand Up @@ -213,7 +204,6 @@ impl StatelessTcpWriteHalf {
source_port,
});

#[cfg(not(feature = "benchmark"))]
self.socket.send_blocking(packet);
}

Expand Down Expand Up @@ -305,7 +295,6 @@ impl StatelessTcpWriteHalf {
pub fn send_tcp(&mut self, repr: PacketRepr) {
let source_addr = SocketAddrV4::new(self.source_ip, repr.source_port);
let packet = build_tcp_packet(repr, self.gateway_mac, self.interface_mac, source_addr);
#[cfg(not(feature = "benchmark"))]
self.socket.send_blocking(&packet);
}
}
Expand Down Expand Up @@ -339,7 +328,6 @@ fn build_tcp_packet(

impl StatelessTcpReadHalf {
pub fn recv(&mut self) -> Option<(Ipv4, Tcp)> {
#[cfg(not(feature = "benchmark"))]
loop {
match self.rx.next() {
Ok(packet) => {
Expand All @@ -362,8 +350,6 @@ impl StatelessTcpReadHalf {
Err(_) => return None,
}
}
#[cfg(feature = "benchmark")]
None
}
}

Expand Down Expand Up @@ -401,7 +387,7 @@ fn process_ipv4(ipv4: &Ipv4Packet) -> Option<Tcp> {
}
}
IpNextHeaderProtocols::IpComp => {
warn!("Recieved an IpComp packet, but it's not supported.");
warn!("Received an IpComp packet, but it's not supported.");
None
}
_ => None,
Expand Down
2 changes: 0 additions & 2 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(lazy_cell)]

use serde::Deserialize;
use smart_default::SmartDefault;
use std::{
Expand Down
6 changes: 3 additions & 3 deletions crates/database/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ impl PlayerInfo {
uuid,
username,
java_account,
bedrock_account,
bedrock_account
) VALUES (
$2::UUID,
$3::TEXT,
$4::BOOLEAN,
$5::BOOLEAN
)
ON CONFLICT (uuid, username) DO UPDATE SET
java_account = excluded.java_account,
bedrock_account = excluded.bedrock_account
java_account = coalesce(players.java_account, false) OR excluded.java_account,
bedrock_account = coalesce(players.bedrock_account, false) OR excluded.bedrock_account
RETURNING id";
let new_id: i64 = sqlx::query(QUERY)
.bind(self.id)
Expand Down
24 changes: 22 additions & 2 deletions crates/database/src/server_joins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,28 @@ pub struct JoinResult {
pub bunger: Option<bool>,
pub kick_message: Option<String>,
pub honeypot: i8,
pub error: Option<String>,
// timestamps
pub first_joined: i64,
pub last_joined: i64,
}

impl JoinResult {
pub fn none(server_id: i64) -> Self {
Self {
id: None,
server_id,
online_mode: None,
whitelist: None,
bunger: None,
kick_message: None,
honeypot: 0b00000000,
error: None,
first_joined: 0,
last_joined: 0,
}
}

#[inline]
pub fn is_honeypot(&self) -> bool {
self.honeypot != 0
Expand Down Expand Up @@ -61,20 +77,23 @@ impl DbPush for JoinResult {
whitelist,
bunger,
kick_message,
honeypot
honeypot,
error
) VALUES (
$2::BIGINT,
$3::BOOLEAN,
$4::BOOLEAN,
$5::BOOLEAN,
$6::TEXT,
$7::BIT(8)
$7::BIT(8),
$8::TEXT
) ON CONFLICT (server_id) DO UPDATE SET
online_mode = excluded.online_mode,
whitelist = excluded.whitelist,
bunger = excluded.bunger,
kick_message = excluded.kick_message,
honeypot = excluded.honeypot,
error = excluded.error,
last_joined = EXTRACT(epoch from now())
RETURNING id";
let new_id: i64 = sqlx::query(query)
Expand All @@ -85,6 +104,7 @@ impl DbPush for JoinResult {
.bind(self.bunger)
.bind(self.kick_message.as_ref())
.bind(self.honeypot)
.bind(self.error.as_ref())
.fetch_one(pool)
.await?
.get("id");
Expand Down
2 changes: 0 additions & 2 deletions crates/mowojang/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(lazy_cell)]

use std::sync::LazyLock;
use uuid::Uuid;

Expand Down
2 changes: 1 addition & 1 deletion crates/ram_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ pub fn run_server(version: impl ToString, port: u16, online_mode: bool) -> Optio

fn run_setup() {
let path = &config::get().ram_server.temp_fs_path;
fs::remove_dir_all(path).expect("Unable to delete old temp dir");
let _ = fs::remove_dir_all(path);
fs::create_dir(path).expect("Unable to create temp dir");
}
6 changes: 4 additions & 2 deletions postgres/scanner/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ CREATE TABLE IF NOT EXISTS servers (
online_players INT,
online_anonymous_players INT,
description TEXT,
description_plain TEXT,
description_plain TEXT, -- description without formatting
enforces_secure_chat BOOLEAN,
previews_chat BOOLEAN,
ping INT, -- two way ping
geyser BOOLEAN,
discovered BIGINT NOT NULL DEFAULT EXTRACT(epoch from now()),
last_seen BIGINT NOT NULL DEFAULT EXTRACT(epoch from now()),
Expand All @@ -30,7 +31,8 @@ CREATE TABLE IF NOT EXISTS server_joins (
whitelist BOOLEAN,
bunger BOOLEAN,
kick_message TEXT,
honeypot BIT(8) NOT NULL DEFAULT B'00000000',
flags BIT(8) NOT NULL DEFAULT B'00000000',
error TEXT,
first_joined BIGINT NOT NULL DEFAULT EXTRACT(epoch from now()),
last_joined BIGINT NOT NULL DEFAULT EXTRACT(epoch from now()),
CONSTRAINT fk_server
Expand Down

0 comments on commit f6f3462

Please sign in to comment.