Compare commits
36 Commits
ed1f9c3ed1
...
0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| b5bc615cbb | |||
| 0193eac6e5 | |||
| dc76c5fb26 | |||
| eb182ac7ce | |||
| ae6981cb02 | |||
| 6511147a41 | |||
| 2f05e48962 | |||
| a79ce64e82 | |||
| 660f2eac0d | |||
| 0a722ff1b2 | |||
| 5875af55bb | |||
| bff59f36a8 | |||
| 73615c65ee | |||
| 531b5baecd | |||
| 629765985b | |||
| 99e1214a83 | |||
| 29c62a8b1f | |||
| 0fdd151e6d | |||
| 89d965a4d2 | |||
| d4e48c2fbf | |||
| a34f1f0a9a | |||
| ab150a88ef | |||
| f1e863b098 | |||
| ad20ad46b0 | |||
| ccf4f5dbbb | |||
| e53827adf0 | |||
| ad0932f4aa | |||
| fff36bf807 | |||
| 1c725993f5 | |||
| 3ad5b1ec0e | |||
| 1ea43ac4cf | |||
| b781399b47 | |||
| 40a41ce7ca | |||
| ef57421268 | |||
| a01c328ac5 | |||
| ac786f533d |
@@ -4,6 +4,14 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
paths:
|
||||||
|
- "go.mod"
|
||||||
|
- "go.sum"
|
||||||
|
- "**/*.go"
|
||||||
|
- "config.example.yaml"
|
||||||
|
- "Dockerfile"
|
||||||
|
- "Makefile"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
DOCKER_REGISTRY: gitea.t000-n.de
|
DOCKER_REGISTRY: gitea.t000-n.de
|
||||||
@@ -14,14 +22,14 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
||||||
- name: Setup go
|
- name: Setup go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6
|
||||||
with:
|
with:
|
||||||
go-version-file: go.mod
|
go-version-file: go.mod
|
||||||
check-latest: true
|
check-latest: true
|
||||||
- name: Create cache key
|
- name: Create cache key
|
||||||
uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
uses: https://gitea.com/actions/go-hashfiles@264ae76b7e50173ce71ed7da4b48e5e517f3f9ec # v0.0.1
|
||||||
id: hash-go
|
id: hash-go
|
||||||
with:
|
with:
|
||||||
patterns: |
|
patterns: |
|
||||||
@@ -29,7 +37,7 @@ jobs:
|
|||||||
go.sum
|
go.sum
|
||||||
- name: cache go
|
- name: cache go
|
||||||
id: cache-go
|
id: cache-go
|
||||||
uses: actions/cache@v4
|
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
/go_path
|
/go_path
|
||||||
@@ -44,25 +52,72 @@ jobs:
|
|||||||
|
|
||||||
build_and_push:
|
build_and_push:
|
||||||
name: Build and push
|
name: Build and push
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
arch: [amd64, arm64]
|
||||||
needs:
|
needs:
|
||||||
- test
|
- test
|
||||||
runs-on: ubuntu-latest
|
runs-on:
|
||||||
|
- ubuntu-latest
|
||||||
|
- linux_${{ matrix.arch }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v2
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v2
|
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3
|
||||||
|
|
||||||
- name: Login to Registry
|
- name: Login to Registry
|
||||||
uses: docker/login-action@v2
|
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
|
||||||
with:
|
with:
|
||||||
registry: ${{ env.DOCKER_REGISTRY }}
|
registry: ${{ env.DOCKER_REGISTRY }}
|
||||||
username: ${{ secrets.REGISTRY_USER }}
|
username: ${{ secrets.REGISTRY_USER }}
|
||||||
password: ${{ secrets.REGISTRY_PASSWORD }}
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
- name: Get Metadata
|
||||||
|
id: meta
|
||||||
|
run: |
|
||||||
|
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}' | tr '[:upper:]' '[:lower:]') >> $GITHUB_OUTPUT
|
||||||
|
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile
|
||||||
|
platforms: linux/${{ matrix.arch }}
|
||||||
|
push: true
|
||||||
|
provenance: false
|
||||||
|
build-args: GOARCH=${{ matrix.arch }}
|
||||||
|
tags: |
|
||||||
|
${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}-${{ matrix.arch }}
|
||||||
|
|
||||||
|
create_tag:
|
||||||
|
name: Create tag
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
outputs:
|
||||||
|
tag: ${{ steps.tag.outputs.new-tag }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- uses: https://gitea.t000-n.de/t.behrendt/conventional-semantic-git-tag-increment@0.1.20
|
||||||
|
id: tag
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITEA_TOKEN }}
|
||||||
|
prerelease: ${{ github.event_name == 'workflow_dispatch' }}
|
||||||
|
- run: |
|
||||||
|
git tag ${{ steps.tag.outputs.new-tag }}
|
||||||
|
git push origin ${{ steps.tag.outputs.new-tag }}
|
||||||
|
- name: Set output
|
||||||
|
run: |
|
||||||
|
echo "tag=${{ steps.tag.outputs.new-tag }}" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
create_manifest:
|
||||||
|
name: Create manifest
|
||||||
|
needs:
|
||||||
|
- build_and_push
|
||||||
|
- create_tag
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
||||||
|
|
||||||
- name: Get Metadata
|
- name: Get Metadata
|
||||||
id: meta
|
id: meta
|
||||||
@@ -70,15 +125,17 @@ jobs:
|
|||||||
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}' | tr '[:upper:]' '[:lower:]') >> $GITHUB_OUTPUT
|
echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}' | tr '[:upper:]' '[:lower:]') >> $GITHUB_OUTPUT
|
||||||
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
|
echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Build and push
|
- name: Login to Registry
|
||||||
uses: docker/build-push-action@v4
|
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
|
||||||
with:
|
with:
|
||||||
context: .
|
registry: ${{ env.DOCKER_REGISTRY }}
|
||||||
file: ./Dockerfile
|
username: ${{ secrets.REGISTRY_USER }}
|
||||||
platforms: |
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
linux/amd64
|
|
||||||
linux/arm64
|
- name: Create manifest
|
||||||
push: true
|
run: |
|
||||||
tags: |
|
docker manifest create ${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ needs.create_tag.outputs.tag }} \
|
||||||
${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
|
${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}-amd64 \
|
||||||
${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:latest
|
${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}-arm64
|
||||||
|
|
||||||
|
docker manifest push ${{ env.DOCKER_REGISTRY }}/t.behrendt/${{ steps.meta.outputs.REPO_NAME }}:${{ needs.create_tag.outputs.tag }}
|
||||||
|
|||||||
@@ -13,14 +13,14 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6
|
||||||
- name: Setup go
|
- name: Setup go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6
|
||||||
with:
|
with:
|
||||||
go-version-file: go.mod
|
go-version-file: go.mod
|
||||||
check-latest: true
|
check-latest: true
|
||||||
- name: Create cache key
|
- name: Create cache key
|
||||||
uses: https://gitea.com/actions/go-hashfiles@v0.0.1
|
uses: https://gitea.com/actions/go-hashfiles@264ae76b7e50173ce71ed7da4b48e5e517f3f9ec # v0.0.1
|
||||||
id: hash-go
|
id: hash-go
|
||||||
with:
|
with:
|
||||||
patterns: |
|
patterns: |
|
||||||
@@ -28,7 +28,7 @@ jobs:
|
|||||||
go.sum
|
go.sum
|
||||||
- name: cache go
|
- name: cache go
|
||||||
id: cache-go
|
id: cache-go
|
||||||
uses: actions/cache@v4
|
uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
|
||||||
with:
|
with:
|
||||||
path: |
|
path: |
|
||||||
/go_path
|
/go_path
|
||||||
@@ -40,3 +40,5 @@ jobs:
|
|||||||
run: make build
|
run: make build
|
||||||
- name: test
|
- name: test
|
||||||
run: make test
|
run: make test
|
||||||
|
- name: check:format
|
||||||
|
run: make check-format
|
||||||
|
|||||||
23
Dockerfile
23
Dockerfile
@@ -1,21 +1,14 @@
|
|||||||
FROM golang:1.21-alpine
|
FROM golang:1.25-alpine@sha256:26111811bc967321e7b6f852e914d14bede324cd1accb7f81811929a6a57fea9 as build
|
||||||
|
|
||||||
|
ARG GOARCH=amd64
|
||||||
|
|
||||||
# Set the Current Working Directory inside the container
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Copy go mod and sum files
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
|
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
# Copy the source from the current directory to the Working Directory inside the container
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${GOARCH} \
|
||||||
|
go build -trimpath -ldflags="-s -w" -o main .
|
||||||
|
|
||||||
# Build the Go app
|
FROM gcr.io/distroless/static-debian12@sha256:4b2a093ef4649bccd586625090a3c668b254cfe180dee54f4c94f3e9bd7e381e
|
||||||
RUN go build -o main .
|
COPY --from=build /app/main /
|
||||||
|
CMD ["/main"]
|
||||||
|
|
||||||
# Command to run the executable
|
|
||||||
|
|
||||||
CMD ["./main"]
|
|
||||||
|
|||||||
15
Makefile
15
Makefile
@@ -6,3 +6,18 @@ build:
|
|||||||
|
|
||||||
lint:
|
lint:
|
||||||
golint ./...
|
golint ./...
|
||||||
|
|
||||||
|
run:
|
||||||
|
make build
|
||||||
|
./realdyndns
|
||||||
|
|
||||||
|
format:
|
||||||
|
gofmt -w .
|
||||||
|
|
||||||
|
check-format:
|
||||||
|
@OUTPUT=$$(gofmt -l .); \
|
||||||
|
if [ -n "$$OUTPUT" ]; then \
|
||||||
|
echo "Formatter failed for:"; \
|
||||||
|
echo "$$OUTPUT"; \
|
||||||
|
exit 1; \
|
||||||
|
fi
|
||||||
|
|||||||
30
README.md
30
README.md
@@ -1,10 +1,12 @@
|
|||||||
# realDynDNS
|
# realDynDNS
|
||||||
|
|
||||||
RealDynDNS aims to be a replacement to "classical" dynDNS solutions that offer a subdomain. Instead realDynDns actually changes your DNS entries.
|
RealDynDNS aims to be a replacement to "classical" dynDNS solutions that offer a subdomain. Instead realDynDns actually changes your DNS entries.
|
||||||
|
|
||||||
This service requires your DNS provider to expose an API that allows your DNS entries to be changed.
|
This service requires your DNS provider to expose an API that allows your DNS entries to be changed.
|
||||||
A service that provides your current external IP is also required.
|
A service that provides your current external IP is also required.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
The configuration is done via a YAML file called `config.yaml`. The following example shows the configuration for a domain with two subdomains.
|
The configuration is done via a YAML file called `config.yaml`. The following example shows the configuration for a domain with two subdomains.
|
||||||
|
|
||||||
Configuration of the IP provider and the DNS provider is mandatory.
|
Configuration of the IP provider and the DNS provider is mandatory.
|
||||||
@@ -35,40 +37,52 @@ domains:
|
|||||||
- "@"
|
- "@"
|
||||||
- www
|
- www
|
||||||
check_interval: 0 0 0/6 * * * *
|
check_interval: 0 0 0/6 * * * *
|
||||||
|
mode: Scheduled
|
||||||
|
log_level: info
|
||||||
```
|
```
|
||||||
|
|
||||||
The config file is expected to be in the same directory as the binary and called `config.yaml`. For the OCR image, the root directory is `/app`.
|
The config file is expected to be in the same directory as the binary and called `config.yaml`. For the OCR image, the root directory is `/app`.
|
||||||
|
|
||||||
## DNS Providers
|
## DNS Providers
|
||||||
|
|
||||||
The DNS provider abstracts the API of your DNS provider. Currently the following providers are supported:
|
The DNS provider abstracts the API of your DNS provider. Currently the following providers are supported:
|
||||||
|
|
||||||
### IONOS
|
### IONOS
|
||||||
|
|
||||||
IONOS requires two configuration parameters. You can get your API key [here](https://developer.hosting.ionos.com/docs/getstarted).
|
IONOS requires two configuration parameters. You can get your API key [here](https://developer.hosting.ionos.com/docs/getstarted).
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
api_key: <your-api-key>
|
api_key: <your-api-key>
|
||||||
base_url: https://api.hosting.ionos.com/dns
|
base_url: https://api.hosting.ionos.com/dns
|
||||||
```
|
```
|
||||||
|
|
||||||
## External IP Providers
|
## External IP Providers
|
||||||
|
|
||||||
The external IP provider is used to get your current external IP. Currently the following providers are supported:
|
The external IP provider is used to get your current external IP. Currently the following providers are supported:
|
||||||
|
|
||||||
### Plain
|
### Plain
|
||||||
|
|
||||||
Any provider that returns your IP as plain text can be used. The following configuration is required:
|
Any provider that returns your IP as plain text can be used. The following configuration is required:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
url: <your-providers-URL>
|
url: <your-providers-URL>
|
||||||
```
|
```
|
||||||
|
|
||||||
Examples for providers are:
|
Examples for providers are:
|
||||||
|
|
||||||
- https://ifconfig.me
|
- https://ifconfig.me
|
||||||
- https://api.ipify.org
|
- https://api.ipify.org
|
||||||
|
|
||||||
## Notification Providers
|
## Notification Providers
|
||||||
|
|
||||||
The notification provider is used to send notifications whena IP address changes and a DNS record is updated.
|
The notification provider is used to send notifications whena IP address changes and a DNS record is updated.
|
||||||
|
|
||||||
### Console
|
### Console
|
||||||
|
|
||||||
The console notification provider is used to print the notification to the console. This is the default notification provider.
|
The console notification provider is used to print the notification to the console. This is the default notification provider.
|
||||||
|
|
||||||
### Gotify
|
### Gotify
|
||||||
|
|
||||||
The Gotify notification provider is used to send notifications to a Gotify server.
|
The Gotify notification provider is used to send notifications to a Gotify server.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
@@ -78,3 +92,19 @@ priority: 0
|
|||||||
```
|
```
|
||||||
|
|
||||||
The priority must be between 0 and 4.
|
The priority must be between 0 and 4.
|
||||||
|
|
||||||
|
## Mode Selection
|
||||||
|
|
||||||
|
Two modes are available:
|
||||||
|
|
||||||
|
### RunOnce
|
||||||
|
|
||||||
|
The RunOnce mode is used to run the application once and exit. This is useful when providing your own external scheduler, like cron.
|
||||||
|
|
||||||
|
Set the `mode` to `RunOnce`.
|
||||||
|
|
||||||
|
### Scheduled
|
||||||
|
|
||||||
|
The Scheduled mode is used to run the application in a scheduled interval.
|
||||||
|
|
||||||
|
Set the `mode` to `Scheduled` and provide a cron expression for the `check_interval`.
|
||||||
|
|||||||
@@ -21,3 +21,4 @@ domains:
|
|||||||
- www
|
- www
|
||||||
check_interval: 0 0 0/6 * * * *
|
check_interval: 0 0 0/6 * * * *
|
||||||
mode: Scheduled
|
mode: Scheduled
|
||||||
|
log_level: info
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -1,15 +1,16 @@
|
|||||||
module realdnydns
|
module realdnydns
|
||||||
|
|
||||||
go 1.20
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-co-op/gocron v1.37.0
|
github.com/go-co-op/gocron v1.37.0
|
||||||
|
github.com/go-co-op/gocron/v2 v2.18.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||||
github.com/stretchr/testify v1.8.4 // indirect
|
github.com/stretchr/testify v1.11.1 // indirect
|
||||||
go.uber.org/atomic v1.11.0 // indirect
|
go.uber.org/atomic v1.11.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -4,6 +4,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0=
|
github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0=
|
||||||
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
|
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
|
||||||
|
github.com/go-co-op/gocron/v2 v2.18.0/go.mod h1:Zii6he+Zfgy5W9B+JKk/KwejFOW0kZTFvHtwIpR4aBI=
|
||||||
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
@@ -30,8 +31,9 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
|||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||||
|
|||||||
79
main.go
79
main.go
@@ -2,6 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
"realdnydns/pkg/dnsProvider"
|
"realdnydns/pkg/dnsProvider"
|
||||||
@@ -15,48 +18,84 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: slog.LevelInfo,
|
||||||
|
}))
|
||||||
|
|
||||||
configClient := config.Config{}
|
configClient := config.Config{}
|
||||||
err := configClient.Load("config.yaml")
|
err := configClient.Load("config.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to load config file", slog.String("error", err.Error()))
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if configClient.LogLevel != "" {
|
||||||
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: slog.Level(config.LogLevelMap[strings.ToLower(configClient.LogLevel)]),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
var externalIpProvider externalIpProvider.ExternalIpProvider
|
var externalIpProvider externalIpProvider.ExternalIpProvider
|
||||||
switch configClient.ExternalIPProvider.Type {
|
switch configClient.ExternalIPProvider.Type {
|
||||||
case "plain":
|
case "plain":
|
||||||
|
logger.Info("Using plain external IP provider", slog.String("external_ip_provider", "plain"))
|
||||||
|
|
||||||
var plainConfig plainExternalIpProvider.PlainExternalIpProviderConfig
|
var plainConfig plainExternalIpProvider.PlainExternalIpProviderConfig
|
||||||
err := configClient.ExternalIPProvider.ProviderConfig.Decode(&plainConfig)
|
err := configClient.ExternalIPProvider.ProviderConfig.Decode(&plainConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create config",
|
||||||
|
slog.String("external_ip_provider", "plain"),
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
externalIpProvider, err = plainExternalIpProvider.New(plainConfig)
|
externalIpProvider, err = plainExternalIpProvider.New(plainConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create plain external IP provider",
|
||||||
|
slog.String("external_ip_provider", "plain"),
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
logger.Error("Unknown external IP provider", slog.String("external_ip_provider", configClient.ExternalIPProvider.Type))
|
||||||
panic(fmt.Errorf("unknown external IP provider: %s", configClient.ExternalIPProvider.Type))
|
panic(fmt.Errorf("unknown external IP provider: %s", configClient.ExternalIPProvider.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
var dnsProvider dnsProvider.DNSProvider
|
var dnsProvider dnsProvider.DNSProvider
|
||||||
switch configClient.DNSProvider.Type {
|
switch configClient.DNSProvider.Type {
|
||||||
case "ionos":
|
case "ionos":
|
||||||
|
logger.Info("Using IONOS DNS provider", slog.String("dns_provider", "ionos"))
|
||||||
|
|
||||||
var ionosConfig ionos.IONOSConfig
|
var ionosConfig ionos.IONOSConfig
|
||||||
err := configClient.DNSProvider.ProviderConfig.Decode(&ionosConfig)
|
err := configClient.DNSProvider.ProviderConfig.Decode(&ionosConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create IONOS DNS provider",
|
||||||
|
slog.String("dns_provider", "ionos"),
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dnsProvider, err = ionos.NewIonos(&ionosConfig)
|
dnsProvider, err = ionos.NewIonos(&ionosConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create IONOS DNS provider",
|
||||||
|
slog.String("dns_provider", "ionos"),
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
logger.Error("Unknown DNS provider", slog.String("dns_provider", configClient.DNSProvider.Type))
|
||||||
panic(fmt.Errorf("unknown DNS provider: %s", configClient.DNSProvider.Type))
|
panic(fmt.Errorf("unknown DNS provider: %s", configClient.DNSProvider.Type))
|
||||||
}
|
}
|
||||||
|
|
||||||
var notificationProvider notificationProvider.NotificationProvider
|
var notificationProvider notificationProvider.NotificationProvider
|
||||||
switch configClient.NotificationProvider.Type {
|
switch configClient.NotificationProvider.Type {
|
||||||
case "gotify":
|
case "gotify":
|
||||||
|
logger.Info("Using Gotify notification provider", slog.String("notification_provider", "gotify"))
|
||||||
|
|
||||||
var gotifyConfig gotify.NotificationProviderImplGotifyConfig
|
var gotifyConfig gotify.NotificationProviderImplGotifyConfig
|
||||||
err := configClient.NotificationProvider.ProviderConfig.Decode(&gotifyConfig)
|
err := configClient.NotificationProvider.ProviderConfig.Decode(&gotifyConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -65,29 +104,39 @@ func main() {
|
|||||||
|
|
||||||
notificationProvider, err = gotify.New(gotifyConfig)
|
notificationProvider, err = gotify.New(gotifyConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to create Gotify notification provider",
|
||||||
|
slog.String("notification_provider", "gotify"),
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
logger.Info("Using console notification provider", slog.String("notification_provider", "console"))
|
||||||
|
|
||||||
notificationProvider = notificationProviderConsole.New()
|
notificationProvider = notificationProviderConsole.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
rdd := realDynDns.New(externalIpProvider, dnsProvider, notificationProvider, configClient.Domains)
|
rdd := realDynDns.New(externalIpProvider, dnsProvider, notificationProvider, configClient.Domains, logger.With(slog.String("service", "realDynDns")))
|
||||||
|
|
||||||
switch configClient.Mode {
|
switch configClient.Mode {
|
||||||
case config.ScheduledMode:
|
case config.ScheduledMode:
|
||||||
schedule, job, err := rdd.RunWithSchedule(configClient.CheckInterval)
|
logger.Info("Running in scheduled mode", slog.String("interval", configClient.CheckInterval))
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("Starting scheduler")
|
schedule, job, err := rdd.RunWithSchedule(configClient.CheckInterval)
|
||||||
fmt.Println("Next run:", job.NextRun())
|
if err != nil {
|
||||||
schedule.StartBlocking()
|
logger.Error("Failed to create scheduler", slog.String("error", err.Error()))
|
||||||
case config.RunOnceMode:
|
panic(err)
|
||||||
numberOfChanges, err := rdd.RunOnce()
|
}
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
logger.Info("Next run:", slog.String("time", job.NextRun().String()))
|
||||||
}
|
schedule.StartBlocking()
|
||||||
fmt.Println("Number of changes:", numberOfChanges)
|
case config.RunOnceMode:
|
||||||
|
logger.Info("Running in run once mode")
|
||||||
|
|
||||||
|
_, err := rdd.RunOnce()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to run once", slog.String("error", err.Error()))
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package config
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -15,13 +17,26 @@ type Config struct {
|
|||||||
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
|
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
|
||||||
Domains []DomainConfig `yaml:"domains"`
|
Domains []DomainConfig `yaml:"domains"`
|
||||||
CheckInterval string `yaml:"check_interval"`
|
CheckInterval string `yaml:"check_interval"`
|
||||||
|
LogLevel string `yaml:"log_level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RunOnceMode = "RunOnce"
|
RunOnceMode = "RunOnce"
|
||||||
ScheduledMode = "Scheduled"
|
ScheduledMode = "Scheduled"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var LogLevelMap = map[string]slog.Level{
|
||||||
|
"debug": slog.LevelDebug,
|
||||||
|
"info": slog.LevelInfo,
|
||||||
|
"warn": slog.LevelWarn,
|
||||||
|
"error": slog.LevelError,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidLogLevel(level string) bool {
|
||||||
|
_, ok := LogLevelMap[strings.ToLower(level)]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
type DomainConfig struct {
|
type DomainConfig struct {
|
||||||
TLD string `yaml:"tld"`
|
TLD string `yaml:"tld"`
|
||||||
Subdomains []string `yaml:"subdomains"`
|
Subdomains []string `yaml:"subdomains"`
|
||||||
@@ -56,7 +71,7 @@ func (c *Config) Load(filePath string) error {
|
|||||||
return fmt.Errorf("failed to validate config: %w", err)
|
return fmt.Errorf("failed to validate config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) validate() error {
|
func (c *Config) validate() error {
|
||||||
@@ -68,5 +83,9 @@ func (c *Config) validate() error {
|
|||||||
return errors.New("check interval must be set when mode is 'Scheduled'")
|
return errors.New("check interval must be set when mode is 'Scheduled'")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
if c.LogLevel != "" && !isValidLogLevel(c.LogLevel) {
|
||||||
|
return fmt.Errorf("log level must be one of 'debug', 'info', 'warn', 'error', but got %s", c.LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"realdnydns/model/common"
|
"realdnydns/model/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ type IonosAPI interface {
|
|||||||
SetARecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
SetARecord(tld string, subdomain string, ip net.IP, ttl int, prio int, disabled bool) (*common.ARecord, error)
|
||||||
GetZoneId(tld string) (string, error)
|
GetZoneId(tld string) (string, error)
|
||||||
GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error)
|
GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error)
|
||||||
HttpCall(method string, url string, body io.Reader) (*http.Response, error)
|
HttpCall(method string, url string, body io.Reader, queryParams map[string]string) (*http.Response, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type IonosAPIImpl struct {
|
type IonosAPIImpl struct {
|
||||||
@@ -76,8 +77,16 @@ func New(APIKey string, BaseURL string) IonosAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IonosAPIImpl) HttpCall(method string, url string, body io.Reader) (*http.Response, error) {
|
func (i *IonosAPIImpl) HttpCall(method string, path string, body io.Reader, queryParams map[string]string) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(method, i.BaseURL+url, body)
|
requestUrl, _ := url.Parse(i.BaseURL + path)
|
||||||
|
|
||||||
|
query := requestUrl.Query()
|
||||||
|
for key, value := range queryParams {
|
||||||
|
query.Add(key, value)
|
||||||
|
}
|
||||||
|
requestUrl.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
req, err := http.NewRequest(method, requestUrl.String(), body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -90,7 +99,7 @@ func (i *IonosAPIImpl) HttpCall(method string, url string, body io.Reader) (*htt
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *IonosAPIImpl) GetZoneId(tld string) (string, error) {
|
func (i *IonosAPIImpl) GetZoneId(tld string) (string, error) {
|
||||||
res, err := i.HttpCall("GET", "/v1/zones", nil)
|
res, err := i.HttpCall("GET", "/v1/zones", nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -111,7 +120,14 @@ func (i *IonosAPIImpl) GetZoneId(tld string) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error) {
|
func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string, recordType string) (string, error) {
|
||||||
res, err := i.HttpCall("GET", "/v1/zones/"+zoneId, nil)
|
var domain string
|
||||||
|
if subdomain == "@" || subdomain == "" {
|
||||||
|
domain = tld
|
||||||
|
} else {
|
||||||
|
domain = subdomain + "." + tld
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := i.HttpCall("GET", "/v1/zones/"+zoneId, nil, map[string]string{"recordName": domain, "recordType": recordType})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -122,13 +138,6 @@ func (i *IonosAPIImpl) GetRecordId(zoneId string, tld string, subdomain string,
|
|||||||
zone := ZoneResponse{}
|
zone := ZoneResponse{}
|
||||||
json.Unmarshal(responseBody, &zone)
|
json.Unmarshal(responseBody, &zone)
|
||||||
|
|
||||||
var domain string
|
|
||||||
if subdomain == "@" || subdomain == "" {
|
|
||||||
domain = tld
|
|
||||||
} else {
|
|
||||||
domain = subdomain + "." + tld
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, record := range zone.Records {
|
for _, record := range zone.Records {
|
||||||
if record.Type == recordType && record.Name == domain {
|
if record.Type == recordType && record.Name == domain {
|
||||||
return record.Id, nil
|
return record.Id, nil
|
||||||
@@ -159,7 +168,7 @@ func (i *IonosAPIImpl) SetARecord(tld string, subdomain string, ip net.IP, ttl i
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := i.HttpCall("PUT", "/v1/zones/"+zoneId+"/records/"+recordId, bytes.NewReader(changeRecordRequest))
|
res, err := i.HttpCall("PUT", "/v1/zones/"+zoneId+"/records/"+recordId, bytes.NewReader(changeRecordRequest), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -171,8 +180,6 @@ func (i *IonosAPIImpl) SetARecord(tld string, subdomain string, ip net.IP, ttl i
|
|||||||
return nil, errors.New("error updating record")
|
return nil, errors.New("error updating record")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
changeRecord := ChangeRecord{}
|
changeRecord := ChangeRecord{}
|
||||||
json.Unmarshal(responseBody, &changeRecord)
|
json.Unmarshal(responseBody, &changeRecord)
|
||||||
|
|
||||||
@@ -196,7 +203,7 @@ func (ionos *IonosAPIImpl) GetARecord(tld string, subdomain string) (*common.ARe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := ionos.HttpCall("GET", "/v1/zones/"+zoneId+"/records/"+recordId, nil)
|
res, err := ionos.HttpCall("GET", "/v1/zones/"+zoneId+"/records/"+recordId, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ func utilMockServerImpl() func(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
if r.RequestURI == "/v1/zones" {
|
if r.RequestURI == "/v1/zones" {
|
||||||
response = zonesResponseJson
|
response = zonesResponseJson
|
||||||
} else if r.RequestURI == "/v1/zones/1234567890" {
|
} else if r.RequestURI == "/v1/zones/1234567890?recordName=example.com&recordType=A" ||
|
||||||
|
r.RequestURI == "/v1/zones/1234567890?recordName=sub.example.com&recordType=A" {
|
||||||
response = zoneResponseJson
|
response = zoneResponseJson
|
||||||
} else if r.RequestURI == "/v1/zones/1234567890/records/abcdefghij" {
|
} else if r.RequestURI == "/v1/zones/1234567890/records/abcdefghij" {
|
||||||
response = recordResponseSubJson
|
response = recordResponseSubJson
|
||||||
@@ -106,7 +107,7 @@ func TestHttpCall(t *testing.T) {
|
|||||||
|
|
||||||
func testHttpCallGet(api IonosAPI) func(t *testing.T) {
|
func testHttpCallGet(api IonosAPI) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
res, err := api.HttpCall("GET", "/v1/zones", nil)
|
res, err := api.HttpCall("GET", "/v1/zones", nil, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
||||||
@@ -120,7 +121,7 @@ func testHttpCallGet(api IonosAPI) func(t *testing.T) {
|
|||||||
|
|
||||||
func testHttpCallPut(api IonosAPI) func(t *testing.T) {
|
func testHttpCallPut(api IonosAPI) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
res, err := api.HttpCall("PUT", "/v1/zones/1234567890/records/abcdefghij", nil)
|
res, err := api.HttpCall("PUT", "/v1/zones/1234567890/records/abcdefghij", nil, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
||||||
@@ -141,7 +142,7 @@ func testHttpCallNonExistingEndpoint() func(t *testing.T) {
|
|||||||
|
|
||||||
api := New("dummyKey", mockServer.URL)
|
api := New("dummyKey", mockServer.URL)
|
||||||
|
|
||||||
res, err := api.HttpCall("GET", "/v1/nonExistingEndpoint", nil)
|
res, err := api.HttpCall("GET", "/v1/nonExistingEndpoint", nil, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
t.Fatalf("HttpCall() returned unexpected error: %v", err)
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ func (m *MockIonosAPI) GetZoneId(tld string) (string, error) {
|
|||||||
return m.GetZoneIdFunc(tld)
|
return m.GetZoneIdFunc(tld)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockIonosAPI) HttpCall(method string, url string, body io.Reader) (*http.Response, error) {
|
func (m *MockIonosAPI) HttpCall(method string, url string, body io.Reader, queryParams map[string]string) (*http.Response, error) {
|
||||||
return m.HttpCallFunc(method, url, body)
|
return m.HttpCallFunc(method, url, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package externalIpProvider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -40,12 +41,15 @@ func (p *ExternalIpProviderImplPlain) GetExternalIp() (net.IP, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
|
res.Body.Close()
|
||||||
return nil, errors.New("unexpected status code")
|
return nil, errors.New("unexpected status code")
|
||||||
}
|
}
|
||||||
|
|
||||||
responseBody := make([]byte, res.ContentLength)
|
responseBody, err := io.ReadAll(res.Body)
|
||||||
res.Body.Read(responseBody)
|
res.Body.Close()
|
||||||
defer res.Body.Close()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
parsedIp := net.ParseIP(string(responseBody))
|
parsedIp := net.ParseIP(string(responseBody))
|
||||||
if parsedIp == nil {
|
if parsedIp == nil {
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package realDynDns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
@@ -17,6 +19,7 @@ type ChangeDetector struct {
|
|||||||
dnsProvider dnsProvider.DNSProvider
|
dnsProvider dnsProvider.DNSProvider
|
||||||
notificationProvider notificationProvider.NotificationProvider
|
notificationProvider notificationProvider.NotificationProvider
|
||||||
domains []config.DomainConfig
|
domains []config.DomainConfig
|
||||||
|
logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(
|
func New(
|
||||||
@@ -24,12 +27,14 @@ func New(
|
|||||||
dnsProvider dnsProvider.DNSProvider,
|
dnsProvider dnsProvider.DNSProvider,
|
||||||
notificationProvider notificationProvider.NotificationProvider,
|
notificationProvider notificationProvider.NotificationProvider,
|
||||||
domains []config.DomainConfig,
|
domains []config.DomainConfig,
|
||||||
|
logger *slog.Logger,
|
||||||
) ChangeDetector {
|
) ChangeDetector {
|
||||||
return ChangeDetector{
|
return ChangeDetector{
|
||||||
externalIpProvider: externalIpProvider,
|
externalIpProvider: externalIpProvider,
|
||||||
dnsProvider: dnsProvider,
|
dnsProvider: dnsProvider,
|
||||||
notificationProvider: notificationProvider,
|
notificationProvider: notificationProvider,
|
||||||
domains: domains,
|
domains: domains,
|
||||||
|
logger: logger,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,37 +63,90 @@ func (c *ChangeDetector) RunOnce() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ChangeDetector) detectAndApplyChanges() (int, error) {
|
func (c *ChangeDetector) detectAndApplyChanges() (int, error) {
|
||||||
|
c.logger.Info("Detecting and applying changes")
|
||||||
|
|
||||||
externalIp, err := c.externalIpProvider.GetExternalIp()
|
externalIp, err := c.externalIpProvider.GetExternalIp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.logger.Error("Failed to retrieve external IP", slog.String("error", err.Error()))
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var numberUpdated int
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
|
numberUpdatedChannel := make(chan int)
|
||||||
|
|
||||||
for _, domain := range c.domains {
|
for _, domain := range c.domains {
|
||||||
for _, subdomain := range domain.Subdomains {
|
for _, subdomain := range domain.Subdomains {
|
||||||
currentRecord, err := c.dnsProvider.GetRecord(domain.TLD, subdomain)
|
wg.Add(1)
|
||||||
if err != nil {
|
|
||||||
return numberUpdated, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if currentRecord.IP != externalIp.String() {
|
go func(domain config.DomainConfig, subdomain string) {
|
||||||
err = c.notificationProvider.SendNotification(
|
defer wg.Done()
|
||||||
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
|
||||||
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
c.logger.Info("Checking record",
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
)
|
)
|
||||||
|
currentRecord, err := c.dnsProvider.GetRecord(domain.TLD, subdomain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return numberUpdated, err
|
c.logger.Error("Failed to retrieve record",
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
if currentRecord.IP != externalIp.String() {
|
||||||
numberUpdated++
|
c.logger.Info("Record has changed",
|
||||||
if err != nil {
|
slog.String("tld", domain.TLD),
|
||||||
return numberUpdated, err
|
slog.String("subdomain", subdomain),
|
||||||
|
slog.String("current_ip", currentRecord.IP),
|
||||||
|
slog.String("external_ip", externalIp.String()),
|
||||||
|
)
|
||||||
|
|
||||||
|
err = c.notificationProvider.SendNotification(
|
||||||
|
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
||||||
|
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Warn("Failed to send notification",
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("Updating record",
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
slog.String("current_ip", currentRecord.IP),
|
||||||
|
slog.String("external_ip", externalIp.String()),
|
||||||
|
)
|
||||||
|
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Error("Failed to update record",
|
||||||
|
slog.String("error", err.Error()),
|
||||||
|
slog.String("tld", domain.TLD),
|
||||||
|
slog.String("subdomain", subdomain),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
numberUpdatedChannel <- 1
|
||||||
}
|
}
|
||||||
}
|
}(domain, subdomain)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(numberUpdatedChannel)
|
||||||
|
}()
|
||||||
|
|
||||||
|
numberUpdated := 0
|
||||||
|
for v := range numberUpdatedChannel {
|
||||||
|
numberUpdated += v
|
||||||
|
}
|
||||||
|
|
||||||
|
c.logger.Info("Run completed", slog.Int("number_of_changes", numberUpdated))
|
||||||
return numberUpdated, nil
|
return numberUpdated, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package realDynDns
|
package realDynDns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"realdnydns/model/common"
|
"realdnydns/model/common"
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
@@ -72,7 +73,9 @@ func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
|
|||||||
"@",
|
"@",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
slog.Default(),
|
||||||
|
)
|
||||||
|
|
||||||
numberUpdated, err := changeDetector.RunOnce()
|
numberUpdated, err := changeDetector.RunOnce()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -101,7 +104,9 @@ func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
|
|||||||
"@",
|
"@",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
|
slog.Default(),
|
||||||
|
)
|
||||||
|
|
||||||
numberUpdated, err := changeDetector.RunOnce()
|
numberUpdated, err := changeDetector.RunOnce()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
14
renovate.json
Normal file
14
renovate.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||||
|
"extends": [
|
||||||
|
"local>t.behrendt/renovate-configs:common",
|
||||||
|
"local>t.behrendt/renovate-configs:action"
|
||||||
|
],
|
||||||
|
"packageRules": [
|
||||||
|
{
|
||||||
|
"matchPackageNames": ["golang", "gomod", "go"],
|
||||||
|
"groupName": "go version",
|
||||||
|
"updateTypes": ["major", "minor", "patch"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user