Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix embedding url broke equality #18

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}