2023-12-24 10:18:08 +00:00
|
|
|
// This file is part of the happyDomain (R) project.
|
|
|
|
// Copyright (c) 2020-2024 happyDomain
|
|
|
|
// Authors: Pierre-Olivier Mercier, et al.
|
2020-06-25 21:47:50 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is offered under a commercial and under the AGPL license.
|
|
|
|
// For commercial licensing, contact us at <contact@happydomain.org>.
|
2020-06-25 21:47:50 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// For AGPL licensing:
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2020-06-25 21:47:50 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
2020-06-25 21:47:50 +00:00
|
|
|
//
|
2023-12-24 10:18:08 +00:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-06-25 21:47:50 +00:00
|
|
|
|
|
|
|
package utils
|
|
|
|
|
2020-06-27 14:18:50 +00:00
|
|
|
import (
|
2023-09-15 18:25:31 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/StackExchange/dnscontrol/v4/models"
|
2020-06-27 14:18:50 +00:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
2020-06-25 21:47:50 +00:00
|
|
|
|
|
|
|
// SplitN splits a string into N sized string chunks.
|
|
|
|
// This function is a copy of https://github.com/miekg/dns/blob/master/types.go#L1509
|
|
|
|
// awaiting its exportation
|
|
|
|
func SplitN(s string, n int) []string {
|
|
|
|
if len(s) < n {
|
|
|
|
return []string{s}
|
|
|
|
}
|
|
|
|
sx := []string{}
|
|
|
|
p, i := 0, n
|
|
|
|
for {
|
|
|
|
if i <= len(s) {
|
|
|
|
sx = append(sx, s[p:i])
|
|
|
|
} else {
|
|
|
|
sx = append(sx, s[p:])
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
p, i = p+n, i+n
|
|
|
|
}
|
|
|
|
|
|
|
|
return sx
|
|
|
|
}
|
2020-06-27 14:18:50 +00:00
|
|
|
|
|
|
|
// IsDNSSECType returns true if the given rrtype is generally autogenerated by
|
|
|
|
// NS server.
|
|
|
|
func IsDNSSECType(rrtype uint16) bool {
|
|
|
|
return rrtype == dns.TypeNSEC ||
|
|
|
|
rrtype == dns.TypeNSEC3 ||
|
|
|
|
rrtype == dns.TypeNSEC3PARAM ||
|
|
|
|
rrtype == dns.TypeDNSKEY ||
|
|
|
|
rrtype == dns.TypeRRSIG
|
|
|
|
}
|
2020-06-27 17:25:45 +00:00
|
|
|
|
|
|
|
// DomainFQDN normalizes the domain by adding the origin if it is relative (not
|
|
|
|
// ended by .).
|
|
|
|
func DomainFQDN(subdomain string, origin string) string {
|
|
|
|
if subdomain == "" || subdomain[len(subdomain)-1] == '.' {
|
|
|
|
return subdomain
|
|
|
|
} else if subdomain == "@" {
|
|
|
|
return origin
|
|
|
|
} else {
|
|
|
|
return subdomain + "." + origin
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 23:30:53 +00:00
|
|
|
|
|
|
|
// DomainJoin appends each relative domains passed as argument.
|
|
|
|
func DomainJoin(domains ...string) (ret string) {
|
|
|
|
for _, d := range domains {
|
|
|
|
if d == "@" {
|
|
|
|
break
|
|
|
|
} else if d != "" {
|
|
|
|
ret += "." + d
|
|
|
|
}
|
|
|
|
|
2020-10-07 16:51:42 +00:00
|
|
|
if len(ret) > 0 && ret[len(ret)-1] == '.' {
|
2020-09-29 23:30:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ret) >= 1 {
|
|
|
|
ret = ret[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2023-09-15 18:25:31 +00:00
|
|
|
|
|
|
|
// DomainJoin appends each relative domains passed as argument.
|
|
|
|
func NewRecordConfig(domain string, rtype string, ttl uint32, origin string) *models.RecordConfig {
|
|
|
|
return &models.RecordConfig{
|
|
|
|
Name: strings.TrimSuffix(strings.TrimSuffix(DomainJoin(domain), origin), "."),
|
|
|
|
NameFQDN: strings.TrimSuffix(DomainJoin(domain), "."),
|
|
|
|
Type: rtype,
|
|
|
|
TTL: ttl,
|
|
|
|
}
|
|
|
|
}
|