mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2026-12-01 17:02:33 +00:00
- 让全局 DNS 作用域等待可取消,并按唯一地址计算 PTR 查询预算 - 校验 query ID、ODoH Stamp 和 disabled flavor 的 DNS 调用 - 修复 HTTP/ODoH 失败清理并阻止 AXFR 路径穿越
133 lines
2.9 KiB
Go
133 lines
2.9 KiB
Go
//go:build !flavor_tiny && !flavor_ntr
|
|
|
|
package dnsclient
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
qlog "github.com/charmbracelet/log"
|
|
"github.com/miekg/dns"
|
|
"github.com/natesales/q/cli"
|
|
qoutput "github.com/natesales/q/output"
|
|
"github.com/natesales/q/transport"
|
|
)
|
|
|
|
func postProcessReplies(replies []*dns.Msg, opts *cli.Flags) {
|
|
for _, reply := range replies {
|
|
if opts.TXTConcat {
|
|
concatTXT(reply)
|
|
}
|
|
if opts.RoundTTLs {
|
|
roundAnswerTTLs(reply)
|
|
}
|
|
}
|
|
}
|
|
|
|
func concatTXT(msg *dns.Msg) {
|
|
for _, answer := range msg.Answer {
|
|
txt, ok := answer.(*dns.TXT)
|
|
if ok {
|
|
qlog.Debugf("Concatenating TXT response: %+v", txt.Txt)
|
|
txt.Txt = []string{strings.Join(txt.Txt, "")}
|
|
}
|
|
}
|
|
}
|
|
|
|
func roundAnswerTTLs(msg *dns.Msg) {
|
|
for _, answer := range msg.Answer {
|
|
answer.Header().Ttl -= answer.Header().Ttl % 60
|
|
}
|
|
}
|
|
|
|
func loadPTRs(ctx context.Context, entry *qoutput.Entry, txp transport.Transport) error {
|
|
if entry.PTRs == nil {
|
|
entry.PTRs = make(map[string]string)
|
|
}
|
|
for _, ip := range uniqueAddressRecordIPs(entry.Replies) {
|
|
if _, resolved := entry.PTRs[ip]; resolved {
|
|
continue
|
|
}
|
|
qname, err := dns.ReverseAddr(ip)
|
|
if err != nil {
|
|
return fmt.Errorf("reverse PTR address %s: %w", ip, err)
|
|
}
|
|
query := new(dns.Msg)
|
|
query.SetQuestion(qname, dns.TypePTR)
|
|
response, err := exchangeWithContext(ctx, txp, query)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return ctx.Err()
|
|
}
|
|
qlog.Warnf("error resolving PTR record: %s", err)
|
|
continue
|
|
}
|
|
if response == nil {
|
|
continue
|
|
}
|
|
for _, answer := range response.Answer {
|
|
if ptr, ok := answer.(*dns.PTR); ok {
|
|
entry.PTRs[ip] = ptr.Ptr
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func uniqueAddressRecordIPs(replies []*dns.Msg) []string {
|
|
seen := make(map[string]struct{})
|
|
var addresses []string
|
|
for _, reply := range replies {
|
|
for _, rr := range reply.Answer {
|
|
ip := addressRecordIP(rr)
|
|
if ip == "" {
|
|
continue
|
|
}
|
|
if _, exists := seen[ip]; exists {
|
|
continue
|
|
}
|
|
seen[ip] = struct{}{}
|
|
addresses = append(addresses, ip)
|
|
}
|
|
}
|
|
return addresses
|
|
}
|
|
|
|
func addressRecordIP(rr dns.RR) string {
|
|
switch record := rr.(type) {
|
|
case *dns.A:
|
|
return record.A.String()
|
|
case *dns.AAAA:
|
|
return record.AAAA.String()
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func renderEntries(out io.Writer, opts *cli.Flags, entries []*qoutput.Entry) error {
|
|
printer := qoutput.Printer{Out: out, Opts: opts}
|
|
if (opts.NSID && (opts.Format == qoutput.FormatPretty || opts.Format == qoutput.FormatColumn)) || opts.NSIDOnly {
|
|
printer.PrettyPrintNSID(entries, !opts.NSIDOnly)
|
|
}
|
|
if opts.NSIDOnly {
|
|
return nil
|
|
}
|
|
|
|
switch opts.Format {
|
|
case qoutput.FormatPretty:
|
|
printer.PrintPretty(entries)
|
|
case qoutput.FormatColumn:
|
|
printer.PrintColumn(entries)
|
|
case qoutput.FormatRAW:
|
|
printer.PrintRaw(entries)
|
|
case qoutput.FormatJSON, qoutput.FormatYAML, "yml":
|
|
printer.PrintStructured(entries)
|
|
default:
|
|
return fmt.Errorf("invalid output format %s", opts.Format)
|
|
}
|
|
return nil
|
|
}
|