2018-09-15 17:07:24 +00:00
|
|
|
package glesys
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/log"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// types for JSON method calls, parameters, and responses
|
|
|
|
|
|
|
|
type addRecordRequest struct {
|
|
|
|
DomainName string `json:"domainname"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
TTL int `json:"ttl,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteRecordRequest struct {
|
|
|
|
RecordID int `json:"recordid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseStruct struct {
|
|
|
|
Response struct {
|
|
|
|
Status struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
} `json:"status"`
|
|
|
|
Record deleteRecordRequest `json:"record"`
|
|
|
|
} `json:"response"`
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func (d *DNSProvider) addTXTRecord(fqdn, domain, name, value string, ttl int) (int, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
response, err := d.sendRequest(http.MethodPost, "addrecord", addRecordRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
Host: name,
|
|
|
|
Type: "TXT",
|
|
|
|
Data: value,
|
|
|
|
TTL: ttl,
|
|
|
|
})
|
|
|
|
|
|
|
|
if response != nil && response.Response.Status.Code == http.StatusOK {
|
|
|
|
log.Infof("[%s]: Successfully created record id %d", fqdn, response.Response.Record.RecordID)
|
|
|
|
return response.Response.Record.RecordID, nil
|
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) deleteTXTRecord(fqdn string, recordid int) error {
|
|
|
|
response, err := d.sendRequest(http.MethodPost, "deleterecord", deleteRecordRequest{
|
|
|
|
RecordID: recordid,
|
|
|
|
})
|
|
|
|
if response != nil && response.Response.Status.Code == 200 {
|
|
|
|
log.Infof("[%s]: Successfully deleted record id %d", fqdn, recordid)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-09 23:48:18 +00:00
|
|
|
func (d *DNSProvider) sendRequest(method, resource string, payload interface{}) (*responseStruct, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", defaultBaseURL, resource)
|
|
|
|
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, url, bytes.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.SetBasicAuth(d.config.APIUser, d.config.APIKey)
|
|
|
|
|
|
|
|
resp, err := d.config.HTTPClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
return nil, fmt.Errorf("request failed with HTTP status code %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
var response responseStruct
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
|
|
|
|
|
|
|
return &response, err
|
|
|
|
}
|