From 326c993612df33f8ae796d652157bf49281a1596 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 24 Oct 2024 12:11:39 +0300 Subject: [PATCH] feat: zoneOf function --- internal/dns/misc.go | 26 ++++++++++++++++++++++++++ internal/dns/misc_test.go | 10 ++++++++++ 2 files changed, 36 insertions(+) create mode 100644 internal/dns/misc.go create mode 100644 internal/dns/misc_test.go diff --git a/internal/dns/misc.go b/internal/dns/misc.go new file mode 100644 index 0000000..85a5cca --- /dev/null +++ b/internal/dns/misc.go @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2024 Jonni Liljamo + * + * 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 +} diff --git a/internal/dns/misc_test.go b/internal/dns/misc_test.go new file mode 100644 index 0000000..0d01113 --- /dev/null +++ b/internal/dns/misc_test.go @@ -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") + } +} -- 2.44.1