lego/providers/dns/joker/client.go

212 lines
4.8 KiB
Go
Raw Normal View History

2019-04-28 12:33:50 +00:00
package joker
import (
2020-02-27 18:14:46 +00:00
"errors"
2019-04-28 12:33:50 +00:00
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
2020-09-02 01:20:01 +00:00
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/log"
2019-04-28 12:33:50 +00:00
)
const defaultBaseURL = "https://dmapi.joker.com/request/"
2020-05-08 17:35:25 +00:00
// Joker DMAPI Response.
2019-04-28 12:33:50 +00:00
type response struct {
Headers url.Values
Body string
StatusCode int
StatusText string
AuthSid string
}
2020-05-08 17:35:25 +00:00
// parseResponse parses HTTP response body.
2019-04-28 12:33:50 +00:00
func parseResponse(message string) *response {
r := &response{Headers: url.Values{}, StatusCode: -1}
parts := strings.SplitN(message, "\n\n", 2)
for _, line := range strings.Split(parts[0], "\n") {
if strings.TrimSpace(line) == "" {
continue
}
kv := strings.SplitN(line, ":", 2)
val := ""
if len(kv) == 2 {
val = strings.TrimSpace(kv[1])
}
r.Headers.Add(kv[0], val)
switch kv[0] {
case "Status-Code":
i, err := strconv.Atoi(val)
if err == nil {
r.StatusCode = i
}
case "Status-Text":
r.StatusText = val
case "Auth-Sid":
r.AuthSid = val
}
}
if len(parts) > 1 {
r.Body = parts[1]
}
return r
}
2020-05-08 17:35:25 +00:00
// login performs a login to Joker's DMAPI.
2019-04-28 12:33:50 +00:00
func (d *DNSProvider) login() (*response, error) {
if d.config.AuthSid != "" {
// already logged in
return nil, nil
}
var values url.Values
switch {
case d.config.Username != "" && d.config.Password != "":
values = url.Values{
"username": {d.config.Username},
"password": {d.config.Password},
}
case d.config.APIKey != "":
values = url.Values{"api-key": {d.config.APIKey}}
default:
2020-02-27 18:14:46 +00:00
return nil, errors.New("no username and password or api-key")
}
response, err := d.postRequest("login", values)
2019-04-28 12:33:50 +00:00
if err != nil {
return response, err
}
if response == nil {
2020-02-27 18:14:46 +00:00
return nil, errors.New("login returned nil response")
2019-04-28 12:33:50 +00:00
}
if response.AuthSid == "" {
2020-02-27 18:14:46 +00:00
return response, errors.New("login did not return valid Auth-Sid")
2019-04-28 12:33:50 +00:00
}
d.config.AuthSid = response.AuthSid
return response, nil
}
2020-05-08 17:35:25 +00:00
// logout closes authenticated session with Joker's DMAPI.
2019-04-28 12:33:50 +00:00
func (d *DNSProvider) logout() (*response, error) {
if d.config.AuthSid == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("already logged out")
2019-04-28 12:33:50 +00:00
}
response, err := d.postRequest("logout", url.Values{})
if err == nil {
d.config.AuthSid = ""
}
return response, err
}
2020-05-08 17:35:25 +00:00
// getZone returns content of DNS zone for domain.
2019-04-28 12:33:50 +00:00
func (d *DNSProvider) getZone(domain string) (*response, error) {
if d.config.AuthSid == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("must be logged in to get zone")
2019-04-28 12:33:50 +00:00
}
return d.postRequest("dns-zone-get", url.Values{"domain": {dns01.UnFqdn(domain)}})
}
2020-05-08 17:35:25 +00:00
// putZone uploads DNS zone to Joker DMAPI.
2019-04-28 12:33:50 +00:00
func (d *DNSProvider) putZone(domain, zone string) (*response, error) {
if d.config.AuthSid == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("must be logged in to put zone")
2019-04-28 12:33:50 +00:00
}
return d.postRequest("dns-zone-put", url.Values{"domain": {dns01.UnFqdn(domain)}, "zone": {strings.TrimSpace(zone)}})
}
2020-05-08 17:35:25 +00:00
// postRequest performs actual HTTP request.
2019-04-28 12:33:50 +00:00
func (d *DNSProvider) postRequest(cmd string, data url.Values) (*response, error) {
uri := d.config.BaseURL + cmd
if d.config.AuthSid != "" {
data.Set("auth-sid", d.config.AuthSid)
}
if d.config.Debug {
log.Infof("postRequest:\n\tURL: %q\n\tData: %v", uri, data)
}
resp, err := d.config.HTTPClient.PostForm(uri, data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP error %d [%s]: %v", resp.StatusCode, http.StatusText(resp.StatusCode), string(body))
}
return parseResponse(string(body)), nil
}
2020-05-08 17:35:25 +00:00
// Temporary workaround, until it get fixed on API side.
2019-04-28 12:33:50 +00:00
func fixTxtLines(line string) string {
fields := strings.Fields(line)
if len(fields) < 6 || fields[1] != "TXT" {
return line
}
if fields[3][0] == '"' && fields[4] == `"` {
fields[3] = strings.TrimSpace(fields[3]) + `"`
fields = append(fields[:4], fields[5:]...)
}
return strings.Join(fields, " ")
}
2020-05-08 17:35:25 +00:00
// removeTxtEntryFromZone clean-ups all TXT records with given name.
2019-04-28 12:33:50 +00:00
func removeTxtEntryFromZone(zone, relative string) (string, bool) {
prefix := fmt.Sprintf("%s TXT 0 ", relative)
modified := false
var zoneEntries []string
for _, line := range strings.Split(zone, "\n") {
if strings.HasPrefix(line, prefix) {
modified = true
continue
}
zoneEntries = append(zoneEntries, line)
}
return strings.TrimSpace(strings.Join(zoneEntries, "\n")), modified
}
2020-05-08 17:35:25 +00:00
// addTxtEntryToZone returns DNS zone with added TXT record.
2019-04-28 12:33:50 +00:00
func addTxtEntryToZone(zone, relative, value string, ttl int) string {
var zoneEntries []string
for _, line := range strings.Split(zone, "\n") {
zoneEntries = append(zoneEntries, fixTxtLines(line))
}
newZoneEntry := fmt.Sprintf("%s TXT 0 %q %d", relative, value, ttl)
zoneEntries = append(zoneEntries, newZoneEntry)
return strings.TrimSpace(strings.Join(zoneEntries, "\n"))
}