-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
188 lines (148 loc) · 5.46 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
package main
import (
"context"
_ "embed"
"fmt"
"html/template"
"net"
"net/http"
"os"
"os/signal"
"path"
"slices"
"strconv"
"syscall"
"github.com/fiatjaf/eventstore/badger"
"github.com/fiatjaf/eventstore/bluge"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/blossom"
"github.com/kehiy/blobstore"
"github.com/kehiy/blobstore/disk"
"github.com/kehiy/blobstore/minio"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/keyer"
"github.com/nbd-wtf/go-nostr/nip86"
)
var (
relay *khatru.Relay
config Config
plainKeyer nostr.Keyer
simplePool *nostr.SimplePool
//go:embed static/index.html
landingTempl []byte
)
func main() {
Info("Running", "version", StringVersion())
InitGlobalLogger()
LoadConfig()
relay = khatru.NewRelay()
relay.Info.Name = config.RelayName
relay.Info.Description = config.RelayDescription
relay.Info.Icon = config.RelayIcon
relay.Info.Contact = config.RelayContact
relay.Info.PubKey = config.RelayPublicKey
relay.Info.URL = config.RelayURL
relay.Info.Version = StringVersion()
relay.Info.Software = "https://github.com/dezh-tech/alienos"
relay.Info.SupportedNIPs = []any{1, 9, 11, 17, 40, 42, 50, 56, 59, 70, 86}
badgerDB := badger.BadgerBackend{
Path: path.Join(config.WorkingDirectory, "/db"),
}
if err := badgerDB.Init(); err != nil {
Fatal("can't setup db", "err", err.Error())
}
blugeDB := bluge.BlugeBackend{
Path: path.Join(config.WorkingDirectory, "/search_db"),
RawEventStore: &badgerDB,
}
if err := blugeDB.Init(); err != nil {
Fatal("can't setup db", "err", err.Error())
}
relay.StoreEvent = append(relay.StoreEvent, badgerDB.SaveEvent, blugeDB.SaveEvent, StoreEvent)
relay.QueryEvents = append(relay.QueryEvents, blugeDB.QueryEvents, badgerDB.QueryEvents)
relay.DeleteEvent = append(relay.DeleteEvent, badgerDB.DeleteEvent, blugeDB.DeleteEvent)
relay.ReplaceEvent = append(relay.ReplaceEvent, badgerDB.ReplaceEvent, blugeDB.ReplaceEvent)
relay.CountEvents = append(relay.CountEvents, badgerDB.CountEvents)
relay.CountEventsHLL = append(relay.CountEventsHLL, badgerDB.CountEventsHLL)
relay.RejectFilter = append(relay.RejectFilter, RejectFilter)
relay.RejectEvent = append(relay.RejectEvent, RejectEvent)
bl := blossom.New(relay, fmt.Sprintf("http://%s:%d", config.RelayBind, config.RelayPort))
bl.Store = blossom.EventStoreBlobIndexWrapper{Store: &badgerDB, ServiceURL: bl.ServiceURL}
if !PathExists(path.Join(config.WorkingDirectory, "/blossom")) {
if err := Mkdir(path.Join(config.WorkingDirectory, "/blossom")); err != nil {
Fatal("can't initialize blossom directory", "err", err.Error())
}
}
var blobStorage blobstore.Store
blobStorage = disk.New(path.Join(config.WorkingDirectory, "/blossom"))
if config.S3ForBlossom {
blobStorage = minio.New(config.S3Endpoint, config.S3AccessKeyID,
config.S3SecretKey, true, config.S3BlossomBucket, "")
if err := blobStorage.Init(context.Background()); err != nil {
Fatal("can't init s3 for blossom", "err", err.Error())
}
}
bl.StoreBlob = append(bl.StoreBlob, blobStorage.Store)
bl.LoadBlob = append(bl.LoadBlob, blobStorage.Load)
bl.DeleteBlob = append(bl.DeleteBlob, blobStorage.Delete)
bl.ReceiveReport = append(bl.ReceiveReport, ReceiveReport)
LoadManagement()
relay.ManagementAPI.AllowPubKey = AllowPubkey
relay.ManagementAPI.BanPubKey = BanPubkey
relay.ManagementAPI.AllowKind = AllowKind
relay.ManagementAPI.DisallowKind = DisallowKind
relay.ManagementAPI.BlockIP = BlockIP
relay.ManagementAPI.UnblockIP = UnblockIP
relay.ManagementAPI.BanEvent = BanEvent
relay.ManagementAPI.ListAllowedKinds = ListAllowedKinds
relay.ManagementAPI.ListAllowedPubKeys = ListAllowedPubKeys
relay.ManagementAPI.ListBannedEvents = ListBannedEvents
relay.ManagementAPI.ListBannedPubKeys = ListBannedPubKeys
relay.ManagementAPI.ListBlockedIPs = ListBlockedIPs
relay.ManagementAPI.ListEventsNeedingModeration = ListEventsNeedingModeration
relay.ManagementAPI.RejectAPICall = append(relay.ManagementAPI.RejectAPICall,
func(ctx context.Context, mp nip86.MethodParams) (reject bool, msg string) {
auth := khatru.GetAuthed(ctx)
if !slices.Contains(config.Admins, auth) {
return true, "your are not an admin"
}
return false, ""
})
mux := relay.Router()
mux.HandleFunc("GET /{$}", StaticViewHandler)
mux.HandleFunc("/.well-known/nostr.json", NIP05Handler)
if config.BackupEnabled {
go backupWorker()
}
simplePool = nostr.NewSimplePool(context.Background())
pKeyer, err := keyer.NewPlainKeySigner(config.RelaySelf)
if err != nil {
Fatal("can't create keyer", "err", err.Error())
}
plainKeyer = pKeyer
Info("Serving", "address", net.JoinHostPort(config.RelayBind, strconv.Itoa(config.RelayPort)))
if err := relay.Start(config.RelayBind, config.RelayPort); err != nil {
Error("can't start the server", "err", err)
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-sigChan
Info("Received signal: Initiating graceful shutdown", "signal", sig.String())
badgerDB.Close()
blugeDB.Close()
relay.Shutdown(context.Background())
}
func StaticViewHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
t := template.New("webpage")
t, err := t.Parse(string(landingTempl))
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)
return
}
err = t.Execute(w, relay.Info)
if err != nil {
http.Error(w, "Error executing template", http.StatusInternalServerError)
return
}
}