NewLife/Stardust

如果没有DNS地址,在Linux下直接读取/etc/resolv.conf文件
大石头 authored at 2025-08-03 10:25:35
804bb7a
Tree
1 Parent(s) dc5e386
Summary: 1 changed files with 25 additions and 1 deletions.
Modified +25 -1
Modified +25 -1
diff --git a/Stardust/Models/AgentInfo.cs b/Stardust/Models/AgentInfo.cs
index 71620e5..674fc08 100644
--- a/Stardust/Models/AgentInfo.cs
+++ b/Stardust/Models/AgentInfo.cs
@@ -119,7 +119,31 @@ public class AgentInfo
             var dns = NetworkInterface.GetAllNetworkInterfaces()
                 .SelectMany(e => e.GetIPProperties().DnsAddresses)
                 .FirstOrDefault(e => e.AddressFamily == AddressFamily.InterNetwork);
-            return _dns = dns?.ToString() ?? String.Empty;
+            if (dns != null) return _dns = dns.ToString();
+
+            // 如果没有DNS地址,在Linux下直接读取/etc/resolv.conf文件
+            if (dns == null && Runtime.Linux)
+            {
+                var file = "/etc/resolv.conf";
+                if (File.Exists(file))
+                {
+                    // 读取文件所有行,清空前后空白后,找到第一个nameserver开头的行,截取IP地址数据,去掉#结尾的注释
+                    var lines = File.ReadAllLines(file);
+                    foreach (var item in lines)
+                    {
+                        var line = item.Trim();
+                        if (line.IsNullOrEmpty() || !line.StartsWith("nameserver")) continue;
+
+                        line = line["nameserver".Length..].Trim();
+                        var p = line.IndexOf('#');
+                        if (p > 0) line = line[..p].Trim(); // 去掉#结尾的注释
+
+                        return _dns = line;
+                    }
+                }
+            }
+
+            return _dns = String.Empty;
         }
         catch
         {