From cbf17e7d27bd5def235bca1edd10792e64478949 Mon Sep 17 00:00:00 2001 From: TQ Hirsch Date: Sat, 6 Aug 2022 16:47:46 +0200 Subject: [PATCH] Forgot to connect to the DB in the admin tool --- cmd/qddns-admin/main.go | 6 +- cmd/qddns-server/main.go | 151 ++++++++++++++++++++++----------------- common/types.go | 4 +- 3 files changed, 92 insertions(+), 69 deletions(-) diff --git a/cmd/qddns-admin/main.go b/cmd/qddns-admin/main.go index 484f97f..d4c227e 100644 --- a/cmd/qddns-admin/main.go +++ b/cmd/qddns-admin/main.go @@ -36,6 +36,7 @@ func (cmd *AddToken) Run() error { if err != nil { return err } + tx.Commit(ctx) return nil } @@ -54,7 +55,10 @@ var cli Cli func main() { ctx := kong.Parse(&cli) - //db.Connect(cli.Db) + if err := db.Connect(cli.Db); err != nil { + println(err.Error()) + os.Exit(0) + } println(ctx.Command()) if err := ctx.Run(); err != nil { println(err.Error()) diff --git a/cmd/qddns-server/main.go b/cmd/qddns-server/main.go index ad837d5..b4d56a9 100644 --- a/cmd/qddns-server/main.go +++ b/cmd/qddns-server/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "github.com/gin-gonic/gin" "github.com/thequux/qddns/common" @@ -12,7 +13,79 @@ import ( "strings" ) +func Update(c *gin.Context) { + authHdr := strings.Fields(c.GetHeader("Authorization")) + domain := c.Param("domain") + + clientIP := c.ClientIP() + ip := net.ParseIP(clientIP) + var token string + if len(authHdr) == 0 || authHdr[0] != "bearer" { + // we have a token; check if it's valid for domain + token = authHdr[1] + } else { + c.JSON(http.StatusUnauthorized, common.Response{Status: "error", Message: "Missing bearer token", Code: "QDA0001"}) + return + } + + if ip == nil { + c.JSON(http.StatusBadRequest, common.Response{Status: "error", Message: "Unable to determine client IP (got " + clientIP + ")", Code: "QDE0003"}) + return + } + + // get a connection + tx, err := db.Db.Begin(c) + defer tx.Rollback(c) + if err != nil { + c.JSON(http.StatusInternalServerError, common.Response{Status: "error", Message: "Failed to access database", Code: "QDE0001"}) + return + } + // check token validity + row := tx.QueryRow(c, "SELECT COUNT(*) from qddns_auth WHERE token = $1 AND domain = $2", token, domain) + var rcount int + if err := row.Scan(&rcount); err != nil { + c.JSON(http.StatusInternalServerError, common.Response{Status: "error", Message: "Failed to check token " + err.Error(), Code: "QDE0002"}) + return + } else if rcount < 1 { + c.JSON(http.StatusUnauthorized, common.Response{Status: "error", Message: "Not authorized for domain " + domain, Code: "QDA0002"}) + return + } + + // Identify the type of address + + var recordType string + if ip.To4() == nil { + recordType = "AAAA" + clientIP = ip.To16().String() + } else { + recordType = "A" + clientIP = ip.To4().String() + } + + // Do the update + tag, err := tx.Exec(c, "UPDATE records SET content = $1 WHERE domain = $2 AND type = $3", clientIP, domain, recordType) + if err != nil { + c.JSON(http.StatusInternalServerError, common.Response{ + Status: "error", + Message: "Failed to update record: " + err.Error(), + Code: "QDD0001", + }) + return + } + if tag.RowsAffected() != 1 { + c.JSON(http.StatusInternalServerError, common.Response{ + Status: "error", + Message: fmt.Sprintf("Wrong number of rows affected: %v", tag.RowsAffected()), + }) + return + } + tx.Commit(c) +} + +var listen = flag.String("listen", ":8081", "Address or path on which to listen") + func main() { + flag.Parse() if err := db.Connect(""); err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to database: %v", err) os.Exit(1) @@ -20,72 +93,18 @@ func main() { // Set up server r := gin.Default() - r.POST("/update/:domain", func(c *gin.Context) { - authHdr := strings.Fields(c.GetHeader("Authorization")) - domain := c.Param("domain") + r.POST("/update/:domain", Update) - clientIP := c.ClientIP() - ip := net.ParseIP(clientIP) - var token string - if len(authHdr) == 0 || authHdr[0] != "bearer" { - // we have a token; check if it's valid for domain - token = authHdr[1] - } else { - c.JSON(http.StatusUnauthorized, common.Response{Status: "error", Message: "Missing bearer token", Code: "QDA0001"}) - return - } + var err error + if _, err = net.ResolveTCPAddr("tcp", *listen); err == nil { + err = r.Run(*listen) + } else { + // Probably a UNIX address + err = r.RunUnix(*listen) + } + if err != nil { + println("Failed to listen: " + err.Error()) + os.Exit(1) + } - if ip == nil { - c.JSON(http.StatusBadRequest, common.Response{Status: "error", Message: "Unable to determine client IP (got " + clientIP + ")", Code: "QDE0003"}) - return - } - - // get a connection - tx, err := db.Db.Begin(c) - defer tx.Rollback(c) - if err != nil { - c.JSON(http.StatusInternalServerError, common.Response{Status: "error", Message: "Failed to access database", Code: "QDE0001"}) - return - } - // check token validity - row := tx.QueryRow(c, "SELECT COUNT(*) from qddns_auth WHERE token = $1 AND domain = $2", token, domain) - var rcount int - if err := row.Scan(&rcount); err != nil { - c.JSON(http.StatusInternalServerError, common.Response{Status: "error", Message: "Failed to check token " + err.Error(), Code: "QDE0002"}) - return - } else if rcount < 1 { - c.JSON(http.StatusUnauthorized, common.Response{Status: "error", Message: "Not authorized for domain " + domain, Code: "QDA0002"}) - return - } - - // Identify the type of address - - var recordType string - if ip.To4() == nil { - recordType = "AAAA" - clientIP = ip.To16().String() - } else { - recordType = "A" - clientIP = ip.To4().String() - } - - // Do the update - tag, err := tx.Exec(c, "UPDATE records SET content = $1 WHERE domain = $2 AND type = $3", clientIP, domain, recordType) - if err != nil { - c.JSON(http.StatusInternalServerError, common.Response{ - Status: "error", - Message: "Failed to update record: " + err.Error(), - Code: "QDD0001", - }) - return - } - if tag.RowsAffected() != 1 { - c.JSON(http.StatusInternalServerError, common.Response{ - Status: "error", - Message: fmt.Sprintf("Wrong number of rows affected: %v", tag.RowsAffected()), - }) - return - } - tx.Commit(c) - }) } diff --git a/common/types.go b/common/types.go index f8a6cc8..107a76d 100644 --- a/common/types.go +++ b/common/types.go @@ -2,6 +2,6 @@ package common type Response struct { Status string - Code string `json:"omitempty"` - Message string `json:"omitempty"` + Code string //`json:"omitempty"` + Message string //`json:"omitempty"` }