-
Notifications
You must be signed in to change notification settings - Fork 32
/
resolver.go
142 lines (116 loc) · 2.92 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package retryabledns
import (
"net"
"strings"
stringsutil "github.com/projectdiscovery/utils/strings"
)
type Protocol string
const (
UDP Protocol = "udp"
TCP Protocol = "tcp"
DOH Protocol = "doh"
DOT Protocol = "dot"
)
func (p Protocol) String() string {
return string(p)
}
func (p Protocol) StringWithSemicolon() string {
return p.String() + ":"
}
type DohProtocol string
const (
JsonAPI DohProtocol = "jsonapi"
GET DohProtocol = "get"
POST DohProtocol = "post"
)
func (p DohProtocol) String() string {
return string(p)
}
func (p DohProtocol) StringWithSemicolon() string {
return ":" + p.String()
}
type Resolver interface {
String() string
}
type NetworkResolver struct {
Protocol Protocol
Host string
Port string
}
func (r NetworkResolver) String() string {
return net.JoinHostPort(r.Host, r.Port)
}
type DohResolver struct {
Protocol DohProtocol
URL string
}
func (r DohResolver) Method() string {
if r.Protocol == POST {
return POST.String()
}
return GET.String()
}
func (r DohResolver) String() string {
return r.URL
}
func parseResolver(r string) (resolver Resolver) {
rNetworkTokens := trimProtocol(r)
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?
}
}
if protocol != DOH {
networkResolver := &NetworkResolver{Protocol: protocol}
parseHostPort(networkResolver, rNetworkTokens)
resolver = networkResolver
}
return
}
func parseHostPort(networkResolver *NetworkResolver, r string) {
if host, port, err := net.SplitHostPort(r); err == nil {
networkResolver.Host = host
networkResolver.Port = port
} else {
networkResolver.Host = r
if networkResolver.Protocol == DOT {
networkResolver.Port = "853"
} else {
networkResolver.Port = "53"
}
}
}
func hasDohProtocol(resolver, protocol string) bool {
return strings.HasSuffix(resolver, protocol)
}
func trimProtocol(resolver string) string {
return stringsutil.TrimPrefixAny(resolver, TCP.StringWithSemicolon(), UDP.StringWithSemicolon(), DOH.StringWithSemicolon(), DOT.StringWithSemicolon())
}
func trimDohProtocol(resolver string) string {
return stringsutil.TrimSuffixAny(resolver, GET.StringWithSemicolon(), POST.StringWithSemicolon(), JsonAPI.StringWithSemicolon())
}
func parseResolvers(resolvers []string) []Resolver {
var parsedResolvers []Resolver
for _, resolver := range resolvers {
parsedResolvers = append(parsedResolvers, parseResolver(resolver))
}
return parsedResolvers
}