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"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// function for everything else.
|
|
|
|
// var err error
|
|
|
|
// switch rType {
|
|
|
|
// case "MX":
|
|
|
|
// // MX priority in a separate field.
|
|
|
|
// if err := rc.SetTargetMX(cr.Priority, target); err != nil {
|
|
|
|
// return nil, fmt.Errorf("unparsable MX record received from cloudflare: %w", err)
|
|
|
|
// }
|
|
|
|
// case "TXT":
|
|
|
|
// // TXT records are stored verbatim; no quoting/escaping to parse.
|
|
|
|
// err = rc.SetTargetTXT(target)
|
|
|
|
// // ProTip: Use rc.SetTargetTXTs(manystrings) if the API or parser returns a list of substrings.
|
|
|
|
// default:
|
|
|
|
// err = rec.PopulateFromString(rType, target, origin)
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, fmt.Errorf("unparsable record received from CHANGE_TO_PROVDER_NAME: %w", err)
|
|
|
|
// }
|
2018-02-16 01:02:50 +08:00
|
|
|
//
|
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)
|
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":
|
|
|
|
return rc.SetTargetTXTString(contents)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|