diff --git a/fbx/api_version.go b/fbx/api_version.go index 7322339..7d735b9 100644 --- a/fbx/api_version.go +++ b/fbx/api_version.go @@ -124,54 +124,51 @@ func (f *FreeboxAPIVersion) newFreeboxAPIVersionMDNS(*FreeboxHttpClient) error { // mDNS lookup go func() { - defer close(entries) - if err := mdns.Lookup(mdnsService, entries); err != nil { - log.Error.Println("mDNS lookup failed:", err) + for entry := range entries { + deviceName := entry.Name + idx := strings.Index(deviceName, ".") + if idx >= 0 { + deviceName = deviceName[0:idx] + } + deviceName = strings.ReplaceAll(deviceName, "\\", "") + + *f = FreeboxAPIVersion{ + DeviceName: deviceName, + } + for i := range entry.InfoFields { + kv := strings.SplitN(entry.InfoFields[i], "=", 2) + if len(kv) != 2 { + break + } + switch kv[0] { + case "api_domain": + f.APIDomain = kv[1] + case "uid": + f.UID = kv[1] + case "https_available": + f.HTTPSAvailable = (kv[1] == "1") + case "https_port": + port, _ := strconv.ParseUint(kv[1], 10, 16) + f.HTTPSPort = uint16(port) + case "api_version": + f.APIVersion = kv[1] + case "api_base_url": + f.APIBaseURL = kv[1] + case "device_type": + f.DeviceType = kv[1] + default: + } + } } - log.Debug.Println("End of mDNS lookup") }() - for entry := range entries { - deviceName := entry.Name - idx := strings.Index(deviceName, ".") - if idx >= 0 { - deviceName = deviceName[0:idx] - } - deviceName = strings.ReplaceAll(deviceName, "\\", "") - - *f = FreeboxAPIVersion{ - DeviceName: deviceName, - } - for i := range entry.InfoFields { - kv := strings.SplitN(entry.InfoFields[i], "=", 2) - if len(kv) != 2 { - break - } - switch kv[0] { - case "api_domain": - f.APIDomain = kv[1] - case "uid": - f.UID = kv[1] - case "https_available": - f.HTTPSAvailable = (kv[1] == "1") - case "https_port": - port, _ := strconv.ParseUint(kv[1], 10, 16) - f.HTTPSPort = uint16(port) - case "api_version": - f.APIVersion = kv[1] - case "api_base_url": - f.APIBaseURL = kv[1] - case "device_type": - f.DeviceType = kv[1] - default: - } - } - if f.IsValid() { - return nil - } + if err := mdns.Lookup(mdnsService, entries); err != nil { + log.Error.Println("mDNS lookup failed:", err) + return err } - - return errors.New("MDNS timeout") + log.Debug.Println("End of mDNS lookup") + close(entries) + return nil } func (f *FreeboxAPIVersion) setQueryApiVersion(forceApiVersion int) error {