Files
invario b17b09109f feature: Support use of FQDN resolution with DNS and mDNS (#1760)
* feature: Support use of FQDN resolution with DNS and mDNS

Signed-off-by: invario <67800603+invario@users.noreply.github.com>

* - fixed `cronjobs.go` call to `port.Set()`
- refactored lots of the new code and fixed errors
- added validation functions into `validate.go` to handle unicode FQDN and punycode
- fixed memory leak from not closing avahi server connection by establishing permanent connection
- updated regex used in on device form and PB validation pattern to accept international unicode FQDN or IPv4 addresses
- updated regex used for netmask validation on form
- timeout mDNS queries after 2 seconds

Signed-off-by: invario <67800603+invario@users.noreply.github.com>

* fix: remove unintentional change

Signed-off-by: invario <67800603+invario@users.noreply.github.com>

* perform FQDN/IP validation in Svelte instead of HTML form

Signed-off-by: invario <67800603+invario@users.noreply.github.com>

* disable IP tracking if field value does not match regex for IPv4

Signed-off-by: invario <67800603+invario@users.noreply.github.com>

* fix: port state not always saved, don't resolve deviceIP for every port

* send wol packets also to broadcast

* fix: handle lowercase string comparision

---------

Signed-off-by: invario <67800603+invario@users.noreply.github.com>
Co-authored-by: Maxi Quoß <maxi@quoss.org>
2026-09-17 22:15:03 +02:00

58 lines
1.6 KiB
Go

//go:build windows || darwin
package networking
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"strings"
"time"
)
/*
Takes one parameter 'host' and returns its IP. If 'host' is already formatted
as an IP, simply return it. If 'host' is formatted as an FQDN, attempt to
resolve it with mDNS (if .local suffix) or else use a DNS lookup with
net.DefaultResolver
Windows and MacOS can resolve mDNS/DNS with one call to net.DefaultResolver
*/
func ResolveToIPAddr(host string) (string, error) {
// If host is already an IP, simply return it
addr, err := netip.ParseAddr(host)
if err == nil && addr.Is4() {
return host, nil
}
if err := ValidateFQDN(host); err != nil {
return "", fmt.Errorf("%q is the host", host)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
ips, err := net.DefaultResolver.LookupNetIP(ctx, "ip4", host)
if err != nil {
if strings.HasSuffix(strings.ToLower(host), ".local") {
var dnsErr *net.DNSError
isNotFound := errors.As(err, &dnsErr) && dnsErr.IsNotFound
isTimeout := errors.Is(err, context.DeadlineExceeded) || ctx.Err() == context.DeadlineExceeded
if isTimeout || isNotFound {
return "", nil
}
}
return "", err
}
if len(ips) > 0 {
// only return one result
return ips[0].String(), nil
}
// No results, but it's expected for a .local FQDN device that is offline. Don't return an error.
if strings.HasSuffix(strings.ToLower(host), ".local") {
return "", nil
} else {
// No results, but FQDN should have resolved since it's not .local. Return an error.
return "", fmt.Errorf("No IP address found for %s", host)
}
}