diff options
Diffstat (limited to 'web/middleware/ddns.go')
-rw-r--r-- | web/middleware/ddns.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/web/middleware/ddns.go b/web/middleware/ddns.go new file mode 100644 index 0000000..b213926 --- /dev/null +++ b/web/middleware/ddns.go | |||
@@ -0,0 +1,40 @@ | |||
1 | package middleware | ||
2 | |||
3 | import ( | ||
4 | "log" | ||
5 | "net/http" | ||
6 | |||
7 | "github.com/gin-gonic/gin" | ||
8 | ) | ||
9 | |||
10 | const DDNS_AUTH_KEY = "DDNSAuthZone" | ||
11 | |||
12 | type DDNSUpdateRequest struct { | ||
13 | Key string `form:"key" binding:"required"` | ||
14 | } | ||
15 | |||
16 | func DDNSAuthMiddleware(c *gin.Context) { | ||
17 | cfg := GetServerConfig(c) | ||
18 | |||
19 | var req DDNSUpdateRequest | ||
20 | if err := c.ShouldBind(&req); err != nil { | ||
21 | log.Println("ddnsAuthMiddleware: No key in request") | ||
22 | c.AbortWithStatus(http.StatusNotFound) | ||
23 | return | ||
24 | } | ||
25 | |||
26 | res := cfg.GetDDNSZoneName(req.Key) | ||
27 | if res == "" { | ||
28 | log.Println("ddnsAuthMiddleware: Unknown secret") | ||
29 | c.AbortWithStatus(http.StatusNotFound) | ||
30 | return | ||
31 | } else { | ||
32 | c.Set(DDNS_AUTH_KEY, res) | ||
33 | } | ||
34 | |||
35 | c.Next() | ||
36 | } | ||
37 | |||
38 | func GetDDNSAuthKey(c *gin.Context) string { | ||
39 | return c.GetString(DDNS_AUTH_KEY) | ||
40 | } | ||