Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
retrieve list of records instead of a single record
Browse files Browse the repository at this point in the history
  • Loading branch information
mfuentesg committed Jun 15, 2022
1 parent 5d98cef commit 43d7241
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
35 changes: 18 additions & 17 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ func (h *Handler) buildMessage(m *dns.Msg) (*dns.Msg, error) {
return nil, errors.New("unsupported dns type")
}

record, err := h.storage.Get(domain)

if errors.Is(err, storage.ErrRecordNotFound) {
records, err := h.storage.GetByDomain(domain)
if err != nil {
logEntry.WithField("reason", err).Error("unable to get record from database")
return nil, err
}
if len(records) == 0 {
logEntry.Info("unregistered domain")

forwardedMessage, err := h.forwardQuery(m)
Expand All @@ -76,25 +79,23 @@ func (h *Handler) buildMessage(m *dns.Msg) (*dns.Msg, error) {
return forwardedMessage, err
}

if err != nil {
logEntry.WithField("reason", err).Error("unable to get record from database")
return nil, err
}

logEntry.Info("domain found")

var message dns.Msg
message.SetReply(m)
message.Authoritative = true
message.Answer = append(message.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: domain,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP(record.IPv4),
})

for _, record := range records {
message.Answer = append(message.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: domain,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60,
},
A: net.ParseIP(record.IPv4),
})
}

return &message, nil
}
Expand Down
16 changes: 13 additions & 3 deletions storage/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New(dsn string) (*SQLite, error) {
func (sq *SQLite) Put(r storage.Record) (string, error) {
query := `insert into records(
domain, ipv4, ipv6, ttl, type
) values(?, ?, ?, ?, ?) returning id`
) values(?, nullif(?, ''), nullif(?, ''), ?, ?) returning id`

var id string
err := sq.db.QueryRow(query, r.Domain, r.IPv4, r.IPv6, r.TTL, r.Type).Scan(&id)
Expand All @@ -45,7 +45,7 @@ func (sq *SQLite) Put(r storage.Record) (string, error) {

func (sq *SQLite) Get(identifier string) (*storage.Record, error) {
var record storage.Record
query := `select id, domain, ipv4, ipv6, ttl, type from records where id = ?`
query := `select id, domain, ifnull(ipv4, '') as ipv4, ifnull(ipv6, '') as ipv6, ttl, type from records where id = ?`
err := sq.db.QueryRow(query, identifier).
Scan(&record.ID, &record.Domain, &record.IPv4, &record.IPv6, &record.TTL, &record.Type)

Expand All @@ -60,6 +60,16 @@ func (sq *SQLite) Get(identifier string) (*storage.Record, error) {
return &record, nil
}

func (sq *SQLite) GetByDomain(domain string) ([]*storage.Record, error) {
var records []*storage.Record

query := `select id, domain, ifnull(ipv4, '') as ipv4, ifnull(ipv6, '') as ipv6, ttl, type from records where domain = ?`
if err := sq.db.Select(&records, query, domain); err != nil {
return nil, err
}
return records, nil
}

func (sq *SQLite) Delete(identifier string) error {
query := `delete from records where id = ?`
_, err := sq.db.Exec(query, identifier)
Expand All @@ -69,7 +79,7 @@ func (sq *SQLite) Delete(identifier string) error {

func (sq *SQLite) List() ([]*storage.Record, error) {
var records []*storage.Record
query := `select id, domain, ipv4, ipv6, ttl, type from records`
query := `select id, domain, ifnull(ipv4, '') as ipv4, ifnull(ipv6, '') as ipv6, ttl, type from records`
if err := sq.db.Select(&records, query); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Record struct {
type Storage interface {
Put(r Record) (string, error)
Get(identifier string) (*Record, error)
GetByDomain(domain string) ([]*Record, error)
Delete(identifier string) error
List() ([]*Record, error)
Close() error
Expand Down

0 comments on commit 43d7241

Please sign in to comment.