Skip to content

Commit 0c028f7

Browse files
authored
Using url package to build urls for APIs (#57)
1 parent c194681 commit 0c028f7

File tree

10 files changed

+166
-90
lines changed

10 files changed

+166
-90
lines changed

internal/constants/url.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/update/cloudflare.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"net"
77
"net/http"
8+
"net/url"
89

9-
"github.com/qdm12/ddns-updater/internal/constants"
1010
"github.com/qdm12/ddns-updater/internal/network"
1111
libnetwork "github.com/qdm12/golibs/network"
1212
)
@@ -22,9 +22,13 @@ func updateCloudflare(client libnetwork.Client, zoneIdentifier, identifier, host
2222
Proxied bool `json:"proxied"` // whether the record is receiving the performance and security benefits of Cloudflare
2323
TTL uint `json:"ttl"`
2424
}
25-
URL := constants.CloudflareURL + "/zones/" + zoneIdentifier + "/dns_records/" + identifier
25+
u := url.URL{
26+
Scheme: "https",
27+
Host: "api.cloudflare.com",
28+
Path: fmt.Sprintf("/client/v4/zones/%s/dns_records/%s", zoneIdentifier, identifier),
29+
}
2630
r, err := network.BuildHTTPPut(
27-
URL,
31+
u.String(),
2832
cloudflarePutBody{
2933
Type: "A",
3034
Name: host,
@@ -36,6 +40,7 @@ func updateCloudflare(client libnetwork.Client, zoneIdentifier, identifier, host
3640
if err != nil {
3741
return err
3842
}
43+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
3944
switch {
4045
case len(token) > 0:
4146
r.Header.Set("Authorization", "Bearer "+token)

internal/update/ddnss.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,39 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7+
"net/url"
78
"strings"
89

910
"github.com/qdm12/golibs/network"
1011
)
1112

1213
func updateDDNSS(client network.Client, domain, host, username, password string, ip net.IP) error {
13-
var hostname string
14-
if host == "@" {
15-
hostname = strings.ToLower(domain)
16-
} else {
17-
hostname = strings.ToLower(host + "." + domain)
14+
u := url.URL{
15+
Scheme: "https",
16+
Host: "www.ddnss.de",
17+
Path: "/upd.php",
1818
}
19-
url := fmt.Sprintf("http://www.ddnss.de/upd.php?user=%s&pwd=%s&host=%s", username, password, hostname)
19+
values := url.Values{}
20+
values.Set("user", username)
21+
values.Set("pwd", password)
22+
fqdn := domain
23+
if host != "@" {
24+
fqdn = host + "." + domain
25+
}
26+
values.Set("host", fqdn)
2027
if ip != nil {
2128
if ip.To4() == nil { // ipv6
22-
url += fmt.Sprintf("&ip6=%s", ip)
29+
values.Set("ip6", ip.String())
2330
} else {
24-
url += fmt.Sprintf("&ip=%s", ip)
31+
values.Set("ip", ip.String())
2532
}
2633
}
27-
r, err := http.NewRequest(http.MethodGet, url, nil)
34+
u.RawQuery = values.Encode()
35+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
2836
if err != nil {
2937
return err
3038
}
39+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
3140
status, content, err := client.DoHTTPRequest(r)
3241
if err != nil {
3342
return err
@@ -42,7 +51,7 @@ func updateDDNSS(client network.Client, domain, host, username, password string,
4251
case strings.Contains(s, "badauth"):
4352
return fmt.Errorf("ddnss.de: bad authentication")
4453
case strings.Contains(s, "notfqdn"):
45-
return fmt.Errorf("ddnss.de: hostname %q does not exist", hostname)
54+
return fmt.Errorf("ddnss.de: hostname %q does not exist", fqdn)
4655
case strings.Contains(s, "Updated 1 hostname"):
4756
return nil
4857
default:

internal/update/dnspod.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,26 @@ func updateDNSPod(client network.Client, domain, host, token string, ip net.IP)
1515
if ip == nil {
1616
return fmt.Errorf("IP address was not given to updater")
1717
}
18-
body := bytes.NewBufferString(url.Values{
19-
"login_token": []string{token},
20-
"format": []string{"json"},
21-
"domain": []string{domain},
22-
"length": []string{"200"},
23-
"sub_domain": []string{host},
24-
"record_type": []string{"A"},
25-
}.Encode())
26-
req, err := http.NewRequest(http.MethodPost, "https://dnsapi.cn/Record.List", body)
18+
u := url.URL{
19+
Scheme: "https",
20+
Host: "dnsapi.cn",
21+
Path: "/Record.List",
22+
}
23+
values := url.Values{}
24+
values.Set("login_token", token)
25+
values.Set("format", "json")
26+
values.Set("domain", domain)
27+
values.Set("length", "200")
28+
values.Set("sub_domain", host)
29+
values.Set("record_type", "A")
30+
u.RawQuery = values.Encode()
31+
r, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBufferString(values.Encode()))
2732
if err != nil {
2833
return err
2934
}
30-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
31-
status, content, err := client.DoHTTPRequest(req)
35+
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
36+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
37+
status, content, err := client.DoHTTPRequest(r)
3238
if err != nil {
3339
return err
3440
} else if status != http.StatusOK {
@@ -61,21 +67,24 @@ func updateDNSPod(client network.Client, domain, host, token string, ip net.IP)
6167
if len(recordID) == 0 {
6268
return fmt.Errorf("record not found")
6369
}
64-
body = bytes.NewBufferString(url.Values{
65-
"login_token": []string{token},
66-
"format": []string{"json"},
67-
"domain": []string{domain},
68-
"record_id": []string{recordID},
69-
"value": []string{ip.String()},
70-
"record_line": []string{recordLine},
71-
"sub_domain": []string{host},
72-
}.Encode())
73-
req, err = http.NewRequest(http.MethodPost, "https://dnsapi.cn/Record.Ddns", body)
70+
71+
u.Path = "/Record.Ddns"
72+
values = url.Values{}
73+
values.Set("login_token", token)
74+
values.Set("format", "json")
75+
values.Set("domain", domain)
76+
values.Set("record_id", recordID)
77+
values.Set("value", ip.String())
78+
values.Set("record_line", recordLine)
79+
values.Set("sub_domain", host)
80+
u.RawQuery = values.Encode()
81+
r, err = http.NewRequest(http.MethodPost, u.String(), bytes.NewBufferString(values.Encode()))
7482
if err != nil {
7583
return err
7684
}
77-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
78-
status, content, err = client.DoHTTPRequest(req)
85+
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
86+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
87+
status, content, err = client.DoHTTPRequest(r)
7988
if err != nil {
8089
return err
8190
} else if status != http.StatusOK {

internal/update/dreamhost.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"fmt"
66
"net"
77
"net/http"
8-
"strings"
8+
"net/url"
99

1010
"github.com/google/uuid"
11-
"github.com/qdm12/ddns-updater/internal/constants"
1211
"github.com/qdm12/golibs/network"
1312
)
1413

@@ -30,12 +29,26 @@ type (
3029
}
3130
)
3231

32+
func makeDreamhostDefaultValues(key string) (values url.Values) { //nolint:unparam
33+
values.Set("key", key)
34+
values.Set("unique_id", uuid.New().String())
35+
values.Set("format", "json")
36+
return values
37+
}
38+
3339
func listDreamhostRecords(client network.Client, key string) (records dreamHostRecords, err error) {
34-
url := constants.DreamhostURL + "/?key=" + key + "&unique_id=" + uuid.New().String() + "&format=json&cmd=dns-list_records"
35-
r, err := http.NewRequest(http.MethodGet, url, nil)
40+
u := url.URL{
41+
Scheme: "https",
42+
Host: "api.dreamhost.com",
43+
}
44+
values := makeDreamhostDefaultValues(key)
45+
values.Set("cmd", "dns-list_records")
46+
u.RawQuery = values.Encode()
47+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
3648
if err != nil {
3749
return records, err
3850
}
51+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
3952
status, content, err := client.DoHTTPRequest(r)
4053
if err != nil {
4154
return records, err
@@ -50,12 +63,22 @@ func listDreamhostRecords(client network.Client, key string) (records dreamHostR
5063
return records, nil
5164
}
5265

53-
func removeDreamhostRecord(client network.Client, key, domain string, ip net.IP) error {
54-
url := constants.DreamhostURL + "?key=" + key + "&unique_id=" + uuid.New().String() + "&format=json&cmd=dns-remove_record&record=" + strings.ToLower(domain) + "&type=A&value=" + ip.String()
55-
r, err := http.NewRequest(http.MethodGet, url, nil)
66+
func removeDreamhostRecord(client network.Client, key, domain string, ip net.IP) error { //nolint:dupl
67+
u := url.URL{
68+
Scheme: "https",
69+
Host: "api.dreamhost.com",
70+
}
71+
values := makeDreamhostDefaultValues(key)
72+
values.Set("cmd", "dns-remove_record")
73+
values.Set("record", domain)
74+
values.Set("type", "A")
75+
values.Set("value", ip.String())
76+
u.RawQuery = values.Encode()
77+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
5678
if err != nil {
5779
return err
5880
}
81+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
5982
status, content, err := client.DoHTTPRequest(r)
6083
if err != nil {
6184
return err
@@ -71,12 +94,22 @@ func removeDreamhostRecord(client network.Client, key, domain string, ip net.IP)
7194
return nil
7295
}
7396

74-
func addDreamhostRecord(client network.Client, key, domain string, ip net.IP) error {
75-
url := constants.DreamhostURL + "?key=" + key + "&unique_id=" + uuid.New().String() + "&format=json&cmd=dns-add_record&record=" + strings.ToLower(domain) + "&type=A&value=" + ip.String()
76-
r, err := http.NewRequest(http.MethodGet, url, nil)
97+
func addDreamhostRecord(client network.Client, key, domain string, ip net.IP) error { //nolint:dupl
98+
u := url.URL{
99+
Scheme: "https",
100+
Host: "api.dreamhost.com",
101+
}
102+
values := makeDreamhostDefaultValues(key)
103+
values.Set("cmd", "dns-add_record")
104+
values.Set("record", domain)
105+
values.Set("type", "A")
106+
values.Set("value", ip.String())
107+
u.RawQuery = values.Encode()
108+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
77109
if err != nil {
78110
return err
79111
}
112+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
80113
status, content, err := client.DoHTTPRequest(r)
81114
if err != nil {
82115
return err

internal/update/duckdns.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7-
"strings"
7+
"net/url"
88

9-
"github.com/qdm12/ddns-updater/internal/constants"
109
"github.com/qdm12/golibs/network"
1110
"github.com/qdm12/golibs/verification"
1211
)
1312

1413
func updateDuckDNS(client network.Client, domain, token string, ip net.IP) (newIP net.IP, err error) {
15-
url := constants.DuckDNSURL + "?domains=" + strings.ToLower(domain) + "&token=" + token + "&verbose=true"
14+
u := url.URL{
15+
Scheme: "https",
16+
Host: "www.duckdns.org",
17+
Path: "/update",
18+
}
19+
values := url.Values{}
20+
values.Set("verbose", "true")
21+
values.Set("domains", domain)
22+
values.Set("token", token)
23+
u.RawQuery = values.Encode()
1624
if ip != nil {
17-
url += "&ip=" + ip.String()
25+
values.Set("ip", ip.String())
1826
}
19-
r, err := http.NewRequest(http.MethodGet, url, nil)
27+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
2028
if err != nil {
2129
return nil, err
2230
}
31+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
2332
status, content, err := client.DoHTTPRequest(r)
2433
if err != nil {
2534
return nil, err

internal/update/godaddy.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import (
55
"fmt"
66
"net"
77
"net/http"
8-
"strings"
8+
"net/url"
99

10-
"github.com/qdm12/ddns-updater/internal/constants"
1110
"github.com/qdm12/ddns-updater/internal/network"
1211
libnetwork "github.com/qdm12/golibs/network"
1312
)
@@ -19,18 +18,16 @@ func updateGoDaddy(client libnetwork.Client, host, domain, key, secret string, i
1918
type goDaddyPutBody struct {
2019
Data string `json:"data"` // IP address to update to
2120
}
22-
URL := constants.GoDaddyURL + "/" + strings.ToLower(domain) + "/records/A/" + strings.ToLower(host)
23-
r, err := network.BuildHTTPPut(
24-
URL,
25-
[]goDaddyPutBody{
26-
{
27-
ip.String(),
28-
},
29-
},
30-
)
21+
u := url.URL{
22+
Scheme: "https",
23+
Host: "api.godaddy.com",
24+
Path: fmt.Sprintf("/v1/domains/%s/records/A/%s", domain, host),
25+
}
26+
r, err := network.BuildHTTPPut(u.String(), []goDaddyPutBody{{ip.String()}})
3127
if err != nil {
3228
return err
3329
}
30+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
3431
r.Header.Set("Authorization", "sso-key "+key+":"+secret)
3532
status, content, err := client.DoHTTPRequest(r)
3633
if err != nil {

internal/update/infomaniak.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,33 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7+
"net/url"
78
"strings"
89

910
"github.com/qdm12/golibs/network"
1011
)
1112

1213
func updateInfomaniak(client network.Client, domain, host, username, password string, ip net.IP) (newIP net.IP, err error) {
13-
var hostname string
14-
if host == "@" {
15-
hostname = strings.ToLower(domain)
16-
} else {
17-
hostname = strings.ToLower(host + "." + domain)
14+
u := url.URL{
15+
Scheme: "https",
16+
Host: "infomaniak.com",
17+
Path: "/nic/update",
18+
User: url.UserPassword(username, password),
19+
}
20+
values := url.Values{}
21+
values.Set("hostname", domain)
22+
if host != "@" {
23+
values.Set("hostname", host+"."+domain)
1824
}
19-
url := fmt.Sprintf("https://%s:%[email protected]/nic/update?hostname=%s", username, password, hostname)
2025
if ip != nil {
21-
url += fmt.Sprintf("&myip=%s", ip)
26+
values.Set("myip", ip.String())
2227
}
23-
r, err := http.NewRequest(http.MethodGet, url, nil)
28+
u.RawQuery = values.Encode()
29+
r, err := http.NewRequest(http.MethodGet, u.String(), nil)
2430
if err != nil {
2531
return nil, err
2632
}
33+
r.Header.Set("User-Agent", "DDNS-Updater [email protected]")
2734
status, content, err := client.DoHTTPRequest(r)
2835
if err != nil {
2936
return nil, err

0 commit comments

Comments
 (0)