local-ip.sh/xip/records.go
2024-07-22 01:06:47 +02:00

154 lines
3.9 KiB
Go

package xip
import (
"fmt"
"net"
"github.com/miekg/dns"
"local-ip.sh/utils"
)
type hardcodedRecord struct {
A []net.IP // => dns.A
AAAA []net.IP // => dns.AAAA
TXT []string // => dns.TXT
MX []*dns.MX
CNAME []string // => dns.CNAME
SRV *dns.SRV
}
var config = utils.GetConfig()
var hardcodedRecords = map[string]hardcodedRecord{
// TODO: maybe --nameservers ns1.local-ip.sh.=137.66.40.11,ns2.local-ip.sh.=137.66.40.12
fmt.Sprintf("ns.%s.", config.Domain): {
// record holding ip addresses of ns1 and ns2
A: []net.IP{
net.IPv4(137, 66, 40, 11),
net.IPv4(137, 66, 40, 12),
},
},
fmt.Sprintf("ns1.%s.", config.Domain): {
A: []net.IP{
net.IPv4(137, 66, 40, 11), // fly.io edge-only ip address, see https://community.fly.io/t/custom-domains-certificate-is-stuck-on-awaiting-configuration/8329
},
},
fmt.Sprintf("ns2.%s.", config.Domain): {
A: []net.IP{
net.IPv4(137, 66, 40, 12), // fly.io edge-only ip address #2
},
},
fmt.Sprintf("%s.", config.Domain): {
// same as ns.local-ip.sh, it's the same machine :)
A: []net.IP{
net.IPv4(137, 66, 40, 11),
net.IPv4(137, 66, 40, 12),
},
},
fmt.Sprintf("_acme-challenge.%s.", config.Domain): {
// will be filled in later when requesting the wildcard certificate
TXT: []string{},
},
}
// additional records I set up to host emails, feel free to change or remove them for your own needs
var extraRecords = map[string]hardcodedRecord{
"local-ip.sh.": {
TXT: []string{"v=spf1 include:capsulecorp.dev ~all"},
MX: []*dns.MX{
{Preference: 10, Mx: "email.capsulecorp.dev."},
},
},
"autodiscover.local-ip.sh.": {
CNAME: []string{
"email.capsulecorp.dev.",
},
},
"_autodiscover._tcp.local-ip.sh.": {
SRV: &dns.SRV{
Priority: 0,
Weight: 0,
Port: 443,
Target: "email.capsulecorp.dev.",
},
},
"autoconfig.local-ip.sh.": {
CNAME: []string{
"email.capsulecorp.dev.",
},
},
"_dmarc.local-ip.sh.": {
TXT: []string{"v=DMARC1; p=none; rua=mailto:postmaster@local-ip.sh; ruf=mailto:admin@local-ip.sh"},
},
"dkim._domainkey.local-ip.sh.": {
TXT: []string{
"v=DKIM1;k=rsa;t=s;s=email;p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsMW6NFo34qzKRPbzK41GwbWncB8IDg1i2eA2VWznIVDmTzzsqILaBOGv2xokVpzZm0QRF9wSbeVUmvwEeQ7Z6wkfMjawenDEc3XxsNSvQUVBP6LU/xcm1zsR8wtD8r5J+Jm45pNFaateiM/kb/Eypp2ntdtd8CPsEgCEDpNb62LWdy0yzRdZ/M/fNn51UMN8hVFp4YfZngAt3bQwa6kPtgvTeqEbpNf5xanpDysNJt2S8zfqJMVGvnr8JaJiTv7ZlKMMp94aC5Ndcir1WbMyfmgSnGgemuCTVMWDGPJnXDi+8BQMH1b1hmTpWDiVdVlehyyWx5AfPrsWG9cEuDIfXwIDAQAB",
},
},
}
var records = mergeRecords(hardcodedRecords, extraRecords)
func mergeRecords(a, b map[string]hardcodedRecord) map[string]hardcodedRecord {
result := make(map[string]hardcodedRecord)
for k, v := range a {
result[k] = v
}
for k, v := range b {
if r, ok := result[k]; ok {
result[k] = hardcodedRecord{
A: uniqueIPs(append(r.A, v.A...)),
AAAA: uniqueIPs(append(r.AAAA, v.AAAA...)),
TXT: uniqueStrings(append(r.TXT, v.TXT...)),
MX: uniqueMX(append(r.MX, v.MX...)),
CNAME: uniqueStrings(append(r.CNAME, v.CNAME...)),
SRV: firstNonNil(r.SRV, v.SRV),
}
} else {
result[k] = v
}
}
return result
}
func uniqueIPs(ips []net.IP) []net.IP {
seen := make(map[string]bool)
result := []net.IP{}
for _, ip := range ips {
if !seen[ip.String()] {
seen[ip.String()] = true
result = append(result, ip)
}
}
return result
}
func uniqueStrings(strs []string) []string {
seen := make(map[string]bool)
result := []string{}
for _, str := range strs {
if !seen[str] {
seen[str] = true
result = append(result, str)
}
}
return result
}
func uniqueMX(mxs []*dns.MX) []*dns.MX {
seen := make(map[string]uint16)
result := []*dns.MX{}
for _, mx := range mxs {
if pref, exists := seen[mx.Mx]; !exists || pref > mx.Preference {
seen[mx.Mx] = mx.Preference
result = append(result, mx)
}
}
return result
}
func firstNonNil[T any](a, b *T) *T {
if a != nil {
return a
}
return b
}