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
|
package controllers
import (
"fmt"
"log"
"net/http"
"code.crute.me/mcrute/go_ddns_manager/dns"
"code.crute.me/mcrute/go_ddns_manager/web/middleware"
"github.com/gin-gonic/gin"
)
func CreateAcmeChallengeV2(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
}
if err := cfg.DNSClient.WaitForDNSPropagation(
c.Request.Context(),
fmt.Sprintf("_acme-challenge.%s.", prefix),
ch.Challenge,
); err != nil {
jsonError(c, http.StatusInternalServerError, fmt.Errorf("Error polling for DNS propagation: %w", err))
return
}
c.JSON(http.StatusCreated, "")
}
func DeleteAcmeChallengeV2(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).Remove(&dns.TXT{
Name: joinDomainParts("_acme-challenge", prefix),
Ttl: 5,
Txt: []string{ch.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, "")
}
|