-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.go
324 lines (275 loc) · 7.65 KB
/
app.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package fedbox
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"syscall"
"time"
cache2 "git.sr.ht/~mariusor/cache"
"git.sr.ht/~mariusor/lw"
w "git.sr.ht/~mariusor/wrapper"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/auth"
"github.com/go-ap/cache"
"github.com/go-ap/client"
"github.com/go-ap/errors"
ap "github.com/go-ap/fedbox/activitypub"
"github.com/go-ap/fedbox/internal/config"
st "github.com/go-ap/fedbox/storage"
"github.com/go-ap/processing"
"github.com/go-chi/chi/v5"
"github.com/openshift/osin"
)
func init() {
// set local path typer to validate collections
processing.Typer = pathTyper{}
}
type LogFn func(string, ...any)
type canStore = cache.CanStore
type FedBOX struct {
R chi.Router
conf config.Options
self vocab.Service
storage st.FullStorage
caches canStore
logger lw.Logger
keyGenerator func(act *vocab.Actor) error
startFn func(ctx context.Context) error
stopFn func(ctx context.Context) error
}
var emptyCtxtFn = func(_ context.Context) error {
return nil
}
var InternalIRI = vocab.IRI("https://fedbox/")
func Client(tr http.RoundTripper, conf config.Options, l lw.Logger) *client.C {
cachePath, err := os.UserCacheDir()
if err != nil {
cachePath = os.TempDir()
}
if tr == nil {
tr = &http.Transport{}
}
baseClient := &http.Client{
Transport: cache2.Private(tr, cache2.FS(filepath.Join(cachePath, conf.AppName))),
}
ua := fmt.Sprintf("%s/%s (+%s)", conf.BaseURL, conf.Version, ap.ProjectURL)
return client.New(
client.WithUserAgent(ua),
client.WithLogger(l.WithContext(lw.Ctx{"log": "client"})),
client.WithHTTPClient(baseClient),
client.SkipTLSValidation(!conf.Env.IsProd()),
client.SetDefaultHTTPClient(),
)
}
// New instantiates a new FedBOX instance
func New(l lw.Logger, conf config.Options, db st.FullStorage) (*FedBOX, error) {
if db == nil {
return nil, errors.Newf("invalid storage")
}
if err := db.Open(); err != nil {
return nil, errors.Annotatef(err, "unable to open storage: %s", conf.StoragePath)
}
if conf.BaseURL == "" {
return nil, errors.Newf("invalid empty BaseURL config")
}
app := FedBOX{
conf: conf,
R: chi.NewRouter(),
storage: db,
logger: l,
caches: cache.New(conf.RequestCache),
startFn: emptyCtxtFn,
stopFn: emptyCtxtFn,
}
if metaSaver, ok := db.(st.MetadataTyper); ok {
keysType := "ED25519"
if conf.MastodonCompatible {
keysType = "RSA"
}
l.Infof("Setting actor key generator %T[%s]", metaSaver, keysType)
app.keyGenerator = AddKeyToPerson(metaSaver, keysType)
}
errors.IncludeBacktrace = conf.LogLevel == lw.TraceLevel
if err := app.setupService(); err != nil {
app.errFn("unable to save the instance's self service: %s", err)
return nil, err
}
app.R.Group(app.Routes())
sockType := ""
setters := []w.SetFn{w.Handler(app.R)}
if app.conf.Secure {
if len(app.conf.CertPath)+len(app.conf.KeyPath) > 0 {
setters = append(setters, w.WithTLSCert(app.conf.CertPath, app.conf.KeyPath))
} else {
app.conf.Secure = false
}
}
if app.conf.Listen == "systemd" {
sockType = "Systemd"
setters = append(setters, w.OnSystemd())
} else if filepath.IsAbs(app.conf.Listen) {
dir := filepath.Dir(app.conf.Listen)
if _, err := os.Stat(dir); err == nil {
sockType = "socket"
setters = append(setters, w.OnSocket(app.conf.Listen))
defer func() {
if err := os.RemoveAll(app.conf.Listen); err != nil {
app.logger.Errorf("Failed cleaning up: %s", err)
}
}()
}
} else {
sockType = "TCP"
setters = append(setters, w.OnTCP(app.conf.Listen))
}
// Get start/stop functions for the http server
app.startFn, app.stopFn = w.HttpServer(setters...)
app.conf.Listen += "[" + sockType + "]"
return &app, nil
}
func (f *FedBOX) setupService() error {
db := f.storage
conf := f.conf
selfIRI := ap.DefaultServiceIRI(conf.BaseURL)
var err error
f.self, err = ap.LoadActor(db, selfIRI)
if err != nil && errors.IsNotFound(err) {
f.infFn("No service actor found, creating one: %s", selfIRI)
self := ap.Self(selfIRI)
if err = CreateService(db, self); err != nil {
return err
}
f.self = self
keysType := KeyTypeED25519
if conf.MastodonCompatible {
keysType = KeyTypeRSA
}
if err = AddKeyToItem(db, &f.self, keysType); err != nil {
f.errFn("Unable to save the instance's self service public key: %s", err)
}
}
return nil
}
func (f *FedBOX) Config() config.Options {
return f.conf
}
func (f *FedBOX) Storage() st.FullStorage {
return f.storage
}
// Stop
func (f *FedBOX) Stop(ctx context.Context) {
f.storage.Close()
if err := f.stopFn(ctx); err != nil {
f.logger.Errorf("Error: %+v", err)
} else {
f.logger.Infof("Stopped")
}
}
func (f *FedBOX) reload() (err error) {
f.conf, err = config.Load(f.conf.Env, f.conf.TimeOut)
f.caches.Delete()
return err
}
func (f *FedBOX) actorFromRequestWithClient(r *http.Request, cl *client.C) vocab.Actor {
// NOTE(marius): if the storage is nil, we can still use the remote client in the load function
isLocalFn := func(iri vocab.IRI) bool {
return iri.Contains(vocab.IRI(f.conf.BaseURL), true)
}
var logFn auth.LoggerFn = func(ctx lw.Ctx, msg string, p ...interface{}) {
f.logger.WithContext(ctx).Debugf(msg, p...)
}
ar := auth.ClientResolver(cl,
auth.SolverWithLogger(logFn),
auth.SolverWithStorage(f.storage),
auth.SolverWithLocalIRIFn(isLocalFn),
)
act, err := ar.LoadActorFromRequest(r)
if err != nil {
f.logger.WithContext(lw.Ctx{"err": err.Error()}).Errorf("unable to load an authorized Actor from request")
}
return act
}
// Run is the wrapper for starting the web-server and handling signals
func (f *FedBOX) Run(ctx context.Context) error {
logCtx := lw.Ctx{
"URL": f.conf.BaseURL,
"version": f.conf.Version,
"listenOn": f.conf.Listen,
"TLS": f.conf.Secure,
}
ctx, cancelFn := context.WithCancel(ctx)
defer f.Stop(ctx)
logger := f.logger.WithContext(logCtx)
logger.Infof("Starting")
err := w.RegisterSignalHandlers(w.SignalHandlers{
syscall.SIGHUP: func(_ chan<- error) {
logger.Infof("SIGHUP received, reloading configuration")
if err := f.reload(); err != nil {
logger.Errorf("Failed: %+s", err.Error())
}
},
syscall.SIGINT: func(exit chan<- error) {
logger.Infof("SIGINT received, stopping")
exit <- nil
},
syscall.SIGTERM: func(exit chan<- error) {
logger.Infof("SIGTERM received, force stopping")
exit <- nil
},
syscall.SIGQUIT: func(exit chan<- error) {
logger.Infof("SIGQUIT received, force stopping with core-dump")
cancelFn()
exit <- nil
},
}).Exec(ctx, f.startFn)
if err == nil {
logger.Infof("Shutting down")
}
return err
}
func (f *FedBOX) infFn(s string, p ...any) {
if f.logger != nil {
f.logger.Infof(s, p...)
}
}
func (f *FedBOX) errFn(s string, p ...any) {
if f.logger != nil {
f.logger.Errorf(s, p...)
}
}
func CreateService(r st.FullStorage, self vocab.Item) (err error) {
_ = vocab.OnActor(self, func(service *vocab.Actor) error {
service.Published = time.Now().UTC()
return nil
})
self, err = r.Save(self)
if err != nil {
return err
}
c := osin.DefaultClient{Id: string(self.GetLink())}
_ = r.CreateClient(&c)
rr, ok := r.(processing.CollectionStore)
if !ok {
return nil
}
col := func(iri vocab.IRI) vocab.CollectionInterface {
return &vocab.OrderedCollection{
ID: iri,
Type: vocab.OrderedCollectionType,
Published: time.Now().UTC(),
AttributedTo: self.GetLink(),
CC: vocab.ItemCollection{vocab.PublicNS},
}
}
return vocab.OnActor(self, func(service *vocab.Actor) error {
var multi error
for _, stream := range service.Streams {
if _, err := rr.Create(col(stream.GetID())); err != nil {
multi = errors.Join(multi, err)
}
}
return multi
})
}