Get service url when handling auth rather than mutating the relay, allow user to override service url via env var

This commit is contained in:
Jon Staab
2024-12-30 16:20:15 -08:00
committed by fiatjaf_
parent 4dba9376a0
commit 5b9b89543f
3 changed files with 6 additions and 8 deletions

View File

@@ -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()

View File

@@ -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 {

View File

@@ -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)