Clean up cfsingleredirect

This commit is contained in:
Thomas Limoncelli 2025-11-30 09:28:24 -05:00
parent 779d2f3bef
commit a5a624ff96
No known key found for this signature in database
5 changed files with 37 additions and 37 deletions

View file

@ -59,7 +59,9 @@ func (rc *RecordConfig) PopulateFromStringFunc(rtype, contents, origin string, t
return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type) return fmt.Errorf("assertion failed: rtype already set (%s) (%s)", rtype, rc.Type)
} }
switch rc.Type = rtype; rtype { // #rtype_variations rc.Type = rtype
switch rtype { // #rtype_variations
case "A": case "A":
ip := net.ParseIP(contents) ip := net.ParseIP(contents)
if ip == nil || ip.To4() == nil { if ip == nil || ip.To4() == nil {

View file

@ -60,19 +60,19 @@ func NewRecordConfigFromRaw(t string, args []any, dc *models.DomainConfig) (*mod
return rec, nil return rec, nil
} }
func stringifyMetas(metas []map[string]any) map[string]string { // func stringifyMetas(metas []map[string]any) map[string]string {
result := make(map[string]string) // result := make(map[string]string)
for _, m := range metas { // for _, m := range metas {
for mk, mv := range m { // for mk, mv := range m {
if v, ok := mv.(string); ok { // if v, ok := mv.(string); ok {
result[mk] = v // Already a string. No new malloc. // result[mk] = v // Already a string. No new malloc.
} else { // } else {
result[mk] = fmt.Sprintf("%v", mv) // result[mk] = fmt.Sprintf("%v", mv)
} // }
} // }
} // }
return result // return result
} // }
func setRecordNames(rec *models.RecordConfig, dc *models.DomainConfig, n string) { func setRecordNames(rec *models.RecordConfig, dc *models.DomainConfig, n string) {

View file

@ -8,7 +8,7 @@ import (
) )
// backwards compatibility: // backwards compatibility:
var validTypes = map[string]struct{}{} //var validTypes = map[string]struct{}{}
type RType interface { type RType interface {
// Returns the name of the rtype ("A", "MX", etc.) // Returns the name of the rtype ("A", "MX", etc.)
@ -16,9 +16,6 @@ type RType interface {
// RecordConfig factory. Updates a RecordConfig's fields based on args. // RecordConfig factory. Updates a RecordConfig's fields based on args.
FromArgs(*models.DomainConfig, *models.RecordConfig, []any) error FromArgs(*models.DomainConfig, *models.RecordConfig, []any) error
// Returns a string representation of the record in RFC1038 format.
// AsRFC1038String([]string) (string, error)
} }
// Map of registered rtypes. // Map of registered rtypes.

View file

@ -78,6 +78,6 @@ func FromArgs_helper(dc *models.DomainConfig, rec *models.RecordConfig, args []a
if err != nil { if err != nil {
return err return err
} }
rec.SetTarget(target) // Overwrite target to old-style for compatibility _ = rec.SetTarget(target) // Overwrite target to old-style for compatibility
return nil return nil
} }

View file

@ -32,34 +32,39 @@ func (handle *SingleRedirectConfig) Name() string {
func (handle *SingleRedirectConfig) FromArgs(dc *models.DomainConfig, rec *models.RecordConfig, args []any) error { func (handle *SingleRedirectConfig) FromArgs(dc *models.DomainConfig, rec *models.RecordConfig, args []any) error {
//fmt.Printf("DEBUG: CLOUDFLAREAPI_SINGLE_REDIRECT FromArgs called with args=%+v\n", args) //fmt.Printf("DEBUG: CLOUDFLAREAPI_SINGLE_REDIRECT FromArgs called with args=%+v\n", args)
// Pave the args to be the expected types. // Pave the args to be the expected types.
if err := rtypecontrol.PaveArgs(args, "siss"); err != nil { if err := rtypecontrol.PaveArgs(args, "siss"); err != nil {
return err return err
} }
// Unpack the args: // Unpack the args:
var name, when, then string var name = args[0].(string)
var code uint16 var code = args[1].(uint16)
var when = args[2].(string)
var then = args[3].(string)
name = args[0].(string) // Validate
code = args[1].(uint16)
if code != 301 && code != 302 && code != 303 && code != 307 && code != 308 { if code != 301 && code != 302 && code != 303 && code != 307 && code != 308 {
return fmt.Errorf("%s: code (%03d) is not 301,302,303,307,308", rec.FilePos, code) return fmt.Errorf("%s: code (%03d) is not 301,302,303,307,308", rec.FilePos, code)
} }
when = args[2].(string)
then = args[3].(string)
display := targetFromRaw(name, code, when, then)
// Calclate the Comparable and ZonefilePartial values:
display := targetFromRaw(name, code, when, then)
rec.Comparable = display
rec.ZonefilePartial = display
// Set the fields
rec.F = &SingleRedirectConfig{ rec.F = &SingleRedirectConfig{
Code: code, Code: code,
//
SRName: name, SRName: name,
SRWhen: when, SRWhen: when,
SRThen: then, SRThen: then,
SRDisplay: display, SRDisplay: display,
} }
//rec.Name = name // Usually these fields do not need to be changed. The caller sets appropriate values.
// But Cloudflare Single Redirects always use "@" as the name and TTL=1. We override here.
rec.Name = "@" rec.Name = "@"
rec.NameRaw = "@" rec.NameRaw = "@"
rec.NameUnicode = "@" rec.NameUnicode = "@"
@ -67,10 +72,9 @@ func (handle *SingleRedirectConfig) FromArgs(dc *models.DomainConfig, rec *model
rec.NameFQDNRaw = dc.NameRaw rec.NameFQDNRaw = dc.NameRaw
rec.NameFQDNUnicode = dc.NameUnicode rec.NameFQDNUnicode = dc.NameUnicode
rec.TTL = 1 rec.TTL = 1
rec.Comparable = display
rec.ZonefilePartial = display
_ = rec.SetTarget(name) // Fill in the legacy fields:
handle.CopyToLegacyFields(rec)
return nil return nil
} }
@ -84,12 +88,9 @@ func targetFromRaw(name string, code uint16, when, then string) string {
) )
} }
func (handle *SingleRedirectConfig) AsRFC1038String(*models.RecordConfig) string {
return handle.SRDisplay
}
func (handle *SingleRedirectConfig) CopyToLegacyFields(rec *models.RecordConfig) { func (handle *SingleRedirectConfig) CopyToLegacyFields(rec *models.RecordConfig) {
rec.SetTarget(handle.SRDisplay) //rec.SetTarget(handle.SRDisplay)
_ = rec.SetTarget(handle.SRName)
} }
//func (handle *SingleRedirectConfig) IDNFields(argsRaw) (argsIDN, argsUnicode, error) {} //func (handle *SingleRedirectConfig) IDNFields(argsRaw) (argsIDN, argsUnicode, error) {}