-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (89 loc) · 2.32 KB
/
main.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"github.com/evsio0n/log"
"github.com/gin-gonic/gin"
"net/http"
"os/exec"
)
type DnsAddReqParams struct {
Zone string `json:"zone"`
Hostname string `json:"hostname"`
Value string `json:"value"`
RecordType string `json:"record_type"`
}
type DnsDelReqParams struct {
Zone string `json:"zone"`
Hostname string `json:"hostname"`
RecordType string `json:"record_type"`
}
func main() {
gin.SetMode(gin.ReleaseMode)
r := runConfig()
//disable gin output
//run on internal port 8080
r.Run("172.17.6.16:8880")
}
func runConfig() *gin.Engine {
r := gin.New()
r.POST("/set", handleSet)
r.POST("/del", handleDel)
return r
}
func handleSet(c *gin.Context) {
//get json data
var dnsResponse DnsAddReqParams
err := c.ShouldBindJSON(&dnsResponse)
if err == nil {
//set dns record
err = SetDnsRecord(dnsResponse.Zone, dnsResponse.Hostname, dnsResponse.Value, dnsResponse.RecordType)
if err == nil {
c.JSON(200, gin.H{
"message": "success",
})
return
}
}
log.Info(err.Error())
c.JSON(http.StatusInternalServerError, gin.H{
"message": "fail",
"err": err.Error(),
})
}
func handleDel(c *gin.Context) {
//get json data
var dnsResponse DnsDelReqParams
err := c.ShouldBindJSON(&dnsResponse)
if err == nil {
//set dns record
err = DelDnsRecord(dnsResponse.Zone, dnsResponse.Hostname, dnsResponse.RecordType)
if err == nil {
c.JSON(200, gin.H{
"message": "success",
})
return
}
}
c.JSON(http.StatusInternalServerError, gin.H{
"message": "fail",
"err": err.Error(),
})
}
func SetDnsRecord(zone string, hostname string, value string, recordType string) error {
// run cmd on Windows
//eg: dnscmd . /RecordAdd {zone} {hostname} {record type} {IP address}
cmd := exec.Command("cmd", "/C", "dnscmd . /RecordAdd "+zone+" "+hostname+" "+recordType+" "+value)
//get output and print
//seeing if exit code is 0
output, err := cmd.CombinedOutput()
log.Info(string(output))
return err
}
func DelDnsRecord(zone string, hostname string, recordType string) error {
// run cmd on Windows
//eg: dnscmd . /RecordDelete {zone} {hostname} {record type}
cmd := exec.Command("cmd", "/C", "dnscmd . /RecordDelete "+zone+" "+hostname+" "+recordType+" /f")
// run cmd and enter y to confirm
output, err := cmd.CombinedOutput()
log.Info(string(output))
return err
}