Forgot to connect to the DB in the admin tool
This commit is contained in:
@@ -36,6 +36,7 @@ func (cmd *AddToken) Run() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
tx.Commit(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +55,10 @@ var cli Cli
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := kong.Parse(&cli)
|
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())
|
println(ctx.Command())
|
||||||
if err := ctx.Run(); err != nil {
|
if err := ctx.Run(); err != nil {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/thequux/qddns/common"
|
"github.com/thequux/qddns/common"
|
||||||
@@ -12,7 +13,79 @@ import (
|
|||||||
"strings"
|
"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() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
if err := db.Connect(""); err != nil {
|
if err := db.Connect(""); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v", err)
|
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -20,72 +93,18 @@ func main() {
|
|||||||
|
|
||||||
// Set up server
|
// Set up server
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.POST("/update/:domain", func(c *gin.Context) {
|
r.POST("/update/:domain", Update)
|
||||||
authHdr := strings.Fields(c.GetHeader("Authorization"))
|
|
||||||
domain := c.Param("domain")
|
|
||||||
|
|
||||||
clientIP := c.ClientIP()
|
var err error
|
||||||
ip := net.ParseIP(clientIP)
|
if _, err = net.ResolveTCPAddr("tcp", *listen); err == nil {
|
||||||
var token string
|
err = r.Run(*listen)
|
||||||
if len(authHdr) == 0 || authHdr[0] != "bearer" {
|
} else {
|
||||||
// we have a token; check if it's valid for domain
|
// Probably a UNIX address
|
||||||
token = authHdr[1]
|
err = r.RunUnix(*listen)
|
||||||
} else {
|
}
|
||||||
c.JSON(http.StatusUnauthorized, common.Response{Status: "error", Message: "Missing bearer token", Code: "QDA0001"})
|
if err != nil {
|
||||||
return
|
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)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,6 @@ package common
|
|||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Status string
|
Status string
|
||||||
Code string `json:"omitempty"`
|
Code string //`json:"omitempty"`
|
||||||
Message string `json:"omitempty"`
|
Message string //`json:"omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user