DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 3f2c5e507f91280dc48837725387ace4899189e1 felu/internal/dns/query.go -rw-r--r-- 2.8 KiB
3f2c5e50Jonni Liljamo feat: initial NS and SOA responses a month ago
                                                                                
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

package dns

import (
	"log/slog"
	"net"
	"strings"

	"git.src.quest/~liljamo/felu/internal/config"
	"git.src.quest/~liljamo/felu/internal/db"
	"github.com/miekg/dns"
)

func parseQuery(m *dns.Msg, r *dns.Msg) {
	for _, q := range m.Question {
		slog.Info("Got Query",
			slog.Any("id", r.Id),
			slog.String("type", dns.TypeToString[q.Qtype]),
			slog.String("qname", q.Name),
		)

		switch q.Qtype {
		case dns.TypeA:
			handleARecord(&q, m, r)
		case dns.TypeNS:
			handleNSRecord(&q, m, r)
		case dns.TypeSOA:
			handleSOARecord(&q, m, r)
		default:
			m.SetRcode(r, dns.RcodeNotImplemented)
		}

		slog.Info("Responding to Query",
			slog.Any("id", r.Id),
			slog.String("rcode", dns.RcodeToString[m.Rcode]),
		)
	}
}

func handleARecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	if index := strings.IndexByte(q.Name, '.'); index >= 0 {
		aRecord, err := db.FetchDomainARecord(strings.ToLower(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)
	}
}

func handleNSRecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	ns := &dns.NS{
		Hdr: dns.RR_Header{Name: q.Name, Rrtype: dns.TypeNS, Class: dns.ClassINET, Ttl: 0},
		Ns:  dns.Fqdn(config.FeluConfig.DDNSDomain),
	}

	// "Root" DDNS domain NS.
	if q.Name == dns.Fqdn(config.FeluConfig.DDNSDomain) {
		m.Answer = append(m.Answer, ns)
		return
	}

	if index := strings.IndexByte(q.Name, '.'); index >= 0 {
		// FIXME: other way of checking that the domain exists
		_, err := db.FetchDomainARecord(strings.ToLower(q.Name[:index]))
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, ns)
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}

func handleSOARecord(q *dns.Question, m *dns.Msg, r *dns.Msg) {
	soa := &dns.SOA{
		Hdr:     dns.RR_Header{Name: q.Name, Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 0},
		Ns:      dns.Fqdn(config.FeluConfig.DDNSDomain),
		Mbox:    dns.Fqdn(config.FeluConfig.SOAEmail),
		Serial:  2024100301,
		Refresh: 86400,
		Retry:   7200,
		Expire:  3600000,
		Minttl:  172800,
	}

	// "Root" DDNS domain SOA.
	if q.Name == dns.Fqdn(config.FeluConfig.DDNSDomain) {
		m.Answer = append(m.Answer, soa)
		return
	}

	if index := strings.IndexByte(q.Name, '.'); index >= 0 {
		// FIXME: other way of checking that the domain exists
		_, err := db.FetchDomainARecord(strings.ToLower(q.Name[:index]))
		if err != nil {
			m.SetRcode(r, dns.RcodeNameError)
		} else {
			m.Answer = append(m.Answer, soa)
		}
	} else {
		m.SetRcode(r, dns.RcodeNameError)
	}
}