2018-02-16 01:02:50 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
"fmt"
|
2018-02-16 01:02:50 +08:00
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2023-12-05 06:45:25 +08:00
|
|
|
// PopulateFromStringFunc populates a RecordConfig by parsing a common RFC1035-like format.
|
|
|
|
//
|
|
|
|
// rtype: the resource record type (rtype)
|
|
|
|
// contents: a string that contains all parameters of the record's rdata (see below)
|
|
|
|
// txtFn: If rtype == "TXT", this function is used to parse contents, or nil if no parsing is needed.
|
|
|
|
//
|
|
|
|
// The "contents" field is the format used in RFC1035 zonefiles. It is the text
|
|
|
|
// after the rtype. For example, in the line: foo IN MX 10 mx.example.com.
|
|
|
|
// contents stores everything after the "MX" (not including the space).
|
|
|
|
//
|
|
|
|
// Typical values for txtFn include:
|
|
|
|
//
|
|
|
|
// nil: no parsing required.
|
|
|
|
// txtutil.ParseQuoted: Parse via Tom's interpretation of RFC1035.
|
|
|
|
// txtutil.ParseCombined: Backwards compatible with Parse via miekg's interpretation of RFC1035.
|
|
|
|
//
|
|
|
|
// Many providers deliver record data in this format or something close to it.
|
|
|
|
// This function is provided to reduce the amount of duplicate code across
|
|
|
|
// providers. If a particular rtype is not handled as a particular provider
|
|
|
|
// expects, simply handle it beforehand as a special case.
|
|
|
|
//
|
|
|
|
// Example 1: Normal use.
|
|
|
|
//
|
|
|
|
// rtype := FILL_IN_RTYPE
|
|
|
|
// rc := &models.RecordConfig{Type: rtype, TTL: FILL_IN_TTL}
|
|
|
|
// rc.SetLabelFromFQDN(FILL_IN_NAME, origin)
|
|
|
|
// rc.Original = FILL_IN_ORIGINAL // The raw data received from provider (if needed later)
|
|
|
|
// if err = rc.PopulateFromStringFunc(rtype, target, origin, nil); err != nil {
|
|
|
|
// return nil, fmt.Errorf("unparsable record type=%q received from PROVDER_NAME: %w", rtype, err)
|
|
|
|
// }
|
|
|
|
// return rc, nil
|
|
|
|
//
|
|
|
|
// Example 2: Use your own MX parser.
|
|
|
|
//
|
|
|
|
// rtype := FILL_IN_RTYPE
|
|
|
|
// rc := &models.RecordConfig{Type: rtype, TTL: FILL_IN_TTL}
|
|
|
|
// rc.SetLabelFromFQDN(FILL_IN_NAME, origin)
|
|
|
|
// rc.Original = FILL_IN_ORIGINAL // The raw data received from provider (if needed later)
|
|
|
|
// switch rtype {
|
|
|
|
// case "MX":
|
|
|
|
// // MX priority in a separate field.
|
|
|
|
// err = rc.SetTargetMX(cr.Priority, target)
|
|
|
|
// default:
|
|
|
|
// err = rc.PopulateFromString(rtype, target, origin)
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("unparsable record type=%q received from PROVDER_NAME: %w", rtype, err)
|
|
|
|
// }
|
|
|
|
// return rc, nil
|
|
|
|
func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, txtFn func(s string) (string, error)) error {
|
|
|
|
if rc.Type != "" && rc.Type != rtype {
|
|
|
|
return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch rc.Type = rtype; rtype { // #rtype_variations
|
|
|
|
case "A":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To4() == nil {
|
|
|
|
return fmt.Errorf("invalid IP in A record: %s", contents)
|
|
|
|
}
|
|
|
|
return rc.SetTargetIP(ip) // Reformat to canonical form.
|
|
|
|
case "AAAA":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To16() == nil {
|
|
|
|
return fmt.Errorf("invalid IP in AAAA record: %s", contents)
|
|
|
|
}
|
|
|
|
return rc.SetTargetIP(ip) // Reformat to canonical form.
|
|
|
|
case "AKAMAICDN", "ALIAS", "ANAME", "CNAME", "NS", "PTR":
|
|
|
|
return rc.SetTarget(contents)
|
|
|
|
case "CAA":
|
|
|
|
return rc.SetTargetCAAString(contents)
|
|
|
|
case "DS":
|
|
|
|
return rc.SetTargetDSString(contents)
|
|
|
|
case "DHCID":
|
|
|
|
return rc.SetTarget(contents)
|
|
|
|
case "LOC":
|
|
|
|
return rc.SetTargetLOCString(origin, contents)
|
|
|
|
case "MX":
|
|
|
|
return rc.SetTargetMXString(contents)
|
|
|
|
case "NAPTR":
|
|
|
|
return rc.SetTargetNAPTRString(contents)
|
|
|
|
case "SOA":
|
|
|
|
return rc.SetTargetSOAString(contents)
|
|
|
|
case "SPF", "TXT":
|
|
|
|
if txtFn == nil {
|
|
|
|
return rc.SetTargetTXT(contents)
|
|
|
|
}
|
|
|
|
t, err := txtFn(contents)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid TXT record: %s", contents)
|
|
|
|
}
|
|
|
|
return rc.SetTargetTXT(t)
|
|
|
|
case "SRV":
|
|
|
|
return rc.SetTargetSRVString(contents)
|
|
|
|
case "SSHFP":
|
|
|
|
return rc.SetTargetSSHFPString(contents)
|
|
|
|
case "TLSA":
|
|
|
|
return rc.SetTargetTLSAString(contents)
|
|
|
|
default:
|
2024-01-09 00:58:21 +08:00
|
|
|
//return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)", rtype, contents, origin)
|
|
|
|
return MakeUnknown(rc, rtype, contents, origin)
|
2023-12-05 06:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 02:43:07 +08:00
|
|
|
// PopulateFromString populates a RecordConfig given a type and string. Many
|
|
|
|
// providers give all the parameters of a resource record in one big string.
|
|
|
|
// This helper function lets you not re-invent the wheel.
|
|
|
|
//
|
|
|
|
// NOTE: You almost always want to special-case TXT records. Every provider
|
|
|
|
// seems to quote them differently.
|
|
|
|
//
|
|
|
|
// Recommended calling convention: Process the exceptions first, then use the
|
2023-06-02 01:11:36 +08:00
|
|
|
// PopulateFromString function for everything else.
|
2018-02-16 01:02:50 +08:00
|
|
|
//
|
2023-12-05 06:45:25 +08:00
|
|
|
// rtype := FILL_IN_TYPE
|
|
|
|
// var err error
|
|
|
|
// rc := &models.RecordConfig{Type: rtype}
|
|
|
|
// rc.SetLabelFromFQDN(FILL_IN_NAME, origin)
|
|
|
|
// rc.TTL = uint32(FILL_IN_TTL)
|
|
|
|
// rc.Original = FILL_IN_ORIGINAL // The raw data received from provider (if needed later)
|
|
|
|
// switch rtype {
|
|
|
|
// case "MX":
|
|
|
|
// // MX priority in a separate field.
|
|
|
|
// err = rc.SetTargetMX(cr.Priority, target)
|
|
|
|
// case "TXT":
|
|
|
|
// // TXT records are stored verbatim; no quoting/escaping to parse.
|
|
|
|
// err = rc.SetTargetTXT(target)
|
|
|
|
// default:
|
|
|
|
// err = rc.PopulateFromString(rtype, target, origin)
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("unparsable record type=%q received from PROVDER_NAME: %w", rtype, err)
|
|
|
|
// }
|
|
|
|
// return rc, nil
|
2022-02-12 03:30:45 +08:00
|
|
|
func (rc *RecordConfig) PopulateFromString(rtype, contents, origin string) error {
|
|
|
|
if rc.Type != "" && rc.Type != rtype {
|
|
|
|
panic(fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type))
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
2022-02-12 03:30:45 +08:00
|
|
|
switch rc.Type = rtype; rtype { // #rtype_variations
|
2018-02-16 01:02:50 +08:00
|
|
|
case "A":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To4() == nil {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("invalid IP in A record: %s", contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetIP(ip) // Reformat to canonical form.
|
2018-02-16 01:02:50 +08:00
|
|
|
case "AAAA":
|
|
|
|
ip := net.ParseIP(contents)
|
|
|
|
if ip == nil || ip.To16() == nil {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("invalid IP in AAAA record: %s", contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
}
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetIP(ip) // Reformat to canonical form.
|
2021-06-22 22:24:49 +08:00
|
|
|
case "AKAMAICDN", "ALIAS", "ANAME", "CNAME", "NS", "PTR":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTarget(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "CAA":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetCAAString(contents)
|
2020-05-30 22:40:21 +08:00
|
|
|
case "DS":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetDSString(contents)
|
2023-09-10 14:05:26 +08:00
|
|
|
case "DHCID":
|
|
|
|
return rc.SetTarget(contents)
|
2023-03-17 02:04:20 +08:00
|
|
|
case "LOC":
|
|
|
|
return rc.SetTargetLOCString(origin, contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "MX":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetMXString(contents)
|
2019-04-18 04:13:49 +08:00
|
|
|
case "NAPTR":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetNAPTRString(contents)
|
2020-02-24 02:58:49 +08:00
|
|
|
case "SOA":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetSOAString(contents)
|
2022-03-03 00:19:15 +08:00
|
|
|
case "SPF", "TXT":
|
2023-06-02 01:11:36 +08:00
|
|
|
return rc.SetTargetTXTs(ParseQuotedTxt(contents))
|
2022-03-03 00:19:15 +08:00
|
|
|
case "SRV":
|
|
|
|
return rc.SetTargetSRVString(contents)
|
2019-01-29 06:26:20 +08:00
|
|
|
case "SSHFP":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetSSHFPString(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
case "TLSA":
|
2022-02-12 03:30:45 +08:00
|
|
|
return rc.SetTargetTLSAString(contents)
|
2018-02-16 01:02:50 +08:00
|
|
|
default:
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("unknown rtype (%s) when parsing (%s) domain=(%s)",
|
2018-02-16 01:02:50 +08:00
|
|
|
rtype, contents, origin)
|
|
|
|
}
|
|
|
|
}
|