M internal/api/update.go => internal/api/update.go +10 -0
@@ 10,6 10,7 @@ import (
"net/http"
"git.src.quest/~skye/felu-ddns/internal/db"
+ "git.src.quest/~skye/felu-ddns/internal/util"
"github.com/gin-gonic/gin"
)
@@ 40,6 41,15 @@ func UpdateA() gin.HandlerFunc {
aRecord = c.ClientIP()
}
+ if err := util.CheckARecord(aRecord); err != nil {
+ c.JSON(http.StatusBadRequest, gin.H{
+ "status": "error",
+ "error": err.Error(),
+ })
+ c.Abort()
+ return
+ }
+
err := db.UpdateDomainARecord(domain, apiKey, aRecord)
if err != nil {
// FIXME: Handle better, "bad api key" is just the most likely scenario
M internal/handlers/domains.go => internal/handlers/domains.go +3 -3
@@ 7,12 7,12 @@
package handlers
import (
- "net"
"net/http"
"regexp"
"strings"
"git.src.quest/~skye/felu-ddns/internal/db"
+ "git.src.quest/~skye/felu-ddns/internal/util"
"github.com/gin-gonic/gin"
)
@@ 46,8 46,8 @@ func PostDomain() gin.HandlerFunc {
c.Abort()
return
}
- if net.ParseIP(data.ARecord).To4() == nil {
- c.String(http.StatusBadRequest, "The A record is invalid")
+ if err := util.CheckARecord(data.ARecord); err != nil {
+ c.String(http.StatusBadRequest, err.Error())
c.Abort()
return
}
A internal/util/check.go => internal/util/check.go +19 -0
@@ 0,0 1,19 @@
+/*
+ * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
+ *
+ * This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
+ * more information.
+ */
+package util
+
+import (
+ "errors"
+ "net"
+)
+
+func CheckARecord(aRecord string) error {
+ if net.ParseIP(aRecord).To4() == nil {
+ return errors.New("Invalid A record")
+ }
+ return nil
+}