Skip to content

Commit

Permalink
Merge pull request #154 from Plaenkler/11-implement-custom-provider
Browse files Browse the repository at this point in the history
[UPD] Sanitize job params
  • Loading branch information
Plaenkler authored Sep 1, 2023
2 parents 53cdbee + 1e50458 commit aec1ba7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
10 changes: 9 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"strconv"
"sync"

log "github.com/plaenkler/ddns-updater/pkg/logging"
"gopkg.in/yaml.v3"
Expand All @@ -24,7 +25,10 @@ const (
filePerm = 0644
)

var config *Config
var (
config *Config
mutex = &sync.RWMutex{}
)

func init() {
err := load()
Expand Down Expand Up @@ -159,10 +163,14 @@ func Update(updatedConfig *Config) error {
if err != nil {
return err
}
mutex.Lock()
defer mutex.Unlock()
config = updatedConfig
return nil
}

func Get() *Config {
mutex.RLock()
defer mutex.RUnlock()
return config
}
31 changes: 29 additions & 2 deletions pkg/server/routes/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package web

import (
"embed"
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"

"github.com/plaenkler/ddns-updater/pkg/config"
"github.com/plaenkler/ddns-updater/pkg/database"
Expand Down Expand Up @@ -64,17 +66,42 @@ func ProvideIndex(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "[web-ProvideIndex-4] could not find jobs: %s", err)
return
}
err = formatParams(data.Jobs)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "[web-ProvideIndex-5] formatting params failed: %s", err)
return
}
img, err := totps.GetKeyAsQR()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "[web-ProvideIndex-5] could not generate TOTP QR code: %s", err)
fmt.Fprintf(w, "[web-ProvideIndex-6] could not generate TOTP QR code: %s", err)
return
}
data.TOTPQR = template.URL(img)
w.Header().Add("Content-Type", "text/html")
err = tpl.Execute(w, data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "[web-ProvideIndex-6] could not execute parsed template: %v", err)
fmt.Fprintf(w, "[web-ProvideIndex-7] could not execute parsed template: %v", err)
}
}

func formatParams(jobs []model.SyncJob) error {
for j := range jobs {
params := make(map[string]string)
err := json.Unmarshal([]byte(jobs[j].Params), &params)
if err != nil {
return err
}
var paramList []string
for k, v := range params {
if strings.ToLower(k) == "password" {
continue
}
paramList = append(paramList, fmt.Sprintf("%s: %s", k, v))
}
jobs[j].Params = strings.Join(paramList, ", ")
}
return nil
}

0 comments on commit aec1ba7

Please sign in to comment.