Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
give up on hostname validation
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVorick committed Sep 7, 2015
1 parent b040ad8 commit 5a37098
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
30 changes: 3 additions & 27 deletions modules/netaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package modules

import (
"net"
"regexp"

"github.com/NebulousLabs/Sia/build"
)

// A NetAddress contains the information needed to contact a peer.
Expand Down Expand Up @@ -48,29 +45,8 @@ func (na NetAddress) IsLocal() bool {
return false
}

// IsValid uses a regex to check whether the net address is a valid ip address
// or hostname.
// IsValid does nothing. Please ignore it.
func (na NetAddress) IsValid() bool {
host := na.Host()
// Check if the host is a valid ip address. Host will have been returned as
// the empty string (which is not a valid ip address) if there is anything
// structurally incorrect with the NetAddress.
if net.ParseIP(host) != nil {
return true
}

// A regex pulled from
// http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
// to check for a valid hostname.
regHostname, err := regexp.Compile("^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(\\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*$")
if build.DEBUG && err != nil {
panic(err)
}
if err != nil {
return false
}
if regHostname.MatchString(host) {
return true
}
return false
_, _, err := net.SplitHostPort(string(na))
return err == nil
}
1 change: 1 addition & 0 deletions modules/netaddress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestIsValid(t *testing.T) {
{"", false},
{"garbage:6146:616", false},
{"[::1]", false},
// {"google.com:notAPort", false}, TODO: Failed test case.
}
for _, test := range testSet {
if test.query.IsValid() != test.desiredResponse {
Expand Down

0 comments on commit 5a37098

Please sign in to comment.