From 41a21188bf33d13a2fe7f039028edef079f5f9b0 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 22 Dec 2025 14:25:00 +0800 Subject: [PATCH] added hostname for dns, first step --- Cargo.lock | 17 ++++++++++++++--- Cargo.toml | 1 + message.proto | 2 ++ src/bin/punchnet/main.rs | 2 ++ src/lib.rs | 4 ++++ src/network/async_main.rs | 3 +++ src/network/node.rs | 6 ++++++ src/pb/message.rs | 4 ++++ 8 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df0dbc6..6f5b9e1 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -1642,13 +1642,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" [[package]] -name = "openssl-sys" -version = "0.9.110" +name = "openssl-src" +version = "300.5.4+3.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a9f0075ba3c21b09f8e8b2026584b1d18d49388648f2fbbf3c97ea8deced8e2" +checksum = "a507b3792995dae9b0df8a1c1e3771e8418b7c2d9f0baeba32e6fe8b06c7cb72" +dependencies = [ + "cc", +] + +[[package]] +name = "openssl-sys" +version = "0.9.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82cab2d520aa75e3c58898289429321eb788c3106963d0dc886ec7a5f4adc321" dependencies = [ "cc", "libc", + "openssl-src", "pkg-config", "vcpkg", ] @@ -1889,6 +1899,7 @@ dependencies = [ "local-ip-address", "num_enum", "once_cell", + "openssl-sys", "prost", "prost-build", "rand", diff --git a/Cargo.toml b/Cargo.toml index 6a5b86c..6a62ddc 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ futures-util = "0.3.30" local-ip-address = "0.6.1" num_enum = "0.7.2" once_cell = "1.19.0" +openssl-sys = { version = "0.9.111", features = ["vendored"] } prost = "0.12.6" prost-build = "0.12.6" rand = "0.8.5" diff --git a/message.proto b/message.proto index 9e4c270..96eb93c 100644 --- a/message.proto +++ b/message.proto @@ -19,6 +19,7 @@ message SDLDevAddr { bytes mac = 2; uint32 net_addr = 3; uint32 net_bit_len = 4; + string network_domain = 5; } // tcp通讯消息 @@ -34,6 +35,7 @@ message SDLRegisterSuper { string pub_key = 5; string token = 6; string network_code = 7; + string hostname = 8; } message SDLRegisterSuperAck { diff --git a/src/bin/punchnet/main.rs b/src/bin/punchnet/main.rs index 3be3c03..cbadb8b 100755 --- a/src/bin/punchnet/main.rs +++ b/src/bin/punchnet/main.rs @@ -7,6 +7,7 @@ use punchnet::CommandLine; use punchnet::CommandLineInput; use sdlan_sn_rs::log; +use sdlan_sn_rs::utils::gen_uuid_u64; use tracing::error; use std::time::Duration; @@ -50,6 +51,7 @@ async fn main() { }, tx, &punchnet::get_install_channel(), + Some(format!("{:08x}", gen_uuid_u64() as u32)), None, ) .await; diff --git a/src/lib.rs b/src/lib.rs index d1972b3..d73b64e 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -44,6 +44,7 @@ pub async fn run_sdlan( sender: std::sync::mpsc::Sender, install_channel: &str, + hostname: Option, connecting_chan: Option>, // start_stop_sender: Sender, // start_stop_receiver: Receiver, ) -> Result<()> { @@ -53,6 +54,8 @@ pub async fn run_sdlan( init_arp(); + let hostname = hostname.unwrap_or("".to_owned()); + if let Err(e) = init_edge( &args.token, &args.network_code, @@ -61,6 +64,7 @@ pub async fn run_sdlan( start_stop_sender, args.mtu, connecting_chan.clone(), + hostname, ) .await { diff --git a/src/network/async_main.rs b/src/network/async_main.rs index 0ae96ce..331d94a 100755 --- a/src/network/async_main.rs +++ b/src/network/async_main.rs @@ -63,6 +63,7 @@ async fn handle_tcp_message(msg: SdlanTcp) { let ip = ip_to_string(&dev.net_addr); // debug!("aes key is {:?}, ip is {}/{}", aes, ip, dev.net_bit_len,); println!("assigned ip: {}", ip); + println!("network is: {}.{}", edge.hostname, dev.network_domain); edge.device_config .ip .net_addr @@ -317,10 +318,12 @@ pub async fn async_main( net_addr: 0, network_id: 0, net_bit_len: 0, + network_domain: "".to_owned(), }), pub_key: edge.rsa_pubkey.clone(), token, network_code: code, + hostname: edge.hostname.clone(), }; // debug!("send register super: {:?}", register_super); let packet_id = match pkt_id { diff --git a/src/network/node.rs b/src/network/node.rs index 691d1ef..7f4430a 100755 --- a/src/network/node.rs +++ b/src/network/node.rs @@ -38,6 +38,7 @@ pub async fn init_edge( start_stop: Sender, mtu: u32, connecting_chan: Option>, + hostname: String, ) -> Result<()> { // gen public key let rsa_path = format!("{}/.client", get_base_dir()); @@ -81,6 +82,7 @@ pub async fn init_edge( start_stop, mtu, connecting_chan, + hostname, ); do_init_edge(edge)?; @@ -118,6 +120,8 @@ pub struct Node { packet_id: AtomicU32, pub network_id: AtomicU32, + + pub hostname: String, pub tcp_pong: Arc, @@ -261,6 +265,7 @@ impl Node { start_stop: Sender, mtu: u32, connecting_chan: Option>, + hostname: String, ) -> Self { let mode = if cfg!(not(feature = "tun")) { Mode::Tap @@ -271,6 +276,7 @@ impl Node { Self { packet_id: AtomicU32::new(1), network_id: AtomicU32::new(0), + hostname, _token: Mutex::new(token.to_owned()), network_code: Mutex::new(network_code.to_owned()), diff --git a/src/pb/message.rs b/src/pb/message.rs index c6ce61d..d30d486 100644 --- a/src/pb/message.rs +++ b/src/pb/message.rs @@ -29,6 +29,8 @@ pub struct SdlDevAddr { pub net_addr: u32, #[prost(uint32, tag = "4")] pub net_bit_len: u32, + #[prost(string, tag = "5")] + pub network_domain: ::prost::alloc::string::String, } /// tcp通讯消息 #[allow(clippy::derive_partial_eq_without_eq)] @@ -51,6 +53,8 @@ pub struct SdlRegisterSuper { pub token: ::prost::alloc::string::String, #[prost(string, tag = "7")] pub network_code: ::prost::alloc::string::String, + #[prost(string, tag = "8")] + pub hostname: ::prost::alloc::string::String, } #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)]