-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 595113b
Showing
12 changed files
with
995 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,3 @@ | ||
export LIBDNS_DNSEXIT_API_KEY= | ||
export LIBDNS_DNSEXIT_ZONE= | ||
export LIBDNS_DNSEXIT_DEBUG=TRUE/FALSE |
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,3 @@ | ||
_gitignore/ | ||
.env | ||
|
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Stephen Holmes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,85 @@ | ||
DNSExit for [`libdns`](https://github.com/libdns/libdns) | ||
======================= | ||
|
||
[](https://pkg.go.dev/github.com/libdns/dnsexit) | ||
|
||
This package implements the [libdns interfaces](https://github.com/libdns/libdns) for DNSExit, allowing you to manage DNS records. | ||
|
||
Configuration | ||
============= | ||
|
||
[DNSExit API documentation](https://dnsexit.com/dns/dns-api/) details the process of getting an API key. | ||
|
||
To run clone the `.env_template` to a file named `.env` and populate with the API key and zone. Note that setting the environment variable 'LIBDNS_DNSEXIT_DEBUG=TRUE' will output the request body, which includes the API key. | ||
|
||
Example | ||
======= | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/libdns/dnsexit" | ||
"github.com/libdns/libdns" | ||
) | ||
|
||
func main() { | ||
key := os.Getenv("LIBDNS_DNSEXIT_API_KEY") | ||
if key == "" { | ||
fmt.Println("LIBDNS_DNSEXIT_API_KEY not set") | ||
return | ||
} | ||
|
||
zone := os.Getenv("LIBDNS_DNSEXIT_ZONE") | ||
if zone == "" { | ||
fmt.Println("LIBDNS_DNSEXIT_ZONE not set") | ||
return | ||
} | ||
|
||
p := &dnsexit.Provider{ | ||
APIKey: key, | ||
} | ||
|
||
records := []libdns.Record{ | ||
{ | ||
Type: "A", | ||
Name: "test", | ||
Value: "198.51.100.1", | ||
}, | ||
{ | ||
Type: "AAAA", | ||
Name: "test", | ||
Value: "2001:0db8::1", | ||
}, | ||
{ | ||
Type: "TXT", | ||
Name: "test", | ||
Value: "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
_, err := p.SetRecords(ctx, zone, records) | ||
if err != nil { | ||
fmt.Printf("Error: %v", err) | ||
return | ||
} | ||
} | ||
``` | ||
|
||
Caveats | ||
======= | ||
|
||
The API does not include a GET method, so fetching records is done via Google DNS. There will be some latency. | ||
|
||
If an 'A' and 'AAAA' record have the same name, deleting either of them will remove both records. | ||
|
||
If multiple record updates are sent in one request, the API may return a code other than 0, to indicate partial success. This is currently judged as a fail and API error message is returned instead of the successfully amended records. | ||
|
||
MX records have mail-zone and mail-server properties, which do not exist in the LibDNS record type, so updating these has not been fully implemented. 'name' can be used instead to specify the mail server, but there is no way to specify the mail-zone. See https://dnsexit.com/dns/dns-api/#example-update-mx | ||
|
||
For [Dynamic DNS](https://dnsexit.com/dns/dns-api/#dynamic-ip-update) DNSExit recommend their dedicated GET endpoint, which can set the domain's IP to the one making the request. That is not implemented in this library. |
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,195 @@ | ||
package dnsexit | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net" | ||
"net/netip" | ||
"os" | ||
"time" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"github.com/libdns/libdns" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// API URL to POST updates to | ||
updateURL = "https://api.dnsexit.com/dns/" | ||
) | ||
|
||
var ( | ||
// Set environment variable to "TRUE" to enable debug logging | ||
debug = (os.Getenv("LIBDNS_DNSEXIT_DEBUG") == "TRUE") | ||
client = resty.New() | ||
) | ||
|
||
// Query Google DNS for A/AAAA/TXT record for a given DNS name | ||
func (p *Provider) getDomain(ctx context.Context, zone string) ([]libdns.Record, error) { | ||
p.mutex.Lock() | ||
defer p.mutex.Unlock() | ||
|
||
var libRecords []libdns.Record | ||
|
||
// The API only supports adding/updating/deleting records and no way | ||
// to get current records. So instead, we just make | ||
// simple DNS queries to get the A, AAAA, and TXT records. | ||
r := &net.Resolver{ | ||
PreferGo: true, | ||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
d := net.Dialer{Timeout: 10 * time.Second} | ||
return d.DialContext(ctx, network, "8.8.8.8:53") | ||
}, | ||
} | ||
|
||
ips, err := r.LookupHost(ctx, zone) | ||
if err != nil { | ||
var dnsErr *net.DNSError | ||
// Ignore missing dns record | ||
if !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) { | ||
return libRecords, errors.Wrapf(err, "error looking up host") | ||
} | ||
} | ||
|
||
for _, ip := range ips { | ||
parsed, err := netip.ParseAddr(ip) | ||
if err != nil { | ||
return libRecords, errors.Wrapf(err, "error parsing ip") | ||
} | ||
|
||
if parsed.Is4() { | ||
libRecords = append(libRecords, libdns.Record{ | ||
Type: "A", | ||
Name: "@", | ||
Value: ip, | ||
}) | ||
} else { | ||
libRecords = append(libRecords, libdns.Record{ | ||
Type: "AAAA", | ||
Name: "@", | ||
Value: ip, | ||
}) | ||
} | ||
} | ||
|
||
txt, err := r.LookupTXT(ctx, zone) | ||
if err != nil { | ||
var dnsErr *net.DNSError | ||
// Ignore missing dns record | ||
if !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) { | ||
return libRecords, errors.Wrapf(err, "error looking up txt") | ||
} | ||
} | ||
for _, t := range txt { | ||
if t == "" { | ||
continue | ||
} | ||
libRecords = append(libRecords, libdns.Record{ | ||
Type: "TXT", | ||
Name: "@", | ||
Value: t, | ||
}) | ||
} | ||
|
||
return libRecords, nil | ||
} | ||
|
||
// Set or clear the value of a DNS entry | ||
func (p *Provider) amendRecords(zone string, records []libdns.Record, action Action) ([]libdns.Record, error) { | ||
|
||
var payloadRecords []dnsExitRecord | ||
p.mutex.Lock() | ||
defer p.mutex.Unlock() | ||
|
||
//////////////////////////////////////////////// | ||
// BUILD PAYLOAD | ||
//////////////////////////////////////////////// | ||
for _, record := range records { | ||
if record.TTL/time.Second < 600 { | ||
record.TTL = 600 * time.Second | ||
} | ||
ttlInSeconds := int(record.TTL / time.Second) | ||
|
||
relativeName := libdns.RelativeName(record.Name, zone) | ||
trimmedName := relativeName | ||
if relativeName == "@" { | ||
trimmedName = "" | ||
} | ||
|
||
currentRecord := dnsExitRecord{} | ||
currentRecord.Type = record.Type | ||
currentRecord.Name = trimmedName | ||
|
||
if action != deleteRecords { | ||
recordValue := record.Value | ||
currentRecord.Content = &recordValue | ||
recordPriority := int(record.Priority) | ||
currentRecord.Priority = &recordPriority | ||
recordTTL := ttlInSeconds | ||
currentRecord.TTL = &recordTTL | ||
} | ||
if action == setRecords { | ||
truevalue := true | ||
currentRecord.Overwrite = &truevalue | ||
} | ||
payloadRecords = append(payloadRecords, currentRecord) | ||
} | ||
|
||
payload := dnsExitPayload{} | ||
payload.Apikey = p.APIKey | ||
payload.Zone = zone | ||
|
||
switch action { | ||
case deleteRecords: | ||
payload.DeleteRecords = &payloadRecords | ||
case setRecords: | ||
fallthrough | ||
case appendRecords: | ||
payload.AddRecords = &payloadRecords | ||
default: | ||
return nil, errors.New(fmt.Sprintf("Unknown action type: %d", action)) | ||
} | ||
|
||
//////////////////////////////////////////////// | ||
//SEND PAYLOAD | ||
//////////////////////////////////////////////// | ||
|
||
// Explore response object | ||
reqBody, err := json.Marshal(payload) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if debug { | ||
fmt.Println("Request Info:") | ||
fmt.Println("Body:", string(reqBody)) | ||
} | ||
// Make the API request to DNSExit | ||
// POST Struct, default is JSON content type. No need to set one | ||
resp, err := client.R(). | ||
SetBody(payload). | ||
SetResult(&dnsExitResponse{}). | ||
SetError(&dnsExitResponse{}). | ||
Post(updateURL) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
//TODO - query the response code and text to determine which updates where successful, and return both records and response text in all cases, rather than just assuming all records for a 0 code and no records for other codes. | ||
|
||
// On any non-zero return code return the API response as the error text. | ||
if !isResposeStatusOK(resp.Body()) { | ||
respBody := string(resp.String()) | ||
return nil, errors.New(fmt.Sprintf("API request failed, response=%s", respBody)) | ||
} | ||
|
||
return records, nil | ||
} | ||
|
||
// Convert API response code to human friendly error | ||
func isResposeStatusOK(body []byte) bool { | ||
var respJson dnsExitResponse | ||
json.Unmarshal(body, &respJson) | ||
return respJson.Code == 0 | ||
} |
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,14 @@ | ||
module github.com/libdns/dnsexit | ||
|
||
go 1.22.0 | ||
|
||
toolchain go1.22.2 | ||
|
||
require ( | ||
github.com/go-resty/resty/v2 v2.16.2 | ||
github.com/joho/godotenv v1.5.1 | ||
github.com/libdns/libdns v0.2.2 | ||
github.com/pkg/errors v0.9.1 | ||
) | ||
|
||
require golang.org/x/net v0.33.0 // indirect |
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,12 @@ | ||
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg= | ||
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU= | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s= | ||
github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= | ||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= | ||
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U= | ||
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= |
Oops, something went wrong.