dae/control/dns_utils.go

50 lines
962 B
Go
Raw Permalink Normal View History

2023-02-25 01:38:21 +07:00
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
2023-02-25 01:38:21 +07:00
*/
package control
import (
"fmt"
"strconv"
2023-02-25 01:38:21 +07:00
"strings"
dnsmessage "github.com/miekg/dns"
2023-02-25 01:38:21 +07:00
)
type RscWrapper struct {
Rsc dnsmessage.RR
2023-02-25 01:38:21 +07:00
}
func (w RscWrapper) String() string {
var strBody string
switch body := w.Rsc.(type) {
case *dnsmessage.A:
strBody = body.A.String()
case *dnsmessage.AAAA:
strBody = body.AAAA.String()
case *dnsmessage.CNAME:
strBody = body.Target
2023-02-25 01:38:21 +07:00
default:
strBody = body.String()
2023-02-25 01:38:21 +07:00
}
return fmt.Sprintf("%v(%v): %v", w.Rsc.Header().Name, QtypeToString(w.Rsc.Header().Rrtype), strBody)
2023-02-25 01:38:21 +07:00
}
func FormatDnsRsc(ans []dnsmessage.RR) string {
2023-02-25 01:38:21 +07:00
var w []string
for _, a := range ans {
w = append(w, RscWrapper{Rsc: a}.String())
}
return strings.Join(w, "; ")
}
func QtypeToString(qtype uint16) string {
str, ok := dnsmessage.TypeToString[qtype]
if !ok {
str = strconv.Itoa(int(qtype))
}
return str
}