DEVELOPMENT ENVIRONMENT

~liljamo/felu

8acfdbc07e82191871ec282d9633c1964c00f4e3 — Jonni Liljamo 1 year, 1 month ago 274ac57
feat: dns server to fetch a records from db
2 files changed, 26 insertions(+), 8 deletions(-)

M internal/db/domains.go
M internal/dns/query.go
M internal/db/domains.go => internal/db/domains.go +10 -0
@@ 70,3 70,13 @@ func DeleteDomainsForUser(userId string) error {
	}
	return nil
}

func FetchDomainARecord(ddns_domain string) (string, error) {
	var aRecord string
	err := DBConn.QueryRow(`SELECT a_record FROM domains WHERE ddns_domain = $1`,
		ddns_domain).Scan(&aRecord)
	if err != nil {
		return "", err
	}
	return aRecord, nil
}

M internal/dns/query.go => internal/dns/query.go +16 -8
@@ 8,7 8,10 @@ package dns

import (
	"log"
	"net"
	"strings"

	"git.src.quest/~skye/felu-ddns/internal/db"
	"github.com/miekg/dns"
)



@@ 24,12 27,17 @@ func parseQuery(m *dns.Msg, r *dns.Msg) {
func handleARecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	log.Printf("[felu/dns] Query for '%s'", q.Name)

	// fetch from db here
	// if found do this:
	//m.Answer = append(m.Answer, &dns,A{
  //	Hdr: dns.RR_Header{ Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60 },
	//	A: net.ParseIP(THEIPGOTTENFROMDBLOLOLOLO)
  //}
	// else, do this:
	m.SetRcode(r, dns.RcodeNameError)
	if index := strings.IndexByte(q.Name, '.'); index >= 0 {
		aRecord, err := db.FetchDomainARecord(q.Name[:index])
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, &dns.A{
				Hdr: dns.RR_Header{ Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 60 },
				A: net.ParseIP(aRecord),
			})
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}