token login
This commit is contained in:
parent
c6df26ac85
commit
372afd470b
3
Cargo.lock
generated
3
Cargo.lock
generated
@ -2075,8 +2075,11 @@ dependencies = [
|
|||||||
"dns-lookup",
|
"dns-lookup",
|
||||||
"etherparse",
|
"etherparse",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
"hex",
|
||||||
|
"hmac",
|
||||||
"libc",
|
"libc",
|
||||||
"local-ip-address",
|
"local-ip-address",
|
||||||
|
"md-5",
|
||||||
"myactor",
|
"myactor",
|
||||||
"num_enum",
|
"num_enum",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|||||||
@ -36,6 +36,9 @@ clap = { version = "4.5.60", features = ["derive", "env"] }
|
|||||||
rpassword = "7.4.0"
|
rpassword = "7.4.0"
|
||||||
serde_json = "1.0.149"
|
serde_json = "1.0.149"
|
||||||
chacha20poly1305 = "0.10.1"
|
chacha20poly1305 = "0.10.1"
|
||||||
|
hmac = "0.12.1"
|
||||||
|
md-5 = "0.10.6"
|
||||||
|
hex = "0.4.3"
|
||||||
# rolling-file = { path = "../rolling-file" }
|
# rolling-file = { path = "../rolling-file" }
|
||||||
|
|
||||||
[target.'cfg(unix)'.dependencies]
|
[target.'cfg(unix)'.dependencies]
|
||||||
|
|||||||
@ -1,9 +1,22 @@
|
|||||||
|
use hmac::{Hmac, Mac as HamcMac};
|
||||||
|
use punchnet::TokenLogin;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
|
use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use md5::Md5;
|
||||||
|
|
||||||
pub const TEST_PREFIX: &'static str = "https://punchnet.s5s8.com/api";
|
pub const TEST_PREFIX: &'static str = "https://punchnet.s5s8.com/api";
|
||||||
|
|
||||||
|
const DIGEST_KEY: &'static str = "H6p*2RfEu4ITcL";
|
||||||
|
type HmacMd5 = Hmac<Md5>;
|
||||||
|
|
||||||
|
trait HMacCalculator {
|
||||||
|
fn calculate_hmac(&self) -> Result<String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_hmac<T: HMacCalculator>(cal: T) -> Result<String> {
|
||||||
|
cal.calculate_hmac()
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct TokenLoginData<'a> {
|
struct TokenLoginData<'a> {
|
||||||
@ -14,6 +27,29 @@ struct TokenLoginData<'a> {
|
|||||||
version: &'a str,
|
version: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn do_calculate(data: &[u8]) -> Result<String>{
|
||||||
|
let Ok(mut mac) = HmacMd5::new_from_slice(DIGEST_KEY.as_bytes()) else {
|
||||||
|
return Err(SDLanError::IOError("failed to new hmac".to_owned()));
|
||||||
|
};
|
||||||
|
mac.update(data);
|
||||||
|
let result = mac.finalize().into_bytes();
|
||||||
|
Ok(hex::encode(result))
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <'a> HMacCalculator for TokenLoginData<'a> {
|
||||||
|
fn calculate_hmac(&self) -> Result<String> {
|
||||||
|
let data = format!("client_id={}&mac={}&system={}&token={}&version={}",
|
||||||
|
self.client_id,
|
||||||
|
self.mac,
|
||||||
|
self.system,
|
||||||
|
self.token,
|
||||||
|
self.version,
|
||||||
|
);
|
||||||
|
do_calculate(data.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct UserPassLoginData<'a> {
|
struct UserPassLoginData<'a> {
|
||||||
client_id: &'a str,
|
client_id: &'a str,
|
||||||
@ -24,6 +60,20 @@ struct UserPassLoginData<'a> {
|
|||||||
version: &'a str,
|
version: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HMacCalculator for UserPassLoginData<'_> {
|
||||||
|
fn calculate_hmac(&self) -> Result<String> {
|
||||||
|
let data = format!("client_id={}&mac={}&password={}&system={}&username={}&version={}",
|
||||||
|
self.client_id,
|
||||||
|
self.mac,
|
||||||
|
self.password,
|
||||||
|
self.system,
|
||||||
|
self.username,
|
||||||
|
self.version,
|
||||||
|
);
|
||||||
|
do_calculate(data.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct LoginResponse {
|
pub struct LoginResponse {
|
||||||
pub code: i32,
|
pub code: i32,
|
||||||
@ -54,14 +104,18 @@ async fn post_with_data<T, R>(
|
|||||||
url: &str,
|
url: &str,
|
||||||
data: T,
|
data: T,
|
||||||
) -> Result<R>
|
) -> Result<R>
|
||||||
where T: Serialize,
|
where T: Serialize + HMacCalculator,
|
||||||
R: for<'de> Deserialize<'de>
|
R: for<'de> Deserialize<'de>
|
||||||
{
|
{
|
||||||
|
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
|
let Ok(hmac) = data.calculate_hmac() else {
|
||||||
|
return Err(SDLanError::IOError("failed to calculate hmac".to_owned()));
|
||||||
|
};
|
||||||
|
|
||||||
let Ok(response) = client
|
let Ok(response) = client
|
||||||
.post(url)
|
.post(url)
|
||||||
|
.header("X-sign", hmac)
|
||||||
.json(&data)
|
.json(&data)
|
||||||
.send()
|
.send()
|
||||||
.await else {
|
.await else {
|
||||||
@ -70,7 +124,6 @@ where T: Serialize,
|
|||||||
|
|
||||||
// println!("status: {}", response.status());
|
// println!("status: {}", response.status());
|
||||||
let text = response.text().await.unwrap();
|
let text = response.text().await.unwrap();
|
||||||
// println!("text = {}", text);
|
|
||||||
|
|
||||||
let data = serde_json::from_str(&text).unwrap();
|
let data = serde_json::from_str(&text).unwrap();
|
||||||
|
|
||||||
@ -137,6 +190,16 @@ struct ConnectDisconnectRequest<'a> {
|
|||||||
access_token: &'a str,
|
access_token: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HMacCalculator for ConnectDisconnectRequest<'_> {
|
||||||
|
fn calculate_hmac(&self) -> Result<String> {
|
||||||
|
let data = format!("access_token={}&client_id={}",
|
||||||
|
self.access_token,
|
||||||
|
self.client_id
|
||||||
|
);
|
||||||
|
do_calculate(data.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct ConnectResponse {
|
pub struct ConnectResponse {
|
||||||
pub code: i32,
|
pub code: i32,
|
||||||
@ -210,6 +273,17 @@ struct GetResourceRequest<'a> {
|
|||||||
id: i32,
|
id: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl HMacCalculator for GetResourceRequest<'_> {
|
||||||
|
fn calculate_hmac(&self) -> Result<String> {
|
||||||
|
let data = format!("access_token={}&client_id={}&id={}",
|
||||||
|
self.access_token,
|
||||||
|
self.client_id,
|
||||||
|
self.id,
|
||||||
|
);
|
||||||
|
do_calculate(data.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct GetResourceResponse {
|
pub struct GetResourceResponse {
|
||||||
pub code: i32,
|
pub code: i32,
|
||||||
|
|||||||
@ -354,7 +354,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let should_daemonize = true;
|
let should_daemonize = false;
|
||||||
|
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
if should_daemonize {
|
if should_daemonize {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user