Skip to content

Commit

Permalink
Get service url when handling auth rather than mutating the relay, al…
Browse files Browse the repository at this point in the history
…low user to override service url via env var
  • Loading branch information
Jon Staab authored and fiatjaf committed Jan 1, 2025
1 parent 4dba937 commit 5b9b895
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
10 changes: 5 additions & 5 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"errors"
"net/http"
"os"
"strings"
"sync"
"time"
Expand All @@ -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{
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion nip86.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5b9b895

Please sign in to comment.