From eead7e1573d0bb0b7d8d78cf4cb25a226daca1e1 Mon Sep 17 00:00:00 2001 From: xeon Date: Sun, 26 May 2024 22:08:56 +0300 Subject: [PATCH] SDKServer: config & entry structures --- Cargo.toml | 1 + assets/Application/config.json | 15 ++++ assets/Application/serverlist.json | 11 +++ .../Application}/versions.bundle | Bin sdkserver/Cargo.toml | 2 + sdkserver/src/crypto.rs | 2 +- sdkserver/src/data/config.rs | 38 +++++++++ sdkserver/src/data/entry.rs | 23 ++++++ sdkserver/src/data/mod.rs | 2 + sdkserver/src/main.rs | 1 + sdkserver/src/services/config.rs | 77 ++++++++++-------- sdkserver/src/services/entry.rs | 40 ++++----- 12 files changed, 156 insertions(+), 56 deletions(-) create mode 100644 assets/Application/config.json create mode 100644 assets/Application/serverlist.json rename {sdkserver => assets/Application}/versions.bundle (100%) create mode 100644 sdkserver/src/data/config.rs create mode 100644 sdkserver/src/data/entry.rs create mode 100644 sdkserver/src/data/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 28c7b7c..fe46f83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ lazy_static = "1.4.0" leb128 = "0.2.5" paste = "1.0.14" sysinfo = "0.30.7" +rand = "0.8.5" csv = "1.3.0" serde = { version = "1.0.197", features = ["derive"] } diff --git a/assets/Application/config.json b/assets/Application/config.json new file mode 100644 index 0000000..2f122ac --- /dev/null +++ b/assets/Application/config.json @@ -0,0 +1,15 @@ +{ + "InfoGroups": { + "StandaloneWindows64": { + "VersionInfoGroups": { + "0.1.0": { + "MinVersion": "0.1.0", + "LatestVersion": "0.1.18", + "GameResUrl": "http://127.0.0.1:21000/game_res/NAP_Publish/output_147608_1361f678bc/client/", + "DesignDataUrl": "http://127.0.0.1:21000/", + "ServerListUrl": "http://127.0.0.1:21000/design_data/NAP_Publish_AppStore_0.1.0/oversea/serverlist.bin" + } + } + } + } +} diff --git a/assets/Application/serverlist.json b/assets/Application/serverlist.json new file mode 100644 index 0000000..d9748d5 --- /dev/null +++ b/assets/Application/serverlist.json @@ -0,0 +1,11 @@ +[ + { + "sid": 142, + "serverName": "HollowPS", + "ip": "127.0.0.1", + "port": "21000", + "noticeRegion": "nap_glb_cbus01", + "protocol": "http", + "$Type": "MoleMole.ServerListInfo" + } +] diff --git a/sdkserver/versions.bundle b/assets/Application/versions.bundle similarity index 100% rename from sdkserver/versions.bundle rename to assets/Application/versions.bundle diff --git a/sdkserver/Cargo.toml b/sdkserver/Cargo.toml index d7b131a..8c141dd 100644 --- a/sdkserver/Cargo.toml +++ b/sdkserver/Cargo.toml @@ -17,6 +17,8 @@ serde.workspace = true serde_json.workspace = true tokio-util.workspace = true ansi_term.workspace = true +lazy_static.workspace = true +rand.workspace = true [[bin]] name = "nap-sdkserver" diff --git a/sdkserver/src/crypto.rs b/sdkserver/src/crypto.rs index d8b8f5e..eef6817 100644 --- a/sdkserver/src/crypto.rs +++ b/sdkserver/src/crypto.rs @@ -15,7 +15,7 @@ pub fn encrypt_config(content: &str, key: &str) -> Vec { leb128::write::unsigned(&mut out, u64::try_from(k.len()).unwrap()).unwrap(); out.extend_from_slice(k); - out.extend(0_u32.to_le_bytes()); + out.extend([0u8; 4]); out.extend(u32::try_from(content_bytes.len()).unwrap().to_le_bytes()); out.extend(content_bytes); diff --git a/sdkserver/src/data/config.rs b/sdkserver/src/data/config.rs new file mode 100644 index 0000000..2c45105 --- /dev/null +++ b/sdkserver/src/data/config.rs @@ -0,0 +1,38 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct ConfigInfo { + pub min_version: String, + pub latest_version: String, + pub game_res_url: String, + pub design_data_url: String, + pub server_list_url: String, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct ConfigInfoGroup { + pub version_info_groups: HashMap, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct AppConfig { + pub info_groups: HashMap, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ServerListInfo { + pub sid: i32, + pub server_name: String, + pub ip: String, + pub port: String, + pub notice_region: String, + pub protocol: String, + #[serde(rename = "$Type")] + ty: String, +} diff --git a/sdkserver/src/data/entry.rs b/sdkserver/src/data/entry.rs new file mode 100644 index 0000000..83b96c1 --- /dev/null +++ b/sdkserver/src/data/entry.rs @@ -0,0 +1,23 @@ +use serde::Serialize; + +#[derive(Serialize)] +#[serde(rename_all = "PascalCase")] +pub struct HttpRet { + pub error_code: i32, + pub error_msg: Option, + pub ext: Ext, +} + +#[derive(Serialize)] +#[serde(rename_all = "PascalCase")] +pub struct EntryTokenRet { + pub birthday: String, + pub country: String, + pub token: String, +} + +#[derive(Serialize)] +#[serde(rename_all = "PascalCase")] +pub struct EntryAccountServerRet { + pub address: String, +} diff --git a/sdkserver/src/data/mod.rs b/sdkserver/src/data/mod.rs new file mode 100644 index 0000000..101148b --- /dev/null +++ b/sdkserver/src/data/mod.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod entry; diff --git a/sdkserver/src/main.rs b/sdkserver/src/main.rs index d8b240f..3a5dc91 100644 --- a/sdkserver/src/main.rs +++ b/sdkserver/src/main.rs @@ -7,6 +7,7 @@ use axum::{ }; mod crypto; +mod data; mod services; use services::{auth, config, entry, errors}; diff --git a/sdkserver/src/services/config.rs b/sdkserver/src/services/config.rs index e3019f1..ffaa4d8 100644 --- a/sdkserver/src/services/config.rs +++ b/sdkserver/src/services/config.rs @@ -1,57 +1,62 @@ use axum::{body::Body, response::IntoResponse}; -use serde_json::json; +use lazy_static::lazy_static; +use rand::Rng; +use serde::de::DeserializeOwned; use crate::crypto; +use crate::data::config::*; pub const APP_CONFIG_ENDPOINT: &str = "/design_data/NAP_Publish_AppStore_0.1.0/oversea/config.bin"; pub const SERVER_LIST_ENDPOINT: &str = "/design_data/NAP_Publish_AppStore_0.1.0/oversea/serverlist.bin"; pub const VERSIONS_BUNDLE_ENDPOINT: &str = "/game_res/NAP_Publish/output_147608_1361f678bc/client/StandaloneWindows64/oversea/versions.bundle"; +lazy_static! { + static ref APP_CONFIG: AppConfig = read_config("config.json"); + static ref SERVER_LIST: Vec = read_config("serverlist.json"); + static ref VERSIONS_BUNDLE: Box<[u8]> = read_binary_data("versions.bundle"); +} + pub async fn application() -> Vec { crypto::encrypt_config( - json!({ - "InfoGroups": { - "StandaloneWindows64": { - "VersionInfoGroups": { - "0.1.0": { - "MinVersion": "0.1.0", - "LatestVersion": "0.1.0", - "GameResUrl": "http://127.0.0.1:21000/game_res/NAP_Publish/output_147608_1361f678bc/client/", - "DesignDataUrl": "http://127.0.0.1:21000/", - "ServerListUrl": "http://127.0.0.1:21000/design_data/NAP_Publish_AppStore_0.1.0/oversea/serverlist.bin", - "$Type": "Foundation.ConfigurationInfo" - }, - }, - "$Type": "Foundation.ConfigurationInfoGroup" - }, - }, - }) - .to_string() - .as_str(), - "MostSecureKey", + serde_json::to_string(&*APP_CONFIG).unwrap().as_str(), + &random_xorpad(16), ) } pub async fn server_list() -> Vec { crypto::encrypt_config( - json!([{ - "sid": 142, - "serverName": "HollowPS", - "ip": "127.0.0.1", - "port": "21000", - "noticeRegion": "nap_glb_cbus01", - "protocol": "http", - "$Type": "MoleMole.ServerListInfo" - }]) - .to_string() - .as_str(), - "MostSecureKey", + serde_json::to_string(&*SERVER_LIST).unwrap().as_str(), + &random_xorpad(16), ) } -pub const VERSION_BUNDLE: &[u8] = include_bytes!("../../versions.bundle"); - pub async fn versions_bundle() -> impl IntoResponse { - Body::from(VERSION_BUNDLE) + Body::from(&**VERSIONS_BUNDLE) +} + +fn read_config(file: &str) -> T +where + T: DeserializeOwned, +{ + let data = std::fs::read_to_string(format!("assets/Application/{file}")).unwrap(); + serde_json::from_str::(&data).unwrap() +} + +fn read_binary_data(file: &str) -> Box<[u8]> { + std::fs::read(format!("assets/Application/{file}")) + .unwrap() + .into() +} + +fn random_xorpad(len: usize) -> String { + const CHARSET: &[u8] = b"0123456789"; + + let mut rng = rand::thread_rng(); + (0..len) + .map(|_| { + let idx = rng.gen_range(0..CHARSET.len()); + CHARSET[idx] as char + }) + .collect() } diff --git a/sdkserver/src/services/entry.rs b/sdkserver/src/services/entry.rs index e667ec4..e15dd13 100644 --- a/sdkserver/src/services/entry.rs +++ b/sdkserver/src/services/entry.rs @@ -1,31 +1,33 @@ +use crate::data::entry::*; use axum::Json; -use serde_json::json; pub const ACCOUNT_TOKEN_ENDPOINT: &str = "/account/token"; pub const ACCOUNT_SERVER_ENDPOINT: &str = "/account/account_server"; #[tracing::instrument] -pub async fn account_token() -> Json { +pub async fn account_token() -> Json> { tracing::info!("account_token"); - Json(json!({ - "ErrorCode": 0, - "ErrorMsg": null, - "Ext": { - "Birthday": "01.01", - "Country": "RU", - "Token": "MostSecureTokenEver" - } - })) + + Json(HttpRet { + error_code: 0, + error_msg: None, + ext: EntryTokenRet { + birthday: String::from("01.01"), + country: String::from("RU"), + token: String::from("MostSecureTokenEver"), + }, + }) } #[tracing::instrument] -pub async fn account_server() -> Json { +pub async fn account_server() -> Json> { tracing::info!("account_server"); - Json(json!({ - "ErrorCode": 0, - "ErrorMsg": null, - "Ext": { - "Address": "127.0.0.1:10301/0" - } - })) + + Json(HttpRet { + error_code: 0, + error_msg: None, + ext: EntryAccountServerRet { + address: String::from("127.0.0.1:10301/0"), + }, + }) }