Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolver stats #171

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
70 changes: 70 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
var (
hasResolver bool = resolver != nil
dnsdata DNSData
stats []DNSDataStats
err error
)

Expand Down Expand Up @@ -323,11 +324,27 @@
resp *dns.Msg
trResp chan *dns.Envelope
)
st := time.Now()

var stat = DNSDataStats{}
for i := 0; i < c.options.MaxRetries; i++ {
index := atomic.AddUint32(&c.serversIndex, 1)
if !hasResolver {
resolver = c.resolvers[index%uint32(len(c.resolvers))]
}

if i == 0 {
stat = DNSDataStats{
Resolver: resolver.String(),
Error: DNSResponseErrorNoError,
Spent: 0,
}
} else {
stat.Spent = time.Now().Sub(st)

Check failure on line 343 in client.go

View workflow job for this annotation

GitHub Actions / Lint Test

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
stats = append(stats, stat)
st = time.Now()
}

switch r := resolver.(type) {
case *NetworkResolver:
if requestType == dns.TypeAXFR {
Expand All @@ -343,25 +360,39 @@
dnsconn, err = c.tcpClient.Dial(resolver.String())
}
if err != nil {
stat.Error = DNSResponseErrorDialError
break
}
defer dnsconn.Close()
dnsTransfer := &dns.Transfer{Conn: dnsconn}
trResp, err = dnsTransfer.In(msg, resolver.String())
if err != nil {
stat.Error = DNSResponseErrorTransferIn
}
} else {
switch r.Protocol {
case TCP:
resp, _, err = c.tcpClient.Exchange(msg, resolver.String())
if err != nil {
stat.Error = DNSResponseErrorTCPClientExchange
}
case UDP:
if c.options.ConnectionPoolThreads > 1 {
if udpConnPool, ok := c.udpConnPool.Get(resolver.String()); ok {
resp, _, err = udpConnPool.Exchange(context.TODO(), c.udpClient, msg)
}
} else {
resp, _, err = c.udpClient.Exchange(msg, resolver.String())
if err != nil {
stat.Error = DNSResponseErrorUDPClientExchange
}
}
case DOT:
resp, _, err = c.dotClient.Exchange(msg, resolver.String())
if err != nil {
stat.Error = DNSResponseErrorDOTClientExchange
}

}
}
case *DohResolver:
Expand All @@ -370,6 +401,9 @@
method = doh.MethodGet
}
resp, err = c.dohClient.QueryWithDOHMsg(method, doh.Resolver{URL: r.URL}, msg)
if err != nil {
stat.Error = DNSResponseErrorQueryWithDOHMsg
}
}

if err != nil || (trResp == nil && resp == nil) {
Expand All @@ -379,6 +413,9 @@
// https://github.com/projectdiscovery/retryabledns/issues/25
if resp != nil && resp.Truncated && c.TCPFallback {
resp, _, err = c.tcpClient.Exchange(msg, resolver.String())
if err != nil {
stat.Error = DNSResponseErrorTCPClientExchange
}
if err != nil || resp == nil {
continue
}
Expand All @@ -387,8 +424,14 @@
switch requestType {
case dns.TypeAXFR:
err = dnsdata.ParseFromEnvelopeChan(trResp)
if err != nil {
stat.Error = DNSResponseErrorParseFromEnvelopeChan
}
default:
err = dnsdata.ParseFromMsg(resp)
if err != nil {
stat.Error = DNSResponseErrorParseFromMsg
}
}

// populate anyway basic info
Expand Down Expand Up @@ -417,8 +460,14 @@
break
}
}

stat.Spent = time.Now().Sub(st)

Check failure on line 464 in client.go

View workflow job for this annotation

GitHub Actions / Lint Test

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
stats = append(stats, stat)
st = time.Now()

Check failure on line 466 in client.go

View workflow job for this annotation

GitHub Actions / Lint Test

ineffectual assignment to st (ineffassign)
}

dnsdata.Stats = stats

return &dnsdata, err
}

Expand Down Expand Up @@ -604,6 +653,27 @@
RawResp *dns.Msg `json:"raw_resp,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
HostsFile bool `json:"hosts_file,omitempty"`
Stats []DNSDataStats
}

type DNSResponseError int

const (
DNSResponseErrorNoError DNSResponseError = iota
DNSResponseErrorDialError
DNSResponseErrorTransferIn
DNSResponseErrorTCPClientExchange
DNSResponseErrorUDPClientExchange
DNSResponseErrorDOTClientExchange
DNSResponseErrorQueryWithDOHMsg
DNSResponseErrorParseFromEnvelopeChan
DNSResponseErrorParseFromMsg
)

type DNSDataStats struct {
Resolver string
Error DNSResponseError
Spent time.Duration
}

type SOA struct {
Expand Down