mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-04-23 13:29:11 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0848182c4 | ||
|
|
80586c4573 |
@@ -86,7 +86,11 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, ovw := range rl.OverwriteResponseEvent {
|
||||||
|
ovw(ctx, evt)
|
||||||
|
}
|
||||||
notifyListeners(evt)
|
notifyListeners(evt)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
github.com/fasthttp/websocket v1.5.3
|
github.com/fasthttp/websocket v1.5.3
|
||||||
github.com/fiatjaf/eventstore v0.1.0
|
github.com/fiatjaf/eventstore v0.1.0
|
||||||
github.com/gobwas/ws v1.2.0
|
github.com/gobwas/ws v1.2.0
|
||||||
github.com/nbd-wtf/go-nostr v0.25.0
|
github.com/nbd-wtf/go-nostr v0.25.1
|
||||||
github.com/puzpuzpuz/xsync/v2 v2.5.1
|
github.com/puzpuzpuz/xsync/v2 v2.5.1
|
||||||
github.com/rs/cors v1.7.0
|
github.com/rs/cors v1.7.0
|
||||||
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
|
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -90,8 +90,8 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
|
|||||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||||
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
||||||
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/nbd-wtf/go-nostr v0.25.0 h1:6ArnEX5NqjTaIBH6F5KYIJ0uw0uaKSWu8zjDb9za0Cg=
|
github.com/nbd-wtf/go-nostr v0.25.1 h1:YTLTDUgngfzd3qQ0fWmQmq20flwnGtHH0g0Q8S3HlW4=
|
||||||
github.com/nbd-wtf/go-nostr v0.25.0/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
|
github.com/nbd-wtf/go-nostr v0.25.1/go.mod h1:bkffJI+x914sPQWum9ZRUn66D7NpDnAoWo1yICvj3/0=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
18
handlers.go
18
handlers.go
@@ -124,12 +124,10 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// check signature (requires the ID to be set)
|
// check signature (requires the ID to be set)
|
||||||
if ok, err := evt.CheckSignature(); err != nil {
|
if ok, err := evt.CheckSignature(); err != nil {
|
||||||
reason := "error: failed to verify signature"
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to verify signature"})
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
|
||||||
return
|
return
|
||||||
} else if !ok {
|
} else if !ok {
|
||||||
reason := "invalid: signature is invalid"
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "invalid: signature is invalid"})
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,12 +137,12 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
err = rl.AddEvent(ctx, &evt)
|
err = rl.AddEvent(ctx, &evt)
|
||||||
}
|
}
|
||||||
var reason *string
|
|
||||||
|
var reason string
|
||||||
if err == nil {
|
if err == nil {
|
||||||
ok = true
|
ok = true
|
||||||
} else {
|
} else {
|
||||||
msg := err.Error()
|
reason = err.Error()
|
||||||
reason = &msg
|
|
||||||
}
|
}
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: ok, Reason: reason})
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: ok, Reason: reason})
|
||||||
case "COUNT":
|
case "COUNT":
|
||||||
@@ -236,6 +234,9 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
go func(ch chan *nostr.Event) {
|
go func(ch chan *nostr.Event) {
|
||||||
for event := range ch {
|
for event := range ch {
|
||||||
|
for _, ovw := range rl.OverwriteResponseEvent {
|
||||||
|
ovw(ctx, event)
|
||||||
|
}
|
||||||
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
|
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
|
||||||
}
|
}
|
||||||
eose.Done()
|
eose.Done()
|
||||||
@@ -273,8 +274,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
|
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
|
||||||
} else {
|
} else {
|
||||||
reason := "error: failed to authenticate"
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: "error: failed to authenticate"})
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
relay.go
1
relay.go
@@ -45,6 +45,7 @@ type Relay struct {
|
|||||||
RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
RejectFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
||||||
RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
RejectCountFilter []func(ctx context.Context, filter nostr.Filter) (reject bool, msg string)
|
||||||
OverwriteDeletionOutcome []func(ctx context.Context, target *nostr.Event, deletion *nostr.Event) (acceptDeletion bool, msg string)
|
OverwriteDeletionOutcome []func(ctx context.Context, target *nostr.Event, deletion *nostr.Event) (acceptDeletion bool, msg string)
|
||||||
|
OverwriteResponseEvent []func(ctx context.Context, event *nostr.Event)
|
||||||
StoreEvent []func(ctx context.Context, event *nostr.Event) error
|
StoreEvent []func(ctx context.Context, event *nostr.Event) error
|
||||||
DeleteEvent []func(ctx context.Context, event *nostr.Event) error
|
DeleteEvent []func(ctx context.Context, event *nostr.Event) error
|
||||||
QueryEvents []func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error)
|
QueryEvents []func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error)
|
||||||
|
|||||||
Reference in New Issue
Block a user