summaryrefslogtreecommitdiff
path: root/web/controllers/dns_manage.go
blob: 6db0b13fd914c531dbf9798706e3af2b2e2dcf41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/miekg/dns"

	"code.crute.me/mcrute/go_ddns_manager/bind"
	"code.crute.me/mcrute/go_ddns_manager/util"
	"code.crute.me/mcrute/go_ddns_manager/web/middleware"
)

func DnsManageRoot(c *gin.Context) {
	c.IndentedJSON(http.StatusOK, gin.H{
		"_rels": gin.H{
			"zones": util.MakeURL(c.Request, "/dns/zone").String(),
			"views": util.MakeURL(c.Request, "/dns/view").String(),
		},
	})
}

func DnsViews(c *gin.Context) {
	cfg := middleware.GetServerConfig(c)
	out := map[string]map[string]string{}

	for _, v := range cfg.BindConfig.Views() {
		k := map[string]string{}
		out[v] = k
		for _, z := range cfg.BindConfig.ZonesInView(v) {
			zn := bind.DeCanonicalize(z.Name)
			k[zn] = util.MakeURL(c.Request, "/dns/zone/%s/%s", v, zn).String()
		}
	}

	c.IndentedJSON(http.StatusOK, gin.H{"views": out})
}

func DnsZones(c *gin.Context) {

}

func DnsZoneDetails(c *gin.Context) {
	cfg := middleware.GetServerConfig(c)

	z := cfg.BindConfig.Zone(c.Param("view"), c.Param("name"))
	if z == nil {
		c.JSON(http.StatusNotFound, gin.H{
			"error": "Zone not found",
		})
		return
	}

	envs := []*dns.Envelope{}
	ec, err := cfg.DNSClient.AXFR(z)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}

	for e := range ec {
		envs = append(envs, e)
	}

	c.JSON(http.StatusOK, envs)
}