mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-04-18 11:26:49 +02:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8df7c9d773 |
@@ -69,11 +69,6 @@ func main() {
|
|||||||
|
|
||||||
// there are many other configurable things you can set
|
// there are many other configurable things you can set
|
||||||
relay.RejectEvent = append(relay.RejectEvent,
|
relay.RejectEvent = append(relay.RejectEvent,
|
||||||
// built-in policies
|
|
||||||
policies.ValidateKind,
|
|
||||||
|
|
||||||
// define your own policies
|
|
||||||
policies.PreventLargeTags(80),
|
|
||||||
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
||||||
return true, "we don't allow this person to write here"
|
return true, "we don't allow this person to write here"
|
||||||
@@ -84,10 +79,6 @@ func main() {
|
|||||||
|
|
||||||
// you can request auth by rejecting an event or a request with the prefix "auth-required: "
|
// you can request auth by rejecting an event or a request with the prefix "auth-required: "
|
||||||
relay.RejectFilter = append(relay.RejectFilter,
|
relay.RejectFilter = append(relay.RejectFilter,
|
||||||
// built-in policies
|
|
||||||
policies.NoComplexFilters,
|
|
||||||
|
|
||||||
// define your own policies
|
|
||||||
func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||||
if pubkey := khatru.GetAuthed(ctx); pubkey != "" {
|
if pubkey := khatru.GetAuthed(ctx); pubkey != "" {
|
||||||
log.Printf("request from %s\n", pubkey)
|
log.Printf("request from %s\n", pubkey)
|
||||||
|
|||||||
46
adding.go
46
adding.go
@@ -31,53 +31,29 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
oee(ctx, evt)
|
oee(ctx, evt)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// will store
|
|
||||||
|
|
||||||
// but first check if we already have it
|
|
||||||
filter := nostr.Filter{IDs: []string{evt.ID}}
|
|
||||||
for _, query := range rl.QueryEvents {
|
|
||||||
ch, err := query(ctx, filter)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for range ch {
|
|
||||||
// if we run this it means we already have this event, so we just return a success and exit
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if it's replaceable we first delete old versions
|
|
||||||
if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
|
if evt.Kind == 0 || evt.Kind == 3 || (10000 <= evt.Kind && evt.Kind < 20000) {
|
||||||
// replaceable event, delete before storing
|
// replaceable event, delete before storing
|
||||||
filter := nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}}
|
|
||||||
for _, query := range rl.QueryEvents {
|
for _, query := range rl.QueryEvents {
|
||||||
ch, err := query(ctx, filter)
|
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for previous := range ch {
|
if previous := <-ch; previous != nil && isOlder(previous, evt) {
|
||||||
if isOlder(previous, evt) {
|
for _, del := range rl.DeleteEvent {
|
||||||
for _, del := range rl.DeleteEvent {
|
del(ctx, previous)
|
||||||
del(ctx, previous)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if 30000 <= evt.Kind && evt.Kind < 40000 {
|
} else if 30000 <= evt.Kind && evt.Kind < 40000 {
|
||||||
// parameterized replaceable event, delete before storing
|
// parameterized replaceable event, delete before storing
|
||||||
d := evt.Tags.GetFirst([]string{"d", ""})
|
d := evt.Tags.GetFirst([]string{"d", ""})
|
||||||
if d == nil {
|
if d != nil {
|
||||||
return fmt.Errorf("invalid: missing 'd' tag on parameterized replaceable event")
|
for _, query := range rl.QueryEvents {
|
||||||
}
|
ch, err := query(ctx, nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{d.Value()}}})
|
||||||
|
if err != nil {
|
||||||
filter := nostr.Filter{Authors: []string{evt.PubKey}, Kinds: []int{evt.Kind}, Tags: nostr.TagMap{"d": []string{(*d)[1]}}}
|
continue
|
||||||
for _, query := range rl.QueryEvents {
|
}
|
||||||
ch, err := query(ctx, filter)
|
if previous := <-ch; previous != nil && isOlder(previous, evt) {
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for previous := range ch {
|
|
||||||
if isOlder(previous, evt) {
|
|
||||||
for _, del := range rl.DeleteEvent {
|
for _, del := range rl.DeleteEvent {
|
||||||
del(ctx, previous)
|
del(ctx, previous)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,7 @@ func (rl *Relay) handleDeleteRequest(ctx context.Context, evt *nostr.Event) erro
|
|||||||
if acceptDeletion {
|
if acceptDeletion {
|
||||||
// delete it
|
// delete it
|
||||||
for _, del := range rl.DeleteEvent {
|
for _, del := range rl.DeleteEvent {
|
||||||
if err := del(ctx, target); err != nil {
|
del(ctx, target)
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// fail and stop here
|
// fail and stop here
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/fiatjaf/khatru"
|
"github.com/fiatjaf/khatru"
|
||||||
"github.com/fiatjaf/khatru/policies"
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,11 +53,6 @@ func main() {
|
|||||||
|
|
||||||
// there are many other configurable things you can set
|
// there are many other configurable things you can set
|
||||||
relay.RejectEvent = append(relay.RejectEvent,
|
relay.RejectEvent = append(relay.RejectEvent,
|
||||||
// built-in policies
|
|
||||||
policies.ValidateKind,
|
|
||||||
|
|
||||||
// define your own policies
|
|
||||||
policies.PreventLargeTags(80),
|
|
||||||
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
||||||
return true, "we don't allow this person to write here"
|
return true, "we don't allow this person to write here"
|
||||||
@@ -69,10 +63,6 @@ func main() {
|
|||||||
|
|
||||||
// you can request auth by rejecting an event or a request with the prefix "auth-required: "
|
// you can request auth by rejecting an event or a request with the prefix "auth-required: "
|
||||||
relay.RejectFilter = append(relay.RejectFilter,
|
relay.RejectFilter = append(relay.RejectFilter,
|
||||||
// built-in policies
|
|
||||||
policies.NoComplexFilters,
|
|
||||||
|
|
||||||
// define your own policies
|
|
||||||
func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||||
if pubkey := khatru.GetAuthed(ctx); pubkey != "" {
|
if pubkey := khatru.GetAuthed(ctx); pubkey != "" {
|
||||||
log.Printf("request from %s\n", pubkey)
|
log.Printf("request from %s\n", pubkey)
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -5,7 +5,7 @@ go 1.21.4
|
|||||||
require (
|
require (
|
||||||
github.com/fasthttp/websocket v1.5.7
|
github.com/fasthttp/websocket v1.5.7
|
||||||
github.com/fiatjaf/eventstore v0.3.8
|
github.com/fiatjaf/eventstore v0.3.8
|
||||||
github.com/nbd-wtf/go-nostr v0.30.0
|
github.com/nbd-wtf/go-nostr v0.28.1
|
||||||
github.com/puzpuzpuz/xsync/v3 v3.0.2
|
github.com/puzpuzpuz/xsync/v3 v3.0.2
|
||||||
github.com/rs/cors v1.7.0
|
github.com/rs/cors v1.7.0
|
||||||
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a
|
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -115,8 +115,6 @@ github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+
|
|||||||
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||||
github.com/nbd-wtf/go-nostr v0.28.1 h1:XQi/lBsigBXHRm7IDBJE7SR9citCh9srgf8sA5iVW3A=
|
github.com/nbd-wtf/go-nostr v0.28.1 h1:XQi/lBsigBXHRm7IDBJE7SR9citCh9srgf8sA5iVW3A=
|
||||||
github.com/nbd-wtf/go-nostr v0.28.1/go.mod h1:OQ8sNLFJnsj17BdqZiLSmjJBIFTfDqckEYC3utS4qoY=
|
github.com/nbd-wtf/go-nostr v0.28.1/go.mod h1:OQ8sNLFJnsj17BdqZiLSmjJBIFTfDqckEYC3utS4qoY=
|
||||||
github.com/nbd-wtf/go-nostr v0.30.0 h1:rN085pe4IxmSBVht8LChZbWLggonjA8hPIk8l4/+Hjk=
|
|
||||||
github.com/nbd-wtf/go-nostr v0.30.0/go.mod h1:tiKJY6fWYSujbTQb201Y+IQ3l4szqYVt+fsTnsm7FCk=
|
|
||||||
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=
|
||||||
|
|||||||
@@ -272,6 +272,7 @@ func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Type", "application/nostr+json")
|
w.Header().Set("Content-Type", "application/nostr+json")
|
||||||
|
|
||||||
info := *rl.Info
|
info := *rl.Info
|
||||||
|
|
||||||
for _, ovw := range rl.OverwriteRelayInformation {
|
for _, ovw := range rl.OverwriteRelayInformation {
|
||||||
info = ovw(r.Context(), r, info)
|
info = ovw(r.Context(), r, info)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package policies
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -14,9 +14,6 @@ import (
|
|||||||
// If ignoreKinds is given this restriction will not apply to these kinds (useful for allowing a bigger).
|
// If ignoreKinds is given this restriction will not apply to these kinds (useful for allowing a bigger).
|
||||||
// If onlyKinds is given then all other kinds will be ignored.
|
// If onlyKinds is given then all other kinds will be ignored.
|
||||||
func PreventTooManyIndexableTags(max int, ignoreKinds []int, onlyKinds []int) func(context.Context, *nostr.Event) (bool, string) {
|
func PreventTooManyIndexableTags(max int, ignoreKinds []int, onlyKinds []int) func(context.Context, *nostr.Event) (bool, string) {
|
||||||
slices.Sort(ignoreKinds)
|
|
||||||
slices.Sort(onlyKinds)
|
|
||||||
|
|
||||||
ignore := func(kind int) bool { return false }
|
ignore := func(kind int) bool { return false }
|
||||||
if len(ignoreKinds) > 0 {
|
if len(ignoreKinds) > 0 {
|
||||||
ignore = func(kind int) bool {
|
ignore = func(kind int) bool {
|
||||||
@@ -77,25 +74,21 @@ func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Even
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the kinds in increasing order
|
|
||||||
slices.Sort(kinds)
|
|
||||||
|
|
||||||
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
// these are cheap and very questionable optimizations, but they exist for a reason:
|
// these are cheap and very questionable optimizations, but they exist for a reason:
|
||||||
// we would have to ensure that the kind number is within the bounds of a uint16 anyway
|
// we would have to ensure that the kind number is within the bounds of a uint16 anyway
|
||||||
if event.Kind > max {
|
if event.Kind > max {
|
||||||
return true, fmt.Sprintf("event kind not allowed (it should be lower than %d)", max)
|
return true, "event kind not allowed"
|
||||||
}
|
}
|
||||||
if event.Kind < min {
|
if event.Kind < min {
|
||||||
return true, fmt.Sprintf("event kind not allowed (it should be higher than %d)", min)
|
return true, "event kind not allowed"
|
||||||
}
|
}
|
||||||
|
|
||||||
// hopefully this map of uint16s is very fast
|
// hopefully this map of uint16s is very fast
|
||||||
if _, allowed := slices.BinarySearch(kinds, uint16(event.Kind)); allowed {
|
if _, allowed := slices.BinarySearch(kinds, uint16(event.Kind)); allowed {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
return true, "event kind not allowed"
|
||||||
return true, fmt.Sprintf("received event kind %d not allowed", event.Kind)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package policies
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -47,7 +48,7 @@ func NoSearchQueries(ctx context.Context, filter nostr.Filter) (reject bool, msg
|
|||||||
func RemoveSearchQueries(ctx context.Context, filter *nostr.Filter) {
|
func RemoveSearchQueries(ctx context.Context, filter *nostr.Filter) {
|
||||||
if filter.Search != "" {
|
if filter.Search != "" {
|
||||||
filter.Search = ""
|
filter.Search = ""
|
||||||
filter.LimitZero = true // signals that this query should be just skipped
|
filter.Limit = -1 // signals that this query should be just skipped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ func RemoveAllButKinds(kinds ...uint16) func(context.Context, *nostr.Filter) {
|
|||||||
}
|
}
|
||||||
filter.Kinds = newKinds
|
filter.Kinds = newKinds
|
||||||
if len(filter.Kinds) == 0 {
|
if len(filter.Kinds) == 0 {
|
||||||
filter.LimitZero = true // signals that this query should be just skipped
|
filter.Limit = -1 // signals that this query should be just skipped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,7 @@ func RemoveAllButTags(tagNames ...string) func(context.Context, *nostr.Filter) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(filter.Tags) == 0 {
|
if len(filter.Tags) == 0 {
|
||||||
filter.LimitZero = true // signals that this query should be just skipped
|
filter.Limit = -1 // signals that this query should be just skipped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package policies
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ValidateKind(ctx context.Context, evt *nostr.Event) (bool, string) {
|
|
||||||
switch evt.Kind {
|
|
||||||
case 0:
|
|
||||||
var m struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
json.Unmarshal([]byte(evt.Content), &m)
|
|
||||||
if m.Name == "" {
|
|
||||||
return true, "missing json name in kind 0"
|
|
||||||
}
|
|
||||||
case 1:
|
|
||||||
return false, ""
|
|
||||||
case 2:
|
|
||||||
return true, "this kind has been deprecated"
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: all other kinds
|
|
||||||
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
74
relay.go
74
relay.go
@@ -2,6 +2,7 @@ package khatru
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,32 +14,6 @@ import (
|
|||||||
"github.com/puzpuzpuz/xsync/v3"
|
"github.com/puzpuzpuz/xsync/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRelay() *Relay {
|
|
||||||
return &Relay{
|
|
||||||
Log: log.New(os.Stderr, "[khatru-relay] ", log.LstdFlags),
|
|
||||||
|
|
||||||
Info: &nip11.RelayInformationDocument{
|
|
||||||
Software: "https://github.com/fiatjaf/khatru",
|
|
||||||
Version: "n/a",
|
|
||||||
SupportedNIPs: []int{1, 11, 70},
|
|
||||||
},
|
|
||||||
|
|
||||||
upgrader: websocket.Upgrader{
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
},
|
|
||||||
|
|
||||||
clients: xsync.NewMapOf[*websocket.Conn, struct{}](),
|
|
||||||
serveMux: &http.ServeMux{},
|
|
||||||
|
|
||||||
WriteWait: 10 * time.Second,
|
|
||||||
PongWait: 60 * time.Second,
|
|
||||||
PingPeriod: 30 * time.Second,
|
|
||||||
MaxMessageSize: 512000,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Relay struct {
|
type Relay struct {
|
||||||
ServiceURL string
|
ServiceURL string
|
||||||
|
|
||||||
@@ -59,7 +34,7 @@ type Relay struct {
|
|||||||
OnEventSaved []func(ctx context.Context, event *nostr.Event)
|
OnEventSaved []func(ctx context.Context, event *nostr.Event)
|
||||||
OnEphemeralEvent []func(ctx context.Context, event *nostr.Event)
|
OnEphemeralEvent []func(ctx context.Context, event *nostr.Event)
|
||||||
|
|
||||||
// editing info will affect
|
// editing info will affect the responses to the NIP-11 endpoint
|
||||||
Info *nip11.RelayInformationDocument
|
Info *nip11.RelayInformationDocument
|
||||||
|
|
||||||
// Default logger, as set by NewServer, is a stdlib logger prefixed with "[khatru-relay] ",
|
// Default logger, as set by NewServer, is a stdlib logger prefixed with "[khatru-relay] ",
|
||||||
@@ -82,4 +57,49 @@ type Relay struct {
|
|||||||
PongWait time.Duration // Time allowed to read the next pong message from the peer.
|
PongWait time.Duration // Time allowed to read the next pong message from the peer.
|
||||||
PingPeriod time.Duration // Send pings to peer with this period. Must be less than pongWait.
|
PingPeriod time.Duration // Send pings to peer with this period. Must be less than pongWait.
|
||||||
MaxMessageSize int64 // Maximum message size allowed from peer.
|
MaxMessageSize int64 // Maximum message size allowed from peer.
|
||||||
|
|
||||||
|
// this context is used for all things inside the relay
|
||||||
|
Context context.Context
|
||||||
|
cancel context.CancelCauseFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRelay() *Relay {
|
||||||
|
return NewRelayWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRelayWithContext(ctx context.Context) *Relay {
|
||||||
|
ctx, cancel := context.WithCancelCause(ctx)
|
||||||
|
|
||||||
|
rl := &Relay{
|
||||||
|
Log: log.New(os.Stderr, "[khatru-relay] ", log.LstdFlags),
|
||||||
|
|
||||||
|
Info: &nip11.RelayInformationDocument{
|
||||||
|
Software: "https://github.com/fiatjaf/khatru",
|
||||||
|
Version: "n/a",
|
||||||
|
SupportedNIPs: []int{1, 11, 70},
|
||||||
|
},
|
||||||
|
|
||||||
|
upgrader: websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
},
|
||||||
|
|
||||||
|
clients: xsync.NewMapOf[*websocket.Conn, struct{}](),
|
||||||
|
serveMux: &http.ServeMux{},
|
||||||
|
|
||||||
|
WriteWait: 10 * time.Second,
|
||||||
|
PongWait: 60 * time.Second,
|
||||||
|
PingPeriod: 30 * time.Second,
|
||||||
|
MaxMessageSize: 512000,
|
||||||
|
|
||||||
|
Context: ctx,
|
||||||
|
cancel: cancel,
|
||||||
|
}
|
||||||
|
|
||||||
|
return rl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rl *Relay) Close() {
|
||||||
|
rl.cancel(fmt.Errorf("Close called"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGr
|
|||||||
ovw(ctx, &filter)
|
ovw(ctx, &filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if filter.LimitZero {
|
if filter.Limit < 0 {
|
||||||
// don't do any queries, just subscribe to future events
|
// this is a special situation through which the implementor signals to us that it doesn't want
|
||||||
|
// to event perform any queries whatsoever
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGr
|
|||||||
// filter we can just reject it)
|
// filter we can just reject it)
|
||||||
for _, reject := range rl.RejectFilter {
|
for _, reject := range rl.RejectFilter {
|
||||||
if reject, msg := reject(ctx, filter); reject {
|
if reject, msg := reject(ctx, filter); reject {
|
||||||
|
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
||||||
return errors.New(nostr.NormalizeOKMessage(msg, "blocked"))
|
return errors.New(nostr.NormalizeOKMessage(msg, "blocked"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
start.go
3
start.go
@@ -2,6 +2,7 @@ package khatru
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -48,6 +49,8 @@ func (rl *Relay) Start(host string, port int, started ...chan bool) error {
|
|||||||
|
|
||||||
// Shutdown sends a websocket close control message to all connected clients.
|
// Shutdown sends a websocket close control message to all connected clients.
|
||||||
func (rl *Relay) Shutdown(ctx context.Context) {
|
func (rl *Relay) Shutdown(ctx context.Context) {
|
||||||
|
rl.cancel(fmt.Errorf("Shutdown called"))
|
||||||
|
|
||||||
rl.httpServer.Shutdown(ctx)
|
rl.httpServer.Shutdown(ctx)
|
||||||
|
|
||||||
rl.clients.Range(func(conn *websocket.Conn, _ struct{}) bool {
|
rl.clients.Range(func(conn *websocket.Conn, _ struct{}) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user