From 36077a0b6928d25cb20ef7394255f54d9753b9d2 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien <77750390+ethanaobrien@users.noreply.github.com> Date: Tue, 2 Apr 2024 09:53:30 -0500 Subject: [PATCH] Push (not working?) gree code --- Cargo.lock | 42 +++++++++++++++++++++++++++ Cargo.toml | 5 ++++ src/main.rs | 4 +++ src/router.rs | 1 + src/router/global.rs | 1 - src/router/gree.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/router/gree.rs diff --git a/Cargo.lock b/Cargo.lock index c2ce2dc..baa17bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -485,6 +485,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -520,12 +521,17 @@ dependencies = [ "base64", "chrono", "hex", + "hmac", "json", "lazy_static", + "md5", "openssl", "rand", "reqwest", "rusqlite", + "sha1", + "substring", + "urlencoding", ] [[package]] @@ -711,6 +717,15 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.11" @@ -926,6 +941,12 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "md5" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771" + [[package]] name = "memchr" version = "2.7.1" @@ -1441,6 +1462,21 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "substring" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ee6433ecef213b2e72f587ef64a2f5943e7cd16fbd82dbe8bc07486c534c86" +dependencies = [ + "autocfg", +] + +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" @@ -1672,6 +1708,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index e64e476..db6875b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,8 @@ rand = "0.8.5" lazy_static = "1.4.0" chrono = "0.4.31" hex = "0.4.3" +hmac = "0.12.1" +md5 = "0.7.0" +urlencoding = "2.1.3" +sha1 = "0.10.6" +substring = "1.4.5" diff --git a/src/main.rs b/src/main.rs index 38f0324..61ba00e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,9 @@ use actix_web::{ dev::Service }; +#[post("/v1.0/auth/initialize")] +async fn gree_init(req: HttpRequest, body: String) -> HttpResponse { router::gree::initialize(req, body) } + #[post("/api/debug/error")] async fn debug_error(req: HttpRequest, body: String) -> HttpResponse { router::debug::error(req, body) } @@ -122,6 +125,7 @@ async fn main() -> std::io::Result<()> { println!("Request: {}", req.path()); srv.call(req) }) + .service(gree_init) .service(debug_error) .service(login_bonus) .service(reward) diff --git a/src/router.rs b/src/router.rs index e42be14..f1da50a 100644 --- a/src/router.rs +++ b/src/router.rs @@ -15,3 +15,4 @@ pub mod chat; pub mod story; pub mod notice; pub mod debug; +pub mod gree; diff --git a/src/router/global.rs b/src/router/global.rs index ea2d47f..45c57ba 100644 --- a/src/router/global.rs +++ b/src/router/global.rs @@ -7,7 +7,6 @@ use actix_web::{ use std::time::{SystemTime, UNIX_EPOCH}; use base64::{Engine as _, engine::general_purpose}; -//different between ios and android? pub const ASSET_VERSION: &str = "13177023d4b7ad41ff52af4cefba5c55"; pub const ASSET_HASH: &str = "6a6f3be1da2c3734386a1832e251451a"; diff --git a/src/router/gree.rs b/src/router/gree.rs new file mode 100644 index 0000000..efc501d --- /dev/null +++ b/src/router/gree.rs @@ -0,0 +1,68 @@ +use crate::router::global; +use actix_web::{HttpResponse, HttpRequest, http::header::HeaderValue, http::header::ContentType}; +use base64::{Engine as _, engine::general_purpose}; +use std::collections::HashMap; +use sha1::Sha1; +use substring::Substring; + +pub fn initialize(req: HttpRequest, _body: String) -> HttpResponse { + HttpResponse::Ok() + .insert_header(ContentType::json()) + .insert_header(("Expires", "-1")) + .insert_header(("Pragma", "no-cache")) + .insert_header(("Cache-Control", "must-revalidate, no-cache, no-store, private")) + .insert_header(("Vary", "Authorization,Accept-Encoding")) + .insert_header(("X-GREE-Authorization", gree_authorize(&req))) + .body("{\"result\": \"OK\"}") + +} + +use hmac::{Hmac, Mac}; + + +fn gree_authorize(req: &HttpRequest) -> String { + type HmacSha1 = Hmac; + + let blank_header = HeaderValue::from_static(""); + let auth_header = req.headers().get("Authorization").unwrap_or(&blank_header).to_str().unwrap_or(""); + if auth_header == "" { + return String::new(); + } + let auth_header = auth_header.substring(6, auth_header.len()); + + let auth_list: Vec<&str> = auth_header.split(',').collect(); + let mut header_data = HashMap::new(); + + for auth_data in auth_list { + let data: Vec<&str> = auth_data.split('=').collect(); + if data.len() == 2 { + header_data.insert(data[0].to_string(), data[1][1..(data[1].len() - 1)].to_string()); + } + } + + + let current_url = format!("http://127.0.0.1:8080{}", req.path()); + let nonce = format!("{:x}", md5::compute((global::timestamp() * 1000).to_string())); + let timestamp = global::timestamp().to_string(); + let method = "HMAC-SHA1"; + let validate_data = format!("{}&{}&{}", + "POST", + urlencoding::encode(¤t_url), + urlencoding::encode(&format!("oauth_body_hash={}&oauth_consumer_key={}&oauth_nonce={}&oauth_signature_method={}&oauth_timestamp={}&oauth_version=1.0", + header_data.get("oauth_body_hash").unwrap_or(&String::new()), + header_data.get("oauth_consumer_key").unwrap_or(&String::new()), + nonce, + method, + timestamp))); + let mut hasher = HmacSha1::new_from_slice(&hex::decode("6438663638653238346566646636306262616563326432323563306366643432").unwrap()).unwrap(); + hasher.update(validate_data.as_bytes()); + let signature = general_purpose::STANDARD.encode(hasher.finalize().into_bytes()); + + format!("OAuth oauth_version=\"1.0\",oauth_nonce=\"{}\",oauth_timestamp=\"{}\",oauth_consumer_key=\"{}\",oauth_body_hash=\"{}\",oauth_signature_method=\"{}\",oauth_signature=\"{}\"", + nonce, + timestamp, + header_data.get("oauth_consumer_key").unwrap_or(&String::new()), + header_data.get("oauth_body_hash").unwrap_or(&String::new()), + method, + urlencoding::encode(&signature)) +}