Switch to instance, stub out relay methods

This commit is contained in:
Jon Staab
2025-09-24 10:11:37 -07:00
parent a337da1757
commit 9eedeceb6a
11 changed files with 286 additions and 83 deletions
+86 -2
View File
@@ -1,8 +1,92 @@
package zooid
import (
"fiatjaf.com/nostr/khatru"
"bytes"
"context"
"io"
"log"
"net/url"
"fiatjaf.com/nostr"
"fiatjaf.com/nostr/eventstore/lmdb"
"fiatjaf.com/nostr/khatru/blossom"
"github.com/spf13/afero"
)
func EnableBlossom(config *Config, relay *khatru.Relay) {
func EnableBlossom(instance *Instance) {
fs := afero.NewOsFs()
if err := fs.MkdirAll(instance.Config.Blossom.Directory, 0755); err != nil {
log.Fatal("🚫 error creating blossom path:", err)
}
backend := &lmdb.LMDBBackend{Path: instance.Config.Data.Blossom}
if err := backend.Init(); err != nil {
panic(err)
}
blossom := blossom.New(instance.Relay, "https://"+instance.Host)
blossom.Store = backend
blossom.StoreBlob = func(ctx context.Context, sha256 string, ext string, body []byte) error {
file, err := fs.Create(instance.Config.Blossom.Directory + "/" + sha256)
if err != nil {
return err
}
if _, err := io.Copy(file, bytes.NewReader(body)); err != nil {
return err
}
return nil
}
blossom.LoadBlob = func(ctx context.Context, sha256 string, ext string) (io.ReadSeeker, *url.URL, error) {
file, err := fs.Open(instance.Config.Blossom.Directory + "/" + sha256)
if err != nil {
return nil, nil, err
}
return file, nil, nil
}
blossom.DeleteBlob = func(ctx context.Context, sha256 string, ext string) error {
return fs.Remove(instance.Config.Blossom.Directory + "/" + sha256)
}
blossom.RejectUpload = func(ctx context.Context, auth *nostr.Event, size int, ext string) (bool, string, int) {
if size > 10*1024*1024 {
return true, "file too large", 413
}
if auth == nil || !instance.IsMember(auth.PubKey) {
return true, "unauthorized", 403
}
return false, ext, size
}
blossom.RejectGet = func(ctx context.Context, auth *nostr.Event, sha256 string, ext string) (bool, string, int) {
if auth == nil || !instance.IsMember(auth.PubKey) {
return true, "unauthorized", 403
}
return false, "", 200
}
blossom.RejectList = func(ctx context.Context, auth *nostr.Event, pubkey nostr.PubKey) (bool, string, int) {
if auth == nil || !instance.IsMember(auth.PubKey) {
return true, "unauthorized", 403
}
return false, "", 200
}
blossom.RejectDelete = func(ctx context.Context, auth *nostr.Event, sha256 string, ext string) (bool, string, int) {
if auth == nil || !instance.IsMember(auth.PubKey) {
return true, "unauthorized", 403
}
return false, "", 200
}
}
+1
View File
@@ -38,6 +38,7 @@ type Config struct {
Data struct {
Events string `toml:"events"`
Blossom string `toml:"blossom"`
} `toml:"data"`
}
+2 -3
View File
@@ -1,9 +1,8 @@
package zooid
import (
"fiatjaf.com/nostr/khatru"
)
func EnableGroups(config *Config, relay *khatru.Relay) {
relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 29)
func EnableGroups(instance *Instance) {
instance.Relay.Info.SupportedNIPs = append(instance.Relay.Info.SupportedNIPs, 29)
}
+3 -3
View File
@@ -6,12 +6,12 @@ import (
)
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
relay, err := GetRelay(r.Host)
instance, err := GetInstance(r.Host)
if err != nil {
log.Printf("Failed to load relay config for hostname %s: %v", r.Host, err)
log.Printf("Failed to load config for hostname %s: %v", r.Host, err)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
relay.ServeHTTP(w, r)
instance.Relay.ServeHTTP(w, r)
}
+159
View File
@@ -0,0 +1,159 @@
package zooid
import (
"sync"
"iter"
"net/http"
"context"
"fiatjaf.com/nostr/khatru"
"fiatjaf.com/nostr"
)
type Instance struct {
Host string
Config *Config
Relay *khatru.Relay
}
func MakeInstance(hostname string) (*Instance, error) {
config, err := LoadConfig(hostname)
if err != nil {
return nil, err
}
pubkey, err := nostr.PubKeyFromHex(config.Self.Pubkey)
if err != nil {
return nil, err
}
// secret, err := nostr.SecretKeyFromHex(config.Self.Secret)
// if err != nil {
// return nil, err
// }
instance := &Instance{
Host: hostname,
Config: config,
Relay: khatru.NewRelay(),
}
instance.Relay.Info.Name = config.Self.Name
instance.Relay.Info.Icon = config.Self.Icon
instance.Relay.Info.PubKey = &pubkey
instance.Relay.Info.Description = config.Self.Description
// instance.Relay.Info.Self = nostr.GetPublicKey(secret)
instance.Relay.Info.Software = "https://github.com/coracle-social/zooid"
instance.Relay.Info.Version = "v0.1.0"
instance.Relay.OnConnect = instance.OnConnect
instance.Relay.OnEvent = instance.OnEvent
instance.Relay.StoreEvent = instance.StoreEvent
instance.Relay.ReplaceEvent = instance.ReplaceEvent
instance.Relay.DeleteEvent = instance.DeleteEvent
instance.Relay.OnEventSaved = instance.OnEventSaved
instance.Relay.OnEphemeralEvent = instance.OnEphemeralEvent
instance.Relay.OnRequest = instance.OnRequest
instance.Relay.QueryStored = instance.QueryStored
instance.Relay.RejectConnection = instance.RejectConnection
instance.Relay.PreventBroadcast = instance.PreventBroadcast
if config.Groups.Enabled {
EnableGroups(instance)
}
if config.Blossom.Enabled {
EnableBlossom(instance)
}
if config.Management.Enabled {
EnableManagement(instance)
}
return instance, nil
}
var (
instances map[string]*Instance
instanceOnce sync.Once
)
func GetInstance(hostname string) (*Instance, error) {
instanceOnce.Do(func() {
instances = make(map[string]*Instance)
})
instance, exists := instances[hostname]
if !exists {
newInstance, err := MakeInstance(hostname)
if err != nil {
return nil, err
}
instances[hostname] = newInstance
instance = newInstance
}
return instance, nil
}
// Utility methods
func (instance *Instance) IsMember(pubkey nostr.PubKey) bool {
pubkeyStr := pubkey.String()
for _, role := range instance.Config.Roles {
for _, pk := range role.Pubkeys {
if pk == pubkeyStr {
return true
}
}
}
return false
}
// Handlers
func (instance *Instance) OnConnect(ctx context.Context) {
khatru.RequestAuth(ctx)
}
func (instance *Instance) OnEvent(ctx context.Context, event nostr.Event) (reject bool, msg string) {
return false, ""
}
func (instance *Instance) StoreEvent(ctx context.Context, event nostr.Event) error {
return nil
}
func (instance *Instance) ReplaceEvent(ctx context.Context, event nostr.Event) error {
return nil
}
func (instance *Instance) DeleteEvent(ctx context.Context, id nostr.ID) error {
return nil
}
func (instance *Instance) OnEventSaved(ctx context.Context, event nostr.Event) {
}
func (instance *Instance) OnEphemeralEvent(ctx context.Context, event nostr.Event) {
}
func (instance *Instance) OnRequest(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
return false, ""
}
func (instance *Instance) QueryStored(ctx context.Context, filter nostr.Filter) iter.Seq[nostr.Event] {
return func(yield func(nostr.Event) bool) {
// TODO: Implement actual event querying logic
// For now, return empty sequence
}
}
func (instance *Instance) RejectConnection(r *http.Request) bool {
return false
}
func (instance *Instance) PreventBroadcast(ws *khatru.WebSocket, event nostr.Event) bool {
return event.Kind == 28934
}
+1 -2
View File
@@ -1,8 +1,7 @@
package zooid
import (
"fiatjaf.com/nostr/khatru"
)
func EnableManagement(config *Config, relay *khatru.Relay) {
func EnableManagement(instance *Instance) {
}
-66
View File
@@ -1,66 +0,0 @@
package zooid
import (
"sync"
"fiatjaf.com/nostr/khatru"
)
func MakeRelay(hostname string, config *Config) *khatru.Relay {
relay := khatru.NewRelay()
relay.Info.Name = config.Name
relay.Info.Icon = config.Icon
relay.Info.PubKey = config.Pubkey
relay.Info.Description = config.Description
relay.Info.Self = nostr.GetPublicKey(config.Secret)
relay.Info.Software = "https://github.com/coracle-social/zooid"
relay.Info.Version = "v0.1.0"
relay.OnConnect = append(relay.OnConnect, khatru.RequestAuth)
relay.RejectFilter = append(relay.RejectFilter, RejectFilter)
relay.QueryEvents = append(relay.QueryEvents, QueryEvents)
relay.DeleteEvent = append(relay.DeleteEvent, DeleteEvent)
relay.RejectEvent = append(relay.RejectEvent, RejectEvent)
relay.StoreEvent = append(relay.StoreEvent, SaveEvent)
relay.OnEventSaved = append(relay.OnEventSaved, OnEventSaved)
if config.Groups.Enabled {
EnableGroups(config, relay)
}
if config.Blossom.Enabled {
EnableBlossom(config, relay)
}
if config.Management.Enabled {
EnableManagement(config, relay)
}
return relay
}
var (
relays map[string]*khatru.Relay
relayOnce sync.Once
)
func GetRelay(hostname string) (*khatru.Relay, error) {
relayOnce.Do(func() {
relays = make(map[string]*khatru.Relay)
})
relay, exists := relays[hostname]
if !exists {
config, err := LoadConfig(hostname)
if err != nil {
return nil, err
}
newRelay := MakeRelay(hostname, config)
relays[hostname] = newRelay
relay = newRelay
}
return relay, nil
}