2023-02-25 01:38:21 +07:00
|
|
|
/*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2024-01-04 16:28:16 +07:00
|
|
|
* Copyright (c) 2022-2024, daeuniverse Organization <dae@v2raya.org>
|
2023-02-25 01:38:21 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-07-09 15:02:17 +07:00
|
|
|
"strconv"
|
2023-02-25 01:38:21 +07:00
|
|
|
"strings"
|
2023-04-23 12:27:29 +07:00
|
|
|
|
2023-07-09 15:02:17 +07:00
|
|
|
dnsmessage "github.com/miekg/dns"
|
2023-02-25 01:38:21 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type RscWrapper struct {
|
2023-07-09 15:02:17 +07:00
|
|
|
Rsc dnsmessage.RR
|
2023-02-25 01:38:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w RscWrapper) String() string {
|
|
|
|
var strBody string
|
2023-07-09 15:02:17 +07:00
|
|
|
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:
|
2023-07-09 15:02:17 +07:00
|
|
|
strBody = body.String()
|
2023-02-25 01:38:21 +07:00
|
|
|
}
|
2023-07-09 15:02:17 +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
|
|
|
}
|
2023-07-09 15:02:17 +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, "; ")
|
|
|
|
}
|
2023-07-09 15:02:17 +07:00
|
|
|
|
|
|
|
func QtypeToString(qtype uint16) string {
|
|
|
|
str, ok := dnsmessage.TypeToString[qtype]
|
|
|
|
if !ok {
|
|
|
|
str = strconv.Itoa(int(qtype))
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|