Skip to content

Commit

Permalink
fix: fix parsing TXT owner records; fix #126
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlba91 committed Sep 18, 2024
1 parent 3b2c150 commit 46e8923
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk=
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 h1:e66Fs6Z+fZTbFBAxKfP3PALWBtpfqks2bwGcexMxgtk=
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0/go.mod h1:2TbTHSBQa924w8M6Xs1QcRcFwyucIwBGpK1p2f1YFFY=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
13 changes: 10 additions & 3 deletions internal/adguard/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func endpointSupported(e *endpoint.Endpoint) bool {
}

func deserializeToEndpoint(rule string) (*endpoint.Endpoint, error) {
// format: "|DNS.NAME^dnsrewrite=NOERROR;RECORD_TYPE;TARGET"
// format: "|DNS.NAME^dnsrewrite=NOERROR;RECORD_TYPE;TARGET,important"
p := strings.SplitN(rule, ";", 3)
if len(p) != 3 {
return nil, errNotManaged
Expand All @@ -200,14 +200,21 @@ func deserializeToEndpoint(rule string) (*endpoint.Endpoint, error) {
if len(dp) != 2 {
return nil, fmt.Errorf("invalid rule: %s", rule)
}

d := strings.TrimPrefix(dp[0], "|")
t := strings.Split(p[2], ",")

// TXT records can contain commas, so we need to ensure we don't split them incorrectly
t := p[2]
lc := strings.LastIndex(p[2], ",")
if lc > 0 {
t = p[2][:lc]
}

// see serializeToString for the format
r := &endpoint.Endpoint{
RecordType: p[1],
DNSName: d,
Targets: endpoint.Targets{t[0]},
Targets: endpoint.Targets{t},
}

return r, nil
Expand Down
13 changes: 13 additions & 0 deletions internal/adguard/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func TestDeserializeToEndpoint(t *testing.T) {
text: "|domain.com^$dnsrewrite=NOERROR;TXT;external-dns-txt",
endpoint: &endpoint.Endpoint{DNSName: "domain.com", RecordType: endpoint.RecordTypeTXT, Targets: []string{"external-dns-txt"}},
},
{
name: "TXT owner record",
text: "|whoami.local^$dnsrewrite=NOERROR;TXT;heritage=external-dns,external-dns/owner=default,external-dns/resource=httproute/default/whoami-http-route,important",
endpoint: &endpoint.Endpoint{DNSName: "whoami.local", RecordType: endpoint.RecordTypeTXT, Targets: []string{"heritage=external-dns,external-dns/owner=default,external-dns/resource=httproute/default/whoami-http-route"}},
},
{
name: "long TXT record",
text: "|domain.com^$dnsrewrite=NOERROR;TXT;\"external-dns-txt; d=abc; v=...\"",
Expand Down Expand Up @@ -268,6 +273,7 @@ func TestRecords(t *testing.T) {
"|domain.com^$dnsrewrite=NOERROR;A;2.2.2.2",
"|domain.com^$dnsrewrite=NOERROR;AAAA;1111:1111::1",
"|other.org^$dnsrewrite=NOERROR;A;3.3.3.3",
"|whoami.local^$dnsrewrite=NOERROR;TXT;heritage=external-dns,external-dns/owner=default,external-dns/resource=httproute/default/whoami-http-route,important",
},
},
endpoints: []*endpoint.Endpoint{
Expand All @@ -293,6 +299,13 @@ func TestRecords(t *testing.T) {
"3.3.3.3",
},
},
{
DNSName: "whoami.local",
RecordType: endpoint.RecordTypeTXT,
Targets: []string{
"heritage=external-dns,external-dns/owner=default,external-dns/resource=httproute/default/whoami-http-route",
},
},
},
},
{
Expand Down

0 comments on commit 46e8923

Please sign in to comment.