2018-09-08 12:08:07 +00:00
|
|
|
package netcup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2018-09-08 12:08:07 +00:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
|
|
|
)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// defaultBaseURL for reaching the jSON-based API-Endpoint of netcup
|
|
|
|
const defaultBaseURL = "https://ccp.netcup.net/run/webservice/servers/endpoint.php?JSON"
|
2018-09-08 12:08:07 +00:00
|
|
|
|
|
|
|
// success response status
|
|
|
|
const success = "success"
|
|
|
|
|
|
|
|
// Request wrapper as specified in netcup wiki
|
|
|
|
// needed for every request to netcup API around *Msg
|
|
|
|
// https://www.netcup-wiki.de/wiki/CCP_API#Anmerkungen_zu_JSON-Requests
|
|
|
|
type Request struct {
|
|
|
|
Action string `json:"action"`
|
|
|
|
Param interface{} `json:"param"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginMsg as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#login
|
|
|
|
type LoginMsg struct {
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APIPassword string `json:"apipassword"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogoutMsg as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#logout
|
|
|
|
type LogoutMsg struct {
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDNSRecordsMsg as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#updateDnsRecords
|
|
|
|
type UpdateDNSRecordsMsg struct {
|
|
|
|
DomainName string `json:"domainname"`
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
DNSRecordSet DNSRecordSet `json:"dnsrecordset"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRecordSet as specified in netcup WSDL
|
|
|
|
// needed in UpdateDNSRecordsMsg
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Dnsrecordset
|
|
|
|
type DNSRecordSet struct {
|
|
|
|
DNSRecords []DNSRecord `json:"dnsrecords"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfoDNSRecordsMsg as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#infoDnsRecords
|
|
|
|
type InfoDNSRecordsMsg struct {
|
|
|
|
DomainName string `json:"domainname"`
|
|
|
|
CustomerNumber string `json:"customernumber"`
|
|
|
|
APIKey string `json:"apikey"`
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRecord as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Dnsrecord
|
|
|
|
type DNSRecord struct {
|
|
|
|
ID int `json:"id,string,omitempty"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
RecordType string `json:"type"`
|
|
|
|
Priority string `json:"priority,omitempty"`
|
|
|
|
Destination string `json:"destination"`
|
|
|
|
DeleteRecord bool `json:"deleterecord,omitempty"`
|
|
|
|
State string `json:"state,omitempty"`
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL int `json:"ttl,omitempty"`
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResponseMsg as specified in netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php#Responsemessage
|
|
|
|
type ResponseMsg struct {
|
|
|
|
ServerRequestID string `json:"serverrequestid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
StatusCode int `json:"statuscode"`
|
|
|
|
ShortMessage string `json:"shortmessage"`
|
|
|
|
LongMessage string `json:"longmessage"`
|
|
|
|
ResponseData ResponseData `json:"responsedata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogoutResponseMsg similar to ResponseMsg
|
|
|
|
// allows empty ResponseData field whilst unmarshaling
|
|
|
|
type LogoutResponseMsg struct {
|
|
|
|
ServerRequestID string `json:"serverrequestid"`
|
|
|
|
ClientRequestID string `json:"clientrequestid,omitempty"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
StatusCode int `json:"statuscode"`
|
|
|
|
ShortMessage string `json:"shortmessage"`
|
|
|
|
LongMessage string `json:"longmessage"`
|
|
|
|
ResponseData string `json:"responsedata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResponseData to enable correct unmarshaling of ResponseMsg
|
|
|
|
type ResponseData struct {
|
|
|
|
APISessionID string `json:"apisessionid"`
|
|
|
|
DNSRecords []DNSRecord `json:"dnsrecords"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client netcup DNS client
|
|
|
|
type Client struct {
|
|
|
|
customerNumber string
|
|
|
|
apiKey string
|
|
|
|
apiPassword string
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient *http.Client
|
|
|
|
BaseURL string
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a netcup DNS client
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewClient(customerNumber string, apiKey string, apiPassword string) *Client {
|
2018-09-08 12:08:07 +00:00
|
|
|
return &Client{
|
|
|
|
customerNumber: customerNumber,
|
|
|
|
apiKey: apiKey,
|
|
|
|
apiPassword: apiPassword,
|
2018-09-15 17:07:24 +00:00
|
|
|
BaseURL: defaultBaseURL,
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
},
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login performs the login as specified by the netcup WSDL
|
|
|
|
// returns sessionID needed to perform remaining actions
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) Login() (string, error) {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "login",
|
|
|
|
Param: &LoginMsg{
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APIPassword: c.apiPassword,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.sendRequest(payload)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return "", fmt.Errorf("error sending request to DNS-API, %v", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r ResponseMsg
|
|
|
|
|
|
|
|
err = json.Unmarshal(response, &r)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return "", fmt.Errorf("error decoding response of DNS-API, %v", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
if r.Status != success {
|
2018-09-15 17:07:24 +00:00
|
|
|
return "", fmt.Errorf("error logging into DNS-API, %v", r.LongMessage)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
return r.ResponseData.APISessionID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout performs the logout with the supplied sessionID as specified by the netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) Logout(sessionID string) error {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "logout",
|
|
|
|
Param: &LogoutMsg{
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: sessionID,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.sendRequest(payload)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("error logging out of DNS-API: %v", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r LogoutResponseMsg
|
|
|
|
|
|
|
|
err = json.Unmarshal(response, &r)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("error logging out of DNS-API: %v", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status != success {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("error logging out of DNS-API: %v", r.ShortMessage)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateDNSRecord performs an update of the DNSRecords as specified by the netcup WSDL
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) UpdateDNSRecord(sessionID, domainName string, record DNSRecord) error {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "updateDnsRecords",
|
|
|
|
Param: UpdateDNSRecordsMsg{
|
|
|
|
DomainName: domainName,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: sessionID,
|
|
|
|
ClientRequestID: "",
|
|
|
|
DNSRecordSet: DNSRecordSet{DNSRecords: []DNSRecord{record}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.sendRequest(payload)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r ResponseMsg
|
|
|
|
|
|
|
|
err = json.Unmarshal(response, &r)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status != success {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("%s: %+v", r.ShortMessage, r)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSRecords retrieves all dns records of an DNS-Zone as specified by the netcup WSDL
|
|
|
|
// returns an array of DNSRecords
|
|
|
|
// https://ccp.netcup.net/run/webservice/servers/endpoint.php
|
|
|
|
func (c *Client) GetDNSRecords(hostname, apiSessionID string) ([]DNSRecord, error) {
|
|
|
|
payload := &Request{
|
|
|
|
Action: "infoDnsRecords",
|
|
|
|
Param: InfoDNSRecordsMsg{
|
|
|
|
DomainName: hostname,
|
|
|
|
CustomerNumber: c.customerNumber,
|
|
|
|
APIKey: c.apiKey,
|
|
|
|
APISessionID: apiSessionID,
|
|
|
|
ClientRequestID: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := c.sendRequest(payload)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var r ResponseMsg
|
|
|
|
|
|
|
|
err = json.Unmarshal(response, &r)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status != success {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("%s", r.ShortMessage)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
return r.ResponseData.DNSRecords, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendRequest marshals given body to JSON, send the request to netcup API
|
|
|
|
// and returns body of response
|
|
|
|
func (c *Client) sendRequest(payload interface{}) ([]byte, error) {
|
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
req, err := http.NewRequest(http.MethodPost, c.BaseURL, bytes.NewReader(body))
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
req.Close = true
|
|
|
|
|
|
|
|
req.Header.Set("content-type", "application/json")
|
|
|
|
req.Header.Set("User-Agent", acme.UserAgent)
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
resp, err := c.HTTPClient.Do(req)
|
2018-09-08 12:08:07 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, err
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode > 299 {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("API request failed with HTTP Status code %d", resp.StatusCode)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("read of response body failed, %v", err)
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDNSRecordIdx searches a given array of DNSRecords for a given DNSRecord
|
|
|
|
// equivalence is determined by Destination and RecortType attributes
|
|
|
|
// returns index of given DNSRecord in given array of DNSRecords
|
|
|
|
func GetDNSRecordIdx(records []DNSRecord, record DNSRecord) (int, error) {
|
|
|
|
for index, element := range records {
|
|
|
|
if record.Destination == element.Destination && record.RecordType == element.RecordType {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
return -1, fmt.Errorf("no DNS Record found")
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateTxtRecord uses the supplied values to return a DNSRecord of type TXT for the dns-01 challenge
|
2018-09-15 17:07:24 +00:00
|
|
|
func CreateTxtRecord(hostname, value string, ttl int) DNSRecord {
|
2018-09-08 12:08:07 +00:00
|
|
|
return DNSRecord{
|
|
|
|
ID: 0,
|
|
|
|
Hostname: hostname,
|
|
|
|
RecordType: "TXT",
|
|
|
|
Priority: "",
|
|
|
|
Destination: value,
|
|
|
|
DeleteRecord: false,
|
|
|
|
State: "",
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: ttl,
|
2018-09-08 12:08:07 +00:00
|
|
|
}
|
|
|
|
}
|