Skip to content

Commit

Permalink
Merge pull request #8 from jlentink/feature/dns-lookup
Browse files Browse the repository at this point in the history
Feature/dns lookup
  • Loading branch information
jlentink authored Dec 30, 2021
2 parents 1889370 + 42b65f6 commit 397ffd4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 17 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM alpine:latest

RUN apk add --no-cache tzdata
COPY go-transip-dyndns /usr/bin
CMD ["/usr/bin/go-transip-dyndns", "update", "-k"]
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Is a small little executable that will update a domain record of your choice tha
7. run `go-transip-dyndns create` to create the record
8. run `go-transip-dyndns validate` to see if all is 👍
9. You are set. Well done!
10. For a docker install see [Docker](#docker) section


## The configuration file
Expand Down Expand Up @@ -44,7 +45,7 @@ end of the certificate. Ensure you don't put any spaces in the key structure to
...Your certificate data...
-----END PRIVATE KEY-----"""

#### TXT, MX etc records.
#### TXT, records.
These records are handy and can have more markup than just an IP. For this purpose you have the ability to
completly format the record and just inject the IP addresses. This can be done to inject a ip via the tags:

Expand Down Expand Up @@ -168,6 +169,34 @@ Binaries are available for download in multiple formats

Download them [here](https://github.com/jlentink/go-transip-dyndns/releases/latest).

## Docker
For easy usage I personally run it via [docker-compose](https://docs.docker.com/compose/) with a file similar to the one shown below.

version: '3.3'
services:
go-transip-dyndns:
image: jlentink/go-transip-dyndns:latest
container_name: go-transip-dyndns
volumes:
- ./go-transip-dyndns.toml:/etc/go-transip-dyndns.toml
restart: unless-stopped

### Verify the setup
Verify the setup by running the following command in the terminal where the docker-compose file is stored.
It will verify the setup and output the results

docker-compose run --rm --entrypoint /usr/bin/go-transip-dyndns go-transip-dyndns -v verify

### Run create records
To create the initial records run:

docker-compose run --rm --entrypoint /usr/bin/go-transip-dyndns go-transip-dyndns -v create

### start the client
Now all is created start the client as a deamon.

docker-compose up go-transip-dyndns -d

## No association with Transip
This tool has been created for me own comfort. There is no association with Transip. But I would like to thank TransIP for there fine service!

Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: '3.3'
services:
dyndns:
go-transip-dyndns:
image: jlentink/go-transip-dyndns:latest
container_name: dyndns
build: .
container_name: go-transip-dyndns
volumes:
- .:/etc/go-transip-dyndns
- ./go-transip-dyndns.toml:/etc/go-transip-dyndns.toml
restart: unless-stopped
6 changes: 6 additions & 0 deletions internal/config/configObject.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "strings"

type ConfigObject struct {
General General `toml:"general"`
Account Account `toml:"account"`
Expand All @@ -21,3 +23,7 @@ type Record struct {
TTL int `toml:"ttl"`
Type string `toml:"type"`
}

func (r *Record) GetType() string {
return strings.ToUpper(r.Type)
}
12 changes: 6 additions & 6 deletions internal/transipClient/createRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func CreateRecord(record config.Record) error {
if err != nil {
return err
}
getDomainRepo().AddDNSEntry(record.Hostname, entry)
return getDomainRepo().AddDNSEntry(record.Hostname, entry)

return nil
}

func createDNSEntry(record config.Record) (domain.DNSEntry, error) {
Expand All @@ -53,7 +52,8 @@ func createDNSEntry(record config.Record) (domain.DNSEntry, error) {
case "CNAME":
fallthrough
case "MX":
fallthrough
entry, err = createGenericEntry(record)
//fallthrough
case "TXT":
fallthrough
case "SRV":
Expand All @@ -74,7 +74,7 @@ func createAEntry(record config.Record) (domain.DNSEntry, error) {
entry := domain.DNSEntry{
Name: record.Entry,
Expire: record.TTL,
Type: record.Type,
Type: record.GetType(),
Content: ip.IP,
}
return entry, nil
Expand All @@ -92,7 +92,7 @@ func createAAAAEntry(record config.Record) (domain.DNSEntry, error) {
entry := domain.DNSEntry{
Name: record.Entry,
Expire: record.TTL,
Type: record.Type,
Type: record.GetType(),
Content: ip.IP,
}
return entry, nil
Expand Down Expand Up @@ -135,7 +135,7 @@ func createGenericEntry(record config.Record) (domain.DNSEntry, error) {
entry := domain.DNSEntry{
Name: record.Entry,
Expire: record.TTL,
Type: record.Type,
Type: record.GetType(),
Content: buf.String(),
}
return entry, nil
Expand Down
64 changes: 64 additions & 0 deletions internal/transipClient/updateRecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,78 @@ package transipClient
import (
"github.com/transip/gotransip/v6/domain"
"go-transip-dyndns/internal/config"
"go-transip-dyndns/internal/logger"
"net"
)

func constructHostname(entry domain.DNSEntry, record config.Record) string {
hostname := ""
if entry.Name != "@" {
hostname += entry.Name + "."
}
hostname += record.Hostname
return hostname
}

func dnsAChanged(entry domain.DNSEntry, record config.Record) bool {
ips, err := net.LookupIP(constructHostname(entry, record))
if err != nil {
logger.Get().Debug("Error fetching DNS record. (%s)\n", err.Error())
return true
}
for _, ip := range ips {
if record.GetType() == "A" && ip.String() == entry.Content {
logger.Get().Debug("DNS [A] record has not been changed.\n")
return false
}
if record.GetType() == "AAAA" && ip.String() == entry.Content {
logger.Get().Debug("DNS [AAAA] record has not been changed.\n")
return false
}

}
return true
}

func dnsTXTChanged(entry domain.DNSEntry, record config.Record) bool {
txts, err := net.LookupTXT(constructHostname(entry, record))
if err != nil {
logger.Get().Debug("Error fetching DNS record. (%s)\n", err.Error())
return true
}
for _, txt := range txts {
if txt == entry.Content {
logger.Get().Debug("DNS [TXT] record has not been changed.\n")
return false
}

}
return true
}

func dnsChanged(entry domain.DNSEntry, record config.Record) bool {
switch entry.Type {
case "A":
fallthrough
case "AAAA":
return dnsAChanged(entry, record)
case "TXT":
return dnsTXTChanged(entry, record)
default:
return true
}
}

func UpdateRecord(record config.Record) (*domain.DNSEntry, error) {
entry, err := createDNSEntry(record)
if err != nil {
return nil, err
}

if !dnsChanged(entry, record) {
return nil, NotChangedError
}

dnsRecord, err := FindRecord(record.Hostname, record.Type, record.Entry)
if err != nil {
return nil, NotFoundError
Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func main() {
}

updateCmd := &cobra.Command{
Use: "update",
Short: "Update ip address on Transip DNS to current public ip (default command)",
Long: "Use the current ip to update to a record in the TransIP dns.\nAllowing for easy updating when your ip changes.",
Example: "",

Run: commands.Update,
Use: "update",
Short: "Update ip address on Transip DNS to current public ip (default command)",
Long: "Use the current ip to update to a record in the TransIP dns.\nAllowing for easy updating when your ip changes.",
Example: "",
PersistentPreRun: commands.PreRun,
Run: commands.Update,
}
updateCmd.PersistentFlags().BoolVarP(&keepRunning, "keep-alive", "k", false, "keep running continuously.")

Expand Down

0 comments on commit 397ffd4

Please sign in to comment.