-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
352 lines (327 loc) · 9.41 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
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
package report
import (
"context"
"crypto"
"fmt"
"github.com/go-fed/activity/pub"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/vocab"
"github.com/go-fed/httpsig"
"log"
"net/http"
"net/url"
"sync"
)
var _ pub.Application = &app{}
var _ pub.SocialAPI = &app{}
var _ pub.FederateAPI = &app{}
var _ pub.SocialApplication = &app{}
var _ pub.FederateApplication = &app{}
var _ pub.SocialFederateApplication = &app{}
type lockKeyType string
type lockObj struct {
obj pub.PubObject
mu *sync.RWMutex
who int
}
// app shows the basic mechanics for a single-user, non-permanent, dummy server.
type app struct {
scheme string
host string
newPath string
db map[string]*lockObj
dbMu *sync.RWMutex
actor *vocab.Person
actorURL *url.URL
inboxURL *url.URL
outboxURL *url.URL
followingURL *url.URL
followersURL *url.URL
likedURL *url.URL
inbox vocab.OrderedCollectionType
inboxMu *sync.RWMutex
outbox vocab.OrderedCollectionType
outboxMu *sync.RWMutex
following vocab.OrderedCollectionType
followingMu *sync.RWMutex
followers vocab.OrderedCollectionType
followersMu *sync.RWMutex
liked vocab.OrderedCollectionType
likedMu *sync.RWMutex
id int
idMu *sync.Mutex
pubKey crypto.PublicKey
privKey crypto.PrivateKey
verifier pub.SocialAPIVerifier
}
func newApp(scheme, host, newPath string, actorURL, inboxURL, outboxURL, followingURL, followersURL, likedURL *url.URL, pubKey crypto.PublicKey, privKey crypto.PrivateKey, actor *vocab.Person, verifier pub.SocialAPIVerifier) *app {
inbox := &vocab.OrderedCollection{}
inbox.SetId(inboxURL)
outbox := &vocab.OrderedCollection{}
outbox.SetId(outboxURL)
following := &vocab.OrderedCollection{}
following.SetId(followingURL)
followers := &vocab.OrderedCollection{}
followers.SetId(followersURL)
liked := &vocab.OrderedCollection{}
liked.SetId(likedURL)
return &app{
scheme: scheme,
host: host,
newPath: newPath,
db: make(map[string]*lockObj),
dbMu: &sync.RWMutex{},
actor: actor,
actorURL: actorURL,
inboxURL: inboxURL,
outboxURL: outboxURL,
followingURL: followingURL,
followersURL: followersURL,
likedURL: likedURL,
inbox: inbox,
inboxMu: &sync.RWMutex{},
outbox: outbox,
outboxMu: &sync.RWMutex{},
following: following,
followingMu: &sync.RWMutex{},
followers: followers,
followersMu: &sync.RWMutex{},
liked: liked,
likedMu: &sync.RWMutex{},
id: 1,
idMu: &sync.Mutex{},
pubKey: pubKey,
privKey: privKey,
verifier: verifier,
}
}
func (a *app) Owns(c context.Context, id *url.URL) bool {
log.Printf("Owns: %s", id)
return id.Host == a.host
}
func (a *app) Get(c context.Context, id *url.URL, rw pub.RWType) (pub.PubObject, error) {
log.Printf("Getting: %s", id)
has, err := a.Has(c, id)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("%s not found", id)
}
if *id == *a.actorURL {
return a.actor, nil
} else if *id == *a.inboxURL {
a.inboxMu.RLock()
defer a.inboxMu.RUnlock()
return a.inbox, nil
} else if *id == *a.outboxURL {
a.outboxMu.RLock()
defer a.outboxMu.RUnlock()
return a.outbox, nil
} else if *id == *a.followingURL {
a.followingMu.RLock()
defer a.followingMu.RUnlock()
return a.following, nil
} else if *id == *a.followersURL {
a.followersMu.RLock()
defer a.followersMu.RUnlock()
return a.followers, nil
} else if *id == *a.likedURL {
a.likedMu.RLock()
defer a.likedMu.RUnlock()
return a.liked, nil
}
a.dbMu.RLock()
p, ok := a.db[id.String()]
a.dbMu.RUnlock()
if !ok {
return nil, fmt.Errorf("%s not found", id)
}
switch rw {
case pub.Read:
p.mu.RLock()
defer p.mu.RUnlock()
case pub.ReadWrite:
who := c.Value(lockKeyType("lockKey")).(int)
if p.who != who {
log.Printf("locking %s", id)
p.mu.Lock()
p.who = who
go func() {
<-c.Done()
if p.who == who {
log.Printf("unlocking %s", id)
p.mu.Unlock()
}
}()
}
default:
return nil, fmt.Errorf("unrecognized pub.RWType: %v", rw)
}
return p.obj, nil
}
func (a *app) GetAsVerifiedUser(c context.Context, id, authdUser *url.URL, rw pub.RWType) (pub.PubObject, error) {
log.Printf("GetAsVerifiedUser: %s", id)
return a.Get(c, id, rw)
}
func (a *app) Has(c context.Context, id *url.URL) (bool, error) {
log.Printf("Has: %s", id)
if *id == *a.actorURL || *id == *a.inboxURL || *id == *a.outboxURL || *id == *a.followingURL || *id == *a.followersURL || *id == *a.likedURL {
return true, nil
}
_, ok := a.db[id.String()]
return ok, nil
}
func (a *app) Set(c context.Context, o pub.PubObject) error {
b, _ := o.Serialize()
log.Printf("Setting: %s", b)
if id := o.GetId(); id == nil {
return fmt.Errorf("id is nil")
} else if *id == *a.outboxURL {
a.outboxMu.Lock()
defer a.outboxMu.Unlock()
oc, ok := o.(vocab.OrderedCollectionType)
if !ok {
return fmt.Errorf("setting %s but not an OrderedCollectionType", id)
}
a.outbox = oc
return nil
} else if *id == *a.inboxURL {
a.inboxMu.Lock()
defer a.inboxMu.Unlock()
oc, ok := o.(vocab.OrderedCollectionType)
if !ok {
return fmt.Errorf("setting %s but not an OrderedCollectionType", id)
}
a.inbox = oc
return nil
} else if *id == *a.followingURL {
a.followingMu.Lock()
defer a.followingMu.Unlock()
oc, ok := o.(vocab.OrderedCollectionType)
if !ok {
return fmt.Errorf("setting %s but not an OrderedCollectionType", id)
}
a.following = oc
return nil
} else if *id == *a.followersURL {
a.followersMu.Lock()
defer a.followersMu.Unlock()
oc, ok := o.(vocab.OrderedCollectionType)
if !ok {
return fmt.Errorf("setting %s but not an OrderedCollectionType", id)
}
a.followers = oc
return nil
} else if *id == *a.likedURL {
a.likedMu.Lock()
defer a.likedMu.Unlock()
oc, ok := o.(vocab.OrderedCollectionType)
if !ok {
return fmt.Errorf("setting %s but not an OrderedCollectionType", id)
}
a.liked = oc
return nil
} else {
a.dbMu.Lock()
if v, ok := a.db[id.String()]; ok {
a.dbMu.Unlock()
who := c.Value(lockKeyType("lockKey")).(int)
vWho := v.who
// TODO: Use sync.Cond
if vWho == 0 || vWho != who {
log.Printf("locking %s", id)
v.mu.Lock()
v.who = who
}
v.obj = o
v.who = 0
log.Printf("unlocking %s", id)
v.mu.Unlock()
} else {
a.db[id.String()] = &lockObj{
obj: o,
mu: &sync.RWMutex{},
}
a.dbMu.Unlock()
}
return nil
}
}
func (a *app) GetInbox(c context.Context, r *http.Request, rw pub.RWType) (vocab.OrderedCollectionType, error) {
log.Printf("GetInbox: %s", r.URL)
if *r.URL == *a.inboxURL {
a.inboxMu.RLock()
defer a.inboxMu.RUnlock()
return a.inbox, nil
}
return nil, fmt.Errorf("no inbox for url %s", r.URL)
}
func (a *app) GetOutbox(c context.Context, r *http.Request, rw pub.RWType) (vocab.OrderedCollectionType, error) {
log.Printf("GetOutbox: %s", r.URL)
if *r.URL == *a.outboxURL {
a.outboxMu.RLock()
defer a.outboxMu.RUnlock()
return a.outbox, nil
}
return nil, fmt.Errorf("no outbox for url %s", r.URL)
}
func (a *app) NewId(c context.Context, t pub.Typer) *url.URL {
a.idMu.Lock()
id := a.id
a.id++
a.idMu.Unlock()
withoutTrailingSlash := a.newPath
if a.newPath[len(a.newPath)-1] == '/' {
withoutTrailingSlash = a.newPath[:len(a.newPath)-1]
}
return &url.URL{
Scheme: a.scheme,
Host: a.host,
Path: fmt.Sprintf("%s/%d", withoutTrailingSlash, id),
}
}
func (a *app) GetPublicKey(c context.Context, publicKeyId string) (pubKey crypto.PublicKey, algo httpsig.Algorithm, user *url.URL, err error) {
return nil, httpsig.RSA_SHA256, nil, fmt.Errorf("not implemented: GetPublicKey")
}
func (a *app) CanAdd(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool {
return true
}
func (a *app) CanRemove(c context.Context, o vocab.ObjectType, t vocab.ObjectType) bool {
return true
}
func (a *app) ActorIRI(c context.Context, r *http.Request) (*url.URL, error) {
log.Printf("ActorIRI: %s", r.URL)
if *r.URL == *a.inboxURL || *r.URL == *a.outboxURL {
return a.actorURL, nil
}
return nil, fmt.Errorf("no actor for url %s", r.URL)
}
func (a *app) GetSocialAPIVerifier(c context.Context) pub.SocialAPIVerifier {
return a.verifier
}
func (a *app) GetPublicKeyForOutbox(c context.Context, publicKeyId string, boxIRI *url.URL) (crypto.PublicKey, httpsig.Algorithm, error) {
if boxIRI != a.outboxURL {
return nil, httpsig.RSA_SHA256, fmt.Errorf("unknown outbox url %s", boxIRI)
} else if publicKeyId != a.actorURL.String() {
return nil, httpsig.RSA_SHA256, fmt.Errorf("unknown public key id %q", publicKeyId)
}
return a.pubKey, httpsig.RSA_SHA256, nil
}
func (a *app) OnFollow(c context.Context, s *streams.Follow) pub.FollowResponse {
return pub.AutomaticAccept
}
func (a *app) Unblocked(c context.Context, actorIRIs []*url.URL) error {
return nil
}
func (a *app) FilterForwarding(c context.Context, activity vocab.ActivityType, iris []*url.URL) ([]*url.URL, error) {
// Do NOT do this in real implementations. This turns the server into a
// spambot. See the documentation in go-fed/activity/pub.
return iris, nil
}
func (a *app) NewSigner() (httpsig.Signer, error) {
s, _, err := httpsig.NewSigner([]httpsig.Algorithm{httpsig.RSA_SHA256}, nil, httpsig.Signature)
return s, err
}
func (a *app) PrivateKey(boxIRI *url.URL) (privKey crypto.PrivateKey, pubKeyId string, err error) {
return a.privKey, a.actorURL.String(), nil
}