mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-04-05 08:53:52 -04:00
- change should not affect any existing configurations - change solves issues with dual stack updates (#767) - was unneeded and adds unneeded complexity
172 lines
4.2 KiB
Go
172 lines
4.2 KiB
Go
package namecheap
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"regexp"
|
|
|
|
"github.com/qdm12/ddns-updater/internal/models"
|
|
"github.com/qdm12/ddns-updater/internal/provider/constants"
|
|
"github.com/qdm12/ddns-updater/internal/provider/errors"
|
|
"github.com/qdm12/ddns-updater/internal/provider/headers"
|
|
"github.com/qdm12/ddns-updater/internal/provider/utils"
|
|
"github.com/qdm12/ddns-updater/pkg/publicip/ipversion"
|
|
)
|
|
|
|
type Provider struct {
|
|
domain string
|
|
owner string
|
|
password string
|
|
}
|
|
|
|
func New(data json.RawMessage, domain, owner string) (
|
|
p *Provider, err error) {
|
|
extraSettings := struct {
|
|
Password string `json:"password"`
|
|
}{}
|
|
err = json.Unmarshal(data, &extraSettings)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = validateSettings(domain, extraSettings.Password)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("validating provider specific settings: %w", err)
|
|
}
|
|
|
|
return &Provider{
|
|
domain: domain,
|
|
owner: owner,
|
|
password: extraSettings.Password,
|
|
}, nil
|
|
}
|
|
|
|
var passwordRegex = regexp.MustCompile(`^[a-f0-9]{32}$`)
|
|
|
|
func validateSettings(domain, password string) (err error) {
|
|
err = utils.CheckDomain(domain)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %w", errors.ErrDomainNotValid, err)
|
|
}
|
|
|
|
if !passwordRegex.MatchString(password) {
|
|
return fmt.Errorf("%w: password %q does not match regex %q",
|
|
errors.ErrPasswordNotValid, password, passwordRegex)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Provider) String() string {
|
|
return utils.ToString(p.domain, p.owner, constants.Namecheap, ipversion.IP4)
|
|
}
|
|
|
|
func (p *Provider) Domain() string {
|
|
return p.domain
|
|
}
|
|
|
|
func (p *Provider) Owner() string {
|
|
return p.owner
|
|
}
|
|
|
|
func (p *Provider) IPVersion() ipversion.IPVersion {
|
|
return ipversion.IP4
|
|
}
|
|
|
|
func (p *Provider) IPv6Suffix() netip.Prefix {
|
|
return netip.Prefix{}
|
|
}
|
|
|
|
func (p *Provider) Proxied() bool {
|
|
return false
|
|
}
|
|
|
|
func (p *Provider) BuildDomainName() string {
|
|
return utils.BuildDomainName(p.owner, p.domain)
|
|
}
|
|
|
|
func (p *Provider) HTML() models.HTMLRow {
|
|
return models.HTMLRow{
|
|
Domain: fmt.Sprintf("<a href=\"http://%s\">%s</a>", p.BuildDomainName(), p.BuildDomainName()),
|
|
Owner: p.Owner(),
|
|
Provider: "<a href=\"https://namecheap.com\">Namecheap</a>",
|
|
IPVersion: ipversion.IP4.String(),
|
|
}
|
|
}
|
|
|
|
func setHeaders(request *http.Request) {
|
|
headers.SetUserAgent(request)
|
|
headers.SetAccept(request, "application/xml")
|
|
}
|
|
|
|
func (p *Provider) Update(ctx context.Context, client *http.Client, ip netip.Addr) (newIP netip.Addr, err error) {
|
|
u := url.URL{
|
|
Scheme: "https",
|
|
Host: "dynamicdns.park-your-domain.com",
|
|
Path: "/update",
|
|
}
|
|
values := url.Values{}
|
|
values.Set("host", p.owner)
|
|
values.Set("domain", p.domain)
|
|
values.Set("password", p.password)
|
|
values.Set("ip", ip.String())
|
|
u.RawQuery = values.Encode()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return netip.Addr{}, fmt.Errorf("creating http request: %w", err)
|
|
}
|
|
setHeaders(request)
|
|
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return netip.Addr{}, fmt.Errorf("doing http request: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return netip.Addr{}, fmt.Errorf("%w: %d: %s",
|
|
errors.ErrHTTPStatusNotValid, response.StatusCode, utils.BodyToSingleLine(response.Body))
|
|
}
|
|
|
|
decoder := xml.NewDecoder(response.Body)
|
|
decoder.CharsetReader = func(_ string, input io.Reader) (io.Reader, error) {
|
|
return input, nil
|
|
}
|
|
|
|
var parsedXML struct {
|
|
Errors struct {
|
|
Error string `xml:"errors.Err1"`
|
|
} `xml:"errors"`
|
|
IP string `xml:"IP"`
|
|
}
|
|
err = decoder.Decode(&parsedXML)
|
|
if err != nil {
|
|
return netip.Addr{}, fmt.Errorf("xml decoding response body: %w", err)
|
|
}
|
|
|
|
if parsedXML.Errors.Error != "" {
|
|
return netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrUnsuccessful, parsedXML.Errors.Error)
|
|
}
|
|
|
|
if parsedXML.IP == "" {
|
|
// If XML has not IP address, just return the IP we sent.
|
|
newIP = ip
|
|
return newIP, nil
|
|
}
|
|
|
|
newIP, err = netip.ParseAddr(parsedXML.IP)
|
|
if err != nil {
|
|
return netip.Addr{}, fmt.Errorf("%w: %w", errors.ErrIPReceivedMalformed, err)
|
|
} else if ip.Compare(newIP) != 0 {
|
|
return netip.Addr{}, fmt.Errorf("%w: sent ip %s to update but received %s",
|
|
errors.ErrIPReceivedMismatch, ip, newIP)
|
|
}
|
|
return newIP, nil
|
|
}
|