Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f83ca10008 | ||
|
|
7c8cc275ed | ||
|
|
4744ef5922 |
@@ -3,8 +3,8 @@ FROM golang:1.22
|
|||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
|
||||||
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
|
# pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change
|
||||||
COPY go.mod go.sum ./
|
# COPY go.mod go.sum ./
|
||||||
RUN go mod download && go mod verify
|
# RUN go mod download && go mod verify
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN go build -v -o /usr/local/bin/app ./...
|
RUN go build -v -o /usr/local/bin/app ./...
|
||||||
|
|||||||
20
README.md
20
README.md
@@ -1,6 +1,6 @@
|
|||||||
# Dynamic DNS Updates with Cloudflare
|
# Dynamic DNS Updates with Cloudflare
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
This Docker container offers a straightforward and efficient solution for
|
This Docker container offers a straightforward and efficient solution for
|
||||||
automating dynamic DNS updates using the Cloudflare DNS service. It empowers you
|
automating dynamic DNS updates using the Cloudflare DNS service. It empowers you
|
||||||
@@ -24,11 +24,11 @@ pulling the pre-built Docker container and running it with the necessary
|
|||||||
environment variables
|
environment variables
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
docker run -d -e API_KEY=123 -e ZONE_ID=023e105f4ecef8ad9ca31a8372d0c353 -e DOMAIN_NAMES=dyndns.example.com,example.com --restart=always mxmlndml/cloudflare-dynamic-dns
|
docker run -d -e API_KEY=123 -e ZONE_ID=023e105f4ecef8ad9ca31a8372d0c353 -e DOMAIN_NAMES=example.com,*.example.com mxmlndml/cloudflare-dynamic-dns
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternatively you can copy the `docker-compose.yml` from this repository into an
|
Alternatively you can copy the `docker-compose.yml` and `.env.template` from this repository into an
|
||||||
empty directory of your machine, edit the environment variables and start the
|
empty directory of your machine, rename the `.env.template` to `.env`, edit the environment variables in both files and start the
|
||||||
container with `docker compose`
|
container with `docker compose`
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -52,9 +52,15 @@ breakdown of the available configuration variables:
|
|||||||
\
|
\
|
||||||
List of DNS A records that should store your public IP address delimited by a
|
List of DNS A records that should store your public IP address delimited by a
|
||||||
comma (and only a comma)
|
comma (and only a comma)
|
||||||
|
- **`RECORD_TYPES`** _defaults to `A`_
|
||||||
|
\
|
||||||
|
Whether A and/or AAAA records should be updated
|
||||||
|
\
|
||||||
|
`A`: updates only A records
|
||||||
|
\
|
||||||
|
`AAAA`: updates only AAAA records
|
||||||
|
\
|
||||||
|
`*`: updates both A and AAAA records
|
||||||
- **`INTERVAL`** _defaults to `5`_
|
- **`INTERVAL`** _defaults to `5`_
|
||||||
\
|
\
|
||||||
Time interval in minutes between DNS updates
|
Time interval in minutes between DNS updates
|
||||||
- **`LOG_LEVEL`** _defaults to `INFO`_
|
|
||||||
\
|
|
||||||
Logging level for the container, either `DEBUG`, `INFO`, `WARN` or `ERROR`
|
|
||||||
|
|||||||
@@ -8,39 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func setAuthHeader(req *http.Request, apiKey string) {
|
|
||||||
authHeader := fmt.Sprint("bearer ", apiKey)
|
|
||||||
req.Header.Add("Authorization", authHeader)
|
|
||||||
}
|
|
||||||
|
|
||||||
type cloudflareResponse struct {
|
|
||||||
Success bool
|
|
||||||
Result []struct {
|
|
||||||
ID string
|
|
||||||
Content string
|
|
||||||
Type string
|
|
||||||
}
|
|
||||||
Errors []struct {
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkServerErrors(data *cloudflareResponse) {
|
|
||||||
if data.Success {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := ""
|
|
||||||
for i, err := range data.Errors {
|
|
||||||
if i != 0 {
|
|
||||||
msg += ", "
|
|
||||||
}
|
|
||||||
msg += err.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Panic("Server responded with error: ", msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
type dnsRecord struct {
|
type dnsRecord struct {
|
||||||
id string
|
id string
|
||||||
content string
|
content string
|
||||||
@@ -51,6 +18,11 @@ type DNSRecords struct {
|
|||||||
aaaa dnsRecord
|
aaaa dnsRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setAuthHeader(req *http.Request, apiKey string) {
|
||||||
|
authHeader := fmt.Sprint("bearer ", apiKey)
|
||||||
|
req.Header.Add("Authorization", authHeader)
|
||||||
|
}
|
||||||
|
|
||||||
func GetDNSRecord(zoneID string, domainName string, apiKey string) DNSRecords {
|
func GetDNSRecord(zoneID string, domainName string, apiKey string) DNSRecords {
|
||||||
dnsRecords := DNSRecords{
|
dnsRecords := DNSRecords{
|
||||||
name: domainName,
|
name: domainName,
|
||||||
@@ -71,12 +43,32 @@ func GetDNSRecord(zoneID string, domainName string, apiKey string) DNSRecords {
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var data cloudflareResponse
|
var data struct {
|
||||||
|
Success bool
|
||||||
|
Errors []struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
Result []struct {
|
||||||
|
ID string
|
||||||
|
Content string
|
||||||
|
Type string
|
||||||
|
}
|
||||||
|
}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("Error parsing JSON: ", err)
|
log.Panic("Error parsing JSON: ", err)
|
||||||
}
|
}
|
||||||
checkServerErrors(&data)
|
if !data.Success {
|
||||||
|
msg := ""
|
||||||
|
for i, err := range data.Errors {
|
||||||
|
if i != 0 {
|
||||||
|
msg += ", "
|
||||||
|
}
|
||||||
|
msg += err.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Panic("Server responded with error: ", msg)
|
||||||
|
}
|
||||||
|
|
||||||
for _, record := range data.Result {
|
for _, record := range data.Result {
|
||||||
switch record.Type {
|
switch record.Type {
|
||||||
@@ -125,10 +117,25 @@ func UpdateDNSRecord(zoneID string, dnsRecordID string, apiKey string, body DNSR
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
var data cloudflareResponse
|
var data struct {
|
||||||
|
Success bool
|
||||||
|
Errors []struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error parsing JSON: ", err)
|
log.Fatal("Error parsing JSON: ", err)
|
||||||
}
|
}
|
||||||
checkServerErrors(&data)
|
|
||||||
|
if !data.Success {
|
||||||
|
msg := ""
|
||||||
|
for i, err := range data.Errors {
|
||||||
|
if i != 0 {
|
||||||
|
msg += ", "
|
||||||
|
}
|
||||||
|
msg += err.Message
|
||||||
|
}
|
||||||
|
log.Panic("Server responded with error: ", msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
main.go
31
main.go
@@ -1,7 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -57,7 +59,32 @@ func getDNSRecords() []DNSRecords {
|
|||||||
return dnsRecords
|
return dnsRecords
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initialize() {
|
||||||
|
fmt.Println(" _______ _______ ___ _ ___ _ ______")
|
||||||
|
fmt.Println(" / ___/ /__ __ _____/ / _/ /__ ________ / _ \\__ _____ ___ ___ _ (_)___ / _ \\/ |/ / __/")
|
||||||
|
fmt.Println("/ /__/ / _ \\/ // / _ / _/ / _ `/ __/ -_) / // / // / _ \\/ _ `/ ' \\/ / __/ / // / /\\ \\ ")
|
||||||
|
fmt.Println("\\___/_/\\___/\\_,_/\\_,_/_//_/\\_,_/_/ \\__/ /____/\\_, /_//_/\\_,_/_/_/_/_/\\__/ /____/_/|_/___/ ")
|
||||||
|
fmt.Println(" /___/ ")
|
||||||
|
|
||||||
|
var recordType string
|
||||||
|
if UseIPv4() && UseIPv6() {
|
||||||
|
recordType = "A and AAAA"
|
||||||
|
} else if UseIPv4() {
|
||||||
|
recordType = "A"
|
||||||
|
} else if UseIPv6() {
|
||||||
|
recordType = "AAAA"
|
||||||
|
}
|
||||||
|
|
||||||
|
domainNames := strings.Join(GetDomainNames(), ", ")
|
||||||
|
|
||||||
|
interval := GetInterval()
|
||||||
|
|
||||||
|
fmt.Printf("Updating %v records of %v every %v minutes\n\n", recordType, domainNames, interval)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
initialize()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var publicIP publicIP
|
var publicIP publicIP
|
||||||
var dnsRecords []DNSRecords
|
var dnsRecords []DNSRecords
|
||||||
@@ -84,7 +111,7 @@ func main() {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
UpdateDNSRecord(zoneID, dnsRecord.a.id, apiKey, DNSRecordBody{Type: "A", Name: dnsRecord.name, Content: publicIP.v4})
|
UpdateDNSRecord(zoneID, dnsRecord.a.id, apiKey, DNSRecordBody{Type: "A", Name: dnsRecord.name, Content: publicIP.v4})
|
||||||
log.Printf("Set DNS record %v to %v", dnsRecord.name, publicIP.v4)
|
log.Printf("Set DNS A record %v to %v", dnsRecord.name, publicIP.v4)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -93,7 +120,7 @@ func main() {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
UpdateDNSRecord(zoneID, dnsRecord.aaaa.id, apiKey, DNSRecordBody{Type: "AAAA", Name: dnsRecord.name, Content: publicIP.v6})
|
UpdateDNSRecord(zoneID, dnsRecord.aaaa.id, apiKey, DNSRecordBody{Type: "AAAA", Name: dnsRecord.name, Content: publicIP.v6})
|
||||||
log.Printf("Set DNS record %v to %v", dnsRecord.name, publicIP.v6)
|
log.Printf("Set DNS AAAA record %v to %v", dnsRecord.name, publicIP.v6)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user