mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2025-09-18 11:04:23 +08:00
Guard against nil (#1452)
This commit is contained in:
parent
71a849c5af
commit
4d125fca5f
2 changed files with 37 additions and 9 deletions
|
@ -601,6 +601,40 @@ func (c cfTarget) FQDN() string {
|
|||
return strings.TrimRight(string(c), ".") + "."
|
||||
}
|
||||
|
||||
// uint16Zero converts value to uint16 or returns 0.
|
||||
func uint16Zero(value interface{}) uint16 {
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
return uint16(v)
|
||||
case uint16:
|
||||
return v
|
||||
case nil:
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// intZero converts value to int or returns 0.
|
||||
func intZero(value interface{}) int {
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
return int(v)
|
||||
case int:
|
||||
return v
|
||||
case nil:
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// stringDefault returns the value as a string or returns the default value if nil.
|
||||
func stringDefault(value interface{}, def string) string {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return v
|
||||
case nil:
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func (c *cloudflareProvider) nativeToRecord(domain string, cr cloudflare.DNSRecord) (*models.RecordConfig, error) {
|
||||
// normalize cname,mx,ns records with dots to be consistent with our config format.
|
||||
if cr.Type == "CNAME" || cr.Type == "MX" || cr.Type == "NS" {
|
||||
|
@ -628,17 +662,11 @@ func (c *cloudflareProvider) nativeToRecord(domain string, cr cloudflare.DNSReco
|
|||
case "SRV":
|
||||
data := cr.Data.(map[string]interface{})
|
||||
|
||||
target := "MISSING.TARGET"
|
||||
switch data["target"].(type) {
|
||||
case string:
|
||||
target = data["target"].(string)
|
||||
default:
|
||||
}
|
||||
|
||||
target := stringDefault(data["target"], "MISSING.TARGET")
|
||||
if target != "." {
|
||||
target += "."
|
||||
}
|
||||
if err := rc.SetTargetSRV(uint16(data["priority"].(float64)), uint16(data["weight"].(float64)), uint16(data["port"].(float64)),
|
||||
if err := rc.SetTargetSRV(uint16Zero(data["priority"]), uint16Zero(data["weight"]), uint16Zero(data["port"]),
|
||||
target); err != nil {
|
||||
return nil, fmt.Errorf("unparsable SRV record received from cloudflare: %w", err)
|
||||
}
|
||||
|
|
|
@ -241,7 +241,7 @@ func (c *cloudflareProvider) getPageRules(id string, domain string) ([]*models.R
|
|||
pr.Targets[0].Constraint.Value,
|
||||
value["url"],
|
||||
pr.Priority,
|
||||
int(value["status_code"].(float64))))
|
||||
intZero(value["status_code"])))
|
||||
recs = append(recs, r)
|
||||
}
|
||||
return recs, nil
|
||||
|
|
Loading…
Add table
Reference in a new issue