mirror of
https://github.com/seriousm4x/UpSnap.git
synced 2026-11-23 01:22:35 +00:00
b17b09109f
* 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>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package networking
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"golang.org/x/net/idna"
|
|
)
|
|
|
|
var fqdnRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z0-9-]{2,63}$`)
|
|
|
|
/*
|
|
Validate's FQDN by converting it to Punycode and checking it against several criteria
|
|
*/
|
|
func ValidateFQDN(host string) error {
|
|
// Remove trailing . just in case, but DB and form validation should already prevent that
|
|
trimmedHost := strings.TrimSuffix(host, ".")
|
|
if len(trimmedHost) == 0 {
|
|
return fmt.Errorf("%q: invalid length of 0. original %q", trimmedHost, host)
|
|
}
|
|
|
|
// convert to punycode
|
|
punycodeHost, err := idna.Lookup.ToASCII(trimmedHost)
|
|
if err != nil {
|
|
return fmt.Errorf("%q: failed to convert to punycode - %w", trimmedHost, err)
|
|
}
|
|
|
|
if len(punycodeHost) < 1 || len(punycodeHost) > 253 {
|
|
return fmt.Errorf("%q -> %q: invalid length %d bytes. Must be 1-253 bytes (chars) after conversion.", trimmedHost, punycodeHost, len(punycodeHost))
|
|
}
|
|
|
|
if !fqdnRegex.MatchString(punycodeHost) {
|
|
return fmt.Errorf("%q -> %q: Failed validation.", trimmedHost, punycodeHost)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateSubnetMask(mask string) bool {
|
|
addr, err := netip.ParseAddr(mask)
|
|
if err != nil || !addr.Is4() {
|
|
return false
|
|
}
|
|
|
|
byteArray := addr.As4()
|
|
maskInt := uint32(byteArray[0])<<24 | uint32(byteArray[1])<<16 | uint32(byteArray[2])<<8 | uint32(byteArray[3])
|
|
inverted := ^maskInt
|
|
return (inverted & (inverted + 1)) == 0
|
|
}
|