From 5b9b89543fade53d55cb7b6a4809f18c82a5ab8b Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Mon, 30 Dec 2024 16:20:15 -0800 Subject: [PATCH] Get service url when handling auth rather than mutating the relay, allow user to override service url via env var --- handlers.go | 10 +++++----- nip86.go | 2 +- relay.go | 2 -- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/handlers.go b/handlers.go index 5dfd3b5..46bbb60 100644 --- a/handlers.go +++ b/handlers.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "net/http" + "os" "strings" "sync" "time" @@ -24,10 +25,6 @@ import ( // ServeHTTP implements http.Handler interface. func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if rl.ServiceURL == "" { - rl.ServiceURL = getServiceBaseURL(r) - } - corsMiddleware := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{ @@ -319,7 +316,10 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) { id := string(*env) rl.removeListenerId(ws, id) case *nostr.AuthEnvelope: - wsBaseUrl := strings.Replace(rl.ServiceURL, "http", "ws", 1) + wsBaseUrl := os.Getenv("RELAY_URL") + if wsBaseUrl == "" { + wsBaseUrl = strings.Replace(getServiceBaseURL(r), "http", "ws", 1) + } if pubkey, ok := nip42.ValidateAuthEvent(&env.Event, ws.Challenge, wsBaseUrl); ok { ws.AuthedPublicKey = pubkey ws.authLock.Lock() diff --git a/nip86.go b/nip86.go index 73b500e..d2cde38 100644 --- a/nip86.go +++ b/nip86.go @@ -80,7 +80,7 @@ func (rl *Relay) HandleNIP86(w http.ResponseWriter, r *http.Request) { goto respond } - if uTag := evt.Tags.GetFirst([]string{"u", ""}); uTag == nil || rl.ServiceURL != (*uTag)[1] { + if uTag := evt.Tags.GetFirst([]string{"u", ""}); uTag == nil || getServiceBaseURL(r) != (*uTag)[1] { resp.Error = "invalid 'u' tag" goto respond } else if pht := evt.Tags.GetFirst([]string{"payload", hex.EncodeToString(payloadHash[:])}); pht == nil { diff --git a/relay.go b/relay.go index 5234e98..81311d1 100644 --- a/relay.go +++ b/relay.go @@ -45,8 +45,6 @@ func NewRelay() *Relay { } type Relay struct { - ServiceURL string - // hooks that will be called at various times RejectEvent []func(ctx context.Context, event *nostr.Event) (reject bool, msg string) OverwriteDeletionOutcome []func(ctx context.Context, target *nostr.Event, deletion *nostr.Event) (acceptDeletion bool, msg string)