-
Notifications
You must be signed in to change notification settings - Fork 4
fix: refactor relay URLs validation #1405
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
Closed
sameh-farouk
wants to merge
4
commits into
development
from
development-refactor-relay-urls-validation
+163
−20
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0f869b3
fix: refactor relay URLs validation
sameh-farouk 6cfd642
normalize relay URL handling and add tests
sameh-farouk 1383de7
fix: non-adjacent duplicate hosts won’t be removed
sameh-farouk c59f919
test: Add non-adjacent duplicates test
sameh-farouk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import ( | |
"fmt" | ||
"net/url" | ||
"slices" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
|
@@ -145,34 +144,55 @@ func generateSecureKey(identity substrate.Identity) (*secp256k1.PrivateKey, erro | |
priv := secp256k1.PrivKeyFromBytes(keyPair.Seed()) | ||
return priv, nil | ||
} | ||
|
||
// getRelayConnections tries to connect to all relays and returns only the successful ones | ||
// getRelayConnections returns InnerConnections for all valid relay URLs | ||
func getRelayConnections(relayURLs []string, identity substrate.Identity, session string, twinID uint32) ([]string, []InnerConnection, error) { | ||
var validRelayURLs []string | ||
var connections []InnerConnection | ||
func validateRelayURLs(relayURLs []string) ([]*url.URL, error) { | ||
var validRelayURLs []*url.URL | ||
|
||
for _, relayURL := range relayURLs { | ||
parsedURL, err := url.Parse(relayURL) | ||
if err != nil { | ||
log.Warn().Err(err).Str("url", relayURL).Msg("failed to parse relay URL, skipping") | ||
continue | ||
} | ||
validRelayURLs = append(validRelayURLs, parsedURL.Host) | ||
conn := NewConnection(identity, relayURL, session, twinID) | ||
connections = append(connections, conn) | ||
// make sure it is ws or wss | ||
if parsedURL.Scheme != "ws" && parsedURL.Scheme != "wss" { | ||
log.Warn().Str("url", relayURL).Msg("relay URL must be ws or wss, skipping") | ||
continue | ||
} | ||
validRelayURLs = append(validRelayURLs, parsedURL) | ||
} | ||
|
||
if len(connections) == 0 { | ||
return nil, nil, ErrNoValidRelayURLs | ||
if len(validRelayURLs) == 0 { | ||
return nil, ErrNoValidRelayURLs | ||
} | ||
|
||
sort.Slice(validRelayURLs, func(i, j int) bool { | ||
return strings.ToLower(validRelayURLs[i]) < strings.ToLower(validRelayURLs[j]) | ||
slices.SortFunc(validRelayURLs, func(a, b *url.URL) int { | ||
return strings.Compare(a.Host, b.Host) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also we depend on hosts only ignoring ports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
}) | ||
validRelayURLs = slices.CompactFunc(validRelayURLs, func(a, b *url.URL) bool { | ||
return a.Host == b.Host | ||
sameh-farouk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
validRelayURLs = slices.Compact(validRelayURLs) | ||
|
||
return validRelayURLs, connections, nil | ||
return validRelayURLs, nil | ||
} | ||
|
||
// getRelayConnections tries to connect to all relays and returns only the successful ones | ||
// getRelayConnections returns InnerConnections for all valid relay URLs | ||
func getRelayConnections(relayURLs []string, identity substrate.Identity, session string, twinID uint32) ([]string, []InnerConnection, error) { | ||
validRelayURLs, err := validateRelayURLs(relayURLs) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
connections := make([]InnerConnection, 0, len(validRelayURLs)) | ||
hosts := make([]string, 0, len(validRelayURLs)) | ||
|
||
for _, relayURL := range validRelayURLs { | ||
conn := NewConnection(identity, relayURL.String(), session, twinID) | ||
connections = append(connections, conn) | ||
host := strings.ToLower(relayURL.Host) | ||
sameh-farouk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hosts = append(hosts, host) | ||
} | ||
|
||
return hosts, connections, nil | ||
} | ||
|
||
func getIdentity(keytype string, mnemonics string) (substrate.Identity, error) { | ||
|
@@ -266,7 +286,7 @@ func NewPeer( | |
publicKey = privKey.PubKey().SerializeCompressed() | ||
} | ||
|
||
relayURLs, conns, err := getRelayConnections(cfg.relayURLs, identity, cfg.session, twin.ID) | ||
hosts, conns, err := getRelayConnections(cfg.relayURLs, identity, cfg.session, twin.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -281,7 +301,7 @@ func NewPeer( | |
} | ||
} | ||
|
||
joinURLs := strings.Join(relayURLs, "_") | ||
joinURLs := strings.Join(hosts, "_") | ||
if !bytes.Equal(twin.E2EKey, publicKey) || twin.Relay == nil || joinURLs != *twin.Relay { | ||
log.Info().Str("Relay url/s", joinURLs).Msg("twin relay/public key didn't match, updating on chain ...") | ||
if _, err = subConn.UpdateTwin(identity, joinURLs, publicKey); err != nil { | ||
|
@@ -320,7 +340,7 @@ func NewPeer( | |
cons: cons, | ||
handler: handler, | ||
encoder: cfg.encoder, | ||
relays: relayURLs, | ||
relays: hosts, | ||
} | ||
|
||
go cl.process(ctx) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.