-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
408 lines (379 loc) · 14.3 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"context"
"fmt"
apiGrpc "github.com/awakari/int-activitypub/api/grpc"
"github.com/awakari/int-activitypub/api/grpc/queue"
apiHttp "github.com/awakari/int-activitypub/api/http"
"github.com/awakari/int-activitypub/api/http/handler"
"github.com/awakari/int-activitypub/api/http/interests"
"github.com/awakari/int-activitypub/api/http/pub"
"github.com/awakari/int-activitypub/api/http/reader"
"github.com/awakari/int-activitypub/config"
"github.com/awakari/int-activitypub/model"
"github.com/awakari/int-activitypub/service"
"github.com/awakari/int-activitypub/service/activitypub"
"github.com/awakari/int-activitypub/service/converter"
"github.com/awakari/int-activitypub/storage"
"github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb"
"github.com/gin-gonic/gin"
vocab "github.com/go-ap/activitypub"
apiProm "github.com/prometheus/client_golang/api"
apiPromV1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/writeas/go-nodeinfo"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log/slog"
"net/http"
"os"
)
const ceKeyGroupId = "awakarigroupid"
const ceKeyPublic = "public"
func main() {
// init config and logger
cfg, err := config.NewConfigFromEnv()
if err != nil {
panic(fmt.Sprintf("failed to load the config from env: %s", err))
}
//
opts := slog.HandlerOptions{
Level: slog.Level(cfg.Log.Level),
}
log := slog.New(slog.NewTextHandler(os.Stdout, &opts))
log.Info("starting the update for the feeds")
// init storage
var stor storage.Storage
stor, err = storage.NewStorage(context.TODO(), cfg.Db)
if err != nil {
panic(fmt.Sprintf("failed to initialize the storage: %s", err))
}
stor = storage.NewLocalCache(stor, cfg.Db.Table.Following.Cache.Size, cfg.Db.Table.Following.Cache.Ttl)
defer stor.Close()
promauto.NewGaugeFunc(prometheus.GaugeOpts{
Name: "awk_source_activitypub_count_total",
Help: "Total count of ActivityPub sources",
}, func() float64 {
var count int64
count, err = stor.Count(context.Background())
if err != nil {
panic(err)
}
return float64(count)
})
svcPub := pub.NewService(http.DefaultClient, cfg.Api.Writer.Uri, cfg.Api.Token.Internal, cfg.Api.Writer.Timeout)
svcPub = pub.NewLogging(svcPub, log)
log.Info("initialized the Awakari publish API client")
svcInterests := interests.NewService(http.DefaultClient, cfg.Api.Interests.Uri, cfg.Api.Token.Internal)
svcInterests = interests.NewLogging(svcInterests, log)
log.Info("initialized the Awakari interests API client")
// prometheus client
clientProm, err := apiProm.NewClient(apiProm.Config{
Address: cfg.Api.Prometheus.Uri,
})
var ap apiPromV1.API
switch err {
case nil:
ap = apiPromV1.NewAPI(clientProm)
default:
log.Warn(fmt.Sprintf("Failed to connect Prometheus @ %s: %s", cfg.Api.Prometheus.Uri, err))
err = nil
}
clientHttp := &http.Client{}
svcActivityPub := activitypub.NewService(clientHttp, cfg.Api.Http.Host, []byte(cfg.Api.Key.Private), ap)
svcActivityPub = activitypub.NewServiceLogging(svcActivityPub, log)
svcConv := converter.NewService(
cfg.Api.EventType.Self,
fmt.Sprintf("https://%s", cfg.Api.Http.Host),
cfg.Api.Reader.UriEventBase,
vocab.ActivityVocabularyType(cfg.Api.Actor.Type),
)
svcConv = converter.NewLogging(svcConv, log)
// init websub reader
svcReader := reader.NewService(clientHttp, cfg.Api.Reader.Uri)
svcReader = reader.NewServiceLogging(svcReader, log)
urlCallbackBase := fmt.Sprintf(
"%s://%s:%d%s",
cfg.Api.Reader.CallBack.Protocol,
cfg.Api.Reader.CallBack.Host,
cfg.Api.Reader.CallBack.Port,
cfg.Api.Reader.CallBack.Path,
)
svc := service.NewService(stor, svcActivityPub, cfg.Api.Http.Host, svcConv, svcPub, cfg.Api.Writer.Backoff, svcReader, urlCallbackBase)
svc = service.NewLogging(svc, log)
log.Info(fmt.Sprintf("starting to listen the gRPC API @ port #%d...", cfg.Api.Port))
go func() {
if err = apiGrpc.Serve(cfg.Api.Port, svc); err != nil {
panic(err)
}
}()
// init queues
connQueue, err := grpc.NewClient(cfg.Api.Queue.Uri, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
panic(err)
}
log.Info("connected to the queue service")
clientQueue := queue.NewServiceClient(connQueue)
svcQueue := queue.NewService(clientQueue)
svcQueue = queue.NewLoggingMiddleware(svcQueue, log)
err = svcQueue.SetConsumer(context.TODO(), cfg.Api.Queue.InterestsCreated.Name, cfg.Api.Queue.InterestsCreated.Subj)
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("initialized the %s queue", cfg.Api.Queue.InterestsCreated.Name))
go func() {
err = consumeQueue(
context.Background(),
svc,
svcQueue,
cfg.Api.Queue.InterestsCreated.Name,
cfg.Api.Queue.InterestsCreated.Subj,
cfg.Api.Queue.InterestsCreated.BatchSize,
func(ctx context.Context, svc service.Service, evts []*pb.CloudEvent) {
consumeInterestEvents(ctx, svc, evts, cfg, log)
},
)
if err != nil {
panic(err)
}
}()
err = svcQueue.SetConsumer(context.TODO(), cfg.Api.Queue.InterestsUpdated.Name, cfg.Api.Queue.InterestsUpdated.Subj)
if err != nil {
panic(err)
}
log.Info(fmt.Sprintf("initialized the %s queue", cfg.Api.Queue.InterestsUpdated.Name))
go func() {
err = consumeQueue(
context.Background(),
svc,
svcQueue,
cfg.Api.Queue.InterestsUpdated.Name,
cfg.Api.Queue.InterestsUpdated.Subj,
cfg.Api.Queue.InterestsUpdated.BatchSize,
func(ctx context.Context, svc service.Service, evts []*pb.CloudEvent) {
consumeInterestEvents(ctx, svc, evts, cfg, log)
},
)
if err != nil {
panic(err)
}
}()
// nodeinfo
cfgNodeInfo := nodeinfo.Config{
BaseURL: fmt.Sprintf("https://%s", cfg.Api.Http.Host),
InfoURL: "/api/nodeinfo",
Metadata: nodeinfo.Metadata{
NodeName: cfg.Api.Node.Name,
NodeDescription: cfg.Api.Node.Description,
Private: false,
Software: nodeinfo.SoftwareMeta{
HomePage: "https://awakari.com",
GitHub: "https://github.com/awakari/int-activitypub",
Follow: "https://github.com/awakari/int-activitypub",
},
},
Protocols: []nodeinfo.NodeProtocol{
nodeinfo.ProtocolActivityPub,
},
Services: nodeinfo.Services{
Inbound: []nodeinfo.NodeService{
nodeinfo.ServiceAtom,
nodeinfo.ServiceRSS,
"telegram",
},
Outbound: []nodeinfo.NodeService{
nodeinfo.ServiceRSS,
"telegram",
},
},
Software: nodeinfo.SoftwareInfo{
Name: "Awakari",
Version: "1.0.0",
},
}
nodeInfo := nodeinfo.NewService(cfgNodeInfo, svcActivityPub)
// actor
actor := vocab.Actor{
ID: vocab.ID(fmt.Sprintf("https://%s/actor", cfg.Api.Http.Host)),
Type: vocab.ActivityVocabularyType(cfg.Api.Actor.Type),
Name: vocab.DefaultNaturalLanguageValue(cfg.Api.Actor.Name),
Context: vocab.ItemCollection{
vocab.IRI(model.NsAs),
vocab.IRI("https://w3id.org/security/v1"),
},
Icon: vocab.Image{
MediaType: "image/png",
Type: vocab.ImageType,
URL: vocab.IRI("https://awakari.com/logo-color-64.png"),
},
Summary: vocab.DefaultNaturalLanguageValue(
"<p>Awakari is an open-source service that discovers and follows interesting Fediverse publishers on behalf of own users. " +
"The service accepts public only messages and filters these to fulfill own user interest queries.</p>" +
"<p>Before accepting any message, Awakari requests to follow the publisher. " +
"The instance where a publisher is logged in sends messages to the approved followers.</p>" +
"<p>If you don't agree with the following, please don't accept the follow request or remove Awakari from your followers list.</p>" +
"Contact: <a href=\"mailto:[email protected]\">[email protected]</a><br/>" +
"Donate: <a href=\"https://awakari.com/donation.html\">https://awakari.com/donation.html</a><br/>" +
"Opt-Out: <a href=\"https://github.com/awakari/.github/blob/master/OPT-OUT.md\">https://github.com/awakari/.github/blob/master/OPT-OUT.md</a><br/>" +
"Privacy: <a href=\"https://awakari.com/privacy.html\">https://awakari.com/privacy.html</a><br/>" +
"Source: <a href=\"https://github.com/awakari/int-activitypub\">https://github.com/awakari/int-activitypub</a><br/>" +
"Terms: <a href=\"https://awakari.com/tos.html\">https://awakari.com/tos.html</a></p>",
),
URL: vocab.IRI("https://awakari.com/activitypub"),
Inbox: vocab.IRI(fmt.Sprintf("https://%s/inbox", cfg.Api.Http.Host)),
Outbox: vocab.IRI(fmt.Sprintf("https://%s/outbox", cfg.Api.Http.Host)),
Following: vocab.IRI(fmt.Sprintf("https://%s/following", cfg.Api.Http.Host)),
Followers: vocab.IRI(fmt.Sprintf("https://%s/followers", cfg.Api.Http.Host)),
PreferredUsername: vocab.DefaultNaturalLanguageValue(cfg.Api.Actor.Name),
Endpoints: &vocab.Endpoints{
SharedInbox: vocab.IRI(fmt.Sprintf("https://%s/inbox", cfg.Api.Http.Host)),
},
PublicKey: vocab.PublicKey{
ID: vocab.ID(fmt.Sprintf("https://%s/actor#main-key", cfg.Api.Http.Host)),
Owner: vocab.IRI(fmt.Sprintf("https://%s/actor", cfg.Api.Http.Host)),
PublicKeyPem: cfg.Api.Key.Public,
},
Attachment: vocab.ItemCollection{
vocab.Page{
Name: vocab.DefaultNaturalLanguageValue("homepage"),
ID: vocab.ID("https://awakari.com/activitypub"),
URL: vocab.IRI("https://awakari.com/activitypub"),
},
vocab.Page{
Name: vocab.DefaultNaturalLanguageValue("GitHub"),
ID: vocab.ID("https://github.com/awakari"),
URL: vocab.IRI("https://githun.com/awakari"),
},
vocab.Page{
Name: vocab.DefaultNaturalLanguageValue("Telegram Bot"),
ID: vocab.ID("https://t.me/AwakariBot"),
URL: vocab.IRI("https://t.me/AwakariBot"),
},
},
}
actorExtraAttrs := map[string]any{
"manuallyApprovesFollowers": false,
"discoverable": true,
"indexable": true,
"memorial": false,
}
ha := handler.NewActorHandler(actor, actorExtraAttrs, svcInterests, cfg.Api.Interests.DetailsUriPrefix, cfg.Api)
// WebFinger
wfDefault := apiHttp.WebFinger{
Subject: fmt.Sprintf("acct:%s@%s", cfg.Api.Actor.Name, cfg.Api.Http.Host),
Links: []apiHttp.WebFingerLink{
{
Rel: "self",
Type: "application/activity+json",
Href: fmt.Sprintf("https://%s/actor", cfg.Api.Http.Host),
},
},
}
hwf := handler.NewWebFingerHandler(wfDefault, cfg.Api.Http.Host, svcInterests)
// handlers for inbox, outbox, following, followers
hi := handler.NewInboxHandler(svcActivityPub, svc, cfg.Api.Http.Host)
ho := handler.NewOutboxHandler(svcReader, svcConv, fmt.Sprintf("https://%s/outbox", cfg.Api.Http.Host))
hoDummy := handler.NewDummyCollectionHandler(vocab.OrderedCollectionPage{
ID: vocab.IRI(fmt.Sprintf("https://%s/outbox", cfg.Api.Http.Host)),
Context: vocab.IRI("https://www.w3.org/ns/activitystreams"),
PartOf: vocab.IRI(fmt.Sprintf("https://%s/outbox", cfg.Api.Http.Host)),
First: vocab.IRI(fmt.Sprintf("https://%s/outbox?page=1", cfg.Api.Http.Host)),
})
hFollowing := handler.NewFollowingHandler(stor, fmt.Sprintf("https://%s/following", cfg.Api.Http.Host))
hFollowers := handler.NewFollowersHandler(svcReader, fmt.Sprintf("https://%s/followers", cfg.Api.Http.Host))
r := gin.Default()
r.GET("/.well-known/webfinger", hwf.Handle)
r.GET("/actor/:id", ha.Handle)
r.GET("/actor", ha.Handle)
r.POST("/inbox/:id", hi.Handle)
r.POST("/inbox", hi.Handle)
r.GET("/outbox/:id", ho.Handle)
r.GET("/outbox", hoDummy.Handle)
r.GET("/following/:id", handler.NewDummyCollectionHandler(vocab.OrderedCollectionPage{
ID: vocab.IRI(fmt.Sprintf("https://%s/dummy/inbox", cfg.Api.Http.Host)),
Context: vocab.IRI("https://www.w3.org/ns/activitystreams"),
PartOf: vocab.IRI(fmt.Sprintf("https://%s/dummy/inbox", cfg.Api.Http.Host)),
First: vocab.IRI(fmt.Sprintf("https://%s/dummy/inbox?page=1", cfg.Api.Http.Host)),
}).Handle)
r.GET("/following", hFollowing.Handle)
r.GET("/followers/:id", hFollowers.Handle)
r.GET(nodeinfo.NodeInfoPath, func(ctx *gin.Context) {
nodeInfo.NodeInfoDiscover(ctx.Writer, ctx.Request)
return
})
r.GET(cfgNodeInfo.InfoURL, func(ctx *gin.Context) {
nodeInfo.NodeInfo(ctx.Writer, ctx.Request)
return
})
r.GET("/", ha.Handle)
log.Info(fmt.Sprintf("starting to listen the HTTP API @ port #%d...", cfg.Api.Http.Port))
go func() {
err = r.Run(fmt.Sprintf(":%d", cfg.Api.Http.Port))
if err != nil {
panic(err)
}
}()
hc := handler.NewCallbackHandler(cfg.Api.Reader.Uri, cfg.Api.Http.Host, svcConv, svcActivityPub, cfg.Api.EventType)
log.Info(fmt.Sprintf("starting to listen the HTTP API @ port #%d...", cfg.Api.Reader.CallBack.Port))
internalCallbacks := gin.Default()
internalCallbacks.
GET(cfg.Api.Reader.CallBack.Path, hc.Confirm).
POST(cfg.Api.Reader.CallBack.Path, hc.Deliver)
go func() {
err = internalCallbacks.Run(fmt.Sprintf(":%d", cfg.Api.Reader.CallBack.Port))
if err != nil {
panic(err)
}
}()
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf(":%d", cfg.Api.Metrics.Port), nil)
}
func consumeQueue(
ctx context.Context,
svc service.Service,
svcQueue queue.Service,
name, subj string,
batchSize uint32,
consumeEvents func(ctx context.Context, svc service.Service, evts []*pb.CloudEvent),
) (err error) {
for {
err = svcQueue.ReceiveMessages(ctx, name, subj, batchSize, func(evts []*pb.CloudEvent) (err error) {
consumeEvents(ctx, svc, evts)
return
})
if err != nil {
panic(err)
}
}
}
func consumeInterestEvents(
ctx context.Context,
svc service.Service,
evts []*pb.CloudEvent,
cfg config.Config,
log *slog.Logger,
) {
log.Debug(fmt.Sprintf("consumeInterestEvents(%d))\n", len(evts)))
for _, evt := range evts {
interestId := evt.GetTextData()
var groupId string
if groupIdAttr, groupIdIdPresent := evt.Attributes[ceKeyGroupId]; groupIdIdPresent {
groupId = groupIdAttr.GetCeString()
}
if groupId == "" {
log.Error(fmt.Sprintf("interest %s event: empty group id, skipping", interestId))
continue
}
publicAttr, publicAttrPresent := evt.Attributes[ceKeyPublic]
switch publicAttrPresent && publicAttr.GetCeBoolean() {
case true:
actor := interestId + "@" + cfg.Api.Http.Host
_, _ = svc.RequestFollow(ctx, "@[email protected]", groupId, actor, interestId, "", false)
default:
log.Debug(fmt.Sprintf("interest %s event: public: %t/%t", interestId, publicAttrPresent, publicAttr.GetCeBoolean()))
}
}
return
}