/
kx.go
455 lines (397 loc) · 12.9 KB
/
kx.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package clientdb
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/companyzero/bisonrelay/client/clientintf"
"github.com/companyzero/bisonrelay/rpc"
"github.com/companyzero/bisonrelay/zkidentity"
)
// KXExists returns true if there's a KX procedure with the specified RV.
func (db *DB) KXExists(tx ReadTx, initialRV RawRVID) bool {
fname := filepath.Join(db.root, kxDir, initialRV.String())
return fileExists(fname)
}
func (db *DB) SaveKX(tx ReadWriteTx, kx KXData) error {
dir := filepath.Join(db.root, kxDir)
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("unable to make kx dir: %v", err)
}
blob, err := json.Marshal(kx)
if err != nil {
return fmt.Errorf("unable to marshal kxdata: %v", err)
}
fname := filepath.Join(dir, kx.InitialRV.String())
if _, err := os.Stat(fname); !os.IsNotExist(err) {
if err != nil {
return err
}
return fmt.Errorf("kx with initial RV %s: %w", kx.InitialRV, ErrAlreadyExists)
}
return os.WriteFile(fname, blob, 0o600)
}
func (db *DB) DeleteKX(tx ReadWriteTx, initialRV RawRVID) error {
fname := filepath.Join(db.root, kxDir, initialRV.String())
return os.Remove(fname)
}
func (db *DB) GetKX(tx ReadTx, initialRV RawRVID) (KXData, error) {
fname := filepath.Join(db.root, kxDir, initialRV.String())
blob, err := os.ReadFile(fname)
if err != nil {
if os.IsNotExist(err) {
return KXData{}, fmt.Errorf("kx %s: %w",
initialRV.ShortLogID(), ErrNotFound)
}
return KXData{}, err
}
var kxd KXData
err = json.Unmarshal(blob, &kxd)
if err != nil {
return KXData{}, nil
}
return kxd, nil
}
func (db *DB) ListKXs(tx ReadTx) ([]KXData, error) {
dir := filepath.Join(db.root, kxDir)
dirEntries, err := os.ReadDir(dir)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("unable to read content dir: %v", err)
}
var testID UserID
res := make([]KXData, 0, len(dirEntries))
for _, f := range dirEntries {
if f.IsDir() {
continue
}
fname := filepath.Join(dir, f.Name())
if err := testID.FromString(filepath.Base(fname)); err != nil {
db.log.Warnf("Filename %q is not a KX file", fname)
continue
}
blob, err := os.ReadFile(fname)
if err != nil {
return nil, err
}
var kxd KXData
err = json.Unmarshal(blob, &kxd)
if err != nil {
return nil, fmt.Errorf("unable to unmarshal KX file %s: %v",
fname, err)
}
res = append(res, kxd)
}
return res, nil
}
// HasKXWithUser returns any outstanding KX attempt with the given user. This
// will return KXs which were created with an invitee filled to the target
// ID or when they were accepted and the remote user has the target ID.
func (db *DB) HasKXWithUser(tx ReadTx, target UserID) ([]KXData, error) {
dir := filepath.Join(db.root, kxDir)
dirEntries, err := os.ReadDir(dir)
if err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("unable to read content dir: %v", err)
}
var testID UserID
var res []KXData
for _, f := range dirEntries {
if f.IsDir() {
continue
}
fname := filepath.Join(dir, f.Name())
if err := testID.FromString(filepath.Base(fname)); err != nil {
db.log.Warnf("Filename %q is not a KX file", fname)
continue
}
var kxd KXData
if err := db.readJsonFile(fname, &kxd); err != nil {
db.log.Warnf("Unable to unmarshal %s as a KX file: %v",
fname, err)
continue
}
if (kxd.Invitee != nil && kxd.Invitee.Identity == target) ||
kxd.Public.Identity == target {
res = append(res, kxd)
}
}
return res, nil
}
// StoreMediateIDRequested marks that a mediate id request was made on mediator
// to invite us to target.
func (db *DB) StoreMediateIDRequested(tx ReadWriteTx, mediator, target UserID) error {
filepath := filepath.Join(db.root, inboundDir, mediator.String(),
miRequestsDir, target.String())
req := MediateIDRequest{
Mediator: mediator,
Target: target,
Date: time.Now(),
}
return db.saveJsonFile(filepath, req)
}
// HasMediateID returns info about a request to mediate ID made to a mediator
// for introduction to a target.
func (db *DB) HasMediateID(tx ReadTx, mediator, target UserID) (MediateIDRequest, error) {
filepath := filepath.Join(db.root, inboundDir, mediator.String(),
miRequestsDir, target.String())
var req MediateIDRequest
err := db.readJsonFile(filepath, &req)
return req, err
}
// HasAnyRecentMediateID looks if there are any attempts to mediate identity to
// the given target user with any mediator that is no older than the specified
// recentThreshold.
func (db *DB) HasAnyRecentMediateID(tx ReadTx, target UserID, recentThreshold time.Duration) (bool, error) {
pattern := filepath.Join(db.root, inboundDir, "*", miRequestsDir,
target.String())
files, err := filepath.Glob(pattern)
if err != nil {
return false, err
}
if len(files) == 0 {
return false, nil
}
for _, fname := range files {
var req MediateIDRequest
err := db.readJsonFile(fname, &req)
if err != nil {
return false, err
}
if req.Date.Before(time.Now().Add(-recentThreshold)) {
continue
}
return true, nil
}
return false, nil
}
// RemoveMediateID removes the given request to mediate an ID.
func (db *DB) RemoveMediateID(tx ReadWriteTx, mediator, target UserID) error {
filepath := filepath.Join(db.root, inboundDir, mediator.String(),
miRequestsDir, target.String())
err := os.Remove(filepath)
if os.IsNotExist(err) {
return nil // Not an error.
}
return err
}
// ListMediateIDs lists all existing mediate id requests.
func (db *DB) ListMediateIDs(tx ReadTx) ([]MediateIDRequest, error) {
pattern := filepath.Join(db.root, inboundDir, "*", miRequestsDir, "*")
files, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
res := make([]MediateIDRequest, 0, len(files))
for _, fname := range files {
var req MediateIDRequest
err := db.readJsonFile(fname, &req)
if err != nil {
db.log.Warnf("Unable to read mediate id request %s: %v",
fname, err)
continue
}
res = append(res, req)
}
return res, nil
}
// StoreUnkxdUserInfo stores information about an unxked user.
func (db *DB) StoreUnkxdUserInfo(tx ReadWriteTx, info UnkxdUserInfo) error {
fname := filepath.Join(db.root, unkxdUsersDir, info.UID.String())
return db.saveJsonFile(fname, &info)
}
// ReadUnxkdUserInfo returns information about an unkxed user.
func (db *DB) ReadUnxkdUserInfo(tx ReadTx, uid UserID) (UnkxdUserInfo, error) {
fname := filepath.Join(db.root, unkxdUsersDir, uid.String())
var info UnkxdUserInfo
err := db.readJsonFile(fname, &info)
return info, err
}
// RemoveUnkxUserInfo removes the information about an unkxed user if it exists.
func (db *DB) RemoveUnkxUserInfo(tx ReadWriteTx, uid UserID) error {
fname := filepath.Join(db.root, unkxdUsersDir, uid.String())
return removeIfExists(fname)
}
// AddKXSearchQuery updates the search for a given KX opportunity with the
// target user and adds the specified query to the list of attempted queries.
func (db *DB) AddKXSearchQuery(tx ReadWriteTx, target UserID, search rpc.RMKXSearch, query KXSearchQuery) error {
filename := filepath.Join(db.root, kxSearches, target.String())
var kxs KXSearch
err := db.readJsonFile(filename, &kxs)
if err == nil {
// Already exists. Replace search and look for an existing query
// to replace or add.
kxs.Search = search
found := false
for i := range kxs.Queries {
if kxs.Queries[i].User == query.User {
kxs.Queries[i] = query
found = true
break
}
}
if !found {
kxs.Queries = append(kxs.Queries, query)
}
} else if errors.Is(err, ErrNotFound) {
// Does not exist. Just create a new one.
kxs.Target = target
kxs.Search = search
kxs.Queries = []KXSearchQuery{query}
} else {
return err
}
return db.saveJsonFile(filename, kxs)
}
// GetKXSearch returns the KX search for a given target, if it exists.
func (db *DB) GetKXSearch(tx ReadTx, target UserID) (KXSearch, error) {
filename := filepath.Join(db.root, kxSearches, target.String())
var kxs KXSearch
err := db.readJsonFile(filename, &kxs)
return kxs, err
}
// RemoveKXSearch removes the kx search for the given target if it exists.
func (db *DB) RemoveKXSearch(tx ReadWriteTx, target UserID) error {
filename := filepath.Join(db.root, kxSearches, target.String())
return removeIfExists(filename)
}
// ListKXSearches lists the IDs of all outstanding users being KX searched for.
func (db *DB) ListKXSearches(tx ReadTx) ([]UserID, error) {
dir := filepath.Join(db.root, kxSearches)
var res []UserID
files, err := os.ReadDir(dir)
if os.IsNotExist(err) {
// No KX searches yet.
return nil, nil
}
if err != nil {
return nil, err
}
for _, f := range files {
if f.IsDir() {
continue
}
var uid UserID
if err := uid.FromString(f.Name()); err != nil {
db.log.Debugf("File %q is not a UserID: %s", f.Name(), err)
continue
}
res = append(res, uid)
}
return res, nil
}
// AddInitialKXAction adds an action to be taken after kx completes with the given
// initial rendezvous.
func (db *DB) AddInitialKXAction(tx ReadWriteTx, initialRV zkidentity.ShortID, action PostKXAction) error {
filename := filepath.Join(db.root, initKXActionsDir, initialRV.String())
var actions []PostKXAction
err := db.readJsonFile(filename, &actions)
if err != nil && !errors.Is(err, ErrNotFound) {
return err
}
actions = append(actions, action)
return db.saveJsonFile(filename, actions)
}
// RemoveInitialKXActions removes the initial-kx actions registered for the given
// initial rendezvous.
func (db *DB) RemoveInitialKXActions(tx ReadWriteTx, initialRV zkidentity.ShortID) error {
filename := filepath.Join(db.root, initKXActionsDir, initialRV.String())
err := os.Remove(filename)
if os.IsNotExist(err) {
return nil
}
return err
}
// InitialToPostKXAction converts an action based on initial rendezvous to a known user id.
func (db *DB) InitialToPostKXActions(tx ReadWriteTx, initialRV, target zkidentity.ShortID) error {
filename := filepath.Join(db.root, initKXActionsDir, initialRV.String())
var actions []PostKXAction
err := db.readJsonFile(filename, &actions)
switch {
case errors.Is(err, ErrNotFound):
return nil
case err != nil:
return err
default:
}
for _, action := range actions {
if err = db.AddUniquePostKXAction(tx, target, action); err != nil {
return err
}
}
return db.RemoveInitialKXActions(tx, initialRV)
}
// AddPostKXAction adds an action to be taken after kx completes with the given
// target user.
func (db *DB) AddPostKXAction(tx ReadWriteTx, target UserID, action PostKXAction) error {
filename := filepath.Join(db.root, postKXActionsDir, target.String())
var actions []PostKXAction
err := db.readJsonFile(filename, &actions)
if err != nil && !errors.Is(err, ErrNotFound) {
return err
}
actions = append(actions, action)
return db.saveJsonFile(filename, actions)
}
// AddUniquePostKXAction adds a post-kx action unless an action of the same
// type and data already exists.
func (db *DB) AddUniquePostKXAction(tx ReadWriteTx, target UserID, action PostKXAction) error {
filename := filepath.Join(db.root, postKXActionsDir, target.String())
var actions []PostKXAction
err := db.readJsonFile(filename, &actions)
if err != nil && !errors.Is(err, ErrNotFound) {
return err
}
for _, act := range actions {
if act.Type == action.Type && act.Data == action.Data {
// Duplicated.
return nil
}
}
actions = append(actions, action)
return db.saveJsonFile(filename, actions)
}
// ListPostKXActions lists the post-kx actions registered for the given target
// user.
func (db *DB) ListPostKXActions(tx ReadTx, target UserID) ([]PostKXAction, error) {
filename := filepath.Join(db.root, postKXActionsDir, target.String())
var actions []PostKXAction
err := db.readJsonFile(filename, &actions)
if errors.Is(err, ErrNotFound) {
return nil, nil
}
return actions, err
}
// RemovePostKXActions removes the post-kx actions registered for the given
// target user.
func (db *DB) RemovePostKXActions(tx ReadWriteTx, target UserID) error {
filename := filepath.Join(db.root, postKXActionsDir, target.String())
err := os.Remove(filename)
if os.IsNotExist(err) {
return nil
}
return err
}
// ReadOnboardState fetches the existing onboard state of the client. It
// returns an error if there is no onboard state.
func (db *DB) ReadOnboardState(tx ReadTx) (clientintf.OnboardState, error) {
filename := filepath.Join(db.root, onboardStateFile)
var res clientintf.OnboardState
err := db.readJsonFile(filename, &res)
return res, err
}
// UpdateOnboardState updates the client onboard state.
func (db *DB) UpdateOnboardState(tx ReadWriteTx, st *clientintf.OnboardState) error {
filename := filepath.Join(db.root, onboardStateFile)
return db.saveJsonFile(filename, st)
}
// RemoveOnboardState removes any existing onboard state.
func (db *DB) RemoveOnboardState(tx ReadWriteTx) error {
filename := filepath.Join(db.root, onboardStateFile)
return removeIfExists(filename)
}
// HasOnboardState returns true if there is an existing onboard state.
func (db *DB) HasOnboardState(tx ReadTx) bool {
filename := filepath.Join(db.root, onboardStateFile)
return fileExists(filename)
}