Skip to content

Commit

Permalink
fix(settings): Fix DDNS using invalid net address
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger authored and ChrisSchinnerl committed Dec 18, 2024
1 parent 44bba16 commit ae9c8a9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
9 changes: 7 additions & 2 deletions host/settings/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"strconv"
"strings"

"go.sia.tech/core/types"
"go.sia.tech/coreutils/chain"
Expand Down Expand Up @@ -102,9 +103,13 @@ func (m *ConfigManager) Announce() error {
func validateHostname(host string) error {
// Check that the host is not empty or localhost.
if host == "" {
return errors.New("empty net address")
return errors.New("empty hostname")
} else if host == "localhost" {
return errors.New("net address cannot be localhost")
return errors.New("hostname cannot be localhost")
} else if _, _, err := net.SplitHostPort(host); err == nil {
return errors.New("hostname should not contain a port")
} else if strings.HasPrefix(host, "[") || strings.HasSuffix(host, "]") {
return errors.New(`hostname must not start with "[" or end with "]"`)
}

// If the host is an IP address, check that it is a public IP address.
Expand Down
7 changes: 2 additions & 5 deletions host/settings/ddns.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ func (m *ConfigManager) resetDDNS() {
// UpdateDDNS triggers an update of the host's dynamic DNS records.
func (m *ConfigManager) UpdateDDNS(force bool) error {
m.mu.Lock()
hostname, _, err := net.SplitHostPort(m.settings.NetAddress)
if err != nil {
m.mu.Unlock()
return fmt.Errorf("failed to split netaddress host and port: %w", err)
}
hostname := m.settings.NetAddress
settings := m.settings.DDNS
lastIPv4, lastIPv6 := m.lastIPv4, m.lastIPv6
m.mu.Unlock()
Expand All @@ -100,6 +96,7 @@ func (m *ConfigManager) UpdateDDNS(force bool) error {
lastIPv4, lastIPv6 = nil, nil
}

var err error
var ipv4 net.IP
if settings.IPv4 {
// get the IPv4 address
Expand Down
39 changes: 39 additions & 0 deletions host/settings/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package settings

import (
"strings"
"testing"
)

func TestValidateHostname(t *testing.T) {
tests := []struct {
addr string
valid bool
errStr string
}{
{"", false, "empty hostname"},
{"localhost", false, "hostname cannot be localhost"},
{"foo.bar:9982", false, "hostname should not contain a port"},
{"192.168.1.1", false, "only public IP addresses allowed"},
{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9982", false, "hostname should not contain a port"},
{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", false, `hostname must not start with "[" or end with "]"`},
{"2001:0db8:85a3:0000:0000:8a2e:0370:7334", true, ""},
{"55.123.123.123", true, ""},
{"foo.bar", true, ""},
}

for _, test := range tests {
t.Run(test.addr, func(t *testing.T) {
err := validateHostname(test.addr)
if err != nil {
if test.valid {
t.Fatalf("expected valid host, got %v", err)
} else if !strings.Contains(err.Error(), test.errStr) {
t.Fatalf("expected %v, got %v", test.errStr, err)
}
} else if !test.valid {
t.Fatalf("expected %v, got %v", test.errStr, err)
}
})
}
}

0 comments on commit ae9c8a9

Please sign in to comment.