/
context.go
203 lines (174 loc) · 5.36 KB
/
context.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
package chat
import (
"context"
"github.com/keybase/client/go/chat/globals"
"github.com/keybase/client/go/chat/types"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
type appTypeSource interface {
GetAppType() libkb.AppType
}
type keyfinderKey int
type identifyNotifierKey int
type chatTrace int
type identifyModeKey int
type upakfinderKey int
type rateLimitKey int
type nameInfoOverride int
var kfKey keyfinderKey
var inKey identifyNotifierKey
var chatTraceKey chatTrace
var identModeKey identifyModeKey
var upKey upakfinderKey
var rlKey rateLimitKey
var nameInfoOverrideKey nameInfoOverride
type identModeData struct {
mode keybase1.TLFIdentifyBehavior
breaks *[]keybase1.TLFIdentifyFailure
}
func IdentifyModeCtx(ctx context.Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure) context.Context {
if mode == keybase1.TLFIdentifyBehavior_UNSET {
mode = keybase1.TLFIdentifyBehavior_CHAT_CLI
}
return context.WithValue(ctx, identModeKey, identModeData{mode: mode, breaks: breaks})
}
func IdentifyMode(ctx context.Context) (ib keybase1.TLFIdentifyBehavior, breaks *[]keybase1.TLFIdentifyFailure, ok bool) {
var imd identModeData
val := ctx.Value(identModeKey)
if imd, ok = val.(identModeData); ok {
return imd.mode, imd.breaks, ok
}
return keybase1.TLFIdentifyBehavior_CHAT_CLI, nil, false
}
func CtxKeyFinder(ctx context.Context, g *globals.Context) KeyFinder {
var kf KeyFinder
var ok bool
val := ctx.Value(kfKey)
if kf, ok = val.(KeyFinder); ok {
return kf
}
return NewKeyFinder(g)
}
func CtxIdentifyNotifier(ctx context.Context) types.IdentifyNotifier {
var in types.IdentifyNotifier
var ok bool
val := ctx.Value(inKey)
if in, ok = val.(types.IdentifyNotifier); ok {
return in
}
return nil
}
func CtxUPAKFinder(ctx context.Context, g *globals.Context) types.UPAKFinder {
var up types.UPAKFinder
var ok bool
val := ctx.Value(upKey)
if up, ok = val.(types.UPAKFinder); ok {
return up
}
return NewCachingUPAKFinder(g)
}
func CtxAddRateLimit(ctx context.Context, rl []chat1.RateLimit) {
val := ctx.Value(rlKey)
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
for _, r := range rl {
existingRL[r.Name] = r
}
}
}
func CtxRateLimits(ctx context.Context) (res []chat1.RateLimit) {
val := ctx.Value(rlKey)
if existingRL, ok := val.(map[string]chat1.RateLimit); ok {
for _, rl := range existingRL {
res = append(res, rl)
}
}
return res
}
func CtxModifyIdentifyNotifier(ctx context.Context, notifier types.IdentifyNotifier) context.Context {
return context.WithValue(ctx, inKey, notifier)
}
func CtxTrace(ctx context.Context) (string, bool) {
var trace string
var ok bool
val := ctx.Value(chatTraceKey)
if trace, ok = val.(string); ok {
return trace, true
}
return "", false
}
func CtxTestingNameInfoSource(ctx context.Context) (types.NameInfoSource, bool) {
val := ctx.Value(nameInfoOverrideKey)
if ni, ok := val.(types.NameInfoSource); ok {
return ni, true
}
return nil, false
}
func CtxAddTestingNameInfoSource(ctx context.Context, ni types.NameInfoSource) context.Context {
return context.WithValue(ctx, nameInfoOverrideKey, ni)
}
func CtxAddLogTags(ctx context.Context, env appTypeSource) context.Context {
// Add trace context value
trace := libkb.RandStringB64(3)
ctx = context.WithValue(ctx, chatTraceKey, trace)
// Add log tags
ctx = libkb.WithLogTagWithValue(ctx, "chat-trace", trace)
rpcTags := make(map[string]interface{})
rpcTags["user-agent"] = libkb.UserAgent
rpcTags["platform"] = libkb.GetPlatformString()
rpcTags["apptype"] = env.GetAppType()
ctx = rpc.AddRpcTagsToContext(ctx, rpcTags)
return ctx
}
func Context(ctx context.Context, g *globals.Context, mode keybase1.TLFIdentifyBehavior,
breaks *[]keybase1.TLFIdentifyFailure, notifier types.IdentifyNotifier) context.Context {
if breaks == nil {
breaks = new([]keybase1.TLFIdentifyFailure)
}
res := ctx
_, _, ok := IdentifyMode(res)
if !ok {
res = IdentifyModeCtx(res, mode, breaks)
}
val := res.Value(kfKey)
if _, ok := val.(KeyFinder); !ok {
res = context.WithValue(res, kfKey, NewKeyFinder(g))
}
val = res.Value(inKey)
if _, ok := val.(types.IdentifyNotifier); !ok {
res = context.WithValue(res, inKey, notifier)
}
val = res.Value(upKey)
if _, ok := val.(types.UPAKFinder); !ok {
res = context.WithValue(res, upKey, NewCachingUPAKFinder(g))
}
val = res.Value(rlKey)
if _, ok := val.(map[string]chat1.RateLimit); !ok {
res = context.WithValue(res, rlKey, make(map[string]chat1.RateLimit))
}
if _, ok = CtxTrace(res); !ok {
res = CtxAddLogTags(res, g.GetEnv())
}
return res
}
func BackgroundContext(sourceCtx context.Context, g *globals.Context) context.Context {
rctx := libkb.CopyTagsToBackground(sourceCtx)
in := CtxIdentifyNotifier(sourceCtx)
if ident, breaks, ok := IdentifyMode(sourceCtx); ok {
rctx = Context(rctx, g, ident, breaks, in)
}
// Overwrite trace tag
if tr, ok := sourceCtx.Value(chatTraceKey).(string); ok {
rctx = context.WithValue(rctx, chatTraceKey, tr)
}
if ni, ok := CtxTestingNameInfoSource(sourceCtx); ok {
rctx = CtxAddTestingNameInfoSource(rctx, ni)
}
rctx = context.WithValue(rctx, kfKey, CtxKeyFinder(sourceCtx, g))
rctx = context.WithValue(rctx, upKey, CtxUPAKFinder(sourceCtx, g))
rctx = context.WithValue(rctx, inKey, in)
return rctx
}