From 5594ce639b4d4a24f941f914e540fe6d1ed214a6 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 13 Apr 2026 18:02:18 +0800 Subject: [PATCH] api setting --- .vscode/settings.json | 2 +- Cargo.lock | 190 +++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/bin/punchnet/api/mod.rs | 5 +- src/network/tun_linux.rs | 64 +++++++----- 5 files changed, 234 insertions(+), 28 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fb0827b..9eb94d8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - // "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", + "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", // "rust-analyzer.cargo.features": ["tun"] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 539380e..11cd6be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -149,6 +149,15 @@ version = "1.0.102" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c" +[[package]] +name = "aok" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a8746a6637032084d12397dcd4b1f93d7c5e27dab4a2abe37515e5dadb7cdad" +dependencies = [ + "anyhow", +] + [[package]] name = "ar" version = "0.9.0" @@ -275,6 +284,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "boxleak" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "313d0a9cc1832e6636eec848805ab5471a546dc68dcfc058eea4d4602892cf64" +dependencies = [ + "aok", +] + [[package]] name = "bumpalo" version = "3.20.2" @@ -384,6 +402,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "cfg_aliases" version = "0.2.1" @@ -438,6 +462,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "citer" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe4fe42a458d9c1b72d540bb4bb0226bcfc2136c53b4407af5e23c9f7532f3ea" +dependencies = [ + "aok", +] + [[package]] name = "clap" version = "2.34.0" @@ -502,6 +535,17 @@ dependencies = [ "cc", ] +[[package]] +name = "coarsetime" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e58eb270476aa4fc7843849f8a35063e8743b4dbcdf6dd0f8ea0886980c204c2" +dependencies = [ + "libc", + "wasix", + "wasm-bindgen", +] + [[package]] name = "colorchoice" version = "1.0.5" @@ -784,6 +828,18 @@ dependencies = [ "windows-sys 0.60.2", ] +[[package]] +name = "dns_parse" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3943bb4aeaf3b6b8f9b8a8fa04609423ee8ab803e81c55385a18a7f86da339a9" +dependencies = [ + "bytes", + "hex", + "idns", + "thiserror 2.0.18", +] + [[package]] name = "dotenvy" version = "0.15.7" @@ -875,6 +931,19 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "expire_cache" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d65c4482fb3725e7d25768dd33864b29dcb1f9ad365742649a8eae8b6bec43ed" +dependencies = [ + "boxleak", + "papaya", + "parking_lot", + "sendptr", + "tokio", +] + [[package]] name = "fastbloom" version = "0.14.1" @@ -1459,6 +1528,22 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "idns" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45ddb9b2206e9ff4b548671e4372986ca71f90efbef3d74c3e9c5893c7eec869" +dependencies = [ + "expire_cache", + "ip_set", + "log", + "papaya", + "pick_fast", + "race", + "static_init", + "ts_", +] + [[package]] name = "indexmap" version = "2.13.0" @@ -1481,6 +1566,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "ip_set" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f71689e048d8568faba23fdc3a0822cd10738515b1c0b3851d84221663fec5" + [[package]] name = "ipnet" version = "2.12.0" @@ -1878,6 +1969,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe" +[[package]] +name = "papaya" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "997ee03cd38c01469a7046643714f0ad28880bcb9e6679ff0666e24817ca19b7" +dependencies = [ + "equivalent", + "seize", +] + [[package]] name = "parking_lot" version = "0.12.5" @@ -1932,6 +2033,17 @@ dependencies = [ "indexmap", ] +[[package]] +name = "pick_fast" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8e330233974b83cda2e28df3ddf3e0d1675f1d4496f031e00a7a4a0b67d0506" +dependencies = [ + "citer", + "fastrand", + "log", +] + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -2154,6 +2266,7 @@ dependencies = [ "daemonize", "dashmap 6.1.0", "dns-lookup", + "dns_parse", "etherparse", "futures-util", "hex", @@ -2199,7 +2312,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20" dependencies = [ "bytes", - "cfg_aliases", + "cfg_aliases 0.2.1", "pin-project-lite", "quinn-proto", "quinn-udp", @@ -2241,7 +2354,7 @@ version = "0.5.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd" dependencies = [ - "cfg_aliases", + "cfg_aliases 0.2.1", "libc", "once_cell", "socket2", @@ -2270,6 +2383,17 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "race" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402085c832dd003de54bfa3b728c8b4b69632dc32a402c0c70cae8361bc2ec79" +dependencies = [ + "coarsetime", + "futures", + "tokio", +] + [[package]] name = "rand" version = "0.8.5" @@ -2691,12 +2815,28 @@ dependencies = [ "libc", ] +[[package]] +name = "seize" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b55fb86dfd3a2f5f76ea78310a88f96c4ea21a3031f8d212443d56123fd0521" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "semver" version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2" +[[package]] +name = "sendptr" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ada566c72d62f4c53e5d0e593f4bab893dfb7d14b505a130b148189928f0988" + [[package]] name = "serde" version = "1.0.228" @@ -3088,6 +3228,34 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "static_init" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bae1df58c5fea7502e8e352ec26b5579f6178e1fdb311e088580c980dee25ed" +dependencies = [ + "bitflags 1.3.2", + "cfg_aliases 0.2.1", + "libc", + "parking_lot", + "parking_lot_core", + "static_init_macro", + "winapi", +] + +[[package]] +name = "static_init_macro" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1389c88ddd739ec6d3f8f83343764a0e944cd23cfbf126a9796a714b0b6edd6f" +dependencies = [ + "cfg_aliases 0.1.1", + "memchr", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "stringprep" version = "0.1.5" @@ -3553,6 +3721,15 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "ts_" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d4270cf460c46e2cafb448d9abebc506dff81e15148f19b2fc97686bfe8d4f" +dependencies = [ + "coarsetime", +] + [[package]] name = "typenum" version = "1.19.0" @@ -3740,6 +3917,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" +[[package]] +name = "wasix" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1757e0d1f8456693c7e5c6c629bdb54884e032aa0bb53c155f6a39f94440d332" +dependencies = [ + "wasi", +] + [[package]] name = "wasm-bindgen" version = "0.2.114" diff --git a/Cargo.toml b/Cargo.toml index e4897ce..4b8fd8f 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ ahash = "0.8.12" ipnet = "2.12.0" arc-swap = "1.9.0" rustls-native-certs = "0.8.3" +dns_parse = "0.1.2" # rolling-file = { path = "../rolling-file" } [target.'cfg(unix)'.dependencies] diff --git a/src/bin/punchnet/api/mod.rs b/src/bin/punchnet/api/mod.rs index a777d7c..d0e7946 100644 --- a/src/bin/punchnet/api/mod.rs +++ b/src/bin/punchnet/api/mod.rs @@ -91,12 +91,12 @@ pub struct LoginData { pub network_id: u32, pub network_name: String, // pub network_domain: String, - pub exit_node: Vec, + // pub exit_node: Vec, } #[derive(Debug, Deserialize)] pub struct ExitNode { - pub nnid: u32, + pub node_id: u32, pub node_name: String, } @@ -216,6 +216,7 @@ pub struct ConnectData { pub resource_list: Vec, pub node_list: Vec, // pub acl: Vec, + pub exit_node: Vec, } #[derive(Deserialize, Debug)] diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index e102a3f..7d33de8 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -1,7 +1,9 @@ +use bytes::Bytes; #[cfg(not(feature = "tun"))] use bytes::BytesMut; #[cfg(feature = "tun")] -use bytes::{Bytes, BytesMut}; +use bytes::{BytesMut}; + use etherparse::{Ethernet2Header}; use ipnet::Ipv4Net; use sdlan_sn_rs::config::SDLAN_DEFAULT_TTL; @@ -175,11 +177,9 @@ impl Iface { } // TODO: set dns should be opened - /* if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) { error!("failed to set dns: {}", e.as_str()); } - */ } else { info!("set tun device"); let res = Command::new("ifconfig") @@ -294,30 +294,31 @@ impl TunTapPacketHandler for Iface { if let Some(transport) = headers.transport { match ipv4.protocol { IpNumber::TCP => { - let tcp = transport.tcp().unwrap(); + if let Some(tcp) = transport.tcp() { + let out_five_tuple = FiveTuple { + src_ip: ipv4.source.into(), + dst_ip: ipv4.destination.into(), + src_port: tcp.source_port, + dst_port: tcp.destination_port, + proto: IpNumber::TCP.0, + }; - let out_five_tuple = FiveTuple { - src_ip: ipv4.source.into(), - dst_ip: ipv4.destination.into(), - src_port: tcp.source_port, - dst_port: tcp.destination_port, - proto: IpNumber::TCP.0, - }; + edge.rule_cache.touch_packet(out_five_tuple); + } - edge.rule_cache.touch_packet(out_five_tuple); // is tcp } IpNumber::UDP => { - let udp = transport.udp().unwrap(); - - let out_five_tuple = FiveTuple { - src_ip: ipv4.source.into(), - dst_ip: ipv4.destination.into(), - src_port: udp.source_port, - dst_port: udp.destination_port, - proto: IpNumber::UDP.0, - }; - edge.rule_cache.touch_packet(out_five_tuple); + if let Some(udp) = transport.udp() { + let out_five_tuple = FiveTuple { + src_ip: ipv4.source.into(), + dst_ip: ipv4.destination.into(), + src_port: udp.source_port, + dst_port: udp.destination_port, + proto: IpNumber::UDP.0, + }; + edge.rule_cache.touch_packet(out_five_tuple); + } } _other => { @@ -327,6 +328,7 @@ impl TunTapPacketHandler for Iface { if u32::from_be_bytes(ipv4.destination) == DNS_IP { // should send to dns + parse_dns_payload(edge, &headers.payload); if let Err(e) = edge.udp_sock_for_dns.send_to(&data[14..], format!("{}:15353", edge.server_ip)).await { error!("failed to send request to 15353: {}", e); } @@ -1025,4 +1027,20 @@ pub async fn arp_reply_arrived(edge: &Node, data: SdlArpResponse) { if let Err(_e) = edge.device.send(&data) { error!("failed to write arp response to device"); } -} \ No newline at end of file +} + +/* +fn parse_dns_payload(edge: &Node, payload: &[u8]) { + match dns_parse::parse(Bytes::from(payload)) { + Ok(packet) => { + packet[0]. + if let Some(questions) = packet.questions.first() { + debug!("got dns query for {:?}", questions.qname); + } + } + Err(e) => { + error!("failed to parse dns packet: {}", e.to_string()); + } + } +} +*/ \ No newline at end of file