2017-01-13 20:42:13 +01:00
|
|
|
package cloudflare
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validateRecordType ensures that the cloudflare record type is valid
|
2017-02-16 03:52:04 +01:00
|
|
|
func validateRecordType(t string, proxied bool) error {
|
|
|
|
switch t {
|
|
|
|
case "A":
|
|
|
|
return nil
|
|
|
|
case "AAAA":
|
|
|
|
return nil
|
|
|
|
case "CNAME":
|
|
|
|
return nil
|
|
|
|
case "TXT":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "SRV":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "LOC":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "MX":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "NS":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case "SPF":
|
|
|
|
if !proxied {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(
|
|
|
|
`Invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS" or "SPF"`, t)
|
2017-01-13 20:42:13 +01:00
|
|
|
}
|
|
|
|
|
2017-02-16 03:52:04 +01:00
|
|
|
return fmt.Errorf("Type %q cannot be proxied", t)
|
2017-01-13 20:42:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// validateRecordName ensures that based on supplied record type, the name content matches
|
|
|
|
// Currently only validates A and AAAA types
|
|
|
|
func validateRecordName(t string, value string) error {
|
|
|
|
switch t {
|
|
|
|
case "A":
|
|
|
|
// Must be ipv4 addr
|
|
|
|
addr := net.ParseIP(value)
|
|
|
|
if addr == nil || !strings.Contains(value, ".") {
|
|
|
|
return fmt.Errorf("A record must be a valid IPv4 address, got: %q", value)
|
|
|
|
}
|
|
|
|
case "AAAA":
|
|
|
|
// Must be ipv6 addr
|
|
|
|
addr := net.ParseIP(value)
|
|
|
|
if addr == nil || !strings.Contains(value, ":") {
|
|
|
|
return fmt.Errorf("AAAA record must be a valid IPv6 address, got: %q", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|