Skip to content

Commit

Permalink
Improve performance in hot parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Gia. Bui Dai committed Jan 19, 2022
1 parent 177540b commit 2a3c736
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
20 changes: 20 additions & 0 deletions client_test.go
Expand Up @@ -23,6 +23,26 @@ func TestConsistentResolve(t *testing.T) {
}
}

func TestUDP(t *testing.T) {
client := New([]string{"1.1.1.1:53", "udp:8.8.8.8"}, 5)

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)

// From current dig result
require.True(t, len(d.A) > 0)
}

func TestTCP(t *testing.T) {
client := New([]string{"tcp:1.1.1.1:53", "tcp:8.8.8.8"}, 5)

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)

// From current dig result
require.True(t, len(d.A) > 0)
}

func TestDOH(t *testing.T) {
client := New([]string{"doh:https://doh.opendns.com/dns-query:post", "doh:https://doh.opendns.com/dns-query:get"}, 5)

Expand Down
93 changes: 37 additions & 56 deletions resolver.go
Expand Up @@ -7,53 +7,33 @@ import (
"github.com/projectdiscovery/stringsutil"
)

type Protocol int
type Protocol string

const (
UDP Protocol = iota
TCP
DOH
DOT
UDP Protocol = "udp"
TCP Protocol = "tcp"
DOH Protocol = "doh"
DOT Protocol = "dot"
)

func (p Protocol) String() string {
switch p {
case DOH:
return "doh"
case UDP:
return "udp"
case TCP:
return "tcp"
case DOT:
return "dot"
}

return ""
return string(p)
}

func (p Protocol) StringWithSemicolon() string {
return p.String() + ":"
}

type DohProtocol int
type DohProtocol string

const (
JsonAPI DohProtocol = iota
GET
POST
JsonAPI DohProtocol = "jsonapi"
GET DohProtocol = "get"
POST DohProtocol = "post"
)

func (p DohProtocol) String() string {
switch p {
case JsonAPI:
return "jsonapi"
case GET:
return "get"
case POST:
return "post"
}

return ""
return string(p)
}

func (p DohProtocol) StringWithSemicolon() string {
Expand Down Expand Up @@ -92,29 +72,34 @@ func (r DohResolver) String() string {
}

func parseResolver(r string) (resolver Resolver) {
isTcp, isUDP, isDoh, isDot := hasProtocol(r, TCP.StringWithSemicolon()), hasProtocol(r, UDP.StringWithSemicolon()), hasProtocol(r, DOH.StringWithSemicolon()), hasProtocol(r, DOT.StringWithSemicolon())
rNetworkTokens := trimProtocol(r)
if isTcp || isUDP || isDot {
networkResolver := &NetworkResolver{Protocol: UDP}
if isTcp {
networkResolver.Protocol = TCP
} else if isDot {
networkResolver.Protocol = DOT
}
parseHostPort(networkResolver, rNetworkTokens)
resolver = networkResolver
} else if isDoh {
isJsonApi, isGet := hasDohProtocol(r, JsonAPI.StringWithSemicolon()), hasDohProtocol(r, GET.StringWithSemicolon())
URL := trimDohProtocol(rNetworkTokens)
dohResolver := &DohResolver{URL: URL, Protocol: POST}
if isJsonApi {
dohResolver.Protocol = JsonAPI
} else if isGet {
dohResolver.Protocol = GET
protocol := UDP

if len(r) >= 4 && r[3] == 58 { // 58 is ":"
switch r[0:3] {
case "udp":
case "tcp":
protocol = TCP
case "dot":
protocol = DOT
case "doh":
protocol = DOH
isJsonApi, isGet := hasDohProtocol(r, JsonAPI.StringWithSemicolon()), hasDohProtocol(r, GET.StringWithSemicolon())
URL := trimDohProtocol(rNetworkTokens)
dohResolver := &DohResolver{URL: URL, Protocol: POST}
if isJsonApi {
dohResolver.Protocol = JsonAPI
} else if isGet {
dohResolver.Protocol = GET
}
resolver = dohResolver
default:
// unsupported protocol?
}
resolver = dohResolver
} else {
networkResolver := &NetworkResolver{Protocol: UDP}
}

if protocol != DOH {
networkResolver := &NetworkResolver{Protocol: protocol}
parseHostPort(networkResolver, rNetworkTokens)
resolver = networkResolver
}
Expand All @@ -136,10 +121,6 @@ func parseHostPort(networkResolver *NetworkResolver, r string) {
}
}

func hasProtocol(resolver, protocol string) bool {
return strings.HasPrefix(resolver, protocol)
}

func hasDohProtocol(resolver, protocol string) bool {
return strings.HasSuffix(resolver, protocol)
}
Expand Down

0 comments on commit 2a3c736

Please sign in to comment.