From 26fb4886ba18a084e9fbda60fe6e23f09e30237d Mon Sep 17 00:00:00 2001 From: Asara Date: Tue, 13 Aug 2024 23:09:59 -0400 Subject: [PATCH 1/3] feat: add nostr relay for registered users --- go.mod | 31 ++++++++++++++++++-- go.sum | 62 ++++++++++++++++++++++++++++++++++++++- main.go | 8 +++++ nostr/nostr.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 176 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 6f3c9c0..fe50ecc 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,43 @@ module git.minhas.io/asara/well-goknown -go 1.19 +go 1.21.4 + +toolchain go1.22.6 require ( git.minhas.io/asara/gologger v0.7.0 + github.com/fiatjaf/eventstore v0.5.1 + github.com/fiatjaf/khatru v0.7.4 github.com/jmoiron/sqlx v1.4.0 github.com/lib/pq v1.10.9 + github.com/nbd-wtf/go-nostr v0.34.5 ) require ( + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect + github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect + github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect + github.com/fasthttp/websocket v1.5.7 // indirect + github.com/gobwas/httphead v0.1.0 // indirect + github.com/gobwas/pool v0.2.1 // indirect + github.com/gobwas/ws v1.3.1 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/klauspost/compress v1.17.8 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/puzpuzpuz/xsync/v3 v3.0.2 // indirect + github.com/rs/cors v1.7.0 // indirect github.com/rs/zerolog v1.30.0 // indirect - golang.org/x/sys v0.6.0 // indirect + github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect + github.com/tidwall/gjson v1.17.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.51.0 // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/net v0.18.0 // indirect + golang.org/x/sys v0.20.0 // indirect ) diff --git a/go.sum b/go.sum index aa10b1a..23a606e 100644 --- a/go.sum +++ b/go.sum @@ -2,25 +2,85 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= git.minhas.io/asara/gologger v0.7.0 h1:pY3III9B/gBIXKyCHuyXwUV+4cBUj0swZciXQtsgVlY= git.minhas.io/asara/gologger v0.7.0/go.mod h1:5VKIJo/rWCJayKr4PteS9s9LE6zPlrsOC70C86/Zipg= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= +github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9w277ABlgELO7H04IM= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/fasthttp/websocket v1.5.7 h1:0a6o2OfeATvtGgoMKleURhLT6JqWPg7fYfWnH4KHau4= +github.com/fasthttp/websocket v1.5.7/go.mod h1:bC4fxSono9czeXHQUVKxsC0sNjbm7lPJR04GDFqClfU= +github.com/fiatjaf/eventstore v0.5.1 h1:tTh+JYP0RME51VY2QB2Gvtzj6QTaZAnSVhgZtrYqY2A= +github.com/fiatjaf/eventstore v0.5.1/go.mod h1:r5yCFmrVNT2b1xUOuMnDVS3xPGh97y8IgTcLyY2rYP8= +github.com/fiatjaf/khatru v0.7.4 h1:o2vdwnKVO1eJMklhs9deIh7MsL7DqdgMNeRWiDdNE2U= +github.com/fiatjaf/khatru v0.7.4/go.mod h1:WVqij7X9Vr9UAMIwafQbKVFKxc42Np37vyficwUr/nQ= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= +github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= +github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM= +github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= +github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= +github.com/gobwas/ws v1.3.1 h1:Qi34dfLMWJbiKaNbDVzM9x27nZBjmkaW6i4+Ku+pGVU= +github.com/gobwas/ws v1.3.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= +github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/nbd-wtf/go-nostr v0.34.5 h1:vti8WqvGWbVoWAPniaz7li2TpCyC+7ZS62Gmy7ib/z0= +github.com/nbd-wtf/go-nostr v0.34.5/go.mod h1:NZQkxl96ggbO8rvDpVjcsojJqKTPwqhP4i82O7K5DJs= 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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/puzpuzpuz/xsync/v3 v3.0.2 h1:3yESHrRFYr6xzkz61LLkvNiPFXxJEAABanTQpKbAaew= +github.com/puzpuzpuz/xsync/v3 v3.0.2/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= +github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= +github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c= github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= +github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM= +github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= +github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index b6d4547..c94f3e0 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "git.minhas.io/asara/well-goknown/db" "git.minhas.io/asara/well-goknown/matrix" "git.minhas.io/asara/well-goknown/nostr" + "github.com/fiatjaf/eventstore/postgresql" ) func main() { @@ -16,6 +17,12 @@ func main() { defer db.Close() nostr.DB = db + nostr.RelayDb = postgresql.PostgresBackend{DatabaseURL: config.GetConfig().DbUrl} + if err := nostr.RelayDb.Init(); err != nil { + l.Panic().Msgf("unable to connect to relay db: %s", err.Error()) + } + + relay := nostr.NewRelay() // matrix endpoints l.Debug().Msg("enabling matrix well-known endpoints") @@ -25,6 +32,7 @@ func main() { // nostr endpoints l.Debug().Msg("enabling nostr well-known endpoints") http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr) + http.Handle("/relay", relay) // start server port := config.GetConfig().ListenAddr diff --git a/nostr/nostr.go b/nostr/nostr.go index 2ab3596..de31d03 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -1,18 +1,27 @@ package nostr import ( + "context" "encoding/json" + "fmt" "net" "net/http" + "slices" "strings" "git.minhas.io/asara/gologger" "git.minhas.io/asara/well-goknown/config" + "github.com/fiatjaf/eventstore/postgresql" + "github.com/fiatjaf/khatru" + "github.com/fiatjaf/khatru/policies" "github.com/jmoiron/sqlx" + "github.com/nbd-wtf/go-nostr" ) var ( - DB *sqlx.DB + DB *sqlx.DB + RelayDb postgresql.PostgresBackend + relay *khatru.Relay ) type nostrUser struct { @@ -99,3 +108,71 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) { w.Write(j) return } + +func NewRelay() *khatru.Relay { + // relay configuration + relay = khatru.NewRelay() + relay.Info.Name = "Devvul Nostr Relay" + relay.Info.PubKey = "ac7d66c7065e211b27759ffe2b333fde34e307cead79ce7c00f401d8fa6c08d1" + relay.Info.Description = "nostr relay for devvul users" + relay.Info.Icon = "https://pfp.nostr.build/ee442b3c54143099fb483b5e3a6e1e41144df5c7457313a30586a082baa1babc.png" + relay.Info.Version = "0.0.1" + relay.Info.Contact = "asara@devvul.com" + relay.Info.Software = "https://git.devvul.com/asara/well-goknown" + // contact lists + relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 2) + // dms + relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 4) + // sort relays + slices.Sort(relay.Info.SupportedNIPs) + + // storage + relay.StoreEvent = append(relay.StoreEvent, RelayDb.SaveEvent) + relay.QueryEvents = append(relay.QueryEvents, RelayDb.QueryEvents) + relay.CountEvents = append(relay.CountEvents, RelayDb.CountEvents) + relay.DeleteEvent = append(relay.DeleteEvent, RelayDb.DeleteEvent) + + // apply policies + policies.ApplySaneDefaults(relay) + + relay.RejectEvent = append( + relay.RejectEvent, + RejectUnregisteredNpubs, + policies.PreventLargeTags(80), + policies.RejectEventsWithBase64Media, + policies.ValidateKind, + ) + + relay.RejectFilter = append( + relay.RejectFilter, + policies.RejectKind04Snoopers, + policies.AntiSyncBots, + policies.NoComplexFilters, + policies.NoEmptyFilters, + ) + return relay +} + +func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bool, msg string) { + l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "nostr-reject-unregistered").Logger() + + // reject nip-04 messages to users who aren't registered + if event.Kind == 4 { + receiver := event.Tags.GetFirst([]string{"p"}).Value() + var uid int + err := DB.QueryRow("SELECT id FROM users WHERE pubkey=$1", receiver).Scan(&uid) + if err != nil { + l.Debug().Msgf("recipient pubkey %s not found: %s", receiver, err.Error()) + return true, fmt.Sprintf("recipient pubkey %s is not registered to any users", receiver) + } + } + + // check if user is registered + var uid int + err := DB.QueryRow("SELECT id FROM users WHERE pubkey=$1", event.PubKey).Scan(&uid) + if err != nil { + l.Debug().Msgf("poster pubkey %s not found: %s", event.PubKey, err.Error()) + return true, fmt.Sprintf("pubkey %s is not registered to any users", event.PubKey) + } + return false, "" +} -- 2.45.2 From d6a09a364b55b58a58fb2e10af15a5ca3fc3b574 Mon Sep 17 00:00:00 2001 From: Asara Date: Wed, 14 Aug 2024 20:38:38 -0400 Subject: [PATCH 2/3] chore: cleanup and add auth --- db/db.go | 4 ++-- go.mod | 18 ++++++++---------- go.sum | 29 +++++++++++++++-------------- main.go | 10 +++++----- matrix/matrix.go | 4 ++-- nostr/nostr.go | 12 +++++++----- 6 files changed, 39 insertions(+), 38 deletions(-) diff --git a/db/db.go b/db/db.go index 007a793..d115120 100644 --- a/db/db.go +++ b/db/db.go @@ -1,8 +1,8 @@ package db import ( - "git.minhas.io/asara/gologger" - "git.minhas.io/asara/well-goknown/config" + "git.devvul.com/asara/gologger" + "git.devvul.com/asara/well-goknown/config" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" ) diff --git a/go.mod b/go.mod index fe50ecc..6d35e60 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,11 @@ -module git.minhas.io/asara/well-goknown +module git.devvul.com/asara/well-goknown -go 1.21.4 - -toolchain go1.22.6 +go 1.23.0 require ( - git.minhas.io/asara/gologger v0.7.0 - github.com/fiatjaf/eventstore v0.5.1 - github.com/fiatjaf/khatru v0.7.4 + git.devvul.com/asara/gologger v0.8.0 + github.com/fiatjaf/eventstore v0.7.1 + github.com/fiatjaf/khatru v0.7.5 github.com/jmoiron/sqlx v1.4.0 github.com/lib/pq v1.10.9 github.com/nbd-wtf/go-nostr v0.34.5 @@ -26,11 +24,11 @@ require ( github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.17.8 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/puzpuzpuz/xsync/v3 v3.0.2 // indirect github.com/rs/cors v1.7.0 // indirect - github.com/rs/zerolog v1.30.0 // indirect + github.com/rs/zerolog v1.33.0 // indirect github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect github.com/tidwall/gjson v1.17.0 // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/go.sum b/go.sum index 23a606e..335d9d6 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -git.minhas.io/asara/gologger v0.7.0 h1:pY3III9B/gBIXKyCHuyXwUV+4cBUj0swZciXQtsgVlY= -git.minhas.io/asara/gologger v0.7.0/go.mod h1:5VKIJo/rWCJayKr4PteS9s9LE6zPlrsOC70C86/Zipg= +git.devvul.com/asara/gologger v0.8.0 h1:UsBgNr1ZNpDVqVMTNy3KzSV1afqP0vmTiZvKXZ4YoGk= +git.devvul.com/asara/gologger v0.8.0/go.mod h1:APr1DdVYByFfPUGHqHtRMhxphQbj92/vT/t0iM40H/0= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= @@ -17,10 +17,10 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/fasthttp/websocket v1.5.7 h1:0a6o2OfeATvtGgoMKleURhLT6JqWPg7fYfWnH4KHau4= github.com/fasthttp/websocket v1.5.7/go.mod h1:bC4fxSono9czeXHQUVKxsC0sNjbm7lPJR04GDFqClfU= -github.com/fiatjaf/eventstore v0.5.1 h1:tTh+JYP0RME51VY2QB2Gvtzj6QTaZAnSVhgZtrYqY2A= -github.com/fiatjaf/eventstore v0.5.1/go.mod h1:r5yCFmrVNT2b1xUOuMnDVS3xPGh97y8IgTcLyY2rYP8= -github.com/fiatjaf/khatru v0.7.4 h1:o2vdwnKVO1eJMklhs9deIh7MsL7DqdgMNeRWiDdNE2U= -github.com/fiatjaf/khatru v0.7.4/go.mod h1:WVqij7X9Vr9UAMIwafQbKVFKxc42Np37vyficwUr/nQ= +github.com/fiatjaf/eventstore v0.7.1 h1:5f2yvEtYvsvMBNttysmXhSSum5M1qwvPzjEQ/BFue7Q= +github.com/fiatjaf/eventstore v0.7.1/go.mod h1:ek/yWbanKVG767fK51Q3+6Mvi5oEHYSsdPym40nZexw= +github.com/fiatjaf/khatru v0.7.5 h1:UFo+cdbqHDn1W4Q4h03y3vzh1BiU+6fLYK48oWU2K34= +github.com/fiatjaf/khatru v0.7.5/go.mod h1:WVqij7X9Vr9UAMIwafQbKVFKxc42Np37vyficwUr/nQ= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU= @@ -40,10 +40,11 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/nbd-wtf/go-nostr v0.34.5 h1:vti8WqvGWbVoWAPniaz7li2TpCyC+7ZS62Gmy7ib/z0= @@ -56,8 +57,8 @@ github.com/puzpuzpuz/xsync/v3 v3.0.2/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPK github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= -github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c= -github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= @@ -77,9 +78,9 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/main.go b/main.go index c94f3e0..2edb670 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,11 @@ package main import ( "net/http" - "git.minhas.io/asara/gologger" - "git.minhas.io/asara/well-goknown/config" - "git.minhas.io/asara/well-goknown/db" - "git.minhas.io/asara/well-goknown/matrix" - "git.minhas.io/asara/well-goknown/nostr" + "git.devvul.com/asara/gologger" + "git.devvul.com/asara/well-goknown/config" + "git.devvul.com/asara/well-goknown/db" + "git.devvul.com/asara/well-goknown/matrix" + "git.devvul.com/asara/well-goknown/nostr" "github.com/fiatjaf/eventstore/postgresql" ) diff --git a/matrix/matrix.go b/matrix/matrix.go index fdbb4e7..987311b 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -5,8 +5,8 @@ import ( "fmt" "net/http" - "git.minhas.io/asara/gologger" - "git.minhas.io/asara/well-goknown/config" + "git.devvul.com/asara/gologger" + "git.devvul.com/asara/well-goknown/config" ) type matrixServerWellKnown struct { diff --git a/nostr/nostr.go b/nostr/nostr.go index de31d03..6df21ca 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -6,11 +6,10 @@ import ( "fmt" "net" "net/http" - "slices" "strings" - "git.minhas.io/asara/gologger" - "git.minhas.io/asara/well-goknown/config" + "git.devvul.com/asara/gologger" + "git.devvul.com/asara/well-goknown/config" "github.com/fiatjaf/eventstore/postgresql" "github.com/fiatjaf/khatru" "github.com/fiatjaf/khatru/policies" @@ -123,8 +122,10 @@ func NewRelay() *khatru.Relay { relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 2) // dms relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 4) - // sort relays - slices.Sort(relay.Info.SupportedNIPs) + + relay.OnConnect = append(relay.OnConnect, func(ctx context.Context) { + khatru.RequestAuth(ctx) + }) // storage relay.StoreEvent = append(relay.StoreEvent, RelayDb.SaveEvent) @@ -165,6 +166,7 @@ func RejectUnregisteredNpubs(ctx context.Context, event *nostr.Event) (reject bo l.Debug().Msgf("recipient pubkey %s not found: %s", receiver, err.Error()) return true, fmt.Sprintf("recipient pubkey %s is not registered to any users", receiver) } + return false, "" } // check if user is registered -- 2.45.2 From 50c878dc679ff6a1df71beb39a8940895fdfd15d Mon Sep 17 00:00:00 2001 From: Asara Date: Wed, 14 Aug 2024 21:20:14 -0400 Subject: [PATCH 3/3] chore: variablize relay info --- README.md | 5 +++++ config/config.go | 10 ++++++++++ main.go | 6 +++++- nostr/nostr.go | 14 +++++++------- sample.env | 9 +++++++-- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c38d7ac..8ec1459 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,8 @@ GRANT ALL PRIVILEGES ON DATABASE wgk TO wgk; ALTER DATABASE wgk OWNER TO wgk; EOF ``` + +To build a release version, go to a tag and run: +``` +go build -ldflags="-X main.Commit=$(git tag)" +``` diff --git a/config/config.go b/config/config.go index 7c5f383..77c7f25 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,11 @@ type ( MatrixIdentityServer string MatrixMsc3575Address string MatrixWellKnownAddress string + RelayName string + RelayPubkey string + RelayDescription string + RelayIcon string + RelayContact string } ) @@ -23,6 +28,11 @@ func GetConfig() Config { MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""), MatrixMsc3575Address: getEnv("MATRIX_MSC3575_ADDRESS", ""), MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""), + RelayName: getEnv("RELAY_NAME", ""), + RelayPubkey: getEnv("RELAY_PUBKEY", ""), + RelayDescription: getEnv("RELAY_DESCRIPTION", ""), + RelayIcon: getEnv("RELAY_ICON", ""), + RelayContact: getEnv("RELAY_CONTACT", ""), } } diff --git a/main.go b/main.go index 2edb670..159f4ab 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,10 @@ import ( "github.com/fiatjaf/eventstore/postgresql" ) +var ( + Version string +) + func main() { l := gologger.Get(config.GetConfig().LogLevel).With().Str("context", "main").Logger() db, _ := db.NewDB() @@ -22,7 +26,7 @@ func main() { l.Panic().Msgf("unable to connect to relay db: %s", err.Error()) } - relay := nostr.NewRelay() + relay := nostr.NewRelay(Version) // matrix endpoints l.Debug().Msg("enabling matrix well-known endpoints") diff --git a/nostr/nostr.go b/nostr/nostr.go index 6df21ca..edb1701 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -108,15 +108,15 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) { return } -func NewRelay() *khatru.Relay { +func NewRelay(version string) *khatru.Relay { // relay configuration relay = khatru.NewRelay() - relay.Info.Name = "Devvul Nostr Relay" - relay.Info.PubKey = "ac7d66c7065e211b27759ffe2b333fde34e307cead79ce7c00f401d8fa6c08d1" - relay.Info.Description = "nostr relay for devvul users" - relay.Info.Icon = "https://pfp.nostr.build/ee442b3c54143099fb483b5e3a6e1e41144df5c7457313a30586a082baa1babc.png" - relay.Info.Version = "0.0.1" - relay.Info.Contact = "asara@devvul.com" + relay.Info.Name = config.GetConfig().RelayName + relay.Info.PubKey = config.GetConfig().RelayPubkey + relay.Info.Description = config.GetConfig().RelayDescription + relay.Info.Icon = config.GetConfig().RelayIcon + relay.Info.Contact = config.GetConfig().RelayContact + relay.Info.Version = version relay.Info.Software = "https://git.devvul.com/asara/well-goknown" // contact lists relay.Info.SupportedNIPs = append(relay.Info.SupportedNIPs, 2) diff --git a/sample.env b/sample.env index 5f0a635..dcbc1b6 100644 --- a/sample.env +++ b/sample.env @@ -1,5 +1,10 @@ +export DATABASE_URL="postgres://user:password@127.0.0.1:5432/db?sslmode=disable" export LISTEN_ADDR=":8090" export LOG_LEVEL=0 -export MATRIX_WELL_KNOWN_ADDRESS=matrix.example.com export MATRIX_MSC3575_ADDRESS=https://matrix.example.com -export DATABASE_URL="postgres://user:password@127.0.0.1:5432/db?sslmode=disable" +export MATRIX_WELL_KNOWN_ADDRESS=matrix.example.com +export RELAY_CONTACT="matrix:u/asara:devvul.com" +export RELAY_DESCRIPTION="nostr relay running via git.devvul.com/asara/well-goknown" +export RELAY_ICON="" +export RELAY_NAME="Nostr Relay" +export RELAY_PUBKEY="" -- 2.45.2