Update lego.
This commit is contained in:
parent
63c3ed3931
commit
0034bef6b9
27 changed files with 1663 additions and 14 deletions
197
vendor/github.com/go-acme/lego/providers/dns/joker/client.go
generated
vendored
Normal file
197
vendor/github.com/go-acme/lego/providers/dns/joker/client.go
generated
vendored
Normal file
|
@ -0,0 +1,197 @@
|
|||
package joker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-acme/lego/challenge/dns01"
|
||||
"github.com/go-acme/lego/log"
|
||||
)
|
||||
|
||||
const defaultBaseURL = "https://dmapi.joker.com/request/"
|
||||
|
||||
// Joker DMAPI Response
|
||||
type response struct {
|
||||
Headers url.Values
|
||||
Body string
|
||||
StatusCode int
|
||||
StatusText string
|
||||
AuthSid string
|
||||
}
|
||||
|
||||
// parseResponse parses HTTP response body
|
||||
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
|
||||
}
|
||||
|
||||
// login performs a login to Joker's DMAPI
|
||||
func (d *DNSProvider) login() (*response, error) {
|
||||
if d.config.AuthSid != "" {
|
||||
// already logged in
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
response, err := d.postRequest("login", url.Values{"api-key": {d.config.APIKey}})
|
||||
if err != nil {
|
||||
return response, err
|
||||
}
|
||||
|
||||
if response == nil {
|
||||
return nil, fmt.Errorf("login returned nil response")
|
||||
}
|
||||
|
||||
if response.AuthSid == "" {
|
||||
return response, fmt.Errorf("login did not return valid Auth-Sid")
|
||||
}
|
||||
|
||||
d.config.AuthSid = response.AuthSid
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// logout closes authenticated session with Joker's DMAPI
|
||||
func (d *DNSProvider) logout() (*response, error) {
|
||||
if d.config.AuthSid == "" {
|
||||
return nil, fmt.Errorf("already logged out")
|
||||
}
|
||||
|
||||
response, err := d.postRequest("logout", url.Values{})
|
||||
if err == nil {
|
||||
d.config.AuthSid = ""
|
||||
}
|
||||
return response, err
|
||||
}
|
||||
|
||||
// getZone returns content of DNS zone for domain
|
||||
func (d *DNSProvider) getZone(domain string) (*response, error) {
|
||||
if d.config.AuthSid == "" {
|
||||
return nil, fmt.Errorf("must be logged in to get zone")
|
||||
}
|
||||
|
||||
return d.postRequest("dns-zone-get", url.Values{"domain": {dns01.UnFqdn(domain)}})
|
||||
}
|
||||
|
||||
// putZone uploads DNS zone to Joker DMAPI
|
||||
func (d *DNSProvider) putZone(domain, zone string) (*response, error) {
|
||||
if d.config.AuthSid == "" {
|
||||
return nil, fmt.Errorf("must be logged in to put zone")
|
||||
}
|
||||
|
||||
return d.postRequest("dns-zone-put", url.Values{"domain": {dns01.UnFqdn(domain)}, "zone": {strings.TrimSpace(zone)}})
|
||||
}
|
||||
|
||||
// postRequest performs actual HTTP request
|
||||
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
|
||||
}
|
||||
|
||||
// Temporary workaround, until it get fixed on API side
|
||||
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, " ")
|
||||
}
|
||||
|
||||
// removeTxtEntryFromZone clean-ups all TXT records with given name
|
||||
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
|
||||
}
|
||||
|
||||
// addTxtEntryToZone returns DNS zone with added TXT record
|
||||
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"))
|
||||
}
|
174
vendor/github.com/go-acme/lego/providers/dns/joker/joker.go
generated
vendored
Normal file
174
vendor/github.com/go-acme/lego/providers/dns/joker/joker.go
generated
vendored
Normal file
|
@ -0,0 +1,174 @@
|
|||
// Package joker implements a DNS provider for solving the DNS-01 challenge using joker.com DMAPI.
|
||||
package joker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-acme/lego/challenge/dns01"
|
||||
"github.com/go-acme/lego/log"
|
||||
"github.com/go-acme/lego/platform/config/env"
|
||||
)
|
||||
|
||||
// Config is used to configure the creation of the DNSProvider.
|
||||
type Config struct {
|
||||
Debug bool
|
||||
BaseURL string
|
||||
APIKey string
|
||||
PropagationTimeout time.Duration
|
||||
PollingInterval time.Duration
|
||||
TTL int
|
||||
HTTPClient *http.Client
|
||||
AuthSid string
|
||||
}
|
||||
|
||||
// NewDefaultConfig returns a default configuration for the DNSProvider
|
||||
func NewDefaultConfig() *Config {
|
||||
return &Config{
|
||||
BaseURL: defaultBaseURL,
|
||||
Debug: env.GetOrDefaultBool("JOKER_DEBUG", false),
|
||||
TTL: env.GetOrDefaultInt("JOKER_TTL", dns01.DefaultTTL),
|
||||
PropagationTimeout: env.GetOrDefaultSecond("JOKER_PROPAGATION_TIMEOUT", dns01.DefaultPropagationTimeout),
|
||||
PollingInterval: env.GetOrDefaultSecond("JOKER_POLLING_INTERVAL", dns01.DefaultPollingInterval),
|
||||
HTTPClient: &http.Client{
|
||||
Timeout: env.GetOrDefaultSecond("JOKER_HTTP_TIMEOUT", 60*time.Second),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// DNSProvider is an implementation of the ChallengeProviderTimeout interface
|
||||
// that uses Joker's DMAPI to manage TXT records for a domain.
|
||||
type DNSProvider struct {
|
||||
config *Config
|
||||
}
|
||||
|
||||
// NewDNSProvider returns a DNSProvider instance configured for Joker DMAPI.
|
||||
// Credentials must be passed in the environment variable JOKER_API_KEY.
|
||||
func NewDNSProvider() (*DNSProvider, error) {
|
||||
values, err := env.Get("JOKER_API_KEY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("joker: %v", err)
|
||||
}
|
||||
|
||||
config := NewDefaultConfig()
|
||||
config.APIKey = values["JOKER_API_KEY"]
|
||||
|
||||
return NewDNSProviderConfig(config)
|
||||
}
|
||||
|
||||
// NewDNSProviderConfig return a DNSProvider instance configured for Joker DMAPI.
|
||||
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
||||
if config == nil {
|
||||
return nil, errors.New("joker: the configuration of the DNS provider is nil")
|
||||
}
|
||||
|
||||
if config.APIKey == "" {
|
||||
return nil, fmt.Errorf("joker: credentials missing")
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(config.BaseURL, "/") {
|
||||
config.BaseURL += "/"
|
||||
}
|
||||
|
||||
return &DNSProvider{config: config}, nil
|
||||
}
|
||||
|
||||
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
||||
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
||||
return d.config.PropagationTimeout, d.config.PollingInterval
|
||||
}
|
||||
|
||||
// Present installs a TXT record for the DNS challenge.
|
||||
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
||||
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
||||
|
||||
zone, err := dns01.FindZoneByFqdn(fqdn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("joker: %v", err)
|
||||
}
|
||||
|
||||
relative := getRelative(fqdn, zone)
|
||||
|
||||
if d.config.Debug {
|
||||
log.Infof("[%s] joker: adding TXT record %q to zone %q with value %q", domain, relative, zone, value)
|
||||
}
|
||||
|
||||
response, err := d.login()
|
||||
if err != nil {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
|
||||
response, err = d.getZone(zone)
|
||||
if err != nil || response.StatusCode != 0 {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
|
||||
dnsZone := addTxtEntryToZone(response.Body, relative, value, d.config.TTL)
|
||||
|
||||
response, err = d.putZone(zone, dnsZone)
|
||||
if err != nil || response.StatusCode != 0 {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CleanUp removes a TXT record used for a previous DNS challenge.
|
||||
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
||||
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
||||
|
||||
zone, err := dns01.FindZoneByFqdn(fqdn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("joker: %v", err)
|
||||
}
|
||||
|
||||
relative := getRelative(fqdn, zone)
|
||||
|
||||
if d.config.Debug {
|
||||
log.Infof("[%s] joker: removing entry %q from zone %q", domain, relative, zone)
|
||||
}
|
||||
|
||||
response, err := d.login()
|
||||
if err != nil {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// Try to logout in case of errors
|
||||
_, _ = d.logout()
|
||||
}()
|
||||
|
||||
response, err = d.getZone(zone)
|
||||
if err != nil || response.StatusCode != 0 {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
|
||||
dnsZone, modified := removeTxtEntryFromZone(response.Body, relative)
|
||||
if modified {
|
||||
response, err = d.putZone(zone, dnsZone)
|
||||
if err != nil || response.StatusCode != 0 {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
}
|
||||
|
||||
response, err = d.logout()
|
||||
if err != nil {
|
||||
return formatResponseError(response, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getRelative(fqdn, zone string) string {
|
||||
return dns01.UnFqdn(strings.TrimSuffix(fqdn, dns01.ToFqdn(zone)))
|
||||
}
|
||||
|
||||
// formatResponseError formats error with optional details from DMAPI response
|
||||
func formatResponseError(response *response, err error) error {
|
||||
if response != nil {
|
||||
return fmt.Errorf("joker: DMAPI error: %v Response: %v", err, response.Headers)
|
||||
}
|
||||
return fmt.Errorf("joker: DMAPI error: %v", err)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue