diff --git a/sdkserver/.env b/sdkserver/.env deleted file mode 100644 index fc74ce2..0000000 --- a/sdkserver/.env +++ /dev/null @@ -1,4 +0,0 @@ -ASSET_BUNDLE_URL=https://autopatchcn.bhsr.com/asb/BetaLive/output_6744505_89b2f5dc973e -EX_RESOURCE_URL=https://autopatchcn.bhsr.com/design_data/BetaLive/output_6759713_b4e0e740f0da -LUA_URL=https://autopatchcn.bhsr.com/lua/BetaLive/output_6755976_3c46d7c46e2c -LUA_VERSION=6755976 \ No newline at end of file diff --git a/sdkserver/Cargo.toml b/sdkserver/Cargo.toml index 11836b4..4354435 100644 --- a/sdkserver/Cargo.toml +++ b/sdkserver/Cargo.toml @@ -13,6 +13,8 @@ axum-server.workspace = true dirs.workspace = true dotenv.workspace = true +lazy_static.workspace = true + serde.workspace = true serde_json.workspace = true diff --git a/sdkserver/src/config/mod.rs b/sdkserver/src/config/mod.rs new file mode 100644 index 0000000..9b7de45 --- /dev/null +++ b/sdkserver/src/config/mod.rs @@ -0,0 +1,3 @@ +mod version_config; + +pub use version_config::INSTANCE as versions; diff --git a/sdkserver/src/config/version_config.rs b/sdkserver/src/config/version_config.rs new file mode 100644 index 0000000..5ede108 --- /dev/null +++ b/sdkserver/src/config/version_config.rs @@ -0,0 +1,40 @@ +use std::collections::HashMap; + +use lazy_static::lazy_static; +use serde::Deserialize; +use serde_json::from_str; + +const DEFAULT_VERSIONS: &str = include_str!("../../versions.json"); + +#[derive(Deserialize)] +pub struct VersionConfig { + pub asset_bundle_url: String, + pub ex_resource_url: String, + pub lua_url: String, + pub lua_version: String, +} + +lazy_static! { + pub static ref INSTANCE: HashMap = { + let local_config = std::path::Path::new("versions.json"); + let data = if local_config.exists() { + std::fs::read_to_string("versions.json").unwrap() + } else { + let config = dirs::config_dir() + .expect("No config directory found") + .join("hkrpg-sdkserver"); + + std::fs::create_dir_all(&config).unwrap(); + + let env = config.join("versions.json"); + + if !env.exists() { + std::fs::write(&env, DEFAULT_VERSIONS).unwrap(); + } + + std::fs::read_to_string(&env).unwrap() + }; + + from_str(&data).unwrap() + }; +} diff --git a/sdkserver/src/main.rs b/sdkserver/src/main.rs index 724c066..765c716 100644 --- a/sdkserver/src/main.rs +++ b/sdkserver/src/main.rs @@ -3,19 +3,17 @@ use axum::routing::{get, post}; use axum::Router; use logging::init_tracing; use services::{auth, dispatch, errors}; -use std::path::Path; use tracing::Level; +mod config; mod logging; mod services; const PORT: u16 = 21000; -const DEFAULT_DOTENV: &str = include_str!("../.env"); #[tokio::main] async fn main() -> Result<()> { init_tracing(); - init_config()?; let span = tracing::span!(Level::DEBUG, "main"); let _ = span.enter(); @@ -52,26 +50,3 @@ async fn main() -> Result<()> { Ok(()) } - -fn init_config() -> Result<()> { - let local_dotenv = Path::new(".env"); - if local_dotenv.exists() { - dotenv::dotenv()?; - } else { - let config = dirs::config_dir() - .ok_or_else(|| anyhow::anyhow!("No config directory found"))? - .join("hkrpg-sdkserver"); - - std::fs::create_dir_all(&config)?; - - let env = config.join(".env"); - - if !env.exists() { - std::fs::write(&env, DEFAULT_DOTENV)?; - } - - dotenv::from_path(&env)?; - } - - Ok(()) -} diff --git a/sdkserver/src/services/dispatch.rs b/sdkserver/src/services/dispatch.rs index a79e8e5..57f080b 100644 --- a/sdkserver/src/services/dispatch.rs +++ b/sdkserver/src/services/dispatch.rs @@ -1,7 +1,8 @@ -use std::env; - +use crate::config::versions; +use axum::extract::Query; use prost::Message; use proto::{Dispatch, Gateserver, RegionInfo}; +use serde::Deserialize; pub const QUERY_DISPATCH_ENDPOINT: &str = "/query_dispatch"; pub const QUERY_GATEWAY_ENDPOINT: &str = "/query_gateway"; @@ -26,32 +27,45 @@ pub async fn query_dispatch() -> String { rbase64::encode(&buff) } +#[derive(Deserialize, Debug)] +pub struct QueryGatewayParameters { + pub version: String, +} + #[tracing::instrument] -pub async fn query_gateway() -> String { - let rsp = Gateserver { - retcode: 0, - ip: String::from("127.0.0.1"), - port: 23301, - asset_bundle_url: env::var("ASSET_BUNDLE_URL").unwrap(), - ex_resource_url: env::var("EX_RESOURCE_URL").unwrap(), - lua_url: env::var("LUA_URL").unwrap(), - lua_version: env::var("LUA_VERSION").unwrap(), - ifix_version: String::from("0"), - jblkncaoiao: true, - hjdjakjkdbi: true, - ldknmcpffim: true, - feehapamfci: true, - eebfeohfpph: true, - dfmjjcfhfea: true, - najikcgjgan: true, - giddjofkndm: true, - fbnbbembcgn: false, - dedgfjhbnok: false, - use_tcp: true, - linlaijbboh: false, - ahmbfbkhmgh: false, - nmdccehcdcc: false, - ..Default::default() +pub async fn query_gateway(parameters: Query) -> String { + let rsp = if let Some(config) = versions.get(¶meters.version) { + Gateserver { + retcode: 0, + ip: String::from("127.0.0.1"), + port: 23301, + asset_bundle_url: config.asset_bundle_url.clone(), + ex_resource_url: config.ex_resource_url.clone(), + lua_url: config.lua_url.clone(), + lua_version: config.lua_version.clone(), + ifix_version: String::from("0"), + jblkncaoiao: true, + hjdjakjkdbi: true, + ldknmcpffim: true, + feehapamfci: true, + eebfeohfpph: true, + dfmjjcfhfea: true, + najikcgjgan: true, + giddjofkndm: true, + fbnbbembcgn: false, + dedgfjhbnok: false, + use_tcp: true, + linlaijbboh: false, + ahmbfbkhmgh: false, + nmdccehcdcc: false, + ..Default::default() + } + } else { + Gateserver { + retcode: 9, + msg: format!("forbidden version: {} or invalid bind", parameters.version), + ..Default::default() + } }; let mut buff = Vec::new(); diff --git a/sdkserver/versions.json b/sdkserver/versions.json new file mode 100644 index 0000000..6ba2be5 --- /dev/null +++ b/sdkserver/versions.json @@ -0,0 +1,8 @@ +{ + "CNBETAWin2.1.51": { + "asset_bundle_url": "https://autopatchcn.bhsr.com/asb/BetaLive/output_6744505_89b2f5dc973e", + "ex_resource_url": "https://autopatchcn.bhsr.com/design_data/BetaLive/output_6759713_b4e0e740f0da", + "lua_url": "https://autopatchcn.bhsr.com/lua/BetaLive/output_6755976_3c46d7c46e2c", + "lua_version": "6755976" + } +}