Skip to content

Commit 709886d

Browse files
committed
fix: validate the IP address
1 parent a781a1f commit 709886d

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

internal/app/ip/external_ip.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ip
22

33
import (
44
log "github.com/sirupsen/logrus"
5+
"net"
56
)
67

78
// Providers - the structure of the providers for fetching the external IP address
@@ -35,13 +36,13 @@ func NewExternalIP() *ExternalIP {
3536
}
3637

3738
// IP gets the external IP address
38-
func (e *ExternalIP) IP() []byte {
39+
func (e *ExternalIP) IP() string {
3940
for _, provider := range e.providers {
4041
log.WithFields(log.Fields{
4142
"provider": provider.name,
4243
}).Debug("Checking an external IP address")
4344

44-
currentIP, err := request(provider.url)
45+
responseProvider, err := request(provider.url)
4546

4647
if err != nil {
4748
log.WithFields(log.Fields{
@@ -51,8 +52,19 @@ func (e *ExternalIP) IP() []byte {
5152
continue
5253
}
5354

54-
return currentIP
55+
currentIP := net.ParseIP(string(responseProvider))
56+
57+
if currentIP == nil {
58+
log.WithFields(log.Fields{
59+
"provider": provider.name,
60+
"response": responseProvider,
61+
}).Error("The IP address has invalid format")
62+
63+
continue
64+
}
65+
66+
return currentIP.String()
5567
}
5668

57-
return nil
69+
return ""
5870
}

internal/app/ip/request.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ip
22

33
import (
4-
"io"
54
"io/ioutil"
65
"net/http"
76
"time"
@@ -23,11 +22,7 @@ func request(url string) ([]byte, error) {
2322
return nil, err
2423
}
2524

26-
defer func(r io.ReadCloser) {
27-
if errReq := r.Close(); errReq != nil {
28-
err = errReq
29-
}
30-
}(r.Body)
25+
defer r.Body.Close()
3126

3227
return ioutil.ReadAll(r.Body)
3328
}

internal/app/service/cloudflare.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"io"
87
"io/ioutil"
98
"net/http"
109
)
@@ -72,6 +71,7 @@ func (c *Cloudflare) UpdateRecordRequest() ([]byte, error) {
7271
}
7372

7473
body, err := c.sendRequest(data)
74+
7575
var payload CFRequestUpdatePayload
7676

7777
if err := json.Unmarshal(body, &payload); err != nil {
@@ -101,11 +101,7 @@ func (c *Cloudflare) sendRequest(data []byte) ([]byte, error) {
101101
return nil, err
102102
}
103103

104-
defer func(r io.ReadCloser) {
105-
if errReq := r.Close(); errReq != nil {
106-
err = errReq
107-
}
108-
}(r.Body)
104+
defer r.Body.Close()
109105

110106
return ioutil.ReadAll(r.Body)
111107
}

internal/app/service/ovh.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package service
33
import (
44
"encoding/base64"
55
"fmt"
6-
"io"
76
"io/ioutil"
87
"net/http"
98
"net/url"
@@ -38,18 +37,20 @@ func (o *OVH) UpdateRecordRequest() ([]byte, error) {
3837
return nil, err
3938
}
4039

41-
req.Header.Add("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", o.Credentials.Username, o.Credentials.Password)))))
40+
credentials := []byte(fmt.Sprintf("%s:%s", o.Credentials.Username, o.Credentials.Password))
41+
42+
req.Header.Add(
43+
"Authorization",
44+
fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(credentials)),
45+
)
46+
4247
r, err := client.Do(req)
4348

4449
if err != nil {
4550
return nil, err
4651
}
4752

48-
defer func(r io.ReadCloser) {
49-
if errReq := r.Close(); errReq != nil {
50-
err = errReq
51-
}
52-
}(r.Body)
53+
defer r.Body.Close()
5354

5455
if err := r.Header.Get("WWW-Authenticate"); r.StatusCode == http.StatusUnauthorized {
5556
return nil, fmt.Errorf("OVH [%d]: %s", r.StatusCode, err)

internal/app/update_ip.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package app
33
import (
44
"github.com/Wojteek/dynhost/internal/app/ip"
55
log "github.com/sirupsen/logrus"
6-
"strings"
76
"time"
87
)
98

109
// UpdateIP fetches and updates the IP when the IP was changed
1110
func UpdateIP(p *ServiceCommand, IPChangedFn IPChangedCallback) func() error {
1211
return func() error {
1312
data, _ := GetData(p.DataPath)
14-
externalIP := strings.TrimSpace(string(ip.NewExternalIP().IP()))
13+
externalIP := ip.NewExternalIP().IP()
1514

1615
if len(externalIP) == 0 {
1716
return nil
@@ -25,13 +24,13 @@ func UpdateIP(p *ServiceCommand, IPChangedFn IPChangedCallback) func() error {
2524
return err
2625
}
2726

28-
var d interface{} = &Data{
27+
d := &Data{
2928
CurrentIP: externalIP,
3029
PrevIP: data.CurrentIP,
3130
ChangedAt: time.Now(),
3231
}
3332

34-
if _, err := d.(*Data).SaveData(p.DataPath); err != nil {
33+
if _, err := d.SaveData(p.DataPath); err != nil {
3534
return err
3635
}
3736

0 commit comments

Comments
 (0)