-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (125 loc) · 4.69 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
package main
import (
"context"
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"path"
"slices"
"syscall"
"github.com/fiatjaf/eventstore/badger"
"github.com/fiatjaf/eventstore/bluge"
"github.com/fiatjaf/khatru"
"github.com/fiatjaf/khatru/blossom"
"github.com/kehiy/blobstore/disk"
"github.com/nbd-wtf/go-nostr/nip86"
)
var (
relay *khatru.Relay
config Config
//go:embed static/index.html
landingTempl []byte
)
func main() {
log.SetPrefix("alienos ")
log.Printf("Running %s\n", StringVersion())
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 {
log.Fatalf("can't setup db: %s", err.Error())
}
blugeDB := bluge.BlugeBackend{
Path: path.Join(config.WorkingDirectory, "/search_db"),
RawEventStore: &badgerDB,
}
if err := blugeDB.Init(); err != nil {
log.Fatalf("can't setup db: %s", 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:%s", 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 {
log.Fatalf("can't initialize blossom directory: %s", err.Error())
}
}
blobStorage := disk.New(path.Join(config.WorkingDirectory, "/blossom"))
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()
}
log.Printf("Serving on ws://%s\n", config.RelayBind+config.RelayPort)
go http.ListenAndServe(config.RelayBind+config.RelayPort, relay)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-sigChan
log.Print("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
}
}