Skip to content

Commit

Permalink
Merge branch 'main' into dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Plaenkler authored Sep 27, 2023
2 parents d8398a4 + b9a7d99 commit e1348fa
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
31 changes: 24 additions & 7 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,38 @@ on:
branches: main

jobs:
build:
name: Lint Code Base
super-linter:
name: Lint Multiple
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Lint Code Base
uses: super-linter/super-linter@v5
env:
DEFAULT_BRANCH: main
VALIDATE_ALL_CODEBASE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_ALL_CODEBASE: true
VALIDATE_HTML: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_GO: false
golangci-lint:
name: Lint Golang
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.21.1
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install dependencies
run: go mod tidy
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout=3m
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Calver Release
uses: StephaneBour/actions-calver@master
Expand All @@ -30,7 +30,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Docker meta
id: meta
Expand Down
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Data generated by the app
/cmd/data
/cmd/def/data
/cmd/svc/data
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ FROM golang:1.21-bullseye AS build
WORKDIR /app
COPY . /app

RUN go mod tidy && go build -o /ddns-updater cmd/main.go
RUN go mod tidy && go build -o /ddns-updater cmd/def/main.go

## Deploy
FROM debian:stable-slim
Expand All @@ -16,7 +16,8 @@ WORKDIR /app
COPY --from=build /ddns-updater /app/ddns-updater

RUN apt-get update && \
apt-get install -y ca-certificates
apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*

EXPOSE 80

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The user-friendly interface allows for straightforward secure setup and manageme
- [x] Encryption of sensitive data
- [x] Scheduled update service
- [x] Supports multiple IP resolvers
- [ ] Deploy as Windows Service
- [x] Deploy as Windows Service
- [x] Available as Docker Container

## 🏷️ Supported providers
Expand Down
File renamed without changes.
71 changes: 71 additions & 0 deletions cmd/svc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"os"
"os/signal"

"github.com/kardianos/service"
"github.com/plaenkler/ddns-updater/pkg/database"
"github.com/plaenkler/ddns-updater/pkg/ddns"
log "github.com/plaenkler/ddns-updater/pkg/logging"
"github.com/plaenkler/ddns-updater/pkg/server"
"github.com/plaenkler/ddns-updater/pkg/server/session"
)

type program struct{}

func (p *program) Start(_ service.Service) error {
database.Start()
log.Infof("[main-Start-1] started database connection")
go ddns.Start()
log.Infof("[main-Start-2] started ddns service")
go session.Start()
log.Infof("[main-Start-3] started session service")
go server.Start()
log.Infof("[main-Start-4] started webserver")
return nil
}

func (p *program) Stop(_ service.Service) error {
server.Stop()
log.Infof("[main-Stop-1] stopped webserver")
session.Stop()
log.Infof("[main-Stop-2] stopped session service")
ddns.Stop()
log.Infof("[main-Stop-3] stopped ddns service")
database.Stop()
log.Infof("[main-Stop-4] stopped database connection")
return nil
}

func main() {
if service.AvailableSystems()[len(service.AvailableSystems())-1].Interactive() {
p := &program{}
err := p.Start(nil)
if err != nil {
log.Fatalf("[main-main-0] failed to start service: %v", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
err = p.Stop(nil)
if err != nil {
log.Fatalf("[main-main-0] failed to stop service: %v", err)
}
return
}
svcConfig := &service.Config{
Name: "DDNS-Updater",
DisplayName: "Dynamic DNS Updater",
Description: "Service to update dynamic DNS entries.",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatalf("[main-main-1] failed to create service: %v", err)
}
err = s.Run()
if err != nil {
log.Errorf("[main-main-2] failed to run service: %v", err)
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module github.com/plaenkler/ddns-updater

go 1.20
go 1.21

toolchain go1.21.1

require (
github.com/aliyun/alibaba-cloud-sdk-go v1.62.567
github.com/kardianos/service v1.2.2
github.com/pquerna/otp v1.4.0
golang.org/x/time v0.3.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -21,5 +24,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
golang.org/x/sys v0.1.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -51,6 +53,9 @@ github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVK
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cipher/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func generate() ([]byte, error) {
randomBytes := make([]byte, keyLength)
_, err := rand.Read(randomBytes)
if err != nil {
return nil, fmt.Errorf("[cipher-GenerateRandomKey-1] generating random key failed: %s", err)
return nil, err
}
return randomBytes, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddns/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func updateDDNSEntries(db *gorm.DB, jobs []model.SyncJob, a model.IPAddress) {
if err != nil {
log.Errorf("[ddns-updateDDNSEntries-5] failed to update IP address for job %v: %s", job.ID, err)
}
log.Infof("[ddns-updateDDNSEntries-6] updated DDNS entry for ID: %v Provider: %s Params: %+v", job.ID, job.Provider, job.Params)
log.Infof("[ddns-updateDDNSEntries-6] updated DDNS entry for ID: %s", job.ID)
}
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/server/routes/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func UpdateConfig(w http.ResponseWriter, r *http.Request) {
if totps.Verify(r.FormValue("otp")) {
cfg.UseTOTP = !config.Get().UseTOTP
}
config.Update(cfg)
err = config.Update(cfg)
if err != nil {
log.Errorf("[api-UpdateConfig-6] could not update config: %s", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(w, r, r.Header.Get("Referer"), http.StatusTemporaryRedirect)
}
6 changes: 5 additions & 1 deletion pkg/server/routes/api/inputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ func GetInputs(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(inputs)
_, err = w.Write(inputs)
if err != nil {
http.Error(w, "[api-GetInputs-4] could not write response", http.StatusInternalServerError)
return
}
}
5 changes: 4 additions & 1 deletion pkg/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func initializeServer() {
}
}()
<-cancel
server.Shutdown(context.Background())
err := server.Shutdown(context.Background())
if err != nil {
log.Fatalf("[server-initializeServer-2] could not shutdown server: %v", err)
}
}

func Stop() {
Expand Down
48 changes: 28 additions & 20 deletions pkg/server/xrclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,50 @@ import (
"strings"
)

const (
xForwardedFor = "X-Forwarded-For"
xRealIP = "X-Real-IP"
xForwardedPort = "X-Forwarded-Port"
)

func getRealClientIP(r *http.Request) (string, error) {
clientIP := r.Header.Get("X-Real-IP")
if clientIP != "" {
return clientIP, nil
rAddr := r.Header.Get(xRealIP)
if rAddr != "" {
return rAddr, nil
}
rAddr = crawlForwardedIPs(r)
if rAddr != "" {
return rAddr, nil
}
rAddr, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
xff := r.Header.Get("X-Forwarded-For")
return rAddr, nil
}

func crawlForwardedIPs(r *http.Request) string {
xff := r.Header.Get(xForwardedFor)
ips := strings.Split(xff, ",")
// Check IP addresses in reverse order
for i := len(ips) - 1; i >= 0; i-- {
// Check IP addresses in reverse order to find real IP
ip := strings.TrimSpace(ips[i])
if ip != "" {
clientIP = ip
break
return ip
}
}
if clientIP != "" {
return clientIP, nil
}
clientIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
return clientIP, nil
return ""
}

func getRealClientPort(r *http.Request) (string, error) {
remotePort := r.Header.Get("X-Forwarded-Port")
remotePort := r.Header.Get(xForwardedPort)
if remotePort != "" {
return remotePort, nil
}
// Extract remote address from TCP connection
remoteAddr := r.RemoteAddr
if !strings.Contains(remoteAddr, ":") {
if !strings.Contains(r.RemoteAddr, ":") {
return "", fmt.Errorf("[server-GetRealClientPort-2] could not determine remote port")
}
_, port, err := net.SplitHostPort(remoteAddr)
_, port, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return "", err
}
Expand Down

0 comments on commit e1348fa

Please sign in to comment.