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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
package controllers
import (
"encoding/base64"
"encoding/json"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"code.crute.me/mcrute/go_ddns_manager/dns"
"code.crute.me/mcrute/go_ddns_manager/util"
"code.crute.me/mcrute/go_ddns_manager/web/middleware"
)
type AcmeChallenge struct {
Zone string `json:"zone" binding:"required"`
Challenge string `json:"challenge" binding:"required"`
}
type AcmeChallengeID struct {
Zone string
Prefix string
Challenge string
}
func DecodeAcmeChallengeID(v string) *AcmeChallengeID {
rid, err := base64.URLEncoding.DecodeString(v)
if err != nil {
return nil
}
var id AcmeChallengeID
if err = json.Unmarshal(rid, &id); err != nil {
return nil
}
return &id
}
func (i AcmeChallengeID) Encode() string {
id, err := json.Marshal(i)
if err != nil {
return ""
}
return base64.URLEncoding.EncodeToString(id)
}
func joinDomainParts(parts ...string) string {
p := []string{}
for _, i := range parts {
if strings.TrimSpace(i) != "" {
p = append(p, i)
}
}
return strings.Join(p, ".")
}
func jsonError(c *gin.Context, status int, msg interface{}) {
var o string
switch msg := msg.(type) {
case string:
o = msg
case error:
o = msg.Error()
}
c.JSON(status, gin.H{"error": o})
}
func CreateAcmeChallenge(c *gin.Context) {
cfg := middleware.GetServerConfig(c)
var ch AcmeChallenge
if err := c.ShouldBindJSON(&ch); err != nil {
jsonError(c, http.StatusBadRequest, err)
return
}
zone, prefix := cfg.BindConfig.FindClosestZone(ch.Zone, cfg.AcmeView)
if zone == nil {
jsonError(c, http.StatusNotFound, "Zone not found")
return
}
if !cfg.IsAcmeClientAllowed(middleware.GetAcmeAuthContext(c), zone.Name) {
jsonError(c, http.StatusForbidden, "Zone update not allowed")
return
}
txn := cfg.DNSClient.StartUpdate(zone).Upsert(&dns.TXT{
Name: joinDomainParts("_acme-challenge", prefix),
Ttl: 5,
Txt: []string{ch.Challenge},
})
if err := cfg.DNSClient.SendUpdate(txn); err != nil {
log.Printf("error Insert: %s", err)
jsonError(c, http.StatusInternalServerError, err)
return
}
url := util.MakeURL(c.Request, "/acme/%s", AcmeChallengeID{
Zone: zone.Name,
Prefix: prefix,
Challenge: ch.Challenge,
}.Encode()).String()
c.Writer.Header().Set("Location", url)
c.JSON(http.StatusCreated, gin.H{"created": url})
}
func DeleteAcmeChallenge(c *gin.Context) {
cfg := middleware.GetServerConfig(c)
id := DecodeAcmeChallengeID(c.Param("id"))
if id == nil {
jsonError(c, http.StatusBadRequest, "unable to decode ID")
return
}
zone := cfg.BindConfig.Zone(cfg.AcmeView, id.Zone)
if zone == nil {
jsonError(c, http.StatusNotFound, "Zone not found")
return
}
if !cfg.IsAcmeClientAllowed(middleware.GetAcmeAuthContext(c), zone.Name) {
jsonError(c, http.StatusForbidden, "Zone update not allowed")
return
}
txn := cfg.DNSClient.StartUpdate(zone).Remove(&dns.TXT{
Name: joinDomainParts("_acme-challenge", id.Prefix),
Ttl: 5,
Txt: []string{id.Challenge},
})
if err := cfg.DNSClient.SendUpdate(txn); err != nil {
log.Printf("error Remove: %s", err)
jsonError(c, http.StatusInternalServerError, err)
return
}
c.String(http.StatusNoContent, "")
}
|