Skip to content

Commit

Permalink
Remove multi tenant feature and add make method and body configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed Aug 31, 2024
1 parent ce7b289 commit 9ee7518
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dyndns-distributor.exe
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1-alpine3.18 AS build-env
FROM golang:1-alpine3.20 AS build-env
WORKDIR /app

# Copy go.mod and go.sum and download as distinct layers
Expand All @@ -10,7 +10,7 @@ COPY . ./
RUN go build -o /app/dyndns-distributor

# Build runtime image
FROM alpine:3.18
FROM alpine:3.20
WORKDIR /app
ENV GIN_MODE=release
COPY --from=build-env /app/dyndns-distributor .
Expand Down
16 changes: 6 additions & 10 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ func NewAuthHandler(settings *Settings) *AuthHandler {
func (h *AuthHandler) Handle(c *gin.Context) {
username, password, _ := c.Request.BasicAuth()

for index, account := range h.settings.Accounts {
if account.Username == username && account.Password == password {
c.Set("account", index)
c.Next()
return
}
if h.settings.Username == username && h.settings.Password == password {
c.Next()
} else {
c.Abort()
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=restricted")
c.Status(401)
}

c.Abort()
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=restricted")
c.Status(401)
}
19 changes: 15 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"io"
"net"
"net/http"
Expand All @@ -16,19 +17,25 @@ func NewDynClient() *DynClient {
return &DynClient{&http.Client{}}
}

func (c *DynClient) Update(urlTemplate string, ip net.IP) (string, string, error) {
func (c *DynClient) Update(record Record, ip net.IP) (string, string, error) {

updateUrl := strings.ReplaceAll(urlTemplate, "<ipaddr>", ip.String())
record.UpdateUrl = strings.ReplaceAll(record.UpdateUrl, "<ipaddr>", ip.String())
record.Body = strings.ReplaceAll(record.Body, "<ipaddr>", ip.String())

parsedUrl, err := url.Parse(updateUrl)
parsedUrl, err := url.Parse(record.UpdateUrl)
if err != nil {
return "", "", err
}

parsedUrl.User = url.UserPassword(parsedUrl.User.Username(), "***")
urlWithoutPassword := strings.ReplaceAll(parsedUrl.String(), "%2A%2A%2A", "***")

request, err := http.NewRequest("GET", updateUrl, nil)
method := record.Method
if method == "" {
method = "GET"
}

request, err := http.NewRequest(method, record.UpdateUrl, strings.NewReader(record.Body))
if err != nil {
return "", urlWithoutPassword, err
}
Expand All @@ -44,6 +51,10 @@ func (c *DynClient) Update(urlTemplate string, ip net.IP) (string, string, error
return "", urlWithoutPassword, err
}

if response.StatusCode < 200 || response.StatusCode >= 300 {
return string(body), urlWithoutPassword, errors.New(method + " " + urlWithoutPassword + " returned " + response.Status + ": " + string(body))
}

return string(body), urlWithoutPassword, nil
}

Expand Down
26 changes: 17 additions & 9 deletions dyndnsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
{
"ListenerAddress": "localhost:8080",
"IpRetrieveUrl": "https://api.ipify.org/",
"UserAgent": "VectorData - DynDNS Distributor - 3.0",
"Accounts": [
"UserAgent": "DynDNS Distributor - 4.0",
"UpdateOnStartup": true,
"Username": "proxyuser",
"Password": "localnetpw123",
"Records": [
{
"Username": "proxyuser",
"Password": "localnetpw123",
"UpdateOnStartup": true,
"UpdateUrls": [
"https://13135:[email protected]/nic/update?hostname=test.feste-ip.net",
"https://username:[email protected]/update?hostname=mydomain.de&myip=<ipaddr>"
]
"UpdateUrl": "https://13135:[email protected]/nic/update?hostname=test.feste-ip.net"
},
{
"UpdateUrl": "https://username:[email protected]/update?hostname=mydomain.de&myip=<ipaddr>"
},
{
"UpdateUrl": "https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_record_id}",
"Method": "PATCH",
"Headers": [
"Authorization: Bearer {api_token}"
],
"Body": "{\"content\":\"<ipaddr>\"}"
}
]
}
26 changes: 12 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ func UpdateOnStartup(settings *Settings, client *DynClient) {
return
}

for _, account := range settings.Accounts {
if account.UpdateOnStartup {
for _, updateUrlTemplate := range account.UpdateUrls {
body, loggingUrl, err := client.Update(updateUrlTemplate, ip)

fmt.Print(loggingUrl)
fmt.Println(":")
fmt.Print("\t")

if err == nil {
fmt.Println(body)
} else {
fmt.Println(err)
}
if settings.UpdateOnStartup {
for _, record := range settings.Records {
body, loggingUrl, err := client.Update(record, ip)

fmt.Print(loggingUrl)
fmt.Println(":")
fmt.Print("\t")

if err == nil {
fmt.Println(body)
} else {
fmt.Println(err)
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import (
)

type Settings struct {
ListenerAddress string `json:"ListenerAddress"`
IpRetrieveUrl string `json:"IpRetrieveUrl"`
UserAgent string `json:"UserAgent"`
Accounts []Account `json:"Accounts"`
}

type Account struct {
ListenerAddress string `json:"ListenerAddress"`
IpRetrieveUrl string `json:"IpRetrieveUrl"`
UserAgent string `json:"UserAgent"`
UpdateOnStartup bool `json:"UpdateOnStartup"`
Username string `json:"Username"`
Password string `json:"Password"`
UpdateOnStartup bool `json:"UpdateOnStartup"`
UpdateUrls []string `json:"UpdateUrls"`
Records []Record `json:"Records"`
}

type Record struct {
UpdateUrl string `json:"UpdateUrl"`
Method string `json:"Method"`
Headers []string `json:"Headers"`
Body string `json:"Body"`
}

func LoadSettings() (*Settings, error) {
Expand Down
12 changes: 3 additions & 9 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ func (h *UpdateHandler) Handle(c *gin.Context) {
return
}

account, exists := c.Get("account")
if !exists {
c.String(500, "An unexpected error occured in the authentication middleware")
return
}

successfulUpdates := 0

for _, updateUrlTemplate := range h.settings.Accounts[account.(int)].UpdateUrls {
for _, record := range h.settings.Records {

body, loggingUrl, err := h.client.Update(updateUrlTemplate, ip)
body, loggingUrl, err := h.client.Update(record, ip)
if err != nil {
c.Error(err)
continue
Expand All @@ -47,5 +41,5 @@ func (h *UpdateHandler) Handle(c *gin.Context) {
fmt.Println(string(body))
}

c.String(200, "Successfully updated %d of %d domains", successfulUpdates, len(h.settings.Accounts[account.(int)].UpdateUrls))
c.String(200, "Successfully updated %d of %d records", successfulUpdates, len(h.settings.Records))
}

0 comments on commit 9ee7518

Please sign in to comment.