mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-18 20:30:23 -07:00
Add proper logging
This commit is contained in:
parent
fe9f5dc936
commit
1de68e7335
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
|||||||
|
RUST_LOG=info
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
|
.env
|
||||||
/target
|
/target
|
||||||
|
|
||||||
# Test server
|
# Test server
|
||||||
|
102
Cargo.lock
generated
102
Cargo.lock
generated
@ -8,6 +8,26 @@ version = "1.0.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "0.7.18"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "atty"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "0.1.7"
|
version = "0.1.7"
|
||||||
@ -62,6 +82,25 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dotenv"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.7.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "flate2"
|
name = "flate2"
|
||||||
version = "1.0.22"
|
version = "1.0.22"
|
||||||
@ -162,6 +201,15 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "1.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
|
||||||
|
dependencies = [
|
||||||
|
"quick-error",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "0.4.8"
|
version = "0.4.8"
|
||||||
@ -173,9 +221,12 @@ name = "lazymc"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"dotenv",
|
||||||
"futures",
|
"futures",
|
||||||
"libc",
|
"libc",
|
||||||
|
"log",
|
||||||
"minecraft-protocol",
|
"minecraft-protocol",
|
||||||
|
"pretty_env_logger",
|
||||||
"rand 0.8.4",
|
"rand 0.8.4",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
@ -316,6 +367,16 @@ version = "0.2.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
|
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pretty_env_logger"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
|
||||||
|
dependencies = [
|
||||||
|
"env_logger",
|
||||||
|
"log",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.32"
|
version = "1.0.32"
|
||||||
@ -325,6 +386,12 @@ dependencies = [
|
|||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quick-error"
|
||||||
|
version = "1.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.10"
|
version = "1.0.10"
|
||||||
@ -489,6 +556,23 @@ dependencies = [
|
|||||||
"rand_core 0.3.1",
|
"rand_core 0.3.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.5.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.6.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ryu"
|
name = "ryu"
|
||||||
version = "1.0.5"
|
version = "1.0.5"
|
||||||
@ -546,6 +630,15 @@ dependencies = [
|
|||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.13.0"
|
version = "1.13.0"
|
||||||
@ -614,6 +707,15 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||||
|
dependencies = [
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
@ -25,3 +25,7 @@ libc = "0.2"
|
|||||||
minecraft-protocol = { git = "https://github.com/timvisee/minecraft-protocol", rev = "c578492" }
|
minecraft-protocol = { git = "https://github.com/timvisee/minecraft-protocol", rev = "c578492" }
|
||||||
rand = "0.8"
|
rand = "0.8"
|
||||||
tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal"] }
|
tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal"] }
|
||||||
|
|
||||||
|
dotenv = "0.15"
|
||||||
|
log = "0.4"
|
||||||
|
pretty_env_logger = "0.4"
|
||||||
|
27
src/main.rs
27
src/main.rs
@ -1,3 +1,6 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
|
||||||
pub(crate) mod config;
|
pub(crate) mod config;
|
||||||
pub(crate) mod monitor;
|
pub(crate) mod monitor;
|
||||||
pub(crate) mod protocol;
|
pub(crate) mod protocol;
|
||||||
@ -28,7 +31,11 @@ use server::ServerState;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), ()> {
|
async fn main() -> Result<(), ()> {
|
||||||
println!(
|
// Initialize logging
|
||||||
|
let _ = dotenv::dotenv();
|
||||||
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
info!(
|
||||||
"Proxying public {} to internal {}",
|
"Proxying public {} to internal {}",
|
||||||
ADDRESS_PUBLIC, ADDRESS_PROXY,
|
ADDRESS_PUBLIC, ADDRESS_PROXY,
|
||||||
);
|
);
|
||||||
@ -62,7 +69,7 @@ async fn main() -> Result<(), ()> {
|
|||||||
// When server is not online, spawn a status server
|
// When server is not online, spawn a status server
|
||||||
let transfer = status_server(client, inbound, server_state.clone()).map(|r| {
|
let transfer = status_server(client, inbound, server_state.clone()).map(|r| {
|
||||||
if let Err(err) = r {
|
if let Err(err) = r {
|
||||||
println!("Failed to serve status: {:?}", err);
|
error!("Failed to serve status: {:?}", err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,7 +78,7 @@ async fn main() -> Result<(), ()> {
|
|||||||
// When server is online, proxy all
|
// When server is online, proxy all
|
||||||
let transfer = proxy(inbound, ADDRESS_PROXY.to_string()).map(|r| {
|
let transfer = proxy(inbound, ADDRESS_PROXY.to_string()).map(|r| {
|
||||||
if let Err(err) = r {
|
if let Err(err) = r {
|
||||||
println!("Failed to proxy: {:?}", err);
|
error!("Failed to proxy: {:?}", err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -110,8 +117,8 @@ pub async fn read_packet<'a>(
|
|||||||
let (consumed, len) = match types::read_var_int(&buf) {
|
let (consumed, len) = match types::read_var_int(&buf) {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Failed to read packet length, should retry!");
|
error!("Failed to read packet length, should retry!");
|
||||||
eprintln!("{:?}", (&buf).as_ref());
|
error!("{:?}", (&buf).as_ref());
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -161,7 +168,7 @@ async fn status_server(
|
|||||||
Ok(Some(packet)) => packet,
|
Ok(Some(packet)) => packet,
|
||||||
Ok(None) => break,
|
Ok(None) => break,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Closing connection, error occurred");
|
error!("Closing connection, error occurred");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -243,10 +250,10 @@ async fn status_server(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// // Show unhandled packet warning
|
// Show unhandled packet warning
|
||||||
// eprintln!("Received unhandled packet:");
|
debug!("Received unhandled packet:");
|
||||||
// eprintln!("- State: {:?}", client.state());
|
debug!("- State: {:?}", client.state());
|
||||||
// eprintln!("- Packet ID: {}", packet.id);
|
debug!("- Packet ID: {}", packet.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gracefully close connection
|
// Gracefully close connection
|
||||||
|
@ -35,7 +35,7 @@ pub async fn poll_server(addr: SocketAddr) -> bool {
|
|||||||
/// Monitor server.
|
/// Monitor server.
|
||||||
pub async fn monitor_server(addr: SocketAddr, state: Arc<ServerState>) {
|
pub async fn monitor_server(addr: SocketAddr, state: Arc<ServerState>) {
|
||||||
loop {
|
loop {
|
||||||
// TODO: eprintln!("Polling {} ... ", addr);
|
trace!("Polling {} ... ", addr);
|
||||||
let online = poll_server(addr).await;
|
let online = poll_server(addr).await;
|
||||||
|
|
||||||
state.set_online(online);
|
state.set_online(online);
|
||||||
|
@ -42,7 +42,7 @@ impl ServerState {
|
|||||||
/// Kill any running server.
|
/// Kill any running server.
|
||||||
pub fn kill_server(&self) -> bool {
|
pub fn kill_server(&self) -> bool {
|
||||||
if let Some(pid) = *self.pid.lock().unwrap() {
|
if let Some(pid) = *self.pid.lock().unwrap() {
|
||||||
eprintln!("[lazymc] Sending kill signal to server");
|
warn!("Sending kill signal to server");
|
||||||
kill_gracefully(pid);
|
kill_gracefully(pid);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -60,13 +60,13 @@ impl ServerState {
|
|||||||
pub async fn start(state: Arc<ServerState>) -> Result<(), Box<dyn std::error::Error>> {
|
pub async fn start(state: Arc<ServerState>) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let mut cmd = Command::new(SERVER_CMD);
|
let mut cmd = Command::new(SERVER_CMD);
|
||||||
|
|
||||||
eprintln!("[lazymc] Starting server...");
|
info!("Starting server...");
|
||||||
let mut child = cmd.spawn()?;
|
let mut child = cmd.spawn()?;
|
||||||
|
|
||||||
state.set_pid(Some(child.id().expect("unknown server PID")));
|
state.set_pid(Some(child.id().expect("unknown server PID")));
|
||||||
|
|
||||||
let status = child.wait().await?;
|
let status = child.wait().await?;
|
||||||
eprintln!("[lazymc] Server stopped (status: {})\n", status);
|
info!("Server stopped (status: {})\n", status);
|
||||||
|
|
||||||
// Reset online and starting state
|
// Reset online and starting state
|
||||||
// TODO: also set this when returning early due to error
|
// TODO: also set this when returning early due to error
|
||||||
|
Loading…
x
Reference in New Issue
Block a user