diff options
Diffstat (limited to 'web')
-rw-r--r-- | web/config.go | 1 | ||||
-rw-r--r-- | web/controllers/manage.go | 61 | ||||
-rw-r--r-- | web/middleware/api_auth.go | 34 |
3 files changed, 96 insertions, 0 deletions
diff --git a/web/config.go b/web/config.go index b307425..9490b44 100644 --- a/web/config.go +++ b/web/config.go | |||
@@ -14,6 +14,7 @@ type ServerConfig struct { | |||
14 | DynamicDnsView string | 14 | DynamicDnsView string |
15 | DDNSSecrets map[string]string `json:"DDNS"` | 15 | DDNSSecrets map[string]string `json:"DDNS"` |
16 | AcmeSecrets map[string]map[string]int `json:"ACME"` | 16 | AcmeSecrets map[string]map[string]int `json:"ACME"` |
17 | ApiSecrets map[string]string `json:"DNS_MANAGE"` | ||
17 | } | 18 | } |
18 | 19 | ||
19 | func (s *ServerConfig) GetDDNSZoneName(k string) string { | 20 | func (s *ServerConfig) GetDDNSZoneName(k string) string { |
diff --git a/web/controllers/manage.go b/web/controllers/manage.go new file mode 100644 index 0000000..53a38a6 --- /dev/null +++ b/web/controllers/manage.go | |||
@@ -0,0 +1,61 @@ | |||
1 | package controllers | ||
2 | |||
3 | import ( | ||
4 | "net/http" | ||
5 | |||
6 | "github.com/gin-gonic/gin" | ||
7 | |||
8 | "code.crute.me/mcrute/frame" | ||
9 | "code.crute.me/mcrute/go_ddns_manager/dns" | ||
10 | "code.crute.me/mcrute/go_ddns_manager/web/middleware" | ||
11 | ) | ||
12 | |||
13 | type UpdateZone struct { | ||
14 | Create []dns.RR | ||
15 | Update []dns.RR | ||
16 | Delete []dns.RR | ||
17 | } | ||
18 | |||
19 | func ManageRoot(c *gin.Context) { | ||
20 | c.JSON(http.StatusOK, map[string]string{ | ||
21 | "views_url": frame.MakeURL(c.Request, "/manage/views").String(), | ||
22 | }) | ||
23 | } | ||
24 | |||
25 | func ListViews(c *gin.Context) { | ||
26 | cfg := middleware.GetServerConfig(c) | ||
27 | |||
28 | out := map[string]string{} | ||
29 | |||
30 | for _, vn := range cfg.BindConfig.Views() { | ||
31 | out[vn] = frame.MakeURL(c.Request, "/manage/views/%s", vn).String() | ||
32 | } | ||
33 | |||
34 | c.JSON(http.StatusOK, out) | ||
35 | } | ||
36 | |||
37 | func ListView(c *gin.Context) { | ||
38 | cfg := middleware.GetServerConfig(c) | ||
39 | view := c.Param("view") | ||
40 | |||
41 | out := map[string]string{} | ||
42 | |||
43 | for _, z := range cfg.BindConfig.ZonesInView(view) { | ||
44 | out[z.Name] = frame.MakeURL(c.Request, "/manage/views/%s/%s", view, z.Name).String() | ||
45 | } | ||
46 | |||
47 | c.JSON(http.StatusOK, out) | ||
48 | } | ||
49 | |||
50 | func ListZone(c *gin.Context) { | ||
51 | cfg := middleware.GetServerConfig(c) | ||
52 | zone := cfg.BindConfig.Zone(c.Param("view"), c.Param("zone")) | ||
53 | |||
54 | rrs, err := cfg.DNSClient.ReadRemoteZone(zone) | ||
55 | if err != nil { | ||
56 | c.JSON(http.StatusInternalServerError, err) | ||
57 | return | ||
58 | } | ||
59 | |||
60 | c.JSON(http.StatusOK, rrs) | ||
61 | } | ||
diff --git a/web/middleware/api_auth.go b/web/middleware/api_auth.go new file mode 100644 index 0000000..b854297 --- /dev/null +++ b/web/middleware/api_auth.go | |||
@@ -0,0 +1,34 @@ | |||
1 | package middleware | ||
2 | |||
3 | import ( | ||
4 | "net/http" | ||
5 | "strings" | ||
6 | |||
7 | "github.com/gin-gonic/gin" | ||
8 | ) | ||
9 | |||
10 | const API_AUTH_USER_KEY = "APIAuthUserKey" | ||
11 | |||
12 | func ApiAuthMiddleware(c *gin.Context) { | ||
13 | cfg := GetServerConfig(c) | ||
14 | |||
15 | auth := strings.Split(c.Request.Header.Get("Authorization"), " ") | ||
16 | |||
17 | if len(auth) != 2 || auth[0] != "Bearer" { | ||
18 | c.AbortWithStatus(http.StatusUnauthorized) | ||
19 | return | ||
20 | } | ||
21 | |||
22 | if user, ok := cfg.ApiSecrets[auth[1]]; ok { | ||
23 | c.Set(API_AUTH_USER_KEY, user) | ||
24 | } else { | ||
25 | c.AbortWithStatus(http.StatusUnauthorized) | ||
26 | return | ||
27 | } | ||
28 | |||
29 | c.Next() | ||
30 | } | ||
31 | |||
32 | func GetAPIAuthUser(c *gin.Context) string { | ||
33 | return c.GetString(API_AUTH_USER_KEY) | ||
34 | } | ||