feat: abstract method to perform action
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Ping;
|
||||
pub struct Ping {}
|
||||
|
||||
Reference in New Issue
Block a user