fix: mDNS lookup

This commit is contained in:
nyyu 2023-03-06 20:44:56 +01:00
parent 95eedc417b
commit a4e2d54457

View File

@ -124,54 +124,51 @@ func (f *FreeboxAPIVersion) newFreeboxAPIVersionMDNS(*FreeboxHttpClient) error {
// mDNS lookup // mDNS lookup
go func() { go func() {
defer close(entries) for entry := range entries {
if err := mdns.Lookup(mdnsService, entries); err != nil { deviceName := entry.Name
log.Error.Println("mDNS lookup failed:", err) 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 { if err := mdns.Lookup(mdnsService, entries); err != nil {
deviceName := entry.Name log.Error.Println("mDNS lookup failed:", err)
idx := strings.Index(deviceName, ".") return err
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
}
} }
log.Debug.Println("End of mDNS lookup")
return errors.New("MDNS timeout") close(entries)
return nil
} }
func (f *FreeboxAPIVersion) setQueryApiVersion(forceApiVersion int) error { func (f *FreeboxAPIVersion) setQueryApiVersion(forceApiVersion int) error {