Преглед изворни кода

Prioritize LAN MACs in mDNS broadcast data

Add `getMaxMacAddrString(maxLength)` to build a capped MAC address list from local interfaces, prioritizing interfaces with private LAN IPv4 ranges (10/8, 172.16/12, 192.168/16) before others. Update `NewMDNS` to use this helper with a 128-character limit when populating the `mac_addr` TXT record, replacing the previous full join of all MACs.
Toby Chui пре 3 дана
родитељ
комит
1ac163cb58
2 измењених фајлова са 95 додато и 12 уклоњено
  1. 94 1
      src/mod/network/mdns/common.go
  2. 1 11
      src/mod/network/mdns/mdns.go

+ 94 - 1
src/mod/network/mdns/common.go

@@ -1,6 +1,8 @@
 package mdns
 
-import "net"
+import (
+	"net"
+)
 
 func stringInSlice(a string, list []string) bool {
 	for _, b := range list {
@@ -25,3 +27,94 @@ func getMacAddr() ([]string, error) {
 	}
 	return as, nil
 }
+
+func getMaxMacAddrString(maxLength int) (string, error) {
+	ifas, err := net.Interfaces()
+	if err != nil {
+		return "", err
+	}
+
+	type ifaceInfo struct {
+		mac    string
+		isLAN  bool
+		priority int
+	}
+
+	var ifaces []ifaceInfo
+	for _, ifa := range ifas {
+		mac := ifa.HardwareAddr.String()
+		if mac == "" {
+			continue
+		}
+
+		isLAN := false
+		priority := 0
+
+		addrs, _ := ifa.Addrs()
+		for _, addr := range addrs {
+			var ip net.IP
+			switch v := addr.(type) {
+			case *net.IPNet:
+				ip = v.IP
+			case *net.IPAddr:
+				ip = v.IP
+			}
+
+			if ip == nil {
+				continue
+			}
+
+			if ip.To4() != nil {
+				parsedIP := ip.To4()
+				if parsedIP[0] == 10 {
+					isLAN = true
+					priority = 3
+				} else if parsedIP[0] == 172 && parsedIP[1] >= 16 && parsedIP[1] <= 31 {
+					isLAN = true
+					priority = 3
+				} else if parsedIP[0] == 192 && parsedIP[1] == 168 {
+					isLAN = true
+					priority = 3
+				} else {
+					isLAN = false
+					priority = 1
+				}
+				break
+			}
+		}
+
+		ifaces = append(ifaces, ifaceInfo{
+			mac:      mac,
+			isLAN:    isLAN,
+			priority: priority,
+		})
+	}
+
+	var lanMacs []string
+	var otherMacs []string
+	for _, iface := range ifaces {
+		if iface.isLAN {
+			lanMacs = append(lanMacs, iface.mac)
+		} else {
+			otherMacs = append(otherMacs, iface.mac)
+		}
+	}
+
+	var prioritizedMacs []string
+	prioritizedMacs = append(prioritizedMacs, lanMacs...)
+	prioritizedMacs = append(prioritizedMacs, otherMacs...)
+
+	var macString string
+	for i, mac := range prioritizedMacs {
+		if i > 0 {
+			macString += ","
+		}
+		macString += mac
+
+		if len(macString) >= maxLength {
+			break
+		}
+	}
+
+	return macString, nil
+}

+ 1 - 11
src/mod/network/mdns/mdns.go

@@ -35,17 +35,7 @@ type NetworkHost struct {
 // Create a new MDNS discoverer, set MacOverride to empty string for using the first NIC discovered
 func NewMDNS(config NetworkHost, MacOverride string) (*MDNSHost, error) {
 	//Get host MAC Address
-	macAddress, err := getMacAddr()
-	if err != nil {
-		return nil, err
-	}
-
-	macAddressBoardcast := ""
-	if err == nil {
-		macAddressBoardcast = strings.Join(macAddress, ",")
-	} else {
-		logger.PrintAndLog("Mdns", fmt.Sprint("[mDNS] Unable to get MAC Address: ", err.Error()), nil)
-	}
+	macAddressBoardcast, _ := getMaxMacAddrString(128)
 
 	//Register the mds services
 	server, err := zeroconf.Register(config.HostName, "_http._tcp", "local.", config.Port, []string{"version_build=" + config.BuildVersion, "version_minor=" + config.MinorVersion, "vendor=" + config.Vendor, "model=" + config.Model, "uuid=" + config.UUID, "domain=" + config.Domain, "mac_addr=" + macAddressBoardcast}, nil)