Move router related functions to router mod file.

This commit is contained in:
Ethan O'Brien 2024-07-10 22:45:46 -05:00
parent e1e32a22b0
commit 577cc0ddd3
2 changed files with 220 additions and 212 deletions

View file

@ -2,7 +2,6 @@ mod encryption;
mod router;
mod sql;
use json::object;
use actix_web::{
App,
HttpServer,
@ -11,165 +10,11 @@ use actix_web::{
HttpRequest,
web,
dev::Service,
http::header::ContentType,
http::header::HeaderValue
http::header::ContentType
};
use crate::router::global;
use json::JsonValue;
use clap::Parser;
use std::fs;
fn unhandled(req: HttpRequest, body: String) -> Option<JsonValue> {
if body != String::new() {
println!("{}", encryption::decrypt_packet(&body).unwrap_or(body));
}
println!("Unhandled request: {}", req.path());
None
}
async fn api_req(req: HttpRequest, body: String) -> HttpResponse {
let headers = req.headers().clone();
if !req.path().starts_with("/api") && !req.path().starts_with("/v1.0") {
return router::webui::main(req);
}
let blank_header = HeaderValue::from_static("");
let uid = req.headers().get("aoharu-user-id").unwrap_or(&blank_header).to_str().unwrap_or("").parse::<i64>().unwrap_or(0);
let resp: Option<JsonValue> = if req.method() == "POST" {
match req.path() {
"/api/debug/error" => router::debug::error(req, body),
"/api/start" => router::start::start(req, body),
"/api/start/assetHash" => router::start::asset_hash(req, body),
"/api/dummy/login" => router::login::dummy(req, body),
"/api/user" => router::user::user_post(req, body),
"/api/chat/home" => router::chat::home(req, body),
"/api/chat/talk/start" => router::chat::start(req, body),
"/api/chat/talk/end" => router::chat::end(req, body),
"/api/story/read" => router::story::read(req, body),
"/api/user/initialize" => router::user::initialize(req, body),
"/api/user/detail" => router::user::detail(req, body),
"/api/gift" => router::user::gift(req, body),
"/api/deck" => router::user::deck(req, body),
"/api/tutorial" => router::tutorial::tutorial(req, body),
"/api/friend" => router::friend::friend(req, body),
"/api/friend/search" => router::friend::search(req, body),
"/api/friend/search/recommend" => router::friend::recommend(req, body),
"/api/friend/request" => router::friend::request(req, body),
"/api/friend/request/approve" => router::friend::approve(req, body),
"/api/friend/request/cancel" => router::friend::cancel(req, body),
"/api/friend/delete" => router::friend::delete(req, body),
"/api/live/guest" => router::live::guest(req, body),
"/api/live/mission" => router::live::mission(req, body),
"/api/live/ranking" => router::clear_rate::ranking(req, body),
"/api/event" => router::event::event(req, body),
"/api/event/star_event" => router::event::star_event(req, body),
"/api/event_star_live/change_target_music" => router::event::change_target_music(req, body),
"/api/event_star_live/start" => router::live::event_start(req, body),
"/api/event_star_live/end" => router::live::event_end(req, body),
//"/api/event_star_live/skip" => router::live::event_skip(req, body),
"/api/live/start" => router::live::start(req, body),
"/api/live/end" => router::live::end(req, body),
"/api/live/skip" => router::live::skip(req, body),
"/api/live/retire" => router::live::retire(req, body),
"/api/live/continue" => router::live::continuee(req, body),
"/api/live/reward" => router::live::reward(req, body),
"/api/mission/clear" => router::mission::clear(req, body),
"/api/mission/receive" => router::mission::receive(req, body),
"/api/home/preset" => router::home::preset(req, body),
"/api/lottery/get_tutorial" => router::lottery::tutorial(req, body),
"/api/lottery" => router::lottery::lottery_post(req, body),
"/api/login_bonus" => router::login::bonus(req, body),
"/api/login_bonus/event" => router::login::bonus_event(req, body),
"/api/notice/reward" => router::notice::reward_post(req, body),
"/api/user/getmigrationcode" => router::user::get_migration_code(req, body),
"/api/user/registerpassword" => router::user::register_password(req, body),
"/api/user/migration" => router::user::migration(req, body),
"/api/user/gglrequestmigrationcode" => router::user::request_migration_code(req, body),
"/api/user/gglverifymigrationcode" => router::user::verify_migration_code(req, body),
"/api/serial_code" => router::serial_code::serial_code(req, body),
"/api/card/reinforce" => router::card::reinforce(req, body),
"/api/card/skill/reinforce" => router::card::skill_reinforce(req, body),
"/api/card/evolve" => router::card::evolve(req, body),
"/api/shop/buy" => router::shop::buy(req, body),
"/api/user/getregisteredplatformlist" => router::user::getregisteredplatformlist(req, body),
"/api/user/sif/migrate" => router::user::sif_migrate(req, body).await,
"/api/user/ss/migrate" => router::user::sifas_migrate(req, body),
"/api/exchange" => router::exchange::exchange_post(req, body),
"/api/item/use" => router::items::use_item_req(req, body),
_ => unhandled(req, body)
}
} else {
match req.path() {
"/api/user" => router::user::user(req),
"/api/gift" => router::home::gift_get(req),
"/api/purchase" => router::purchase::purchase(req),
"/api/friend/ids" => router::friend::ids(req),
"/api/live/clearRate" => router::clear_rate::clearrate(req),
"/api/mission" => router::mission::mission(req),
"/api/home" => router::home::home(req),
"/api/home/preset" => router::home::preset_get(req),
"/api/lottery" => router::lottery::lottery(req),
"/api/notice/reward" => router::notice::reward(req),
"/api/serial_code/events" => router::serial_code::events(req),
"/api/album/sif" => router::user::sif(req),
"/api/home/announcement" => router::user::announcement(req),
"/api/shop" => router::shop::shop(req),
"/api/exchange" => router::exchange::exchange(req),
"/api/location" => router::location::location(req),
_ => unhandled(req, body)
}
};
if resp.is_some() {
let rv = object!{
"code": 0,
"server_time": global::timestamp(),
"data": resp.unwrap()
};
global::send(rv, uid, &headers)
} else {
let rv = object!{
"code": 2,//Idontnermemrmemremremermrme
"server_time": global::timestamp(),
"data": ""
};
global::send(rv, uid, &headers)
}
}
async fn request(req: HttpRequest, body: String) -> HttpResponse {
if req.method() == "POST" {
match req.path() {
"/v1.0/auth/initialize" => router::gree::initialize(req, body),
"/v1.0/moderate/filtering/commit" => router::gree::moderate_commit(req, body),
"/v1.0/auth/authorize" => router::gree::authorize(req, body),
"/v1.0/migration/code/verify" => router::gree::migration_verify(req, body),
"/v1.0/migration/password/register" => router::gree::migration_password_register(req, body),
"/v1.0/migration" => router::gree::migration(req, body),
"/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/webui/admin" => router::webui::admin_post(req, body),
_ => api_req(req, body).await
}
} else {
match req.path() {
"/v1.0/auth/x_uid" => router::gree::uid(req),
"/v1.0/payment/productlist" => router::gree::payment(req),
"/v1.0/payment/subscription/productlist" => router::gree::payment(req),
"/v1.0/payment/ticket/status" => router::gree::payment_ticket(req),
"/v1.0/moderate/keywordlist" => router::gree::moderate_keyword(req),
"/v1.0/migration/code" => router::gree::migration_code(req),
"/v1.0/payment/balance" => router::gree::balance(req),
"/web/announcement" => router::web::announcement(req),
"/api/webui/userInfo" => router::webui::user(req),
"/webui/logout" => router::webui::logout(req),
"/api/webui/admin" => router::webui::admin(req),
"/api/webui/export" => router::webui::export(req),
_ => api_req(req, body).await
}
}
}
#[get("/index.css")]
async fn css(_req: HttpRequest) -> HttpResponse {
HttpResponse::Ok()
@ -183,6 +28,62 @@ async fn js(_req: HttpRequest) -> HttpResponse {
.body(include_file!("webui/dist/index.js"))
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long, default_value_t = 8080, help = "Port to listen on")]
port: u16,
#[arg(long, default_value = "./", help = "Path to store database files")]
path: String,
#[arg(long, default_value_t = false, help = "Serve gree headers with https. WILL NOT ACCEPT HTTPS REQUESTS")]
https: bool,
#[arg(long, default_value = "http://127.0.0.1:51376", help = "Address to NPPS4 server for sif account linking")]
npps4: String
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args = get_args();
let port = args.port;
let rv = HttpServer::new(|| App::new()
.wrap_fn(|req, srv| {
println!("Request: {}", req.path());
srv.call(req)
})
.app_data(web::PayloadConfig::default().limit(1024 * 1024 * 25))
.service(css)
.service(js)
.default_service(web::route().to(router::request))
).bind(("0.0.0.0", port))?.run();
println!("Server started: http://0.0.0.0:{}", port);
println!("Data path is set to {}", args.path);
println!("Sif1 transfer requests will attempt to contact NPPS4 at {}", args.npps4);
if args.https {
println!("Note: gree is set to https mode. http requests will fail on jp clients.");
}
rv.await
}
pub fn get_args() -> Args {
Args::parse()
}
pub fn get_data_path(file_name: &str) -> String {
let args = get_args();
let mut path = args.path;
while path.ends_with("/") {
path.pop();
}
fs::create_dir_all(&path).unwrap();
format!("{}/{}", path, file_name)
}
#[macro_export]
macro_rules! include_file {
( $s:expr ) => {
@ -201,59 +102,3 @@ pub fn decode(bytes: &[u8]) -> Vec<u8> {
dec.read_to_end(&mut ret).unwrap();
ret
}
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Args {
#[arg(short, long, default_value_t = 8080, help = "Port to listen on")]
port: u16,
#[arg(long, default_value = "./", help = "Path to store database files")]
path: String,
#[arg(long, default_value_t = false, help = "Serve gree headers with https. WILL NOT ACCEPT HTTPS REQUESTS")]
https: bool,
#[arg(long, default_value = "http://127.0.0.1:51376", help = "Address to NPPS4 server for sif account linking")]
npps4: String
}
pub fn get_args() -> Args {
Args::parse()
}
pub fn get_data_path(file_name: &str) -> String {
let args = get_args();
let mut path = args.path;
while path.ends_with("/") {
path.pop();
}
fs::create_dir_all(&path).unwrap();
format!("{}/{}", path, file_name)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args = get_args();
let port = args.port;
let rv = HttpServer::new(|| App::new()
.wrap_fn(|req, srv| {
println!("Request: {}", req.path());
srv.call(req)
})
.app_data(web::PayloadConfig::default().limit(1024 * 1024 * 25))
.service(css)
.service(js)
.default_service(web::route().to(request))
).bind(("0.0.0.0", port))?.run();
println!("Server started: http://0.0.0.0:{}", port);
println!("Data path is set to {}", args.path);
println!("Sif1 transfer requests will attempt to contact NPPS4 at {}", args.npps4);
if args.https {
println!("Note: gree is set to https mode. http requests will fail on jp clients.");
}
rv.await
}

View file

@ -26,3 +26,166 @@ pub mod exchange;
pub mod items;
pub mod databases;
pub mod location;
use actix_web::{
HttpResponse,
HttpRequest,
http::header::HeaderValue
};
use json::{JsonValue, object};
use crate::encryption;
fn unhandled(req: HttpRequest, body: String) -> Option<JsonValue> {
if body != String::new() {
println!("{}", encryption::decrypt_packet(&body).unwrap_or(body));
}
println!("Unhandled request: {}", req.path());
None
}
async fn api_req(req: HttpRequest, body: String) -> HttpResponse {
let headers = req.headers().clone();
if !req.path().starts_with("/api") && !req.path().starts_with("/v1.0") {
return webui::main(req);
}
let blank_header = HeaderValue::from_static("");
let uid = req.headers().get("aoharu-user-id").unwrap_or(&blank_header).to_str().unwrap_or("").parse::<i64>().unwrap_or(0);
let resp: Option<JsonValue> = if req.method() == "POST" {
match req.path() {
"/api/debug/error" => debug::error(req, body),
"/api/start" => start::start(req, body),
"/api/start/assetHash" => start::asset_hash(req, body),
"/api/dummy/login" => login::dummy(req, body),
"/api/user" => user::user_post(req, body),
"/api/chat/home" => chat::home(req, body),
"/api/chat/talk/start" => chat::start(req, body),
"/api/chat/talk/end" => chat::end(req, body),
"/api/story/read" => story::read(req, body),
"/api/user/initialize" => user::initialize(req, body),
"/api/user/detail" => user::detail(req, body),
"/api/gift" => user::gift(req, body),
"/api/deck" => user::deck(req, body),
"/api/tutorial" => tutorial::tutorial(req, body),
"/api/friend" => friend::friend(req, body),
"/api/friend/search" => friend::search(req, body),
"/api/friend/search/recommend" => friend::recommend(req, body),
"/api/friend/request" => friend::request(req, body),
"/api/friend/request/approve" => friend::approve(req, body),
"/api/friend/request/cancel" => friend::cancel(req, body),
"/api/friend/delete" => friend::delete(req, body),
"/api/live/guest" => live::guest(req, body),
"/api/live/mission" => live::mission(req, body),
"/api/live/ranking" => clear_rate::ranking(req, body),
"/api/event" => event::event(req, body),
"/api/event/star_event" => event::star_event(req, body),
"/api/event_star_live/change_target_music" => event::change_target_music(req, body),
"/api/event_star_live/start" => live::event_start(req, body),
"/api/event_star_live/end" => live::event_end(req, body),
//"/api/event_star_live/skip" => live::event_skip(req, body),
"/api/live/start" => live::start(req, body),
"/api/live/end" => live::end(req, body),
"/api/live/skip" => live::skip(req, body),
"/api/live/retire" => live::retire(req, body),
"/api/live/continue" => live::continuee(req, body),
"/api/live/reward" => live::reward(req, body),
"/api/mission/clear" => mission::clear(req, body),
"/api/mission/receive" => mission::receive(req, body),
"/api/home/preset" => home::preset(req, body),
"/api/lottery/get_tutorial" => lottery::tutorial(req, body),
"/api/lottery" => lottery::lottery_post(req, body),
"/api/login_bonus" => login::bonus(req, body),
"/api/login_bonus/event" => login::bonus_event(req, body),
"/api/notice/reward" => notice::reward_post(req, body),
"/api/user/getmigrationcode" => user::get_migration_code(req, body),
"/api/user/registerpassword" => user::register_password(req, body),
"/api/user/migration" => user::migration(req, body),
"/api/user/gglrequestmigrationcode" => user::request_migration_code(req, body),
"/api/user/gglverifymigrationcode" => user::verify_migration_code(req, body),
"/api/serial_code" => serial_code::serial_code(req, body),
"/api/card/reinforce" => card::reinforce(req, body),
"/api/card/skill/reinforce" => card::skill_reinforce(req, body),
"/api/card/evolve" => card::evolve(req, body),
"/api/shop/buy" => shop::buy(req, body),
"/api/user/getregisteredplatformlist" => user::getregisteredplatformlist(req, body),
"/api/user/sif/migrate" => user::sif_migrate(req, body).await,
"/api/user/ss/migrate" => user::sifas_migrate(req, body),
"/api/exchange" => exchange::exchange_post(req, body),
"/api/item/use" => items::use_item_req(req, body),
_ => unhandled(req, body)
}
} else {
match req.path() {
"/api/user" => user::user(req),
"/api/gift" => home::gift_get(req),
"/api/purchase" => purchase::purchase(req),
"/api/friend/ids" => friend::ids(req),
"/api/live/clearRate" => clear_rate::clearrate(req),
"/api/mission" => mission::mission(req),
"/api/home" => home::home(req),
"/api/home/preset" => home::preset_get(req),
"/api/lottery" => lottery::lottery(req),
"/api/notice/reward" => notice::reward(req),
"/api/serial_code/events" => serial_code::events(req),
"/api/album/sif" => user::sif(req),
"/api/home/announcement" => user::announcement(req),
"/api/shop" => shop::shop(req),
"/api/exchange" => exchange::exchange(req),
"/api/location" => location::location(req),
_ => unhandled(req, body)
}
};
if resp.is_some() {
let rv = object!{
"code": 0,
"server_time": global::timestamp(),
"data": resp.unwrap()
};
global::send(rv, uid, &headers)
} else {
let rv = object!{
"code": 2,//Idontnermemrmemremremermrme
"server_time": global::timestamp(),
"data": ""
};
global::send(rv, uid, &headers)
}
}
pub async fn request(req: HttpRequest, body: String) -> HttpResponse {
if req.method() == "POST" {
match req.path() {
"/v1.0/auth/initialize" => gree::initialize(req, body),
"/v1.0/moderate/filtering/commit" => gree::moderate_commit(req, body),
"/v1.0/auth/authorize" => gree::authorize(req, body),
"/v1.0/migration/code/verify" => gree::migration_verify(req, body),
"/v1.0/migration/password/register" => gree::migration_password_register(req, body),
"/v1.0/migration" => gree::migration(req, body),
"/api/webui/login" => webui::login(req, body),
"/api/webui/startLoginbonus" => webui::start_loginbonus(req, body),
"/api/webui/import" => webui::import(req, body),
"/api/webui/set_time" => webui::set_time(req, body),
"/api/webui/admin" => webui::admin_post(req, body),
_ => api_req(req, body).await
}
} else {
match req.path() {
"/v1.0/auth/x_uid" => gree::uid(req),
"/v1.0/payment/productlist" => gree::payment(req),
"/v1.0/payment/subscription/productlist" => gree::payment(req),
"/v1.0/payment/ticket/status" => gree::payment_ticket(req),
"/v1.0/moderate/keywordlist" => gree::moderate_keyword(req),
"/v1.0/migration/code" => gree::migration_code(req),
"/v1.0/payment/balance" => gree::balance(req),
"/web/announcement" => web::announcement(req),
"/api/webui/userInfo" => webui::user(req),
"/webui/logout" => webui::logout(req),
"/api/webui/admin" => webui::admin(req),
"/api/webui/export" => webui::export(req),
_ => api_req(req, body).await
}
}
}