-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
provider.go
162 lines (143 loc) · 4.11 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package easydns
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/netip"
"net/url"
"strings"
"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
host string
ipVersion ipversion.IPVersion
ipv6Suffix netip.Prefix
username string
token string
useProviderIP bool
}
func New(data json.RawMessage, domain, host string,
ipVersion ipversion.IPVersion, ipv6Suffix netip.Prefix) (
p *Provider, err error) {
extraSettings := struct {
Username string `json:"username"`
Token string `json:"token"`
UseProviderIP bool `json:"provider_ip"`
}{}
err = json.Unmarshal(data, &extraSettings)
if err != nil {
return nil, err
}
p = &Provider{
domain: domain,
host: host,
ipVersion: ipVersion,
ipv6Suffix: ipv6Suffix,
username: extraSettings.Username,
token: extraSettings.Token,
useProviderIP: extraSettings.UseProviderIP,
}
err = p.isValid()
if err != nil {
return nil, err
}
return p, nil
}
func (p *Provider) isValid() error {
switch {
case p.username == "":
return fmt.Errorf("%w", errors.ErrUsernameNotSet)
case p.token == "":
return fmt.Errorf("%w", errors.ErrTokenNotSet)
}
return nil
}
func (p *Provider) String() string {
return utils.ToString(p.domain, p.host, constants.EasyDNS, 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) IPv6Suffix() netip.Prefix {
return p.ipv6Suffix
}
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: fmt.Sprintf("<a href=\"http://%s\">%s</a>", p.BuildDomainName(), p.BuildDomainName()),
Host: p.Host(),
Provider: "<a href=\"https://easydns.com\">EasyDNS</a>",
IPVersion: p.ipVersion.String(),
}
}
func (p *Provider) Update(ctx context.Context, client *http.Client, ip netip.Addr) (
newIP netip.Addr, err error) {
u := url.URL{
Scheme: "https",
Host: "api.cp.easydns.com",
Path: "dyn/generic.php",
User: url.UserPassword(p.username, p.token),
}
values := url.Values{}
values.Set("hostname", utils.BuildURLQueryHostname(p.host, p.domain))
useProviderIP := p.useProviderIP && (ip.Is4() || !p.ipv6Suffix.IsValid())
if !useProviderIP {
values.Set("myip", ip.String())
}
if p.host == "*" {
values.Set("wildcard", "ON")
}
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)
}
headers.SetUserAgent(request)
response, err := client.Do(request)
if err != nil {
return netip.Addr{}, err
}
defer response.Body.Close()
b, err := io.ReadAll(response.Body)
if err != nil {
return netip.Addr{}, fmt.Errorf("reading response body: %w", err)
}
s := string(b)
if response.StatusCode != http.StatusOK {
return netip.Addr{}, fmt.Errorf("%w: %d: %s", errors.ErrHTTPStatusNotValid,
response.StatusCode, utils.ToSingleLine(s))
}
switch {
case s == "":
return netip.Addr{}, fmt.Errorf("%w", errors.ErrReceivedNoResult)
case strings.Contains(s, "NO_SERVICE"):
return netip.Addr{}, fmt.Errorf("%w", errors.ErrNoService)
case strings.Contains(s, "NO_ACCESS"):
return netip.Addr{}, fmt.Errorf("%w", errors.ErrAuth)
case strings.Contains(s, "ILLEGAL_INPUT"), strings.Contains(s, "TOO_SOON"):
return netip.Addr{}, fmt.Errorf("%w", errors.ErrBannedAbuse)
case strings.Contains(s, "NO_ERROR"), strings.Contains(s, "OK"):
return ip, nil
default:
return netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrUnknownResponse, utils.ToSingleLine(s))
}
}