🐞 HTTP server returns body confirming change

Previously we weren't returning a response when `acme.sh` updated our
TXT record, but the acme-dns endpoint specifies a
[response](https://github.com/joohoi/acme-dns#response), and acme.sh
expects [a
response](b7a3fe05a4/dnsapi/dns_acmedns.sh (L38)).

fixes:
```
[Mon Jan 18 19:09:26 UTC 2021] invalid response of acme-dns
[Mon Jan 18 19:09:26 UTC 2021] Error add txt for domain:_acme-challenge.34-83-219-164.sslip.io
```
This commit is contained in:
Brian Cunnie
2021-01-18 14:19:06 -08:00
parent a346b7d668
commit 6d0d3b9be5

View File

@@ -17,7 +17,7 @@ var txt = `Set this TXT record: curl -X POST http://localhost/update -d '{"txt"
// Txt is for parsing the JSON POST to set the DNS TXT record // Txt is for parsing the JSON POST to set the DNS TXT record
type Txt struct { type Txt struct {
Txt string Txt string `json:"txt"`
} }
func main() { func main() {
@@ -111,26 +111,36 @@ func usageHandler(w http.ResponseWriter, r *http.Request) {
} }
func updateTxtHandler(w http.ResponseWriter, r *http.Request) { func updateTxtHandler(w http.ResponseWriter, r *http.Request) {
var err error
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
err := errors.New("/update requires POST method, not " + r.Method + " method") err = errors.New("/update requires POST method, not " + r.Method + " method")
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
log.Println(err.Error()) log.Println(err.Error())
return return
} }
body, err := ioutil.ReadAll(r.Body) var body []byte
if err != nil { if body, err = ioutil.ReadAll(r.Body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(err.Error()) log.Println(err.Error())
return return
} }
var updateTxt Txt var updateTxt Txt
err = json.Unmarshal(body, &updateTxt) if err := json.Unmarshal(body, &updateTxt); err != nil {
if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(err.Error())
return
}
if body, err = json.Marshal(updateTxt); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(err.Error())
return
}
if _, err = fmt.Fprintf(w, string(body)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(err.Error()) log.Println(err.Error())
return return
} }
// this is the money shot, where we update the DNS TXT record to what was in the POST request
log.Println("Updating TXT record from \"" + txt + "\" → \"" + updateTxt.Txt + "\".") log.Println("Updating TXT record from \"" + txt + "\" → \"" + updateTxt.Txt + "\".")
// this is the money shot, where we update the DNS TXT record to what was in the POST request
txt = updateTxt.Txt txt = updateTxt.Txt
} }