Implement /api/start/assetHash

This commit is contained in:
Ethan O'Brien 2024-02-23 09:59:55 -06:00
parent f9eded5674
commit f48ae94c0e
8 changed files with 953 additions and 35 deletions

8
Cargo.lock generated
View file

@ -483,7 +483,9 @@ version = "0.0.1"
dependencies = [
"actix-web",
"base64",
"json",
"openssl",
"rand",
"reqwest",
]
@ -757,6 +759,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
[[package]]
name = "language-tags"
version = "0.3.2"

View file

@ -8,3 +8,5 @@ actix-web = { version = "4.5.1", features = [ "openssl" ] }
openssl = { version = "0.10" }
base64 = "0.21.5"
reqwest = { version = "0.11", features = ["blocking"] }
json = "0.12.4"
rand = "0.8.5"

821
logs2.txt Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,7 @@
use openssl::symm::{Cipher, Crypter, Mode};
use openssl::error::ErrorStack;
use base64::{Engine as _, engine::general_purpose};
use rand::Rng;
const IV_LENGTH: usize = 16;
const KEY: &str = "3559b435f24b297a79c68b9709ef2125";
@ -22,3 +23,29 @@ pub fn decrypt_packet(base64_input: &str) -> Result<String, ErrorStack> {
Ok(String::from_utf8(decrypted_data).unwrap())
}
pub fn encrypt_packet(input: &str) -> Result<String, ErrorStack> {
let cipher = Cipher::aes_256_cbc();
let encryption_iv = generate_random_iv();
let mut encrypter = Crypter::new(cipher, Mode::Encrypt, KEY.as_bytes(), Some(&encryption_iv))?;
let mut encrypted_data = vec![0u8; input.len() + cipher.block_size()];
let mut encrypted_len = encrypter.update(input.as_bytes(), &mut encrypted_data)?;
encrypted_len += encrypter.finalize(&mut encrypted_data[encrypted_len..])?;
encrypted_data.truncate(encrypted_len);
let mut result = encryption_iv.to_vec();
result.extend_from_slice(&encrypted_data);
Ok(general_purpose::STANDARD.encode(&result))
}
fn generate_random_iv() -> Vec<u8> {
let mut rng = rand::thread_rng();
let mut bytes = vec![0u8; IV_LENGTH];
rng.fill(&mut bytes[..]);
bytes
}

View file

@ -1,14 +1,58 @@
mod encryption;
mod router;
use actix_web::{
// post,
// get,
post,
HttpResponse,
HttpRequest,
http::header::HeaderMap,
web,
dev::Service
};
#[post("/api/start/assetHash")]
async fn start_assethash(req: HttpRequest, body: String) -> HttpResponse { router::start::asset_hash(req, body) }
async fn log_unknown_request(req: HttpRequest) -> HttpResponse {
println!("Unhandled request: {}", req.path());
HttpResponse::Ok().body("ok")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
let rv = HttpServer::new(|| App::new()
.wrap_fn(|req, srv| {
println!("Request: {}", req.path());
srv.call(req)
})
.service(start_assethash)
.default_service(web::route().to(log_unknown_request)))
.bind(("0.0.0.0", 8080))?
.run();
println!("Server started: http://127.0.0.1:{}", 8080);
rv.await
}
/*
fn main() {
let base64_input = "MX2tzmKTxY7EsV46rYFZuAfxeY0tPHuZ0etG15WsK1MAzs/U0WUXE4bJZINrEvCxqqUbvCYxhDtXp3HoeH/zDXtnW183aF/aYycmUW3aAF6zyio4/PJoqFl7EGET37ruotoQ9Teof2PXpXraF94diw==";
match decrypt_packet(base64_input) {
Ok(decrypted_json) => {
// Process the decrypted JSON
println!("Decrypted JSON: {}", decrypted_json);
}
Err(err) => {
eprintln!("Error decrypting packet: {}", err);
}
}
}
*/
/*
async fn make_post_request(url: &str, body: &str, headers: &HeaderMap) -> Result<String, reqwest::Error> {
let client = reqwest::Client::new();
let mut response = client
@ -21,6 +65,7 @@ async fn make_post_request(url: &str, body: &str, headers: &HeaderMap) -> Result
response = response.header("host", "api-sif2.lovelive-sif2.com");
continue;
};
println!("{}: {}", name, value.to_str().unwrap());
response = response.header(name, value.to_str().unwrap());
}
@ -60,35 +105,4 @@ async fn log_unknown_request(req: HttpRequest, body: String) -> HttpResponse {
HttpResponse::Ok().body(resp)
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
let rv = HttpServer::new(|| App::new()
.wrap_fn(|req, srv| {
println!("Request: {}", req.path());
srv.call(req)
})
.default_service(web::route().to(log_unknown_request)))
.bind(("0.0.0.0", 8080))?
.run();
println!("Server started: http://127.0.0.1:{}", 8080);
rv.await
}
/*
fn main() {
let base64_input = "MX2tzmKTxY7EsV46rYFZuAfxeY0tPHuZ0etG15WsK1MAzs/U0WUXE4bJZINrEvCxqqUbvCYxhDtXp3HoeH/zDXtnW183aF/aYycmUW3aAF6zyio4/PJoqFl7EGET37ruotoQ9Teof2PXpXraF94diw==";
match decrypt_packet(base64_input) {
Ok(decrypted_json) => {
// Process the decrypted JSON
println!("Decrypted JSON: {}", decrypted_json);
}
Err(err) => {
eprintln!("Error decrypting packet: {}", err);
}
}
}
*/
}*/

2
src/router.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod start;
pub mod global;

24
src/router/global.rs Normal file
View file

@ -0,0 +1,24 @@
use json::JsonValue;
use crate::encryption;
use actix_web::{
HttpResponse
};
use std::time::{SystemTime, UNIX_EPOCH};
//likely different between ios and android?
pub const ASSET_VERSION: &str = "4a802a747076a91e5e62707f6358bc2d";
pub const ASSET_HASH: &str = "183931205c9dbc39788ef7b361988cf4";
pub fn timestamp() -> u64 {
let now = SystemTime::now();
let unix_timestamp = now.duration_since(UNIX_EPOCH).unwrap();
return unix_timestamp.as_secs();
}
pub fn send(data: JsonValue) -> HttpResponse {
let encrypted = encryption::encrypt_packet(&json::stringify(data)).unwrap();
let resp = encrypted.into_bytes();
HttpResponse::Ok().body(resp)
}

20
src/router/start.rs Normal file
View file

@ -0,0 +1,20 @@
use json;
use json::object;
use crate::router::global;
use crate::encryption;
use actix_web::{HttpResponse, HttpRequest};
pub fn asset_hash(_req: HttpRequest, body: String) -> HttpResponse {
let body = json::parse(&encryption::decrypt_packet(&body).unwrap()).unwrap();
if body["asset_version"].to_string() != global::ASSET_VERSION {
println!("Warning! Asset version is not what was expected. (Did the app update?)");
}
let resp = object!{
"code": 0,
"server_time": global::timestamp(),
"data": {
"asset_hash": global::ASSET_HASH
}
};
global::send(resp)
}