diff --git a/Cargo.lock b/Cargo.lock index 420a6d9..444ac3c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,12 +2,36 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + [[package]] name = "adler" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "bitflags" version = "2.6.0" @@ -29,12 +53,29 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "cc" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26a5c3fd7bfa1ce3897a3a3501d362b2d87b7f2583ebcb4a949ec25911025cbc" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "contracts" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9424f2ca1e42776615720e5746eed6efa19866fdbaac2923ab51c294ac4d1f2" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "crc32fast" version = "1.4.2" @@ -96,6 +137,23 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + [[package]] name = "hashbrown" version = "0.14.5" @@ -148,6 +206,7 @@ version = "0.1.0" dependencies = [ "derivative", "log", + "no_deadlocks", "rust_mc_proto", "serde_yml", "simplelog", @@ -169,6 +228,16 @@ dependencies = [ "adler", ] +[[package]] +name = "no_deadlocks" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0e0af0e4fcf0d695f224cafbf5e4fb750b85aa1cd1ad0b37d8d0d98c0d2ce5e" +dependencies = [ + "backtrace", + "vector-map", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -184,12 +253,30 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.36.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f203fa8daa7bb185f760ae12bd8e097f63d17041dcdcaf675ac54cdf863170e" +dependencies = [ + "memchr", +] + [[package]] name = "powerfmt" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dee4364d9f3b902ef14fab8a1ddffb783a1cb6b4bba3bfc1fa3922732c7de97f" +dependencies = [ + "zerocopy", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -208,6 +295,47 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + [[package]] name = "rust_mc_proto" version = "0.1.15" @@ -219,6 +347,12 @@ dependencies = [ "uuid", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustix" version = "0.38.34" @@ -385,6 +519,22 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" +[[package]] +name = "vector-map" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "550f72ae94a45c0e2139188709e6c4179f0b5ff9bdaa435239ad19048b0cd68c" +dependencies = [ + "contracts", + "rand", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "winapi-util" version = "0.1.8" @@ -466,3 +616,24 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854e949ac82d619ee9a14c66a1b674ac730422372ccb759ce0c39cabcf2bf8e6" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "125139de3f6b9d625c39e2efdd73d41bdac468ccd556556440e322be0e1bbd91" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.71", +] diff --git a/Cargo.toml b/Cargo.toml index 5dc1252..4cbf6db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ uuid = "1.10.0" log = "0.4.22" simplelog = "0.12.2" derivative = "2.2.0" +no_deadlocks = "1.3.2" diff --git a/README.md b/README.md index 06cab3e..b2e57fd 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Proxy for minecraft servers on rust `in developement` todo list: (✅ / ❌) -- ✅ (not tested) add methods `connect_to_ip`, `connect_to_server`, `connect_to_stream` +- ❌ (deadlock) add methods `connect_to_ip`, `connect_to_server`, `connect_to_stream`, `reconnect` - ❌ make setting `no_pf_for_ip_connect` working - ❌ make talk server - ❌ create bukkit plugin for player formatting support and talking diff --git a/src/meexprox/event.rs b/src/meexprox/event.rs index bdecee7..a4135c2 100644 --- a/src/meexprox/event.rs +++ b/src/meexprox/event.rs @@ -6,7 +6,6 @@ use std::{ sync::atomic::{AtomicBool, Ordering}, }; -#[derive(Debug)] pub enum ProxyEvent { /// client <- proxy <- server \ ///   | \ diff --git a/src/meexprox/meexprox.rs b/src/meexprox/meexprox.rs index 28aaefd..169601d 100644 --- a/src/meexprox/meexprox.rs +++ b/src/meexprox/meexprox.rs @@ -1,6 +1,7 @@ use super::{EventListener, PlayerForwarding, ProxyConfig, ProxyError, ProxyEvent, ProxyServer}; use derivative::Derivative; use log::{debug, info}; +use no_deadlocks::Mutex; use rust_mc_proto::{ DataBufferReader, DataBufferWriter, MinecraftConnection, Packet, ProtocolError, Zigzag, }; @@ -9,7 +10,7 @@ use std::{ net::{SocketAddr, TcpListener, TcpStream}, sync::{ atomic::{AtomicUsize, Ordering}, - Arc, Mutex, + Arc, }, thread, }; @@ -61,7 +62,7 @@ impl ProxyPlayer { } pub fn server_conn(&self) -> &MinecraftConnection { - &self.client_conn + &self.server_conn } pub fn client_conn_mut(&mut self) -> &mut MinecraftConnection { @@ -69,7 +70,7 @@ impl ProxyPlayer { } pub fn server_conn_mut(&mut self) -> &mut MinecraftConnection { - &mut self.client_conn + &mut self.server_conn } pub fn name(&self) -> Option<&String> { @@ -191,27 +192,29 @@ impl ProxyPlayer { server_address: &str, server_port: u16, ) -> Result<(), Box> { - this.lock() - .unwrap() - .connection_id - .fetch_add(1, Ordering::Relaxed); + { + let mut player = this.lock().unwrap(); + player.connection_id.fetch_add(1, Ordering::Relaxed); + player.server_conn.close(); - this.lock().unwrap().server_conn.close(); - - let server_host = this.lock().unwrap().server().unwrap().host().to_string(); - println!("connect"); - this.lock().unwrap().server_conn = MinecraftConnection::connect(&server_host)?; - println!("connected"); + let server_host = player.server().unwrap().host().to_string(); + // println!("connect"); + player.server_conn = MinecraftConnection::connect(&server_host)?; + // println!("connected"); + } thread::spawn({ - println!("connecting1"); - let player_forwarding = meexprox.lock().unwrap().config.player_forwarding().clone(); // deadlock here - println!("connecting2"); + // println!("connecting1"); + let player_forwarding = { + let meexprox_guard = meexprox.lock().unwrap(); + meexprox_guard.config.player_forwarding().clone() + }; + // println!("connecting2"); let server_address = server_address.to_string(); - println!("connecting3"); + // println!("connecting3"); move || { - println!("connecting4"); + // println!("connecting4"); let _ = ProxyPlayer::connect( this, meexprox, @@ -298,13 +301,10 @@ impl ProxyPlayer { let mut client_conn = this.lock().unwrap().client_conn.try_clone().unwrap(); let mut server_conn = this.lock().unwrap().server_conn.try_clone().unwrap(); - let server = this.lock().unwrap().server.clone(); - let addr = client_conn.get_ref().peer_addr().unwrap(); let Some(name) = this.lock().unwrap().name.clone() else { return Ok(()); }; - let server_config = meexprox.lock().unwrap().config.clone(); let atomic_connection_id = this.lock().unwrap().connection_id.clone(); let connection_id = this.lock().unwrap().connection_id.load(Ordering::Relaxed); @@ -347,7 +347,6 @@ impl ProxyPlayer { if packet.id() == 0x03 { let threshold = packet.read_isize_varint()?; - if threshold >= 0 { let threshold = threshold.zigzag();