generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
51 lines (40 loc) · 1.2 KB
/
request.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
package hashy
import (
"strings"
"github.com/miekg/dns"
)
// Request represents a DNS request to hashy that hashy recognizes.
type Request struct {
// ID is the value to hash. This is required.
ID string
// Nonce is an optional entropy value sent by the client.
Nonce string
// Group is the optional group name to restrict returned services.
Group string
// Qtype is the record type being requested.
Qtype uint16
// Origin is the domain that hashy recognized when it parsed this request.
Origin string
}
// QuestionParser provides the logic to parse a DNS question into a request
// that hashy recognizes.
type QuestionParser struct {
// Origin is the domain that is supported for hashy queries.
Origin string
}
// Parse examines a DNS question and produces a Request. If the question
// is not supported, this method returns false.
func (qp *QuestionParser) Parse(q dns.Question) (r Request, supported bool) {
if q.Qclass != dns.ClassINET && q.Qclass != dns.ClassANY {
return
}
prefix, found := strings.CutSuffix(q.Name, qp.Origin)
if !found {
return
}
r.Qtype = q.Qtype
r.Origin = qp.Origin
r.ID, r.Group, _ = strings.Cut(prefix, ".")
r.ID, r.Nonce, _ = strings.Cut(r.ID, "-")
return
}