chore: reuse code

This commit is contained in:
Skyxim 2023-03-12 14:37:46 +08:00
parent a510909228
commit c555cec490

View File

@ -31,7 +31,12 @@ func withHosts(hosts R.Hosts, mapping *cache.LruCache[netip.Addr, string]) middl
} }
host := strings.TrimRight(q.Name, ".") host := strings.TrimRight(q.Name, ".")
handleCName := func(resp *D.Msg, domain string) {
rr := &D.CNAME{}
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeCNAME, Class: D.ClassINET, Ttl: 10}
rr.Target = domain + "."
resp.Answer = append([]D.RR{rr}, resp.Answer...)
}
record, ok := hosts.Search(host, q.Qtype != D.TypeA && q.Qtype != D.TypeAAAA) record, ok := hosts.Search(host, q.Qtype != D.TypeA && q.Qtype != D.TypeAAAA)
if !ok { if !ok {
if record != nil && record.IsDomain { if record != nil && record.IsDomain {
@ -42,6 +47,7 @@ func withHosts(hosts R.Hosts, mapping *cache.LruCache[netip.Addr, string]) middl
if err == nil { if err == nil {
resp.Id = r.Id resp.Id = r.Id
resp.Question = r.Question resp.Question = r.Question
handleCName(resp,record.Domain)
} }
return resp, err return resp, err
} }
@ -78,10 +84,7 @@ func withHosts(hosts R.Hosts, mapping *cache.LruCache[netip.Addr, string]) middl
case D.TypeAAAA: case D.TypeAAAA:
handleIPs() handleIPs()
case D.TypeCNAME: case D.TypeCNAME:
rr := &D.CNAME{} handleCName(r,record.Domain)
rr.Hdr = D.RR_Header{Name: q.Name, Rrtype: D.TypeCNAME, Class: D.ClassINET, Ttl: 10}
rr.Target = record.Domain + "."
msg.Answer = append(msg.Answer, rr)
default: default:
return next(ctx, r) return next(ctx, r)
} }