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

a parser for questions that hashy supports #52

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Loading