Skip to content

Commit

Permalink
Fix embedding url broke equality
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 21, 2023
1 parent 76faf4b commit db5b25a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 10 additions & 10 deletions service/domain/relay_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

type RelayAddress struct {
original string
parsed *url.URL
}

func NewRelayAddress(s string) (RelayAddress, error) {
Expand All @@ -28,7 +27,6 @@ func NewRelayAddress(s string) (RelayAddress, error) {

return RelayAddress{
original: s,
parsed: u,
}, nil
}

Expand All @@ -45,20 +43,22 @@ func NewRelayAddressFromMaybeAddress(maybe MaybeRelayAddress) (RelayAddress, err
}

func (r RelayAddress) IsLoopbackOrPrivate() bool {
hostWithoutPort, err := r.getHostWithoutPort()
if err != nil {
return false
}
hostWithoutPort := r.getHostWithoutPort()
ip := net.ParseIP(hostWithoutPort)
return ip.IsLoopback() || ip.IsPrivate()
}

func (r RelayAddress) getHostWithoutPort() (string, error) {
hostWithoutPort, _, err := net.SplitHostPort(r.parsed.Host)
func (r RelayAddress) getHostWithoutPort() string {
u, err := url.Parse(r.original)
if err != nil {
panic(err) // checked in constructor
}

hostWithoutPort, _, err := net.SplitHostPort(u.Host)
if err != nil {
return r.parsed.Host, nil
return u.Host
}
return hostWithoutPort, nil
return hostWithoutPort
}

func (r RelayAddress) String() string {
Expand Down
8 changes: 8 additions & 0 deletions service/domain/relay_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ func TestRelayAddress_IsLocal(t *testing.T) {
})
}
}

func TestRelayAddress_Compare(t *testing.T) {
a := MustNewRelayAddress("wss://example1.com")
b := MustNewRelayAddress("wss://example1.com")
c := MustNewRelayAddress("wss://example2.com")
require.True(t, a == b)
require.True(t, a != c)
}

0 comments on commit db5b25a

Please sign in to comment.