-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f209a0a
commit f255c18
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//go:generate stringer -type=State | ||
|
||
package txtutil | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/miekg/dns" | ||
) | ||
|
||
func ParseCombined(s string) (string, error) { | ||
return txtDecodeCombined(s) | ||
} | ||
|
||
func EncodeCombined(t string) string { | ||
return txtEncodeCombined(ToChunks(t)) | ||
} | ||
|
||
// // txtDecode decodes TXT strings received from ROUTE53 and GCLOUD. | ||
func txtDecodeCombined(s string) (string, error) { | ||
|
||
// The dns package doesn't expose the quote parser. Therefore we create a TXT record and extract the strings. | ||
rr, err := dns.NewRR("example.com. IN TXT " + s) | ||
if err != nil { | ||
return "", fmt.Errorf("could not parse %q TXT: %w", s, err) | ||
} | ||
|
||
return strings.Join(rr.(*dns.TXT).Txt, ""), nil | ||
} | ||
|
||
// txtEncode encodes TXT strings as the old GetTargetCombined() function did. | ||
func txtEncodeCombined(ts []string) string { | ||
//printer.Printf("DEBUG: route53 txt outboundv=%v\n", ts) | ||
|
||
// Don't call this on fake types. | ||
rdtype := dns.StringToType["TXT"] | ||
|
||
// Magically create an RR of the correct type. | ||
rr := dns.TypeToRR[rdtype]() | ||
|
||
// Fill in the header. | ||
rr.Header().Name = "example.com." | ||
rr.Header().Rrtype = rdtype | ||
rr.Header().Class = dns.ClassINET | ||
rr.Header().Ttl = 300 | ||
|
||
// Fill in the TXT data. | ||
rr.(*dns.TXT).Txt = ts | ||
|
||
// Generate the quoted string: | ||
header := rr.Header().String() | ||
full := rr.String() | ||
if !strings.HasPrefix(full, header) { | ||
panic("assertion failed. dns.Hdr.String() behavior has changed in an incompatible way") | ||
} | ||
|
||
//printer.Printf("DEBUG: route53 txt encodedv=%v\n", t) | ||
return full[len(header):] | ||
} |