2018-12-06 21:50:17 +00:00
|
|
|
package mydnsjp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-25 09:44:11 +00:00
|
|
|
"io"
|
2018-12-06 21:50:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func (d *DNSProvider) doRequest(domain, value, cmd string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
req, err := d.buildRequest(domain, value, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("error querying API: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2022-09-02 07:05:52 +00:00
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
2018-12-06 21:50:17 +00:00
|
|
|
var content []byte
|
2021-08-25 09:44:11 +00:00
|
|
|
content, err = io.ReadAll(resp.Body)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("request %s failed [status code %d]: %s", req.URL, resp.StatusCode, string(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func (d *DNSProvider) buildRequest(domain, value, cmd string) (*http.Request, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("CERTBOT_DOMAIN", domain)
|
|
|
|
params.Set("CERTBOT_VALIDATION", value)
|
|
|
|
params.Set("EDIT_CMD", cmd)
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, defaultBaseURL, strings.NewReader(params.Encode()))
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("invalid request: %w", err)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
req.SetBasicAuth(d.config.MasterID, d.config.Password)
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|