mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-04-05 00:43:53 -04:00
156 lines
3.6 KiB
Go
156 lines
3.6 KiB
Go
package he
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/qdm12/ddns-updater/internal/models"
|
|
"github.com/qdm12/ddns-updater/internal/settings/constants"
|
|
"github.com/qdm12/ddns-updater/internal/settings/errors"
|
|
"github.com/qdm12/ddns-updater/internal/settings/headers"
|
|
"github.com/qdm12/ddns-updater/internal/settings/utils"
|
|
"github.com/qdm12/ddns-updater/pkg/publicip/ipversion"
|
|
"github.com/qdm12/golibs/verification"
|
|
)
|
|
|
|
type Provider struct {
|
|
domain string
|
|
host string
|
|
ipVersion ipversion.IPVersion
|
|
password string
|
|
useProviderIP bool
|
|
}
|
|
|
|
func New(data json.RawMessage, domain, host string,
|
|
ipVersion ipversion.IPVersion) (p *Provider, err error) {
|
|
extraSettings := struct {
|
|
Password string `json:"password"`
|
|
UseProviderIP bool `json:"provider_ip"`
|
|
}{}
|
|
if err := json.Unmarshal(data, &extraSettings); err != nil {
|
|
return nil, err
|
|
}
|
|
p = &Provider{
|
|
domain: domain,
|
|
host: host,
|
|
ipVersion: ipVersion,
|
|
password: extraSettings.Password,
|
|
useProviderIP: extraSettings.UseProviderIP,
|
|
}
|
|
if err := p.isValid(); err != nil {
|
|
return nil, err
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (p *Provider) isValid() error {
|
|
if len(p.password) == 0 {
|
|
return errors.ErrEmptyPassword
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Provider) String() string {
|
|
return utils.ToString(p.domain, p.host, constants.HE, p.ipVersion)
|
|
}
|
|
|
|
func (p *Provider) Domain() string {
|
|
return p.domain
|
|
}
|
|
|
|
func (p *Provider) Host() string {
|
|
return p.host
|
|
}
|
|
|
|
func (p *Provider) IPVersion() ipversion.IPVersion {
|
|
return p.ipVersion
|
|
}
|
|
|
|
func (p *Provider) Proxied() bool {
|
|
return false
|
|
}
|
|
|
|
func (p *Provider) BuildDomainName() string {
|
|
return utils.BuildDomainName(p.host, p.domain)
|
|
}
|
|
|
|
func (p *Provider) HTML() models.HTMLRow {
|
|
return models.HTMLRow{
|
|
Domain: models.HTML(fmt.Sprintf("<a href=\"http://%s\">%s</a>", p.BuildDomainName(), p.BuildDomainName())),
|
|
Host: models.HTML(p.Host()),
|
|
Provider: "<a href=\"https://dns.he.net/\">he.net</a>",
|
|
IPVersion: models.HTML(p.ipVersion.String()),
|
|
}
|
|
}
|
|
|
|
func (p *Provider) Update(ctx context.Context, client *http.Client, ip net.IP) (newIP net.IP, err error) {
|
|
fqdn := p.BuildDomainName()
|
|
u := url.URL{
|
|
Scheme: "https",
|
|
Host: "dyn.dns.he.net",
|
|
Path: "/nic/update",
|
|
User: url.UserPassword(fqdn, p.password),
|
|
}
|
|
values := url.Values{}
|
|
values.Set("hostname", fqdn)
|
|
if !p.useProviderIP {
|
|
values.Set("myip", ip.String())
|
|
}
|
|
u.RawQuery = values.Encode()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
headers.SetUserAgent(request)
|
|
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
b, err := io.ReadAll(response.Body)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", errors.ErrUnmarshalResponse, err)
|
|
}
|
|
s := string(b)
|
|
|
|
switch s {
|
|
case "":
|
|
return nil, fmt.Errorf("%w: %d: %s", errors.ErrBadHTTPStatus, response.StatusCode, s)
|
|
case constants.Badauth:
|
|
return nil, errors.ErrAuth
|
|
}
|
|
|
|
if !strings.Contains(s, "nochg") && !strings.Contains(s, "good") {
|
|
return nil, fmt.Errorf("%w: %s", errors.ErrUnknownResponse, s)
|
|
}
|
|
|
|
var ips []string
|
|
verifier := verification.NewVerifier()
|
|
if ip.To4() != nil {
|
|
ips = verifier.SearchIPv4(s)
|
|
} else {
|
|
ips = verifier.SearchIPv6(s)
|
|
}
|
|
|
|
if len(ips) == 0 {
|
|
return nil, errors.ErrNoIPInResponse
|
|
}
|
|
|
|
newIP = net.ParseIP(ips[0])
|
|
if newIP == nil {
|
|
return nil, fmt.Errorf("%w: %s", errors.ErrIPReceivedMalformed, ips[0])
|
|
} else if !p.useProviderIP && !ip.Equal(newIP) {
|
|
return nil, fmt.Errorf("%w: %s", errors.ErrIPReceivedMismatch, newIP.String())
|
|
}
|
|
return newIP, nil
|
|
}
|