From 9d52223f840cb3f31bae1283f780c2fe7aacb803 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 26 Feb 2026 10:58:15 +0800 Subject: [PATCH] added api request with reqwest --- src/bin/punchnet/api/mod.rs | 192 ++++++++++++++++++++++++++++++++++++ src/bin/punchnet/main.rs | 4 + src/network/async_main.rs | 2 - 3 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 src/bin/punchnet/api/mod.rs diff --git a/src/bin/punchnet/api/mod.rs b/src/bin/punchnet/api/mod.rs new file mode 100644 index 0000000..b7ce30d --- /dev/null +++ b/src/bin/punchnet/api/mod.rs @@ -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, +} + + +#[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, +} + +#[derive(Debug, Deserialize)] +pub struct ExitNode { + pub nnid: u32, + pub node_name: String, +} + +async fn post_with_data( + url: &str, + data: T, +) -> Result +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 { + 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 { + 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, +} + +#[derive(Deserialize, Debug)] +pub struct ConnectData { + pub ip: String, + pub resource_list: Vec, + pub node_list: Vec, + // pub acl: Vec, +} + +#[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 { + 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 { + let url = format!("{}/disconnect", url_prefix); + let data = ConnectDisconnectRequest { + client_id, + access_token, + }; + post_with_data(&url, data).await +} \ No newline at end of file diff --git a/src/bin/punchnet/main.rs b/src/bin/punchnet/main.rs index cbc754b..b75e56b 100755 --- a/src/bin/punchnet/main.rs +++ b/src/bin/punchnet/main.rs @@ -1,3 +1,5 @@ +mod api; + use punchnet::get_base_dir; use punchnet::get_edge; use punchnet::mod_hostname; @@ -13,6 +15,8 @@ use tracing::error; use std::net::ToSocketAddrs; use structopt::StructOpt; + + #[tokio::main] async fn main() { set_base_dir("/usr/local/punchnet"); diff --git a/src/network/async_main.rs b/src/network/async_main.rs index 19a834f..f001889 100755 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -38,7 +38,6 @@ pub async fn async_main( tokio::spawn(run_ipv6(edge, rx)); // TODO: change the quic logic - /* tokio::spawn(async move { loop { 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; } }); - */ ////////////////// to here init_tcp_conn(