feat: abstract method to perform action

This commit is contained in:
mokurin000
2025-07-30 03:20:21 +08:00
parent 6ee009715d
commit 1a281e0cc7
7 changed files with 59 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ use std::io::{Read, Write as _};
use aes::cipher::{
BlockDecryptMut, BlockEncryptMut, BlockSizeUser, KeyIvInit, block_padding::Pkcs7,
};
use digest::generic_array::GenericArray;
use flate2::write::ZlibEncoder;
use flate2::{Compression, read::ZlibDecoder};
@@ -12,6 +13,20 @@ use crate::title::{MaiVersion, MaiVersionExt, Sdgb1_40, Sdgb1_50, error::ApiErro
impl MaiVersionExt for Sdgb1_40 {
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
let mut decompressed = decompress(data.as_mut());
if decompressed.is_empty() {
return Err(ApiError::EmptyResponse);
}
let orig_len = decompressed.len();
let remain = 16 - decompressed.len() % 16;
if
// weird but nessacary for Rust Pkcs7
remain != 16 {
decompressed.resize(remain + orig_len, remain as _);
}
let unpad_size = decrypt(&mut decompressed, Self::AES_KEY, Self::AES_IV)?.len();
decompressed.truncate(unpad_size);
Ok(decompressed)
@@ -26,6 +41,10 @@ impl MaiVersionExt for Sdgb1_40 {
impl MaiVersionExt for Sdgb1_50 {
fn decode(mut data: impl AsMut<[u8]>) -> Result<Vec<u8>, ApiError> {
if data.as_mut().is_empty() {
return Err(ApiError::EmptyResponse);
}
let decrypted = decrypt(&mut data, Self::AES_KEY, Self::AES_IV)?;
let decompressed = decompress(decrypted);
Ok(decompressed)

View File

@@ -3,28 +3,34 @@ use snafu::Snafu;
#[derive(Debug, Snafu)]
pub enum ApiError {
#[snafu(display("pad data: {error}"))]
PadError { error: PadError },
#[snafu(display("unpad data: {error}"))]
UnpadError { error: UnpadError },
#[snafu(display("api returned nothing!"))]
EmptyResponse,
#[snafu(display("encrypt data: {error}"))]
Pad { error: PadError },
#[snafu(display("decrypt data: {error}"))]
Unpad { error: UnpadError },
#[snafu(display("io error: {source}"))]
#[snafu(context(false))]
IOError { source: std::io::Error },
IO { source: std::io::Error },
#[snafu(display("json error: {source}"))]
#[snafu(context(false))]
JSONError { source: serde_json::Error },
JSON { source: serde_json::Error },
#[snafu(display("request error: {source}"))]
#[snafu(context(false))]
Request { source: nyquest::Error },
}
impl From<UnpadError> for ApiError {
fn from(error: UnpadError) -> Self {
Self::UnpadError { error }
Self::Unpad { error }
}
}
impl From<PadError> for ApiError {
fn from(error: PadError) -> Self {
Self::PadError { error }
Self::Pad { error }
}
}

View File

@@ -9,7 +9,7 @@ pub mod model;
mod error;
pub use error::ApiError;
use nyquest::{
Body,
AsyncClient, Body,
r#async::Request,
header::{ACCEPT_ENCODING, CONTENT_ENCODING, EXPECT, USER_AGENT},
};
@@ -62,6 +62,23 @@ pub trait MaiVersionExt: MaiVersion {
Ok(req)
}
fn request<D>(
client: &AsyncClient,
api: APIMethod,
agent_extra: impl Display,
data: D,
) -> impl Future<Output = Result<Vec<u8>, ApiError>>
where
D: Serialize,
{
async {
let req = Self::api_request(api, agent_extra, data)?;
let data = client.request(req).await?.bytes().await?;
let decoded = Self::decode(data)?;
Ok(decoded)
}
}
}
pub struct Sdgb1_40;

View File

@@ -1,4 +1,4 @@
use serde::Serialize;
#[derive(Serialize)]
pub struct Ping;
pub struct Ping {}