mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-05-12 03:29:57 +02:00
validate and normalize relay urls received from kind 3 and 10002.
This commit is contained in:
parent
82a51f149a
commit
c0c20b8401
@ -56,6 +56,11 @@ func ParseRelaysFromKind10002(evt *nostr.Event) []Relay {
|
|||||||
result := make([]Relay, 0, len(evt.Tags))
|
result := make([]Relay, 0, len(evt.Tags))
|
||||||
for _, tag := range evt.Tags {
|
for _, tag := range evt.Tags {
|
||||||
if u := tag.Value(); u != "" && tag[0] == "r" {
|
if u := tag.Value(); u != "" && tag[0] == "r" {
|
||||||
|
if !nostr.IsValidRelayURL(u) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
u := nostr.NormalizeURL(u)
|
||||||
|
|
||||||
relay := Relay{
|
relay := Relay{
|
||||||
URL: u,
|
URL: u,
|
||||||
}
|
}
|
||||||
@ -88,6 +93,11 @@ func ParseRelaysFromKind3(evt *nostr.Event) []Relay {
|
|||||||
results := make([]Relay, len(items))
|
results := make([]Relay, len(items))
|
||||||
i := 0
|
i := 0
|
||||||
for u, item := range items {
|
for u, item := range items {
|
||||||
|
if !nostr.IsValidRelayURL(u) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
u := nostr.NormalizeURL(u)
|
||||||
|
|
||||||
relay := Relay{
|
relay := Relay{
|
||||||
URL: u,
|
URL: u,
|
||||||
}
|
}
|
||||||
|
15
utils.go
15
utils.go
@ -1,6 +1,7 @@
|
|||||||
package nostr
|
package nostr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/exp/constraints"
|
"golang.org/x/exp/constraints"
|
||||||
@ -129,3 +130,17 @@ func InsertEventIntoDescendingList(sortedArray []*Event, event *Event) []*Event
|
|||||||
|
|
||||||
return sortedArray
|
return sortedArray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsValidRelayURL(u string) bool {
|
||||||
|
parsed, err := url.Parse(u)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if parsed.Scheme != "wss" && parsed.Scheme != "ws" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if len(strings.Split(parsed.Host, ".")) < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user