added api request with reqwest

This commit is contained in:
alex 2026-02-26 10:58:15 +08:00
parent be71e8404a
commit 9d52223f84
3 changed files with 196 additions and 2 deletions

192
src/bin/punchnet/api/mod.rs Normal file
View File

@ -0,0 +1,192 @@
use reqwest::Client;
use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
use serde::{Deserialize, Serialize};
pub const TEST_PREFIX: &'static str = "https://punchnet.s5s8.com/api";
#[derive(Serialize)]
struct TokenLoginData<'a> {
client_id: &'a str,
token: &'a str,
mac: &'a str,
system: &'a str,
version: &'a str,
}
#[derive(Serialize)]
struct UserPassLoginData<'a> {
client_id: &'a str,
username: &'a str,
password: &'a str,
mac: &'a str,
system: &'a str,
version: &'a str,
}
#[derive(Debug, Deserialize)]
pub struct LoginResponse {
pub code: i8,
pub message: String,
pub data: Option<LoginData>,
}
#[derive(Debug, Deserialize)]
pub struct LoginData {
pub access_token: String,
pub username: String,
pub user_type: String,
pub audit: u32,
pub network_id: u32,
pub network_name: String,
pub network_domain: String,
pub exit_node: Vec<ExitNode>,
}
#[derive(Debug, Deserialize)]
pub struct ExitNode {
pub nnid: u32,
pub node_name: String,
}
async fn post_with_data<T, R>(
url: &str,
data: T,
) -> Result<R>
where T: Serialize,
R: for<'de> Deserialize<'de>
{
let client = Client::new();
let Ok(response) = client
.post(url)
.json(&data)
.send()
.await else {
return Err(SDLanError::IOError("failed to do request".to_owned()));
};
println!("status: {}", response.status());
let Ok(data) = response.json().await else {
return Err(SDLanError::IOError("failed to jsonify response".to_owned()));
};
// println!("got respose: {:?}", data);
Ok(data)
}
pub async fn login_with_user_pass(
url_prefix: &str,
client_id: &str,
username: &str,
password: &str,
mac: Mac,
system: &str,
version: &str,
) -> Result<LoginResponse> {
let mac = format!("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
);
let post_data = UserPassLoginData {
client_id,
username,
password,
mac: &mac,
system,
version,
};
post_with_data(&format!("{}/auth/login", url_prefix), post_data).await
}
pub async fn login_with_token(
url_prefix: &str,
client_id: &str,
token: &str,
mac: Mac,
system: &str,
version: &str,
) -> Result<LoginResponse> {
let mac = format!("{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
);
let post_data = TokenLoginData {
client_id,
token,
mac: &mac,
system,
version,
};
post_with_data(&format!("{}/auth/token", url_prefix), post_data).await
}
#[derive(Serialize)]
struct ConnectDisconnectRequest<'a> {
client_id: &'a str,
access_token: &'a str,
}
#[derive(Deserialize, Debug)]
pub struct ConnectResponse {
pub code: i32,
pub message: String,
pub data: Option<ConnectData>,
}
#[derive(Deserialize, Debug)]
pub struct ConnectData {
pub ip: String,
pub resource_list: Vec<ResourceList>,
pub node_list: Vec<NodeList>,
// pub acl: Vec<ACL>,
}
#[derive(Deserialize, Debug)]
pub struct ResourceList {
pub name: String,
pub url: String,
}
#[derive(Deserialize, Debug)]
pub struct NodeList {
pub name: String,
pub connection_status: String,
pub ip: String,
pub system: String,
}
#[derive(Deserialize, Debug)]
pub struct DisconnectResponse {
pub code: i32,
pub message: String,
}
pub async fn connect(
url_prefix: &str,
client_id: &str,
access_token: &str,
) -> Result<ConnectResponse> {
let url = format!("{}/connect", url_prefix);
let data = ConnectDisconnectRequest {
client_id,
access_token,
};
post_with_data(&url, data).await
}
pub async fn disconnect(
url_prefix: &str,
client_id: &str,
access_token: &str,
) -> Result<DisconnectResponse> {
let url = format!("{}/disconnect", url_prefix);
let data = ConnectDisconnectRequest {
client_id,
access_token,
};
post_with_data(&url, data).await
}

View File

@ -1,3 +1,5 @@
mod api;
use punchnet::get_base_dir; use punchnet::get_base_dir;
use punchnet::get_edge; use punchnet::get_edge;
use punchnet::mod_hostname; use punchnet::mod_hostname;
@ -13,6 +15,8 @@ use tracing::error;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use structopt::StructOpt; use structopt::StructOpt;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
set_base_dir("/usr/local/punchnet"); set_base_dir("/usr/local/punchnet");

View File

@ -38,7 +38,6 @@ pub async fn async_main(
tokio::spawn(run_ipv6(edge, rx)); tokio::spawn(run_ipv6(edge, rx));
// TODO: change the quic logic // TODO: change the quic logic
/*
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
let conn = edge.quic_endpoint.connect("192.168.0.1".parse().unwrap(), "www.punchnet.com").unwrap().await.unwrap(); let conn = edge.quic_endpoint.connect("192.168.0.1".parse().unwrap(), "www.punchnet.com").unwrap().await.unwrap();
@ -70,7 +69,6 @@ pub async fn async_main(
edge.quic_endpoint.wait_idle().await; edge.quic_endpoint.wait_idle().await;
} }
}); });
*/
////////////////// to here ////////////////// to here
init_tcp_conn( init_tcp_conn(