2024-05-01 23:37:15 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetTargetSVCB sets the SVCB fields.
|
|
|
|
func (rc *RecordConfig) SetTargetSVCB(priority uint16, target string, params []dns.SVCBKeyValue) error {
|
|
|
|
rc.SvcPriority = priority
|
2025-01-10 23:43:16 +08:00
|
|
|
if err := rc.SetTarget(target); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-01 23:37:15 +08:00
|
|
|
paramsStr := []string{}
|
|
|
|
for _, kv := range params {
|
|
|
|
paramsStr = append(paramsStr, fmt.Sprintf("%s=%s", kv.Key(), kv.String()))
|
|
|
|
}
|
|
|
|
rc.SvcParams = strings.Join(paramsStr, " ")
|
|
|
|
if rc.Type == "" {
|
|
|
|
rc.Type = "SVCB"
|
|
|
|
}
|
|
|
|
if rc.Type != "SVCB" && rc.Type != "HTTPS" {
|
|
|
|
panic("assertion failed: SetTargetSVCB called when .Type is not SVCB or HTTPS")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTargetSVCBString is like SetTargetSVCB but accepts one big string and the origin so parsing can be done using miekg/dns.
|
|
|
|
func (rc *RecordConfig) SetTargetSVCBString(origin, contents string) error {
|
|
|
|
if rc.Type == "" {
|
|
|
|
rc.Type = "SVCB"
|
|
|
|
}
|
|
|
|
record, err := dns.NewRR(fmt.Sprintf("%s. %s %s", origin, rc.Type, contents))
|
|
|
|
if err != nil {
|
2025-01-10 23:43:16 +08:00
|
|
|
return fmt.Errorf("could not parse SVCB record: %w", err)
|
2024-05-01 23:37:15 +08:00
|
|
|
}
|
|
|
|
switch r := record.(type) {
|
|
|
|
case *dns.HTTPS:
|
|
|
|
return rc.SetTargetSVCB(r.Priority, r.Target, r.Value)
|
|
|
|
case *dns.SVCB:
|
|
|
|
return rc.SetTargetSVCB(r.Priority, r.Target, r.Value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|