remove debugging info and refactoring

This commit is contained in:
Vladimir Avtsenov 2024-08-30 03:37:00 +03:00
parent 1b132499f0
commit fe00189cd6
2 changed files with 17 additions and 43 deletions

View File

@ -159,11 +159,6 @@ func (a *App) processARecord(aRecord dnsProxy.Address) {
a.Records.PutARecord(aRecord.Name.String(), aRecord.Address, ttlDuration) a.Records.PutARecord(aRecord.Name.String(), aRecord.Address, ttlDuration)
cNames := append([]string{aRecord.Name.String()}, a.Records.GetCNameRecords(aRecord.Name.String(), true, true)...) cNames := append([]string{aRecord.Name.String()}, a.Records.GetCNameRecords(aRecord.Name.String(), true, true)...)
fmt.Printf("Relates CNames:\n")
for idx, cName := range cNames {
fmt.Printf("|- #%d: %s\n", idx, cName)
}
for _, group := range a.Groups { for _, group := range a.Groups {
for _, domain := range group.Domains { for _, domain := range group.Domains {
if !domain.IsEnabled() { if !domain.IsEnabled() {
@ -171,7 +166,7 @@ func (a *App) processARecord(aRecord dnsProxy.Address) {
} }
for _, cName := range cNames { for _, cName := range cNames {
if domain.IsMatch(cName) { if domain.IsMatch(cName) {
fmt.Printf("|- Matched %s (%s) for %s in %s group!\n", cName, aRecord.Name, domain.Domain, group.Name) fmt.Printf("Matched %s (%s) for %s in %s group!\n", cName, aRecord.Name, domain.Domain, group.Name)
} }
} }
} }
@ -187,43 +182,26 @@ func (a *App) processCNameRecord(cNameRecord dnsProxy.CName) {
a.Records.PutCNameRecord(cNameRecord.Name.String(), cNameRecord.CName.String(), ttlDuration) a.Records.PutCNameRecord(cNameRecord.Name.String(), cNameRecord.CName.String(), ttlDuration)
} }
func (a *App) handleRecord(msg *dnsProxy.Message) { func (a *App) handleRecord(rr dnsProxy.ResourceRecord) {
printKnownRecords := func() { switch v := rr.(type) {
for _, q := range msg.QD { case dnsProxy.Address:
fmt.Printf("%04x: DBG Known addresses for: %s\n", msg.ID, q.QName.String()) a.processARecord(v)
for idx, addr := range a.Records.GetARecords(q.QName.String(), true, false) { case dnsProxy.CName:
fmt.Printf("%04x: #%d: %s\n", msg.ID, idx, addr.String()) a.processCNameRecord(v)
} default:
}
}
parseResponseRecord := func(rr dnsProxy.ResourceRecord) {
switch v := rr.(type) {
case dnsProxy.Address:
fmt.Printf("%04x: -> A: Name: %s; Address: %s; TTL: %d\n", msg.ID, v.Name, v.Address.String(), v.TTL)
a.processARecord(v)
case dnsProxy.CName:
fmt.Printf("%04x: -> CNAME: Name: %s; CName: %s\n", msg.ID, v.Name, v.CName)
a.processCNameRecord(v)
default:
fmt.Printf("%04x: -> Unknown: %x\n", msg.ID, v.EncodeResource())
}
} }
}
printKnownRecords() func (a *App) handleMessage(msg *dnsProxy.Message) {
for _, q := range msg.QD { for _, rr := range msg.AN {
fmt.Printf("%04x: <- Request name: %s\n", msg.ID, q.QName.String()) a.handleRecord(rr)
} }
for _, a := range msg.AN { for _, rr := range msg.NS {
parseResponseRecord(a) a.handleRecord(rr)
} }
for _, a := range msg.NS { for _, rr := range msg.AR {
parseResponseRecord(a) a.handleRecord(rr)
} }
for _, a := range msg.AR {
parseResponseRecord(a)
}
printKnownRecords()
fmt.Println()
} }
func New(config Config) (*App, error) { func New(config Config) (*App, error) {
@ -234,7 +212,7 @@ func New(config Config) (*App, error) {
app.Config = config app.Config = config
app.DNSProxy = dnsProxy.New(app.Config.ListenPort, app.Config.TargetDNSServerAddress) app.DNSProxy = dnsProxy.New(app.Config.ListenPort, app.Config.TargetDNSServerAddress)
app.DNSProxy.MsgHandler = app.handleRecord app.DNSProxy.MsgHandler = app.handleMessage
app.Records = NewRecords() app.Records = NewRecords()

View File

@ -2,7 +2,6 @@ package dnsProxy
import ( import (
"context" "context"
"encoding/hex"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -91,9 +90,6 @@ func (p DNSProxy) handleDNSRequest(clientAddr *net.UDPAddr, buffer []byte) {
return return
} }
// TODO: Debug log level
log.Printf("Response: %s", hex.EncodeToString(response[:n]))
msg, err := ParseResponse(response[:n]) msg, err := ParseResponse(response[:n])
if err == nil { if err == nil {
if p.MsgHandler != nil { if p.MsgHandler != nil {