@@ 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
+}
@@ 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)
+ }
}