diff --git a/Cargo.lock b/Cargo.lock index 1efae70..1377ae0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -825,6 +825,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a" + [[package]] name = "httparse" version = "1.9.4" @@ -1006,6 +1012,15 @@ dependencies = [ "tracing", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "matchit" version = "0.7.3" @@ -1043,6 +1058,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1462,8 +1487,17 @@ checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -1474,9 +1508,15 @@ checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.4", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.4" @@ -1725,6 +1765,7 @@ dependencies = [ "rbase64", "serde", "thiserror", + "tower-http", "tracing", ] @@ -2273,6 +2314,31 @@ dependencies = [ "tracing", ] +[[package]] +name = "tower-http" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + [[package]] name = "tower-layer" version = "0.3.3" @@ -2335,10 +2401,14 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", ] @@ -2349,6 +2419,12 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicase" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/Cargo.toml b/Cargo.toml index eb2223b..2ebd4da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ version = "0.1.0" [workspace.dependencies] # Framework tokio = { version = "1.39.3", features = ["full"] } +tower-http = { version = "0.6.1", features = ["fs", "trace"] } axum = "0.7.5" axum-server = "0.7.1" zeromq = { version = "0.4.0", default-features = false, features = ["tokio-runtime", "tcp-transport"] } @@ -41,18 +42,18 @@ crc32fast = "1.4.2" # Tracing tracing = "0.1.40" -tracing-subscriber = "0.3.18" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } # Internal kcp = { path = "gateway-server/kcp" } -common = { path = "common/" } -shorekeeper-http = { path = "shorekeeper-http/" } -shorekeeper-data = { path = "shorekeeper-data/" } -shorekeeper-database = { path = "shorekeeper-database/" } -shorekeeper-network = { path = "shorekeeper-network/" } -shorekeeper-protocol = { path = "shorekeeper-protocol/" } +common = { path = "common" } +shorekeeper-http = { path = "shorekeeper-http" } +shorekeeper-data = { path = "shorekeeper-data" } +shorekeeper-database = { path = "shorekeeper-database" } +shorekeeper-network = { path = "shorekeeper-network" } +shorekeeper-protocol = { path = "shorekeeper-protocol" } shorekeeper-protocol-derive = { path = "shorekeeper-protocol/shorekeeper-protocol-derive" } -shorekeeper-protokey = { path = "shorekeeper-protokey/" } +shorekeeper-protokey = { path = "shorekeeper-protokey" } [profile.release] strip = true # Automatically strip symbols from the binary. diff --git a/assets/config/samples/cn_live_1.3.0.json b/assets/config/e66lKLx7RnUf9QlkMCGaW0jdyjRV1weo/index.json similarity index 100% rename from assets/config/samples/cn_live_1.3.0.json rename to assets/config/e66lKLx7RnUf9QlkMCGaW0jdyjRV1weo/index.json diff --git a/assets/config/index.json b/assets/config/fcQrWOPC2RkghpzsAE2kyGWD37QTm2mR/index.json similarity index 100% rename from assets/config/index.json rename to assets/config/fcQrWOPC2RkghpzsAE2kyGWD37QTm2mR/index.json diff --git a/assets/config/rODM5DcqOhYsIOtsEuZWNGFa2guZgl57/index.json b/assets/config/rODM5DcqOhYsIOtsEuZWNGFa2guZgl57/index.json new file mode 100644 index 0000000..5823ac1 --- /dev/null +++ b/assets/config/rODM5DcqOhYsIOtsEuZWNGFa2guZgl57/index.json @@ -0,0 +1,64 @@ +{ + "default": { + "CdnUrl": [ + { + "url": "https://cdn-huoshan-cn-mc.aki-game.com/prod/client/", + "weight": "2323" + }, + { + "url": "https://cdn-qiniu-cn-mc.aki-game.com/prod/client/", + "weight": "0" + }, + { + "url": "https://cdn-qcloud-cn-mc.aki-game.com/prod/client/", + "weight": "5443" + }, + { + "url": "https://cdn-aliyun-cn-mc.aki-game.com/prod/client/", + "weight": "0" + } + ], + "SecondaryUrl": [], + "PriceRatio": 1, + "SpeedRatio": 1, + "GachaUrl": { + "GachaRecord": "http://127.0.0.1:10001/gacha/record", + "GachaPoolDetail": "http://127.0.0.1:10001/gacha/detail" + }, + "LogReport": { + "name": "pioneer-upload-log-1319073642", + "region": "dev-reversedrooms" + }, + "PackageUpdateDescUrl": { + "MainUrl": "http://127.0.0.1:10001/force_update/UpdateDesc.html", + "SubUrl": "http://127.0.0.1:10001/force_update/UpdateDesc.html" + }, + "PackageUpdateUrl": { + "MainUrl": "http://127.0.0.1:10001/force_update/UpdateJs.html", + "SubUrl": "http://127.0.0.1:10001/force_update/UpdateJs.html" + }, + "TDCfg": { + "AppID": "3e2e647670b7498fa645eb9574f78c2c", + "URL": "http://127.0.0.1:10001/TDCfg" + }, + "GmOpen": false, + "IosAuditFirstDownloadTip": false, + "NoticeUrl": "http://127.0.0.1:10001/notice", + "MixUri": "rODM5DcqOhYsIOtsEuZWNGFa2guZgl57", + "ResUri": "rODM5DcqOhYsIOtsEuZWNGFa2guZgl57", + "LoginServers": [ + { + "id": "f9e0fc655c1931bc03ad976e9fc14473", + "ip": "http://127.0.0.1:5500", + "name": "Shorekeeper" + } + ], + "PrivateServers": { + "enable": false, + "serverUrl": "" + } + }, + "p1": { + "IosAuditFirstDownloadTip": false + } +} diff --git a/assets/config/samples/cn_beta_1.3.0.json b/assets/config/samples/cn_beta_1.3.0.json deleted file mode 100644 index f1cdfd6..0000000 --- a/assets/config/samples/cn_beta_1.3.0.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "default": { - "CdnUrl": [ - { - "url": "https://cdn-huoshan-cn-mc.aki-game.com/prod/client/", - "weight": "2323" - }, - { - "url": "https://cdn-qiniu-cn-mc.aki-game.com/prod/client/", - "weight": "0" - }, - { - "url": "https://cdn-qcloud-cn-mc.aki-game.com/prod/client/", - "weight": "5443" - }, - { - "url": "https://cdn-aliyun-cn-mc.aki-game.com/prod/client/", - "weight": "0" - } - ], - "SecondaryUrl": [], - "PriceRatio": 1, - "SpeedRatio": 1, - "GachaUrl": { - "GachaRecord": "http://127.0.0.1:10001/gacha/record", - "GachaPoolDetail": "http://127.0.0.1:10001/gacha/detail" - }, - "LogReport": { - "name": "pioneer-upload-log-1319073642", - "region": "dev-reversedrooms" - }, - "PackageUpdateDescUrl": { - "MainUrl": "http://127.0.0.1:10001/force_update/UpdateDesc.html", - "SubUrl": "http://127.0.0.1:10001/force_update/UpdateDesc.html" - }, - "PackageUpdateUrl": { - "MainUrl": "http://127.0.0.1:10001/force_update/UpdateJs.html", - "SubUrl": "http://127.0.0.1:10001/force_update/UpdateJs.html" - }, - "TDCfg": { - "AppID": "3e2e647670b7498fa645eb9574f78c2c", - "URL": "http://127.0.0.1:10001/TDCfg" - }, - "GmOpen": false, - "IosAuditFirstDownloadTip": false, - "NoticeUrl": "http://127.0.0.1:10001/notice", - "MixUri": "rODM5DcqOhYsIOtsEuZWNGFa2guZgl57", - "ResUri": "rODM5DcqOhYsIOtsEuZWNGFa2guZgl57", - "LoginServers": [ - { - "id": "f9e0fc655c1931bc03ad976e9fc14473", - "ip": "http://127.0.0.1:5500", - "name": "Shorekeeper" - } - ], - "PrivateServers": { - "enable": false, - "serverUrl": "" - } - }, - "p1": { - "IosAuditFirstDownloadTip": false - } -} diff --git a/assets/config/samples/os_live_1.3.0.json b/assets/config/u4S3uXaif7gNKnNGnbZ6WnW67vWFmQgd/index.json similarity index 100% rename from assets/config/samples/os_live_1.3.0.json rename to assets/config/u4S3uXaif7gNKnNGnbZ6WnW67vWFmQgd/index.json diff --git a/common/src/logging.rs b/common/src/logging.rs index 840074f..de76dd5 100644 --- a/common/src/logging.rs +++ b/common/src/logging.rs @@ -1,4 +1,7 @@ use tracing::Level; +use tracing_subscriber::fmt::writer::MakeWriterExt; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; pub fn init(max_level: Level) { tracing_subscriber::fmt() @@ -6,3 +9,24 @@ pub fn init(max_level: Level) { .with_target(false) .init(); } + +pub fn init_axum(max_level: Level) { + tracing_subscriber::registry() + .with( + tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { + // axum logs rejections from built-in extractors with the `axum::rejection` + // target, at `TRACE` level. `axum::rejection=trace` enables showing those events + format!( + "{}=debug,tower_http=debug,axum::rejection=trace", + env!("CARGO_CRATE_NAME") + ) + .into() + }), + ) + .with( + tracing_subscriber::fmt::layer() + .with_writer(std::io::stdout.with_max_level(max_level)) + .with_target(false) + ) + .init(); +} \ No newline at end of file diff --git a/config-server/src/main.rs b/config-server/src/main.rs index b5554f3..218dc82 100644 --- a/config-server/src/main.rs +++ b/config-server/src/main.rs @@ -1,4 +1,3 @@ -use std::fs; use std::sync::LazyLock; use anyhow::Result; @@ -26,20 +25,14 @@ async fn main() -> Result<()> { LazyLock::new(|| config_util::load_or_create("configserver.toml")); ::common::splash::print_splash(); - ::common::logging::init(::tracing::Level::DEBUG); + ::common::logging::init_axum(::tracing::Level::DEBUG); Application::new() - .get("/index.json", get_index) + .serve_dir("/", "assets/config") .with_encryption(&CONFIG.encryption) + .with_logger() .serve(&CONFIG.network) .await?; Ok(()) } - -async fn get_index() -> &'static str { - static INDEX: LazyLock = - LazyLock::new(|| fs::read_to_string("assets/config/index.json").unwrap()); - - &*INDEX -} diff --git a/shorekeeper-http/Cargo.toml b/shorekeeper-http/Cargo.toml index 68e081c..f783741 100644 --- a/shorekeeper-http/Cargo.toml +++ b/shorekeeper-http/Cargo.toml @@ -7,6 +7,7 @@ version.workspace = true # Framework axum.workspace = true axum-server.workspace = true +tower-http.workspace = true # Cryptography aes.workspace = true diff --git a/shorekeeper-http/src/lib.rs b/shorekeeper-http/src/lib.rs index 9ae3725..2b880bf 100644 --- a/shorekeeper-http/src/lib.rs +++ b/shorekeeper-http/src/lib.rs @@ -1,9 +1,11 @@ -use axum::{handler::Handler, middleware::map_response_with_state, routing, Router}; -use config::{AesSettings, NetworkSettings}; - +use axum::{handler::Handler, middleware::map_response_with_state, Router, routing}; pub use axum::extract::{Path, Query, State}; pub use axum::http::StatusCode; pub use axum::response::Json; +use tower_http::services::ServeDir; +use tower_http::trace::TraceLayer; + +use config::{AesSettings, NetworkSettings}; pub mod config; mod encryption; @@ -56,6 +58,11 @@ impl Application { self } + pub fn serve_dir(mut self, path: &str, dir: &str) -> Self { + self.router = self.router.nest_service(path, ServeDir::new(dir)); + self + } + pub fn with_encryption(mut self, aes_settings: &'static AesSettings) -> Self { self.router = self.router.layer(map_response_with_state( aes_settings, @@ -64,6 +71,11 @@ impl Application { self } + pub fn with_logger(mut self) -> Self { + self.router = self.router.layer(TraceLayer::new_for_http()); + self + } + pub async fn serve(self, settings: &NetworkSettings) -> Result<(), Error> { let http_addr = settings.http_addr.parse().map_err(|_| Error::InvalidAddr)?;