2
0
mirror of https://github.com/namecoin/ncdns synced 2024-11-18 03:26:00 +00:00
ncdns/util/util.go

139 lines
4.0 KiB
Go
Raw Normal View History

2014-10-21 20:30:19 +00:00
package util
2014-11-13 10:55:51 +00:00
2014-10-21 20:30:19 +00:00
import "strings"
2014-12-04 12:04:14 +00:00
import "github.com/miekg/dns"
import "github.com/hlandau/madns/merr"
import "fmt"
import "regexp"
import "net/mail"
2014-10-21 20:30:19 +00:00
2014-11-27 20:36:21 +00:00
// Split a domain name a.b.c.d.e into parts e (the head) and a.b.c.d (the rest).
2014-12-01 00:44:06 +00:00
func SplitDomainHead(name string) (head, rest string) {
2014-11-27 20:36:21 +00:00
if len(name) > 0 && name[len(name)-1] == '.' {
name = name[0 : len(name)-1]
}
2014-11-13 10:55:51 +00:00
parts := strings.Split(name, ".")
2014-10-21 20:30:19 +00:00
2014-11-13 10:55:51 +00:00
head = parts[len(parts)-1]
2014-10-21 20:30:19 +00:00
2014-11-13 10:55:51 +00:00
if len(parts) >= 2 {
rest = strings.Join(parts[0:len(parts)-1], ".")
}
2014-10-21 20:30:19 +00:00
2014-11-13 10:55:51 +00:00
return
2014-10-21 20:30:19 +00:00
}
2014-11-11 10:35:48 +00:00
2014-12-01 00:44:06 +00:00
// Split a domain name a.b.c.d.e into parts a (the tail) and b.c.d.e (the rest).
func SplitDomainTail(name string) (tail, rest string) {
s := strings.SplitN(name, ".", 2)
if len(s) == 1 {
return s[0], ""
}
return s[0], s[1]
}
2014-12-04 12:04:14 +00:00
// For domains of the form "Y1.Y2...Yn.ANCHOR.X1.X2...Xn.", returns the following values:
//
// basename is the name appearing directly beneath the anchor (Yn).
//
// subname contains any additional labels appearing beneath the anchor (Y1 through Y(n-1)),
// separated by dots.
//
// rootname contains ANCHOR through Xn inclusive, separated by dots.
//
// If no label corresponds to ANCHOR, an error is returned.
// If ANCHOR is the first label, basename is an empty string.
//
// Examples, where anchor="bit":
// "a.b.c.d." -> merr.ErrNotInZone
// "a.b.c.d.bit." -> subname="a.b.c", basename="d", rootname="bit"
// "d.bit." -> subname="", basename="d", rootname="bit"
// "bit." -> subname="", basename="", rootname="bit"
// "bit.x.y.z." -> subname="", basename="", rootname="bit.x.y.z"
// "d.bit.x.y.z." -> subname="", basename="d", rootname="bit.x.y.z"
// "c.d.bit.x.y.z." -> subname="c", basename="d", rootname="bit.x.y.z"
// "a.b.c.d.bit.x.y.z." -> subname="a.b.c", basename="d", rootname="bit.x.y.z"
func SplitDomainByFloatingAnchor(qname, anchor string) (subname, basename, rootname string, err error) {
qname = strings.TrimRight(qname, ".")
parts := strings.Split(qname, ".")
if len(parts) < 2 {
if parts[0] != anchor {
err = merr.ErrNotInZone
return
}
rootname = parts[0]
return
}
for i := len(parts) - 1; i >= 0; i-- {
v := parts[i]
// scanning for rootname
if v == anchor {
if i == 0 {
// i is alreay zero, so we have something like bit.x.y.z.
rootname = qname
return
}
rootname = strings.Join(parts[i:len(parts)], ".")
basename = parts[i-1]
subname = strings.Join(parts[0:i-1], ".")
return
}
}
err = merr.ErrNotInZone
return
}
// Convert a domain name basename (e.g. "example") to a Namecoin domain name
// key name ("d/example").
func BasenameToNamecoinKey(basename string) (string, error) {
return "d/" + basename, nil
}
// Convert a Namecoin domain name key name (e.g. "d/example") to a domain name
// basename ("example").
func NamecoinKeyToBasename(key string) (string, error) {
if strings.HasPrefix(key, "d/") {
return key[2:], nil
}
return "", fmt.Errorf("not a domain name key")
}
// This is used to validate NS records, targets in SRV records, etc. In these cases
// an IP address is not allowed. Therefore this regex must exclude all-numeric domain names.
// This is done by requiring the final part to start with an alphabetic character.
var re_hostName = regexp.MustCompilePOSIX(`^(([a-z0-9_][a-z0-9_-]{0,62}\.)*[a-z_][a-z0-9_-]{0,62}\.?|\.)$`)
var re_label = regexp.MustCompilePOSIX(`^[a-z_][a-z0-9_-]*$`)
var re_serviceName = regexp.MustCompilePOSIX(`^[a-z_][a-z0-9_-]*$`)
func ValidateHostName(name string) bool {
name = dns.Fqdn(name)
return len(name) <= 255 && re_hostName.MatchString(name)
}
func ValidateLabel(name string) bool {
return len(name) <= 63 && re_label.MatchString(name)
}
func ValidateServiceName(name string) bool {
return len(name) < 63 && re_serviceName.MatchString(name)
}
func ValidateEmail(email string) bool {
addr, err := mail.ParseAddress(email)
if addr == nil || err != nil {
return false
}
return addr.Name == ""
}
2014-11-11 10:35:48 +00:00
// © 2014 Hugo Landau <hlandau@devever.net> GPLv3 or later