From d04b937750a2f60ed81b0636d9b2d8afc957009a Mon Sep 17 00:00:00 2001 From: MarekWojt Date: Mon, 2 Jan 2023 02:58:02 +0100 Subject: [PATCH 1/2] update ipv6 + ipv4 at once --- web/web.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/web/web.go b/web/web.go index 8ac0aaf..8c2a3bc 100644 --- a/web/web.go +++ b/web/web.go @@ -28,6 +28,7 @@ func Init(debugMode bool) { r.GET("/", index) r.GET("/update/{domain}/v4", authenticatedRequest(updateV4)) r.GET("/update/{domain}/v6", authenticatedRequest(updateV6)) + r.GET("/update/{domain}", authenticatedRequest(update)) } func RunSocket() error { @@ -124,6 +125,40 @@ func updateV6(ctx *fasthttp.RequestCtx) { ctx.WriteString("OK") } +func update(ctx *fasthttp.RequestCtx) { + domain := ctx.UserValue("domain").(string) + domain = util.ParseDomain(domain) + + ipv4 := string(ctx.QueryArgs().PeekBytes(ipv4Param)) + ipv6 := string(ctx.QueryArgs().PeekBytes(ipv6Param)) + + if ipv4 == "" && ipv6 == "" { + ctx.WriteString("Missing ipv6 or ipv4 query parameter") + ctx.SetStatusCode(fasthttp.StatusBadRequest) + return + } + + if ipv4 != "" { + err := dns.UpdateIpv4(domain, ipv4) + if err != nil { + ctx.WriteString(err.Error()) + ctx.SetStatusCode(fasthttp.StatusNotFound) + return + } + } + + if ipv6 != "" { + err := dns.UpdateIpv4(domain, ipv4) + if err != nil { + ctx.WriteString(err.Error()) + ctx.SetStatusCode(fasthttp.StatusNotFound) + return + } + } + + ctx.WriteString("OK") +} + func authenticatedRequest(request func(ctx *fasthttp.RequestCtx)) func(ctx *fasthttp.RequestCtx) { return func(ctx *fasthttp.RequestCtx) { domain, ok := ctx.UserValue("domain").(string) From 621ec4b0397a593dca6308a72241a54f16e5f0bc Mon Sep 17 00:00:00 2001 From: MarekWojt Date: Tue, 3 Jan 2023 22:58:00 +0100 Subject: [PATCH 2/2] improve error message for update action --- web/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/web.go b/web/web.go index 8c2a3bc..64367ae 100644 --- a/web/web.go +++ b/web/web.go @@ -133,7 +133,7 @@ func update(ctx *fasthttp.RequestCtx) { ipv6 := string(ctx.QueryArgs().PeekBytes(ipv6Param)) if ipv4 == "" && ipv6 == "" { - ctx.WriteString("Missing ipv6 or ipv4 query parameter") + ctx.WriteString("Provide at least one these query parameters: ipv4, ipv6") ctx.SetStatusCode(fasthttp.StatusBadRequest) return }