Parcourir la source

【新增】支持 IPv6 地址绑定

将默认绑定地址从 "0.0.0.0" 更改为 "::",以同时支持 IPv4 和 IPv6。
更新了命令行参数说明和配置结构体的注释。
增强了地址解析逻辑,正确处理 IPv6 地址格式(如添加方括号)。
新增多个测试用例,覆盖 IPv4、IPv6 及带方括号的地址场景。
更新版本号从 0.4.1 到 0.5.0。
PJ568 il y a 1 an
Parent
commit
fb851f2889
4 fichiers modifiés avec 55 ajouts et 12 suppressions
  1. 1 1
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 2 2
      src/main.rs
  4. 51 8
      src/server.rs

+ 1 - 1
Cargo.lock

@@ -942,7 +942,7 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
 
 [[package]]
 name = "swb-sys-monitor"
-version = "0.4.1"
+version = "0.5.0"
 dependencies = [
  "anyhow",
  "clap",

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "swb-sys-monitor"
-version = "0.4.1"
+version = "0.5.0"
 authors = ["PJ568 <[email protected]>"]
 edition = "2024"
 description = "为嵌入式设备高并发监控场景设计的极简网页资源占用显示系统"

+ 2 - 2
src/main.rs

@@ -12,8 +12,8 @@ use server::{Config, StatusServer};
 #[derive(Parser, Debug)]
 #[command(author, version, about, long_about = None)]
 struct Args {
-    /// 服务器绑定地址 (默认: 0.0.0.0)
-    #[arg(short, long, default_value = "0.0.0.0")]
+    /// 服务器绑定地址 (默认: ::,支持 IPv4 和 IPv6)
+    #[arg(short, long, default_value = "::")]
     address: String,
 
     /// 服务器端口 (默认: 8080)

+ 51 - 8
src/server.rs

@@ -222,18 +222,55 @@ mod tests {
     #[tokio::test]
     async fn test_config_default() {
         let config = Config::default();
-        assert_eq!(config.bind_address, "0.0.0.0");
+        assert_eq!(config.bind_address, "::");
         assert_eq!(config.port, 8080);
         assert_eq!(config.cache_ttl_seconds, 10);
     }
 
     #[tokio::test]
-    async fn test_config_address() {
-        let config = Config::default();
+    async fn test_config_address_ipv4() {
+        let config = Config {
+            bind_address: "0.0.0.0".to_string(),
+            port: 8080,
+            cache_ttl_seconds: 10,
+        };
         let addr = config.address();
         assert_eq!(addr.to_string(), "0.0.0.0:8080");
     }
 
+    #[tokio::test]
+    async fn test_config_address_ipv6() {
+        let config = Config {
+            bind_address: "::".to_string(),
+            port: 8080,
+            cache_ttl_seconds: 10,
+        };
+        let addr = config.address();
+        assert_eq!(addr.to_string(), "[::]:8080");
+    }
+
+    #[tokio::test]
+    async fn test_config_address_ipv6_specific() {
+        let config = Config {
+            bind_address: "2001:db8::1".to_string(),
+            port: 9090,
+            cache_ttl_seconds: 10,
+        };
+        let addr = config.address();
+        assert_eq!(addr.to_string(), "[2001:db8::1]:9090");
+    }
+
+    #[tokio::test]
+    async fn test_config_address_ipv6_with_brackets() {
+        let config = Config {
+            bind_address: "[::1]".to_string(),
+            port: 8080,
+            cache_ttl_seconds: 10,
+        };
+        let addr = config.address();
+        assert_eq!(addr.to_string(), "[::1]:8080");
+    }
+
     #[tokio::test]
     async fn test_status_server_creation() {
         let cache = create_cache(10);
@@ -448,7 +485,7 @@ mod tests {
 /// 配置结构
 #[derive(Debug, Clone)]
 pub struct Config {
-    /// 服务端绑定地址
+    /// 服务端绑定地址(支持 IPv4 和 IPv6)
     pub bind_address: String,
     /// 服务端端口
     pub port: u16,
@@ -460,7 +497,8 @@ impl Default for Config {
     #[inline]
     fn default() -> Self {
         Self {
-            bind_address: "0.0.0.0".to_string(),
+            // 使用 :: 作为默认地址,支持 IPv4 和 IPv6 双栈
+            bind_address: "::".to_string(),
             port: 8080,
             cache_ttl_seconds: 10, // 严格 10 秒过期
         }
@@ -471,8 +509,13 @@ impl Config {
     /// 构建服务器地址
     #[inline]
     pub fn address(&self) -> SocketAddr {
-        format!("{}:{}", self.bind_address, self.port)
-            .parse()
-            .expect("无效的地址格式")
+        // 对于 IPv6 地址,需要用方括号包围
+        let addr_str = if self.bind_address.contains(':') && !self.bind_address.starts_with('[') {
+            format!("[{}]:{}", self.bind_address, self.port)
+        } else {
+            format!("{}:{}", self.bind_address, self.port)
+        };
+
+        addr_str.parse().expect("无效的地址格式")
     }
 }