add functional options to NewXip for testability

This commit is contained in:
m5r
2026-01-18 00:08:44 +01:00
parent 2c34850415
commit f585fb393b
2 changed files with 82 additions and 38 deletions

View File

@@ -15,6 +15,44 @@ import (
type Xip struct { type Xip struct {
server dns.Server server dns.Server
nameServers []string nameServers []string
domain string
email string
dnsPort uint
}
type Option func(*Xip)
func WithDomain(domain string) Option {
return func(x *Xip) {
x.domain = domain
}
}
func WithEmail(email string) Option {
return func(x *Xip) {
x.email = email
}
}
func WithDnsPort(port uint) Option {
return func(x *Xip) {
x.dnsPort = port
}
}
func WithNameServers(nameServers []string) Option {
return func(x *Xip) {
for i, ns := range nameServers {
name := fmt.Sprintf("ns%d.%s.", i+1, x.domain)
ip := net.ParseIP(ns)
entry := hardcodedRecords[name]
entry.A = append(entry.A, ip)
hardcodedRecords[name] = entry
x.nameServers = append(x.nameServers, name)
}
}
} }
var ( var (
@@ -26,29 +64,27 @@ var (
func (xip *Xip) SetTXTRecord(fqdn string, value string) { func (xip *Xip) SetTXTRecord(fqdn string, value string) {
utils.Logger.Trace().Str("fqdn", fqdn).Str("value", value).Msg("Trying to set TXT record") utils.Logger.Trace().Str("fqdn", fqdn).Str("value", value).Msg("Trying to set TXT record")
config := utils.GetConfig() if fqdn != fmt.Sprintf("_acme-challenge.%s.", xip.domain) {
if fqdn != fmt.Sprintf("_acme-challenge.%s.", config.Domain) {
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort setting TXT record") utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort setting TXT record")
return return
} }
if rootRecords, ok := hardcodedRecords[fqdn]; ok { if rootRecords, ok := hardcodedRecords[fqdn]; ok {
rootRecords.TXT = []string{value} rootRecords.TXT = []string{value}
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = rootRecords hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", xip.domain)] = rootRecords
} }
} }
func (xip *Xip) UnsetTXTRecord(fqdn string) { func (xip *Xip) UnsetTXTRecord(fqdn string) {
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Trying to unset TXT record") utils.Logger.Trace().Str("fqdn", fqdn).Msg("Trying to unset TXT record")
config := utils.GetConfig() if fqdn != fmt.Sprintf("_acme-challenge.%s.", xip.domain) {
if fqdn != fmt.Sprintf("_acme-challenge.%s.", config.Domain) {
utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort unsetting TXT record") utils.Logger.Trace().Str("fqdn", fqdn).Msg("Not allowed, abort unsetting TXT record")
return return
} }
if rootRecords, ok := hardcodedRecords[fqdn]; ok { if rootRecords, ok := hardcodedRecords[fqdn]; ok {
rootRecords.TXT = []string{} rootRecords.TXT = []string{}
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = rootRecords hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", xip.domain)] = rootRecords
} }
} }
@@ -268,7 +304,6 @@ func emailToRname(email string) string {
} }
func (xip *Xip) soaRecord(question dns.Question) *dns.SOA { func (xip *Xip) soaRecord(question dns.Question) *dns.SOA {
config := utils.GetConfig()
soa := new(dns.SOA) soa := new(dns.SOA)
soa.Hdr = dns.RR_Header{ soa.Hdr = dns.RR_Header{
Name: question.Name, Name: question.Name,
@@ -278,7 +313,7 @@ func (xip *Xip) soaRecord(question dns.Question) *dns.SOA {
Rdlength: 0, Rdlength: 0,
} }
soa.Ns = xip.nameServers[0] soa.Ns = xip.nameServers[0]
soa.Mbox = emailToRname(config.Email) soa.Mbox = emailToRname(xip.email)
soa.Serial = 2024072600 soa.Serial = 2024072600
soa.Refresh = uint32((time.Minute * 15).Seconds()) soa.Refresh = uint32((time.Minute * 15).Seconds())
soa.Retry = uint32((time.Minute * 15).Seconds()) soa.Retry = uint32((time.Minute * 15).Seconds())
@@ -403,18 +438,28 @@ func (xip *Xip) initHardcodedRecords() {
hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = hardcodedRecord{TXT: []string{}} hardcodedRecords[fmt.Sprintf("_acme-challenge.%s.", config.Domain)] = hardcodedRecord{TXT: []string{}}
} }
func NewXip() (xip *Xip) { func NewXip(opts ...Option) (xip *Xip) {
config := utils.GetConfig() config := utils.GetConfig()
xip = &Xip{} xip = &Xip{
domain: config.Domain,
email: config.Email,
dnsPort: config.DnsPort,
}
for _, opt := range opts {
opt(xip)
}
if len(xip.nameServers) == 0 {
xip.initHardcodedRecords() xip.initHardcodedRecords()
}
xip.server = dns.Server{ xip.server = dns.Server{
Addr: fmt.Sprintf(":%d", config.DnsPort), Addr: fmt.Sprintf(":%d", xip.dnsPort),
Net: "udp", Net: "udp",
} }
zone := fmt.Sprintf("%s.", config.Domain) zone := fmt.Sprintf("%s.", xip.domain)
dns.HandleFunc(zone, xip.handleDnsRequest) dns.HandleFunc(zone, xip.handleDnsRequest)
return xip return xip

View File

@@ -5,13 +5,14 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"testing" "testing"
"github.com/spf13/viper"
) )
func TestResolveDashUnit(t *testing.T) { func TestResolveDashUnit(t *testing.T) {
// viper.Set("dns-port", 9053) xip := NewXip(
xip := NewXip() WithDomain("local-ip.sh"),
WithDnsPort(9053),
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
)
A := xip.fqdnToA("192-168-1-29.local-ip.sh") A := xip.fqdnToA("192-168-1-29.local-ip.sh")
expected := "192.168.1.29" expected := "192.168.1.29"
@@ -41,20 +42,26 @@ func TestResolveDashUnit(t *testing.T) {
} }
func TestConstructor(t *testing.T) { func TestConstructor(t *testing.T) {
viper.Set("dns-port", 9053) xip := NewXip(
xip := NewXip() WithDomain("local-ip.sh"),
WithDnsPort(9053),
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
)
if xip.nameServers[0] != "ns1.local-ip.sh" { if xip.nameServers[0] != "ns1.local-ip.sh." {
t.Error("") t.Errorf("expected ns1.local-ip.sh., got %s", xip.nameServers[0])
} }
if xip.nameServers[1] != "ns2.local-ip.sh" { if xip.nameServers[1] != "ns2.local-ip.sh." {
t.Error("") t.Errorf("expected ns2.local-ip.sh., got %s", xip.nameServers[1])
} }
} }
func TestResolveDashE2E(t *testing.T) { func TestResolveDashE2E(t *testing.T) {
viper.Set("dns-port", 9053) xip := NewXip(
xip := NewXip() WithDomain("local-ip.sh"),
WithDnsPort(9053),
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
)
go xip.StartServer() go xip.StartServer()
cmd := exec.Command("dig", "@localhost", "-p", "9053", "192-168-1-29.local-ip.sh", "+short") cmd := exec.Command("dig", "@localhost", "-p", "9053", "192-168-1-29.local-ip.sh", "+short")
@@ -70,25 +77,17 @@ func TestResolveDashE2E(t *testing.T) {
func BenchmarkResolveDashBasic(b *testing.B) { func BenchmarkResolveDashBasic(b *testing.B) {
b.Skip() b.Skip()
// var semaphore = make(chan int, 40)
// var done = make(chan bool, 1)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
port := 9053 + i port := 9053 + i
viper.Set("dns-port", port) xip := NewXip(
xip := NewXip() WithDomain("local-ip.sh"),
WithDnsPort(uint(port)),
WithNameServers([]string{"1.2.3.4", "5.6.7.8"}),
)
go xip.StartServer() go xip.StartServer()
// semaphore <- 1
// go func() {
cmd := exec.Command("dig", "@localhost", "-p", fmt.Sprint(port), "192-168-1-29.local-ip.sh", "+short") cmd := exec.Command("dig", "@localhost", "-p", fmt.Sprint(port), "192-168-1-29.local-ip.sh", "+short")
cmd.Run() cmd.Run()
// <-semaphore
// if i == b.N {
// done <- true
// }
// }()
} }
// <-done
} }