🎨 Improve the network serve address list https://github.com/siyuan-note/siyuan/issues/14943

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2026-01-16 11:20:55 +08:00
parent 88e1f0312c
commit 8cbf2897bb
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 59 additions and 69 deletions

View file

@ -33,6 +33,64 @@ import (
"github.com/siyuan-note/logging"
)
// GetPrivateIPv4s 获取本地所有的私有 IPv4 地址(排除虚拟网卡)
func GetPrivateIPv4s() (ret []string) {
ret = []string{}
interfaces, err := net.Interfaces()
if err != nil {
return
}
// 常见的虚拟网卡名称关键字黑名单
virtualKeywords := []string{"docker", "veth", "br-", "vmnet", "vbox", "utun", "tun", "tap", "bridge", "cloud", "hyper-"}
for _, itf := range interfaces {
// 1. 基础状态过滤:必须是启动状态且不能是回环网卡
if itf.Flags&net.FlagUp == 0 || itf.Flags&net.FlagLoopback != 0 {
continue
}
// 2. 硬件地址过滤:物理网卡通常必须有 MAC 地址
if len(itf.HardwareAddr) == 0 {
continue
}
// 3. 名称过滤:排除已知虚拟网卡前缀
name := strings.ToLower(itf.Name)
isVirtual := false
for _, kw := range virtualKeywords {
if strings.Contains(name, kw) {
isVirtual = true
break
}
}
if isVirtual {
continue
}
// 4. 提取并校验 IP
addrs, err := itf.Addrs()
if err != nil {
continue
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
ip := ipNet.IP
// 仅保留 IPv4 且必须是私有局域网地址 (10.x, 172.16.x, 192.168.x)
if ip.To4() != nil && ip.IsPrivate() {
ret = append(ret, ip.String())
}
}
}
return
}
func IsLocalHostname(hostname string) bool {
if "localhost" == hostname || strings.HasSuffix(hostname, ".localhost") {
return true