|
|
@@ -55,6 +55,12 @@ func (s *Server) Run(ctx context.Context) error {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
+ lan2IP, mac := s.maintenanceEndpoint()
|
|
|
+ if lan2IP == "" {
|
|
|
+ s.log.Warn("skip discovery response because no 169.254 maintenance address was found")
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
device := s.deviceSvc.Get()
|
|
|
resp := model.DiscoverResponse{
|
|
|
ProtocolVersion: 1,
|
|
|
@@ -63,8 +69,8 @@ func (s *Server) Run(ctx context.Context) error {
|
|
|
DeviceID: device.DeviceID,
|
|
|
Hostname: device.Hostname,
|
|
|
ServerVersion: device.ServerVersion,
|
|
|
- MAC: findMACByIP(s.cfg.MaintenanceIP),
|
|
|
- LAN2IP: s.cfg.MaintenanceIP,
|
|
|
+ MAC: mac,
|
|
|
+ LAN2IP: lan2IP,
|
|
|
HTTPPort: s.cfg.HTTPPort,
|
|
|
AuthRequired: true,
|
|
|
}
|
|
|
@@ -73,6 +79,40 @@ func (s *Server) Run(ctx context.Context) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func (s *Server) maintenanceEndpoint() (string, string) {
|
|
|
+ if s.cfg.MaintenanceIP != "" {
|
|
|
+ mac := findMACByIP(s.cfg.MaintenanceIP)
|
|
|
+ if mac != "" {
|
|
|
+ return s.cfg.MaintenanceIP, mac
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return findFirstLinkLocalEndpoint()
|
|
|
+}
|
|
|
+
|
|
|
+func findFirstLinkLocalEndpoint() (string, string) {
|
|
|
+ ifaces, err := net.Interfaces()
|
|
|
+ if err != nil {
|
|
|
+ return "", ""
|
|
|
+ }
|
|
|
+ for _, iface := range ifaces {
|
|
|
+ if iface.Flags&net.FlagLoopback != 0 || iface.Flags&net.FlagUp == 0 || len(iface.HardwareAddr) == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ addrs, err := iface.Addrs()
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for _, addr := range addrs {
|
|
|
+ current := ipv4FromAddr(addr)
|
|
|
+ if current == nil || !strings.HasPrefix(current.String(), "169.254.") {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ return current.String(), iface.HardwareAddr.String()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "", ""
|
|
|
+}
|
|
|
+
|
|
|
func findMACByIP(ip string) string {
|
|
|
ifaces, err := net.Interfaces()
|
|
|
if err != nil {
|
|
|
@@ -88,14 +128,7 @@ func findMACByIP(ip string) string {
|
|
|
continue
|
|
|
}
|
|
|
for _, addr := range addrs {
|
|
|
- var current net.IP
|
|
|
- switch value := addr.(type) {
|
|
|
- case *net.IPNet:
|
|
|
- current = value.IP
|
|
|
- case *net.IPAddr:
|
|
|
- current = value.IP
|
|
|
- }
|
|
|
- current = current.To4()
|
|
|
+ current := ipv4FromAddr(addr)
|
|
|
if current == nil {
|
|
|
continue
|
|
|
}
|
|
|
@@ -109,3 +142,17 @@ func findMACByIP(ip string) string {
|
|
|
}
|
|
|
return fallback
|
|
|
}
|
|
|
+
|
|
|
+func ipv4FromAddr(addr net.Addr) net.IP {
|
|
|
+ var current net.IP
|
|
|
+ switch value := addr.(type) {
|
|
|
+ case *net.IPNet:
|
|
|
+ current = value.IP
|
|
|
+ case *net.IPAddr:
|
|
|
+ current = value.IP
|
|
|
+ }
|
|
|
+ if current == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return current.To4()
|
|
|
+}
|