mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-05-05 19:28:20 +02:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78dd138ca8 | ||
|
|
6c1a030ad2 | ||
|
|
270096debb | ||
|
|
487b84cf2d | ||
|
|
b277dae743 | ||
|
|
1e51cdbc07 | ||
|
|
a15cd4e545 | ||
|
|
e6078b1a68 | ||
|
|
0ad33f78f1 |
14
.github/workflows/test.yml
vendored
Normal file
14
.github/workflows/test.yml
vendored
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
name: test every commit
|
||||||
|
on:
|
||||||
|
- push
|
||||||
|
- pull_request
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions/setup-go@v3
|
||||||
|
with:
|
||||||
|
go-version-file: ./go.mod
|
||||||
|
- run: go test ./...
|
||||||
121
README.md
121
README.md
@@ -1 +1,120 @@
|
|||||||
khatru
|
# khatru, a relay framework [](https://pkg.go.dev/github.com/fiatjaf/khatru#Relay)
|
||||||
|
|
||||||
|
[](https://github.com/fiatjaf/khatru/actions/workflows/test.yml)
|
||||||
|
[](https://pkg.go.dev/github.com/fiatjaf/khatru)
|
||||||
|
[](https://goreportcard.com/report/github.com/fiatjaf/khatru)
|
||||||
|
|
||||||
|
Khatru makes it easy to write very very custom relays:
|
||||||
|
|
||||||
|
- custom event or filter acceptance policies
|
||||||
|
- custom `AUTH` handlers
|
||||||
|
- custom storage and pluggable databases
|
||||||
|
- custom webpages and other HTTP handlers
|
||||||
|
|
||||||
|
Here's a sample:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/fiatjaf/khatru"
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// create the relay instance
|
||||||
|
relay := khatru.NewRelay()
|
||||||
|
|
||||||
|
// set up some basic properties (will be returned on the NIP-11 endpoint)
|
||||||
|
relay.Name = "my relay"
|
||||||
|
relay.PubKey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||||
|
relay.Description = "this is my custom relay"
|
||||||
|
relay.IconURL = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images"
|
||||||
|
|
||||||
|
// you must bring your own storage scheme -- if you want to have any
|
||||||
|
store := make(map[string]*nostr.Event, 120)
|
||||||
|
|
||||||
|
// set up the basic relay functions
|
||||||
|
relay.StoreEvent = append(relay.StoreEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) error {
|
||||||
|
store[event.ID] = event
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.QueryEvents = append(relay.QueryEvents,
|
||||||
|
func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
|
||||||
|
ch := make(chan *nostr.Event)
|
||||||
|
go func() {
|
||||||
|
for _, evt := range store {
|
||||||
|
if filter.Matches(evt) {
|
||||||
|
ch <- evt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
return ch, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.DeleteEvent = append(relay.DeleteEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) error {
|
||||||
|
delete(store, event.ID)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// there are many other configurable things you can set
|
||||||
|
relay.RejectEvent = append(relay.RejectEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
|
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
||||||
|
return true, "we don't allow this person to write here"
|
||||||
|
}
|
||||||
|
return false, "" // anyone else can
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.OnConnect = append(relay.OnConnect,
|
||||||
|
func(ctx context.Context) {
|
||||||
|
// request NIP-42 AUTH from everybody
|
||||||
|
relay.RequestAuth(ctx)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.OnAuth = append(relay.OnAuth,
|
||||||
|
func(ctx context.Context, pubkey string) {
|
||||||
|
// and when they auth we just log that for nothing
|
||||||
|
log.Println(pubkey + " is authed!")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
// check the docs for more goodies!
|
||||||
|
|
||||||
|
mux := relay.Router()
|
||||||
|
// set up other http handlers
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("content-type", "text/html")
|
||||||
|
fmt.Fprintf(w, `<b>welcome</b> to my relay!`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// start the server
|
||||||
|
fmt.Println("running on :3334")
|
||||||
|
http.ListenAndServe(":3334", relay)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### But I don't want to write my own database!
|
||||||
|
|
||||||
|
Fear no more. Using the https://github.com/fiatjaf/eventstore module you get a bunch of compatible databases out of the box and you can just plug them into your relay. For example, [sqlite](https://pkg.go.dev/github.com/fiatjaf/eventstore/sqlite3):
|
||||||
|
|
||||||
|
```go
|
||||||
|
db := sqlite3.SQLite3Backend{DatabaseURL: "/tmp/khatru-sqlite-tmp"}
|
||||||
|
if err := db.Init(); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
|
||||||
|
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
|
||||||
|
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
|
||||||
|
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||||
|
```
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/fiatjaf/eventstore"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +39,7 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
previous := <-ch
|
if previous := <-ch; previous != nil && isOlder(previous, evt) {
|
||||||
if previous != nil {
|
|
||||||
for _, del := range rl.DeleteEvent {
|
for _, del := range rl.DeleteEvent {
|
||||||
del(ctx, previous)
|
del(ctx, previous)
|
||||||
}
|
}
|
||||||
@@ -54,8 +54,7 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
previous := <-ch
|
if previous := <-ch; previous != nil && isOlder(previous, evt) {
|
||||||
if previous != nil {
|
|
||||||
for _, del := range rl.DeleteEvent {
|
for _, del := range rl.DeleteEvent {
|
||||||
del(ctx, previous)
|
del(ctx, previous)
|
||||||
}
|
}
|
||||||
@@ -68,7 +67,7 @@ func (rl *Relay) AddEvent(ctx context.Context, evt *nostr.Event) error {
|
|||||||
for _, store := range rl.StoreEvent {
|
for _, store := range rl.StoreEvent {
|
||||||
if saveErr := store(ctx, evt); saveErr != nil {
|
if saveErr := store(ctx, evt); saveErr != nil {
|
||||||
switch saveErr {
|
switch saveErr {
|
||||||
case ErrDupEvent:
|
case eventstore.ErrDupEvent:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
errmsg := saveErr.Error()
|
errmsg := saveErr.Error()
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package khatru
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
var ErrDupEvent = fmt.Errorf("duplicate: event already exists")
|
|
||||||
@@ -27,7 +27,7 @@ func main() {
|
|||||||
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
|
||||||
|
|
||||||
relay.RejectEvent = append(relay.RejectEvent, plugins.PreventTooManyIndexableTags(10))
|
relay.RejectEvent = append(relay.RejectEvent, plugins.PreventTooManyIndexableTags(10))
|
||||||
relay.RejectFilter = append(relay.RejectFilter, plugins.NoPrefixFilters, plugins.NoComplexFilters)
|
relay.RejectFilter = append(relay.RejectFilter, plugins.NoComplexFilters)
|
||||||
|
|
||||||
relay.OnEventSaved = append(relay.OnEventSaved, func(ctx context.Context, event *nostr.Event) {
|
relay.OnEventSaved = append(relay.OnEventSaved, func(ctx context.Context, event *nostr.Event) {
|
||||||
})
|
})
|
||||||
|
|||||||
BIN
examples/readme-demo/demo-memory
Executable file
BIN
examples/readme-demo/demo-memory
Executable file
Binary file not shown.
87
examples/readme-demo/main.go
Normal file
87
examples/readme-demo/main.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/fiatjaf/khatru"
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// create the relay instance
|
||||||
|
relay := khatru.NewRelay()
|
||||||
|
|
||||||
|
// set up some basic properties (will be returned on the NIP-11 endpoint)
|
||||||
|
relay.Name = "my relay"
|
||||||
|
relay.PubKey = "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"
|
||||||
|
relay.Description = "this is my custom relay"
|
||||||
|
relay.IconURL = "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images"
|
||||||
|
|
||||||
|
// you must bring your own storage scheme -- if you want to have any
|
||||||
|
store := make(map[string]*nostr.Event, 120)
|
||||||
|
|
||||||
|
// set up the basic relay functions
|
||||||
|
relay.StoreEvent = append(relay.StoreEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) error {
|
||||||
|
store[event.ID] = event
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.QueryEvents = append(relay.QueryEvents,
|
||||||
|
func(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error) {
|
||||||
|
ch := make(chan *nostr.Event)
|
||||||
|
go func() {
|
||||||
|
for _, evt := range store {
|
||||||
|
if filter.Matches(evt) {
|
||||||
|
ch <- evt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(ch)
|
||||||
|
}()
|
||||||
|
return ch, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.DeleteEvent = append(relay.DeleteEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) error {
|
||||||
|
delete(store, event.ID)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// there are many other configurable things you can set
|
||||||
|
relay.RejectEvent = append(relay.RejectEvent,
|
||||||
|
func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
|
if event.PubKey == "fa984bd7dbb282f07e16e7ae87b26a2a7b9b90b7246a44771f0cf5ae58018f52" {
|
||||||
|
return true, "we don't allow this person to write here"
|
||||||
|
}
|
||||||
|
return false, "" // anyone else can
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.OnConnect = append(relay.OnConnect,
|
||||||
|
func(ctx context.Context) {
|
||||||
|
// request NIP-42 AUTH from everybody
|
||||||
|
relay.RequestAuth(ctx)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
relay.OnAuth = append(relay.OnAuth,
|
||||||
|
func(ctx context.Context, pubkey string) {
|
||||||
|
// and when they auth we just log that for nothing
|
||||||
|
log.Println(pubkey + " is authed!")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
// check the docs for more goodies!
|
||||||
|
|
||||||
|
mux := relay.Router()
|
||||||
|
// set up other http handlers
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("content-type", "text/html")
|
||||||
|
fmt.Fprintf(w, `<b>welcome</b> to my relay!`)
|
||||||
|
})
|
||||||
|
|
||||||
|
// start the server
|
||||||
|
fmt.Println("running on :3334")
|
||||||
|
http.ListenAndServe(":3334", relay)
|
||||||
|
}
|
||||||
32
handlers.go
32
handlers.go
@@ -168,12 +168,12 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
filter := filters[i]
|
filter := filters[i]
|
||||||
|
|
||||||
for _, reject := range rl.RejectFilter {
|
// overwrite the filter (for example, to eliminate some kinds or tags that we know we don't support)
|
||||||
if rejecting, msg := reject(ctx, filter); rejecting {
|
for _, ovw := range rl.OverwriteCountFilter {
|
||||||
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
ovw(ctx, &filter)
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// then check if we'll reject this filter
|
||||||
for _, reject := range rl.RejectCountFilter {
|
for _, reject := range rl.RejectCountFilter {
|
||||||
if rejecting, msg := reject(ctx, filter); rejecting {
|
if rejecting, msg := reject(ctx, filter); rejecting {
|
||||||
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
||||||
@@ -181,6 +181,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// run the functions to count (generally it will be just one)
|
||||||
for _, count := range rl.CountEvents {
|
for _, count := range rl.CountEvents {
|
||||||
res, err := count(ctx, filter)
|
res, err := count(ctx, filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -215,14 +216,25 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
filter := filters[i]
|
filter := filters[i]
|
||||||
|
|
||||||
for _, reject := range rl.RejectCountFilter {
|
// overwrite the filter (for example, to eliminate some kinds or
|
||||||
|
// that we know we don't support)
|
||||||
|
for _, ovw := range rl.OverwriteFilter {
|
||||||
|
ovw(ctx, &filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
// then check if we'll reject this filter (we apply this after overwriting
|
||||||
|
// because we may, for example, remove some things from the incoming filters
|
||||||
|
// that we know we don't support, and then if the end result is an empty
|
||||||
|
// filter we can just reject it)
|
||||||
|
for _, reject := range rl.RejectFilter {
|
||||||
if rejecting, msg := reject(ctx, filter); rejecting {
|
if rejecting, msg := reject(ctx, filter); rejecting {
|
||||||
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
ws.WriteJSON(nostr.NoticeEnvelope(msg))
|
||||||
eose.Done()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// run the functions to query events (generally just one,
|
||||||
|
// but we might be fetching stuff from multiple places)
|
||||||
eose.Add(len(rl.QueryEvents))
|
eose.Add(len(rl.QueryEvents))
|
||||||
for _, query := range rl.QueryEvents {
|
for _, query := range rl.QueryEvents {
|
||||||
ch, err := query(ctx, filter)
|
ch, err := query(ctx, filter)
|
||||||
@@ -294,7 +306,9 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
err := ws.WriteMessage(websocket.PingMessage, nil)
|
err := ws.WriteMessage(websocket.PingMessage, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rl.Log.Printf("error writing ping: %v; closing websocket\n", err)
|
if err.Error() != "use of closed network connection" {
|
||||||
|
rl.Log.Printf("error writing ping: %v; closing websocket\n", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -303,7 +317,7 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request) {
|
func (rl *Relay) HandleNIP11(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/nostr+json")
|
||||||
|
|
||||||
supportedNIPs := []int{9, 11, 12, 15, 16, 20, 33}
|
supportedNIPs := []int{9, 11, 12, 15, 16, 20, 33}
|
||||||
if rl.ServiceURL != "" {
|
if rl.ServiceURL != "" {
|
||||||
|
|||||||
@@ -56,3 +56,21 @@ func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Even
|
|||||||
return true, "event kind not allowed"
|
return true, "event kind not allowed"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PreventTimestampsInThePast(thresholdSeconds nostr.Timestamp) func(context.Context, *nostr.Event) (bool, string) {
|
||||||
|
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
|
if nostr.Now()-event.CreatedAt > thresholdSeconds {
|
||||||
|
return true, "event too old"
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PreventTimestampsInTheFuture(thresholdSeconds nostr.Timestamp) func(context.Context, *nostr.Event) (bool, string) {
|
||||||
|
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
|
||||||
|
if event.CreatedAt-nostr.Now() > thresholdSeconds {
|
||||||
|
return true, "event too much in the future"
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,26 +2,11 @@ package plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NoPrefixFilters(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
|
||||||
for _, id := range filter.IDs {
|
|
||||||
if len(id) != 64 {
|
|
||||||
return true, fmt.Sprintf("filters can only contain full ids")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, pk := range filter.Authors {
|
|
||||||
if len(pk) != 64 {
|
|
||||||
return true, fmt.Sprintf("filters can only contain full pubkeys")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func NoComplexFilters(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
func NoComplexFilters(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||||
items := len(filter.Tags) + len(filter.Kinds)
|
items := len(filter.Tags) + len(filter.Kinds)
|
||||||
|
|
||||||
@@ -31,3 +16,49 @@ func NoComplexFilters(ctx context.Context, filter nostr.Filter) (reject bool, ms
|
|||||||
|
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NoEmptyFilters(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||||
|
c := len(filter.Kinds) + len(filter.IDs) + len(filter.Authors)
|
||||||
|
for _, tagItems := range filter.Tags {
|
||||||
|
c += len(tagItems)
|
||||||
|
}
|
||||||
|
if c == 0 {
|
||||||
|
return true, "can't handle empty filters"
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func NoSearchQueries(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
|
||||||
|
if filter.Search != "" {
|
||||||
|
return true, "search is not supported"
|
||||||
|
}
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveSearchQueries(ctx context.Context, filter *nostr.Filter) {
|
||||||
|
filter.Search = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveAllButKinds(kinds ...uint16) func(context.Context, *nostr.Filter) {
|
||||||
|
return func(ctx context.Context, filter *nostr.Filter) {
|
||||||
|
if n := len(filter.Kinds); n > 0 {
|
||||||
|
newKinds := make([]int, 0, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
if k := filter.Kinds[i]; slices.Contains(kinds, uint16(k)) {
|
||||||
|
newKinds = append(newKinds, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
filter.Kinds = newKinds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveAllButTags(tagNames ...string) func(context.Context, *nostr.Filter) {
|
||||||
|
return func(ctx context.Context, filter *nostr.Filter) {
|
||||||
|
for tagName := range filter.Tags {
|
||||||
|
if !slices.Contains(tagNames, tagName) {
|
||||||
|
delete(filter.Tags, tagName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
2
relay.go
2
relay.go
@@ -46,6 +46,8 @@ type Relay struct {
|
|||||||
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)
|
OverwriteResponseEvent []func(ctx context.Context, event *nostr.Event)
|
||||||
|
OverwriteFilter []func(ctx context.Context, filter *nostr.Filter)
|
||||||
|
OverwriteCountFilter []func(ctx context.Context, filter *nostr.Filter)
|
||||||
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)
|
||||||
|
|||||||
@@ -1,93 +0,0 @@
|
|||||||
package khatru
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"net/http"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gobwas/ws/wsutil"
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestServerStartShutdown(t *testing.T) {
|
|
||||||
var (
|
|
||||||
inited bool
|
|
||||||
storeInited bool
|
|
||||||
shutdown bool
|
|
||||||
)
|
|
||||||
rl := &testRelay{
|
|
||||||
name: "test server start",
|
|
||||||
init: func() error {
|
|
||||||
inited = true
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
onShutdown: func(context.Context) { shutdown = true },
|
|
||||||
storage: &testStorage{
|
|
||||||
init: func() error { storeInited = true; return nil },
|
|
||||||
},
|
|
||||||
}
|
|
||||||
srv, _ := NewServer(rl)
|
|
||||||
ready := make(chan bool)
|
|
||||||
done := make(chan error)
|
|
||||||
go func() { done <- srv.Start("127.0.0.1", 0, ready); close(done) }()
|
|
||||||
<-ready
|
|
||||||
|
|
||||||
// verify everything's initialized
|
|
||||||
if !inited {
|
|
||||||
t.Error("didn't call testRelay.init")
|
|
||||||
}
|
|
||||||
if !storeInited {
|
|
||||||
t.Error("didn't call testStorage.init")
|
|
||||||
}
|
|
||||||
|
|
||||||
// check that http requests are served
|
|
||||||
if _, err := http.Get("http://" + srv.Addr); err != nil {
|
|
||||||
t.Errorf("GET %s: %v", srv.Addr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// verify server shuts down
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
srv.Shutdown(ctx)
|
|
||||||
if !shutdown {
|
|
||||||
t.Error("didn't call testRelay.onShutdown")
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case err := <-done:
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("srv.Start: %v", err)
|
|
||||||
}
|
|
||||||
case <-time.After(time.Second):
|
|
||||||
t.Error("srv.Start too long to return")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServerShutdownWebsocket(t *testing.T) {
|
|
||||||
// set up a new relay server
|
|
||||||
srv := startTestRelay(t, &testRelay{storage: &testStorage{}})
|
|
||||||
|
|
||||||
// connect a client to it
|
|
||||||
ctx1, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
client, err := nostr.RelayConnect(ctx1, "ws://"+srv.Addr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("nostr.RelayConnectContext: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// now, shut down the server
|
|
||||||
ctx2, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
srv.Shutdown(ctx2)
|
|
||||||
|
|
||||||
// wait for the client to receive a "connection close"
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
err = client.ConnectionError
|
|
||||||
if e := errors.Unwrap(err); e != nil {
|
|
||||||
err = e
|
|
||||||
}
|
|
||||||
if _, ok := err.(wsutil.ClosedError); !ok {
|
|
||||||
t.Errorf("client.ConnextionError: %v (%T); want wsutil.ClosedError", err, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
91
util_test.go
91
util_test.go
@@ -1,91 +0,0 @@
|
|||||||
package khatru
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
|
||||||
)
|
|
||||||
|
|
||||||
func startTestRelay(t *testing.T, tr *testRelay) *Server {
|
|
||||||
t.Helper()
|
|
||||||
srv, _ := NewServer(tr)
|
|
||||||
started := make(chan bool)
|
|
||||||
go srv.Start("127.0.0.1", 0, started)
|
|
||||||
<-started
|
|
||||||
return srv
|
|
||||||
}
|
|
||||||
|
|
||||||
type testRelay struct {
|
|
||||||
name string
|
|
||||||
storage Storage
|
|
||||||
init func() error
|
|
||||||
onShutdown func(context.Context)
|
|
||||||
acceptEvent func(*nostr.Event) bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tr *testRelay) Name() string { return tr.name }
|
|
||||||
func (tr *testRelay) Storage(context.Context) Storage { return tr.storage }
|
|
||||||
|
|
||||||
func (tr *testRelay) Init() error {
|
|
||||||
if fn := tr.init; fn != nil {
|
|
||||||
return fn()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tr *testRelay) OnShutdown(ctx context.Context) {
|
|
||||||
if fn := tr.onShutdown; fn != nil {
|
|
||||||
fn(ctx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tr *testRelay) AcceptEvent(ctx context.Context, e *nostr.Event) bool {
|
|
||||||
if fn := tr.acceptEvent; fn != nil {
|
|
||||||
return fn(e)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
type testStorage struct {
|
|
||||||
init func() error
|
|
||||||
queryEvents func(context.Context, *nostr.Filter) (chan *nostr.Event, error)
|
|
||||||
deleteEvent func(ctx context.Context, id string, pubkey string) error
|
|
||||||
saveEvent func(context.Context, *nostr.Event) error
|
|
||||||
countEvents func(context.Context, *nostr.Filter) (int64, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (st *testStorage) Init() error {
|
|
||||||
if fn := st.init; fn != nil {
|
|
||||||
return fn()
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (st *testStorage) QueryEvents(ctx context.Context, f *nostr.Filter) (chan *nostr.Event, error) {
|
|
||||||
if fn := st.queryEvents; fn != nil {
|
|
||||||
return fn(ctx, f)
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (st *testStorage) DeleteEvent(ctx context.Context, id string, pubkey string) error {
|
|
||||||
if fn := st.deleteEvent; fn != nil {
|
|
||||||
return fn(ctx, id, pubkey)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (st *testStorage) SaveEvent(ctx context.Context, e *nostr.Event) error {
|
|
||||||
if fn := st.saveEvent; fn != nil {
|
|
||||||
return fn(ctx, e)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (st *testStorage) CountEvents(ctx context.Context, f *nostr.Filter) (int64, error) {
|
|
||||||
if fn := st.countEvents; fn != nil {
|
|
||||||
return fn(ctx, f)
|
|
||||||
}
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
11
utils.go
11
utils.go
@@ -5,6 +5,8 @@ import (
|
|||||||
"hash/maphash"
|
"hash/maphash"
|
||||||
"regexp"
|
"regexp"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -26,4 +28,11 @@ func GetAuthed(ctx context.Context) string {
|
|||||||
return authedPubkey.(string)
|
return authedPubkey.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pointerHasher[V any](_ maphash.Seed, k *V) uint64 { return uint64(uintptr(unsafe.Pointer(k))) }
|
func pointerHasher[V any](_ maphash.Seed, k *V) uint64 {
|
||||||
|
return uint64(uintptr(unsafe.Pointer(k)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isOlder(previous, next *nostr.Event) bool {
|
||||||
|
return previous.CreatedAt < next.CreatedAt ||
|
||||||
|
(previous.CreatedAt == next.CreatedAt && previous.ID > next.ID)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user