DEVELOPMENT ENVIRONMENT

~liljamo/felu

326c993612df33f8ae796d652157bf49281a1596 — Jonni Liljamo 28 days ago bd08cb2
feat: zoneOf function
2 files changed, 36 insertions(+), 0 deletions(-)

A internal/dns/misc.go
A internal/dns/misc_test.go
A internal/dns/misc.go => internal/dns/misc.go +26 -0
@@ 0,0 1,26 @@
/*
 * 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"
	"strings"
)

func zoneOf(fqdn string, zone string) bool {
	// Get the index of the first '.', and compare everything after that to zone.
	if index := strings.IndexByte(fqdn, '.'); index >= 0 {
		index++
		return fqdn[index:] == zone
	}

	// Probably both fqdn and zone were just '.'. Crossing my fingers and hoping
	// this can't be reached in correct configuration.
	slog.Error("Invalid use of zone_of", slog.String("fqdn", fqdn), slog.String("zone", zone))
	return false
}

A internal/dns/misc_test.go => internal/dns/misc_test.go +10 -0
@@ 0,0 1,10 @@
package dns

import "testing"

func TestZoneOf(t *testing.T) {
	want := "test.ddns.felu.arpa."
	if !zoneOf("_acme-challenge.test.ddns.felu.arpa.", want) {
		t.Error("zoneOf")
	}
}