-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
257 lines (221 loc) · 6.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
"github.com/fiatjaf/eventstore/badger"
"github.com/fiatjaf/eventstore/lmdb"
"github.com/fiatjaf/eventstore/postgresql"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/blossom"
"github.com/joho/godotenv"
"github.com/nbd-wtf/go-nostr"
"github.com/spf13/afero"
)
type Config struct {
RelayName string
RelayPubkey string
RelayDescription string
DBEngine *string
DBPath *string
PostgresUser *string
PostgresPassword *string
PostgresDB *string
PostgresHost *string
PostgresPort *string
TeamDomain string
BlossomEnabled bool
BlossomPath *string
BlossomURL *string
}
type NostrData struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays"`
}
var data NostrData
var relay *khatru.Relay
var db DBBackend
var fs afero.Fs
var config Config
func main() {
relay = khatru.NewRelay()
config := LoadConfig()
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
fetchNostrData(config.TeamDomain)
go func() {
for {
time.Sleep(1 * time.Hour)
fetchNostrData(config.TeamDomain)
}
}()
relay.RejectEvent = append(relay.RejectEvent, func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
for _, pubkey := range data.Names {
if event.PubKey == pubkey {
return false, "" // allow
}
}
return true, "you're not part of the team"
})
if !config.BlossomEnabled {
fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
return
}
bl := blossom.New(relay, *config.BlossomURL)
bl.Store = blossom.EventStoreBlobIndexWrapper{Store: db, ServiceURL: bl.ServiceURL}
bl.StoreBlob = append(bl.StoreBlob, func(ctx context.Context, sha256 string, body []byte) error {
file, err := fs.Create(*config.BlossomPath + sha256)
if err != nil {
return err
}
if _, err := io.Copy(file, bytes.NewReader(body)); err != nil {
return err
}
return nil
})
bl.LoadBlob = append(bl.LoadBlob, func(ctx context.Context, sha256 string) (io.ReadSeeker, error) {
return fs.Open(*config.BlossomPath + sha256)
})
bl.DeleteBlob = append(bl.DeleteBlob, func(ctx context.Context, sha256 string) error {
return fs.Remove(*config.BlossomPath + sha256)
})
bl.RejectUpload = append(bl.RejectUpload, func(ctx context.Context, event *nostr.Event, size int, ext string) (bool, string, int) {
for _, pubkey := range data.Names {
if pubkey == event.PubKey {
return false, ext, size
}
}
return true, "you're not part of the team'", 403
})
fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
}
func fetchNostrData(teamDomain string) {
response, err := http.Get("https://" + teamDomain + "/.well-known/nostr.json")
if err != nil {
log.Printf("Error getting well known file: %v", err)
return
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
return
}
var newData NostrData
err = json.Unmarshal(body, &newData)
if err != nil {
log.Printf("Error unmarshalling JSON: %v", err)
return
}
data = newData
for pubkey, names := range data.Names {
fmt.Println(pubkey, names)
}
log.Println("Updated NostrData from .well-known file")
}
func LoadConfig() Config {
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
config = Config{
RelayName: getEnv("RELAY_NAME"),
RelayPubkey: getEnv("RELAY_PUBKEY"),
RelayDescription: getEnv("RELAY_DESCRIPTION"),
DBEngine: getEnvNullable("DB_ENGINE"),
DBPath: getEnvNullable("DB_PATH"),
PostgresUser: getEnvNullable("POSTGRES_USER"),
PostgresPassword: getEnvNullable("POSTGRES_PASSWORD"),
PostgresDB: getEnvNullable("POSTGRES_DB"),
PostgresHost: getEnvNullable("POSTGRES_HOST"),
PostgresPort: getEnvNullable("POSTGRES_PORT"),
TeamDomain: getEnv("TEAM_DOMAIN"),
BlossomEnabled: getEnvBool("BLOSSOM_ENABLED"),
BlossomPath: getEnvNullable("BLOSSOM_PATH"),
BlossomURL: getEnvNullable("BLOSSOM_URL"),
}
relay.Info.Name = config.RelayName
relay.Info.PubKey = config.RelayPubkey
relay.Info.Description = config.RelayDescription
if config.DBPath == nil {
defaultPath := "db/"
config.DBPath = &defaultPath
}
db = newDBBackend(*config.DBPath)
if err := db.Init(); err != nil {
panic(err)
}
fs = afero.NewOsFs()
if config.BlossomEnabled {
if config.BlossomPath == nil {
log.Fatalf("Blossom enabled but no path set")
}
fs.MkdirAll(*config.BlossomPath, 0755)
}
return config
}
func getEnv(key string) string {
value, exists := os.LookupEnv(key)
if !exists {
log.Fatalf("Environment variable %s not set", key)
}
return value
}
func getEnvBool(key string) bool {
value, exists := os.LookupEnv(key)
if !exists {
return false
}
return value == "true"
}
func getEnvNullable(key string) *string {
value, exists := os.LookupEnv(key)
if !exists {
return nil
}
return &value
}
type DBBackend interface {
Init() error
Close()
CountEvents(ctx context.Context, filter nostr.Filter) (int64, error)
DeleteEvent(ctx context.Context, evt *nostr.Event) error
QueryEvents(ctx context.Context, filter nostr.Filter) (chan *nostr.Event, error)
SaveEvent(ctx context.Context, evt *nostr.Event) error
ReplaceEvent(ctx context.Context, evt *nostr.Event) error
}
func newDBBackend(path string) DBBackend {
if config.DBEngine == nil {
defaultEngine := "postgres"
config.DBEngine = &defaultEngine
}
switch *config.DBEngine {
case "lmdb":
return newLMDBBackend(path)
case "badger":
return &badger.BadgerBackend{
Path: path,
}
default:
return newPostgresBackend()
}
}
func newLMDBBackend(path string) *lmdb.LMDBBackend {
return &lmdb.LMDBBackend{
Path: path,
}
}
func newPostgresBackend() DBBackend {
return &postgresql.PostgresBackend{
DatabaseURL: fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
*config.PostgresUser, *config.PostgresPassword, *config.PostgresHost, *config.PostgresPort, *config.PostgresDB),
}
}