From 42bdc28d0f41fd50a8961a6b00f170aee2d0aef6 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien <77750390+ethanaobrien@users.noreply.github.com> Date: Sat, 4 May 2024 18:11:20 -0500 Subject: [PATCH] Add endpoint to set server time --- src/main.rs | 3 ++- src/router/card.rs | 6 ++--- src/router/chat.rs | 10 ++++---- src/router/clear_rate.rs | 8 +++--- src/router/debug.rs | 4 +-- src/router/event.rs | 6 ++--- src/router/exchange.rs | 6 ++--- src/router/friend.rs | 16 ++++++------ src/router/global.rs | 29 +++++++++++++++++---- src/router/home.rs | 8 +++--- src/router/live.rs | 26 +++++++++---------- src/router/login.rs | 6 ++--- src/router/lottery/mod.rs | 10 ++++---- src/router/mission.rs | 8 +++--- src/router/notice.rs | 8 +++--- src/router/purchase.rs | 4 +-- src/router/serial_code.rs | 8 +++--- src/router/shop.rs | 4 +-- src/router/start.rs | 4 +-- src/router/story.rs | 4 +-- src/router/tutorial.rs | 2 +- src/router/user.rs | 52 +++++++++++++++++++------------------- src/router/userdata/mod.rs | 30 ++++++++++++++++++++++ src/router/webui.rs | 13 ++++++++++ 24 files changed, 169 insertions(+), 106 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8995307..68cd3b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn unhandled(req: HttpRequest, body: String) -> HttpResponse { "server_time": router::global::timestamp(), "data": "" }; - router::global::send(resp) + router::global::send(resp, req) } async fn request(req: HttpRequest, body: String) -> HttpResponse { @@ -94,6 +94,7 @@ async fn request(req: HttpRequest, body: String) -> HttpResponse { "/api/webui/login" => router::webui::login(req, body), "/api/webui/startLoginbonus" => router::webui::start_loginbonus(req, body), "/api/webui/import" => router::webui::import(req, body), + "/api/webui/set_time" => router::webui::set_time(req, body), "/api/user/getregisteredplatformlist" => router::user::getregisteredplatformlist(req, body), "/api/user/sif/migrate" => router::user::sif_migrate(req, body), "/api/user/ss/migrate" => router::user::sifas_migrate(req, body), diff --git a/src/router/card.rs b/src/router/card.rs index 5ba4a5f..d855f91 100644 --- a/src/router/card.rs +++ b/src/router/card.rs @@ -54,7 +54,7 @@ pub fn reinforce(req: HttpRequest, body: String) -> HttpResponse { clear_mission_ids: [] } }; - global::send(resp) + global::send(resp, req) } pub fn skill_reinforce(req: HttpRequest, body: String) -> HttpResponse { @@ -76,7 +76,7 @@ pub fn skill_reinforce(req: HttpRequest, body: String) -> HttpResponse { clear_mission_ids: [] } }; - global::send(resp) + global::send(resp, req) } pub fn evolve(req: HttpRequest, body: String) -> HttpResponse { @@ -98,5 +98,5 @@ pub fn evolve(req: HttpRequest, body: String) -> HttpResponse { clear_mission_ids: [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/chat.rs b/src/router/chat.rs index 0b8f6eb..c10e638 100644 --- a/src/router/chat.rs +++ b/src/router/chat.rs @@ -31,25 +31,25 @@ pub fn home(req: HttpRequest, body: String) -> HttpResponse { "master_chat_attachment_ids": [] } }; - global::send(resp) + global::send(resp, req) } -pub fn start(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn start(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": {"select_talk_id_list":[],"get_item_list":[],"is_read":0} }; - global::send(resp) + global::send(resp, req) } -pub fn end(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn end(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/clear_rate.rs b/src/router/clear_rate.rs index fb262ca..f20a913 100644 --- a/src/router/clear_rate.rs +++ b/src/router/clear_rate.rs @@ -215,16 +215,16 @@ fn get_clearrate_json() -> JsonValue { } } -pub fn clearrate(_req: HttpRequest) -> HttpResponse { +pub fn clearrate(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": get_clearrate_json() }; - global::send(resp) + global::send(resp, req) } -pub fn ranking(_req: HttpRequest, body: String) -> HttpResponse { +pub fn ranking(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let live = body["master_live_id"].as_i64().unwrap(); @@ -253,5 +253,5 @@ pub fn ranking(_req: HttpRequest, body: String) -> HttpResponse { "ranking_list": rank } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/debug.rs b/src/router/debug.rs index 502e752..c7c0095 100644 --- a/src/router/debug.rs +++ b/src/router/debug.rs @@ -4,7 +4,7 @@ use actix_web::{HttpResponse, HttpRequest}; use crate::router::global; use crate::encryption; -pub fn error(_req: HttpRequest, body: String) -> HttpResponse { +pub fn error(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); println!("client error: {}", body["code"]); @@ -14,5 +14,5 @@ pub fn error(_req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "message": "" }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/event.rs b/src/router/event.rs index 41c58d3..3249708 100644 --- a/src/router/event.rs +++ b/src/router/event.rs @@ -16,7 +16,7 @@ pub fn event(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": event["event_data"].clone() }; - global::send(resp) + global::send(resp, req) } fn switch_music(event: &mut JsonValue, music_id: i32, target_score: i64, index: i32) { @@ -60,7 +60,7 @@ pub fn star_event(req: HttpRequest, body: String) -> HttpResponse { reward_list: [] } }; - global::send(resp) + global::send(resp, req) } //todo - randomize @@ -75,5 +75,5 @@ pub fn change_target_music(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": event["event_data"]["star_event"].clone() }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/exchange.rs b/src/router/exchange.rs index 60b89b7..7d183c7 100644 --- a/src/router/exchange.rs +++ b/src/router/exchange.rs @@ -24,13 +24,13 @@ lazy_static! { }; } -pub fn exchange(_req: HttpRequest) -> HttpResponse { +pub fn exchange(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": {"exchange_list":[]} }; - global::send(resp) + global::send(resp, req) } pub fn exchange_post(req: HttpRequest, body: String) -> HttpResponse { @@ -66,5 +66,5 @@ pub fn exchange_post(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/friend.rs b/src/router/friend.rs index a069b88..f774b77 100644 --- a/src/router/friend.rs +++ b/src/router/friend.rs @@ -32,7 +32,7 @@ pub fn friend(req: HttpRequest, body: String) -> HttpResponse { "friend_list": rv } }; - global::send(resp) + global::send(resp, req) } pub fn ids(req: HttpRequest) -> HttpResponse { @@ -44,7 +44,7 @@ pub fn ids(req: HttpRequest) -> HttpResponse { "server_time": global::timestamp(), "data": friends }; - global::send(resp) + global::send(resp, req) } pub fn recommend(req: HttpRequest, body: String) -> HttpResponse { @@ -74,7 +74,7 @@ pub fn recommend(req: HttpRequest, body: String) -> HttpResponse { friend_list: rv } }; - global::send(resp) + global::send(resp, req) } pub fn search(req: HttpRequest, body: String) -> HttpResponse { @@ -90,7 +90,7 @@ pub fn search(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": user }; - global::send(resp) + global::send(resp, req) } pub fn request(req: HttpRequest, body: String) -> HttpResponse { @@ -113,7 +113,7 @@ pub fn request(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } pub fn approve(req: HttpRequest, body: String) -> HttpResponse { @@ -139,7 +139,7 @@ pub fn approve(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } pub fn cancel(req: HttpRequest, body: String) -> HttpResponse { @@ -161,7 +161,7 @@ pub fn cancel(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } pub fn delete(req: HttpRequest, body: String) -> HttpResponse { @@ -183,5 +183,5 @@ pub fn delete(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/global.rs b/src/router/global.rs index 4461903..64126c9 100644 --- a/src/router/global.rs +++ b/src/router/global.rs @@ -1,7 +1,8 @@ use json::{array, object, JsonValue}; use actix_web::{ HttpResponse, - http::header::{HeaderValue, HeaderMap} + http::header::{HeaderValue, HeaderMap}, + HttpRequest }; use std::time::{SystemTime, UNIX_EPOCH}; use base64::{Engine as _, engine::general_purpose}; @@ -79,9 +80,27 @@ pub fn timestamp_since_midnight() -> u64 { rv } -pub fn send(mut data: JsonValue) -> HttpResponse { - //println!("{}", json::stringify(data.clone())); +fn set_time(data: &mut JsonValue, req: HttpRequest) { data["server_time"] = 1711741114.into(); + let blank_header = HeaderValue::from_static(""); + let uid = headers.get("aoharu-user-id").unwrap_or(&blank_header).to_str().unwrap_or("").parse().unwrap_or(0); + if uid == 0 { + return; + } + let server_data = userdata::get_server_data(get_login_token(uid)); + + if server_data["timestamp"].as_i64().is_ok() { + if server_data["timestamp"].as_i64().unwrap() == 0 { + data["server_time"] = global::timestamp().into(); + return; + } + data["server_time"] = server_data["timestamp"].clone(); + } +} + +pub fn send(mut data: JsonValue, req: HttpRequest) -> HttpResponse { + //println!("{}", json::stringify(data.clone())); + set_time(&data, req) let encrypted = encryption::encrypt_packet(&json::stringify(data)).unwrap(); let resp = encrypted.into_bytes(); @@ -89,8 +108,8 @@ pub fn send(mut data: JsonValue) -> HttpResponse { HttpResponse::Ok().body(resp) } -pub fn error_resp() -> HttpResponse { - send(object!{}) +pub fn error_resp(req: HttpRequest) -> HttpResponse { + send(object!{}, req) } pub fn start_login_bonus(id: i64, bonus: &mut JsonValue) -> bool { diff --git a/src/router/home.rs b/src/router/home.rs index 3968b58..625e996 100644 --- a/src/router/home.rs +++ b/src/router/home.rs @@ -22,7 +22,7 @@ pub fn preset(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } fn check_gifts(user: &mut JsonValue) { @@ -49,7 +49,7 @@ pub fn gift_get(req: HttpRequest) -> HttpResponse { "gift_list": user["home"]["gift_list"].clone() } }; - global::send(resp) + global::send(resp, req) } pub fn preset_get(req: HttpRequest) -> HttpResponse { @@ -65,7 +65,7 @@ pub fn preset_get(req: HttpRequest) -> HttpResponse { "card_list": user["card_list"].clone() } }; - global::send(resp) + global::send(resp, req) } pub fn home(req: HttpRequest) -> HttpResponse { @@ -80,5 +80,5 @@ pub fn home(req: HttpRequest) -> HttpResponse { "server_time": global::timestamp(), "data": user }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/live.rs b/src/router/live.rs index 14a1d49..a2f87fb 100644 --- a/src/router/live.rs +++ b/src/router/live.rs @@ -7,7 +7,7 @@ use crate::router::{global, userdata, items}; use crate::encryption; use crate::router::clear_rate::live_completed; -pub fn retire(_req: HttpRequest, body: String) -> HttpResponse { +pub fn retire(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); live_completed(body["master_live_id"].as_i64().unwrap(), body["level"].as_i32().unwrap(), true, 0, 0); let resp = object!{ @@ -19,7 +19,7 @@ pub fn retire(_req: HttpRequest, body: String) -> HttpResponse { "event_point_list": [] } }; - global::send(resp) + global::send(resp, req) } fn random_number(lowest: usize, highest: usize) -> usize { @@ -162,10 +162,10 @@ pub fn guest(req: HttpRequest, body: String) -> HttpResponse { "guest_list": guest_list } }; - global::send(resp) + global::send(resp, req) } -pub fn mission(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn mission(req: HttpRequest, _body: String) -> HttpResponse { //todo let resp = object!{ "code": 0, @@ -176,25 +176,25 @@ pub fn mission(_req: HttpRequest, _body: String) -> HttpResponse { "clear_count_ranking": "" } }; - global::send(resp) + global::send(resp, req) } -pub fn start(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn start(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } -pub fn event_start(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn event_start(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } pub fn continuee(req: HttpRequest, body: String) -> HttpResponse { @@ -210,7 +210,7 @@ pub fn continuee(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "gem": user["gem"].clone() }; - global::send(resp) + global::send(resp, req) } pub fn update_live_data(user: &mut JsonValue, data: &JsonValue, add: bool) -> JsonValue { @@ -435,7 +435,7 @@ fn live_end(req: &HttpRequest, body: &String) -> JsonValue { pub fn end(req: HttpRequest, body: String) -> HttpResponse { let resp = live_end(&req, &body); - global::send(resp) + global::send(resp, req) } pub fn event_end(req: HttpRequest, body: String) -> HttpResponse { @@ -484,7 +484,7 @@ pub fn event_end(req: HttpRequest, body: String) -> HttpResponse { userdata::save_acc_event(&key, event); - global::send(resp) + global::send(resp, req) } pub fn skip(req: HttpRequest, body: String) -> HttpResponse { @@ -540,5 +540,5 @@ pub fn skip(req: HttpRequest, body: String) -> HttpResponse { "event_ranking_data": [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/login.rs b/src/router/login.rs index c62fed4..314c946 100644 --- a/src/router/login.rs +++ b/src/router/login.rs @@ -17,7 +17,7 @@ pub fn dummy(req: HttpRequest, body: String) -> HttpResponse { "user_id": user["user"]["id"].clone() } }; - global::send(resp) + global::send(resp, req) } lazy_static! { @@ -105,7 +105,7 @@ pub fn bonus(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": user["clear_mission_ids"].clone() } }; - global::send(resp) + global::send(resp, req) } pub fn bonus_event(req: HttpRequest, body: String) -> HttpResponse { @@ -131,5 +131,5 @@ pub fn bonus_event(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": user["clear_mission_ids"].clone() } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/lottery/mod.rs b/src/router/lottery/mod.rs index 40adc4b..73ccf7a 100644 --- a/src/router/lottery/mod.rs +++ b/src/router/lottery/mod.rs @@ -61,7 +61,7 @@ lazy_static! { }; } -pub fn tutorial(_req: HttpRequest, body: String) -> HttpResponse { +pub fn tutorial(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let id = body["master_character_id"].to_string(); @@ -92,7 +92,7 @@ pub fn tutorial(_req: HttpRequest, body: String) -> HttpResponse { "item_list": [] } }; - global::send(resp) + global::send(resp, req) } fn get_card_master_id(lottery_id: String, lottery_number: String) -> Option { @@ -136,7 +136,7 @@ fn get_random_cards(id: i64, count: usize) -> JsonValue { rv } -pub fn lottery(_req: HttpRequest) -> HttpResponse { +pub fn lottery(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -144,7 +144,7 @@ pub fn lottery(_req: HttpRequest) -> HttpResponse { "lottery_list": [] } }; - global::send(resp) + global::send(resp, req) } pub fn lottery_post(req: HttpRequest, body: String) -> HttpResponse { @@ -243,5 +243,5 @@ pub fn lottery_post(req: HttpRequest, body: String) -> HttpResponse { "draw_count_list": [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/mission.rs b/src/router/mission.rs index 6779b90..35b6470 100644 --- a/src/router/mission.rs +++ b/src/router/mission.rs @@ -15,7 +15,7 @@ pub fn mission(req: HttpRequest) -> HttpResponse { "mission_list": missions } }; - global::send(resp) + global::send(resp, req) } pub fn clear(req: HttpRequest, body: String) -> HttpResponse { @@ -42,10 +42,10 @@ pub fn clear(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": body["master_mission_ids"].clone() } }; - global::send(resp) + global::send(resp, req) } -pub fn receive(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn receive(req: HttpRequest, _body: String) -> HttpResponse { //let key = global::get_login(req.headers(), &body); //let missions = userdata::get_acc_missions(&key); //let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); @@ -59,5 +59,5 @@ pub fn receive(_req: HttpRequest, _body: String) -> HttpResponse { "reward_list": [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/notice.rs b/src/router/notice.rs index 5bce3aa..621b6a6 100644 --- a/src/router/notice.rs +++ b/src/router/notice.rs @@ -4,7 +4,7 @@ use actix_web::{HttpResponse, HttpRequest}; use crate::router::global; //todo -pub fn reward(_req: HttpRequest) -> HttpResponse { +pub fn reward(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, @@ -13,14 +13,14 @@ pub fn reward(_req: HttpRequest) -> HttpResponse { "reward_list": [] } }; - global::send(resp) + global::send(resp, req) } -pub fn reward_post(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn reward_post(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/purchase.rs b/src/router/purchase.rs index a17faf1..5107dca 100644 --- a/src/router/purchase.rs +++ b/src/router/purchase.rs @@ -3,7 +3,7 @@ use actix_web::{HttpResponse, HttpRequest}; use crate::router::global; -pub fn purchase(_req: HttpRequest) -> HttpResponse { +pub fn purchase(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -35,5 +35,5 @@ pub fn purchase(_req: HttpRequest) -> HttpResponse { ] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/serial_code.rs b/src/router/serial_code.rs index a6b062c..ac98ac8 100644 --- a/src/router/serial_code.rs +++ b/src/router/serial_code.rs @@ -4,7 +4,7 @@ use actix_web::{HttpResponse, HttpRequest}; use crate::router::{global, userdata, items}; use crate::encryption; -pub fn events(_req: HttpRequest) -> HttpResponse { +pub fn events(req: HttpRequest) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -12,7 +12,7 @@ pub fn events(_req: HttpRequest) -> HttpResponse { "serial_code_list": [] } }; - global::send(resp) + global::send(resp, req) } pub fn serial_code(req: HttpRequest, body: String) -> HttpResponse { @@ -37,7 +37,7 @@ pub fn serial_code(req: HttpRequest, body: String) -> HttpResponse { "result_code": 3 } }; - return global::send(resp); + return global::send(resp, req); } userdata::save_acc_home(&key, user.clone()); @@ -53,5 +53,5 @@ pub fn serial_code(req: HttpRequest, body: String) -> HttpResponse { "excluded_gift_list": [] } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/shop.rs b/src/router/shop.rs index 78ad0e0..f260772 100644 --- a/src/router/shop.rs +++ b/src/router/shop.rs @@ -31,7 +31,7 @@ pub fn shop(req: HttpRequest) -> HttpResponse { "shop_list": user["shop_list"].clone() } }; - global::send(resp) + global::send(resp, req) } pub fn buy(req: HttpRequest, body: String) -> HttpResponse { @@ -60,5 +60,5 @@ pub fn buy(req: HttpRequest, body: String) -> HttpResponse { } } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/start.rs b/src/router/start.rs index d849f34..01c0aeb 100644 --- a/src/router/start.rs +++ b/src/router/start.rs @@ -39,7 +39,7 @@ pub fn asset_hash(req: HttpRequest, body: String) -> HttpResponse { "asset_hash": get_asset_hash(&req, &body) } }; - global::send(resp) + global::send(resp, req) } pub fn start(req: HttpRequest, body: String) -> HttpResponse { @@ -61,5 +61,5 @@ pub fn start(req: HttpRequest, body: String) -> HttpResponse { "token": hex::encode("Hello") //what is this? } }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/story.rs b/src/router/story.rs index ac8d786..5657b42 100644 --- a/src/router/story.rs +++ b/src/router/story.rs @@ -3,11 +3,11 @@ use actix_web::{HttpResponse, HttpRequest}; use crate::router::global; -pub fn read(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn read(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), "data": {"gift_list":[],"updated_value_list":{"master_chat_room_ids":[3001001,3101001],"master_chat_chapter_ids":[300100101,310100101]},"reward_list":[{"type":16,"value":3001001,"level":0,"amount":1},{"type":16,"value":3101001,"level":0,"amount":1},{"type":17,"value":300100101,"level":0,"amount":1},{"type":17,"value":310100101,"level":0,"amount":1}],"clear_mission_ids":[]} }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/tutorial.rs b/src/router/tutorial.rs index 5b3b94a..dabdcfe 100644 --- a/src/router/tutorial.rs +++ b/src/router/tutorial.rs @@ -20,5 +20,5 @@ pub fn tutorial(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/user.rs b/src/router/user.rs index ce8c364..7c773ee 100644 --- a/src/router/user.rs +++ b/src/router/user.rs @@ -30,7 +30,7 @@ pub fn deck(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": [] } }; - global::send(resp) + global::send(resp, req) } pub fn user(req: HttpRequest) -> HttpResponse { @@ -44,7 +44,7 @@ pub fn user(req: HttpRequest) -> HttpResponse { "server_time": global::timestamp(), "data": user }; - global::send(resp) + global::send(resp, req) } lazy_static! { @@ -116,7 +116,7 @@ pub fn gift(req: HttpRequest, body: String) -> HttpResponse { "reward_list": rewards } }; - global::send(resp) + global::send(resp, req) } pub fn user_post(req: HttpRequest, body: String) -> HttpResponse { @@ -171,7 +171,7 @@ pub fn user_post(req: HttpRequest, body: String) -> HttpResponse { "clear_mission_ids": user_2["clear_mission_ids"].clone() } }; - global::send(resp) + global::send(resp, req) } pub fn announcement(req: HttpRequest) -> HttpResponse { @@ -190,7 +190,7 @@ pub fn announcement(req: HttpRequest) -> HttpResponse { new_announcement_flag: 0 } }; - global::send(resp) + global::send(resp, req) } pub fn uid_to_code(uid: String) -> String { @@ -224,7 +224,7 @@ pub fn code_to_uid(code: String) -> String { .replace("M", "0"); } -pub fn get_migration_code(_req: HttpRequest, body: String) -> HttpResponse { +pub fn get_migration_code(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let code = uid_to_code(body["user_id"].to_string()); @@ -236,7 +236,7 @@ pub fn get_migration_code(_req: HttpRequest, body: String) -> HttpResponse { "migrationCode": code } }; - global::send(resp) + global::send(resp, req) } pub fn register_password(req: HttpRequest, body: String) -> HttpResponse { @@ -253,10 +253,10 @@ pub fn register_password(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": [] }; - global::send(resp) + global::send(resp, req) } -pub fn verify_migration_code(_req: HttpRequest, body: String) -> HttpResponse { +pub fn verify_migration_code(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let uid = code_to_uid(body["migrationCode"].to_string()).parse::().unwrap_or(0); @@ -269,7 +269,7 @@ pub fn verify_migration_code(_req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "message": "" }; - return global::send(resp); + return global::send(resp, req); } let data_user = userdata::get_acc(&user["login_token"].to_string()); @@ -284,9 +284,9 @@ pub fn verify_migration_code(_req: HttpRequest, body: String) -> HttpResponse { "free": data_user["gem"]["free"].clone() } }; - global::send(resp) + global::send(resp, req) } -pub fn request_migration_code(_req: HttpRequest, body: String) -> HttpResponse { +pub fn request_migration_code(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let uid = code_to_uid(body["migrationCode"].to_string()).parse::().unwrap_or(0); @@ -299,7 +299,7 @@ pub fn request_migration_code(_req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "message": "" }; - return global::send(resp); + return global::send(resp, req); } let resp = object!{ @@ -309,9 +309,9 @@ pub fn request_migration_code(_req: HttpRequest, body: String) -> HttpResponse { "twxuid": user["login_token"].to_string() } }; - global::send(resp) + global::send(resp, req) } -pub fn migration(_req: HttpRequest, body: String) -> HttpResponse { +pub fn migration(req: HttpRequest, body: String) -> HttpResponse { let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap(); let user = userdata::get_name_and_rank(body["user_id"].to_string().parse::().unwrap()); @@ -321,7 +321,7 @@ pub fn migration(_req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": user }; - global::send(resp) + global::send(resp, req) } pub fn detail(req: HttpRequest, body: String) -> HttpResponse { @@ -342,7 +342,7 @@ pub fn detail(req: HttpRequest, body: String) -> HttpResponse { user_detail_list: user_detail_list } }; - global::send(resp) + global::send(resp, req) } pub fn sif(req: HttpRequest) -> HttpResponse { @@ -356,10 +356,10 @@ pub fn sif(req: HttpRequest) -> HttpResponse { cards: cards } }; - global::send(resp) + global::send(resp, req) } -pub fn sifas_migrate(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn sifas_migrate(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -370,10 +370,10 @@ pub fn sifas_migrate(_req: HttpRequest, _body: String) -> HttpResponse { "lock_remain_time": null } }; - global::send(resp) + global::send(resp, req) } -pub fn sif_migrate(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn sif_migrate(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -381,10 +381,10 @@ pub fn sif_migrate(_req: HttpRequest, _body: String) -> HttpResponse { "sif_migrate_status": 38 } }; - global::send(resp) + global::send(resp, req) } -pub fn getregisteredplatformlist(_req: HttpRequest, _body: String) -> HttpResponse { +pub fn getregisteredplatformlist(req: HttpRequest, _body: String) -> HttpResponse { let resp = object!{ "code": 0, "server_time": global::timestamp(), @@ -394,7 +394,7 @@ pub fn getregisteredplatformlist(_req: HttpRequest, _body: String) -> HttpRespon "twitter": 0 } }; - global::send(resp) + global::send(resp, req) } pub fn initialize(req: HttpRequest, body: String) -> HttpResponse { @@ -431,7 +431,7 @@ pub fn initialize(req: HttpRequest, body: String) -> HttpResponse { cardstoreward = array![40010001, 40020001, 40030001, 40040001, 40050001, 40060001, 40070001, 40080001, 40090001]; //liella masterid += 9 + 9 + 12; //nijigasaki } else { - return global::error_resp(); + return global::error_resp(req); } masterid += userr; @@ -456,5 +456,5 @@ pub fn initialize(req: HttpRequest, body: String) -> HttpResponse { "server_time": global::timestamp(), "data": user["user"].clone() }; - global::send(resp) + global::send(resp, req) } diff --git a/src/router/userdata/mod.rs b/src/router/userdata/mod.rs index b0867a8..619015b 100644 --- a/src/router/userdata/mod.rs +++ b/src/router/userdata/mod.rs @@ -181,6 +181,9 @@ pub fn get_acc_sif(auth_key: &str) -> JsonValue { pub fn get_acc_friends(auth_key: &str) -> JsonValue { get_data(auth_key, "friends") } +pub fn get_server_data(auth_key: &str) -> JsonValue { + get_data(auth_key, "server_data") +} pub fn get_acc_event(auth_key: &str) -> JsonValue { let event = get_data(auth_key, "event"); if event.is_empty() { @@ -220,6 +223,9 @@ pub fn save_acc_event(auth_key: &str, data: JsonValue) { pub fn save_acc_eventlogin(auth_key: &str, data: JsonValue) { save_data(auth_key, "eventloginbonus", data); } +pub fn save_server_data(auth_key: &str, data: JsonValue) { + save_data(auth_key, "server_data", data); +} fn generate_salt() -> Vec { let mut rng = rand::thread_rng(); @@ -516,6 +522,30 @@ pub fn webui_start_loginbonus(bonus_id: i64, token: &str) -> JsonValue { }; } +pub fn set_server_time(time: i64, token: &str) -> JsonValue { + if time > global::timestamp() { + return object!{ + result: "ERR", + message: "Timestamp is in the future" + }; + } + let login_token = webui_login_token(token); + if login_token.is_none() { + return object!{ + result: "ERR", + message: "Failed to validate token" + }; + } + let login_token = login_token.unwrap(); + let mut server_data = get_server_data(&login_token); + server_data["server_time"] = time.into(); + save_server_data(&login_token, server_data); + + return object!{ + result: "OK" + }; +} + pub fn webui_logout(token: &str) { DATABASE.lock_and_exec("DELETE FROM webui WHERE token=?1", params!(token)); } diff --git a/src/router/webui.rs b/src/router/webui.rs index a3d0f82..1bb9823 100644 --- a/src/router/webui.rs +++ b/src/router/webui.rs @@ -101,6 +101,19 @@ pub fn start_loginbonus(req: HttpRequest, body: String) -> HttpResponse { .body(json::stringify(resp)) } +pub fn set_time(req: HttpRequest, body: String) -> HttpResponse { + let token = get_login_token(&req); + if token.is_none() { + return error("Not logged in"); + } + let body = json::parse(&body).unwrap(); + let resp = userdata::set_server_time(body["timestamp"].as_i64().unwrap(), &token.unwrap()); + + HttpResponse::Ok() + .insert_header(ContentType::json()) + .body(json::stringify(resp)) +} + pub fn logout(req: HttpRequest) -> HttpResponse { let token = get_login_token(&req); if !token.is_none() {