This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybase.go
418 lines (364 loc) · 12.2 KB
/
keybase.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package keybase
import (
"context"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"runtime/trace"
"sync"
"time"
"github.com/keybase/client/go/chat"
"github.com/keybase/client/go/chat/globals"
"strings"
"github.com/keybase/client/go/externals"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/chat1"
"github.com/keybase/client/go/protocol/gregor1"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/client/go/service"
"github.com/keybase/client/go/uidmap"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"github.com/keybase/kbfs/env"
"github.com/keybase/kbfs/fsrpc"
"github.com/keybase/kbfs/libgit"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/simplefs"
)
var kbCtx *libkb.GlobalContext
var kbChatCtx *globals.ChatContext
var conn net.Conn
var startOnce sync.Once
var logSendContext libkb.LogSendContext
var kbfsConfig libkbfs.Config
type PushNotifier interface {
LocalNotification(ident string, msg string, badgeCount int, soundName string, convID string, typ string)
}
type ExternalDNSNSFetcher interface {
GetServers() []byte
}
type dnsNSFetcher struct {
externalFetcher ExternalDNSNSFetcher
}
func newDNSNSFetcher(d ExternalDNSNSFetcher) dnsNSFetcher {
return dnsNSFetcher{
externalFetcher: d,
}
}
func (d dnsNSFetcher) processExternalResult(raw []byte) []string {
return strings.Split(string(raw), ",")
}
func (d dnsNSFetcher) GetServers() []string {
if d.externalFetcher != nil {
return d.processExternalResult(d.externalFetcher.GetServers())
}
return getDNSServers()
}
var _ libkb.DNSNameServerFetcher = dnsNSFetcher{}
// InitOnce runs the Keybase services (only runs one time)
func InitOnce(homeDir string, logFile string, runModeStr string, accessGroupOverride bool,
dnsNSFetcher ExternalDNSNSFetcher) {
startOnce.Do(func() {
if err := Init(homeDir, logFile, runModeStr, accessGroupOverride, dnsNSFetcher); err != nil {
kbCtx.Log.Errorf("Init error: %s", err)
}
})
}
// Init runs the Keybase services
func Init(homeDir string, logFile string, runModeStr string, accessGroupOverride bool,
externalDNSNSFetcher ExternalDNSNSFetcher) error {
fmt.Println("Go: Initializing")
if logFile != "" {
fmt.Printf("Go: Using log: %s\n", logFile)
}
// Reduce OS threads on mobile so we don't have too much contention with JS thread
oldProcs := runtime.GOMAXPROCS(0)
newProcs := oldProcs / 2
runtime.GOMAXPROCS(newProcs)
fmt.Printf("Go: setting GOMAXPROCS to: %d previous: %d\n", newProcs, oldProcs)
startTrace(logFile)
dnsNSFetcher := newDNSNSFetcher(externalDNSNSFetcher)
dnsServers := dnsNSFetcher.GetServers()
for _, srv := range dnsServers {
fmt.Printf("Go: DNS Server: %s\n", srv)
}
kbCtx = libkb.NewGlobalContext()
kbCtx.Init()
kbCtx.SetServices(externals.GetServices())
// 10k uid -> FullName cache entries allowed
kbCtx.SetUIDMapper(uidmap.NewUIDMap(10000))
usage := libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
runMode, err := libkb.StringToRunMode(runModeStr)
if err != nil {
return err
}
config := libkb.AppConfig{
HomeDir: homeDir,
LogFile: logFile,
RunMode: runMode,
Debug: true,
LocalRPCDebug: "",
VDebugSetting: "mobile", // use empty string for same logging as desktop default
SecurityAccessGroupOverride: accessGroupOverride,
ChatInboxSourceLocalizeThreads: 5,
}
err = kbCtx.Configure(config, usage)
if err != nil {
return err
}
svc := service.NewService(kbCtx, false)
err = svc.StartLoopbackServer()
if err != nil {
return err
}
kbCtx.SetService()
uir := service.NewUIRouter(kbCtx)
kbCtx.SetUIRouter(uir)
kbCtx.SetDNSNameServerFetcher(dnsNSFetcher)
svc.SetupCriticalSubServices()
svc.RunBackgroundOperations(uir)
kbChatCtx = svc.ChatContextified.ChatG()
serviceLog := config.GetLogFile()
logs := libkb.Logs{
Service: serviceLog,
}
logSendContext = libkb.LogSendContext{
Contextified: libkb.NewContextified(kbCtx),
Logs: logs,
}
// open the connection
err = Reset()
if err != nil {
return err
}
go func() {
kbfsCtx := env.NewContextFromGlobalContext(kbCtx)
kbfsParams := libkbfs.DefaultInitParams(kbfsCtx)
// Setting this flag will enable KBFS debug logging to always
// be true in a mobile setting. Change these back to the
// commented-out values if we need to make a mobile release
// before KBFS-on-mobile is ready.
kbfsParams.Debug = true // false
kbfsParams.Mode = libkbfs.InitConstrainedString // libkbfs.InitMinimalString
kbfsConfig, _ = libkbfs.Init(
context.Background(), kbfsCtx, kbfsParams, serviceCn{}, func() {},
kbCtx.Log)
}()
return nil
}
type serviceCn struct {
ctx *libkb.GlobalContext
}
func (s serviceCn) NewKeybaseService(config libkbfs.Config, params libkbfs.InitParams, ctx libkbfs.Context, log logger.Logger) (libkbfs.KeybaseService, error) {
// TODO: plumb the func somewhere it can be called on shutdown?
gitrpc, _ := libgit.NewRPCHandlerWithCtx(
ctx, config, nil)
additionalProtocols := []rpc.Protocol{
keybase1.SimpleFSProtocol(
simplefs.NewSimpleFS(ctx.GetGlobalContext(), config)),
keybase1.KBFSGitProtocol(gitrpc),
keybase1.FsProtocol(fsrpc.NewFS(config, log)),
}
keybaseService := libkbfs.NewKeybaseDaemonRPC(
config, ctx, log, true, additionalProtocols)
return keybaseService, nil
}
func (s serviceCn) NewCrypto(config libkbfs.Config, params libkbfs.InitParams, ctx libkbfs.Context, log logger.Logger) (libkbfs.Crypto, error) {
return libkbfs.NewCryptoClientRPC(config, ctx), nil
}
func (s serviceCn) NewChat(config libkbfs.Config, params libkbfs.InitParams, ctx libkbfs.Context, log logger.Logger) (libkbfs.Chat, error) {
return libkbfs.NewChatRPC(config, ctx), nil
}
// LogSend sends a log to Keybase
func LogSend(status string, feedback string, sendLogs bool, uiLogPath, traceDir string) (string, error) {
logSendContext.Logs.Desktop = uiLogPath
logSendContext.Logs.Trace = traceDir
env := kbCtx.Env
return logSendContext.LogSend(status, feedback, sendLogs, 10*1024*1024, env.GetUID(), env.GetInstallID())
}
// WriteB64 sends a base64 encoded msgpack rpc payload
func WriteB64(str string) error {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return fmt.Errorf("Base64 decode error: %s; %s", err, str)
}
n, err := conn.Write(data)
if err != nil {
return fmt.Errorf("Write error: %s", err)
}
if n != len(data) {
return errors.New("Did not write all the data")
}
return nil
}
const targetBufferSize = 50 * 1024
// bufferSize must be divisible by 3 to ensure that we don't split
// our b64 encode across a payload boundary if we go over our buffer
// size.
const bufferSize = targetBufferSize - (targetBufferSize % 3)
// buffer for the conn.Read
var buffer = make([]byte, bufferSize)
// ReadB64 is a blocking read for base64 encoded msgpack rpc data.
// It is called serially by the mobile run loops.
func ReadB64() (string, error) {
n, err := conn.Read(buffer)
if n > 0 && err == nil {
str := base64.StdEncoding.EncodeToString(buffer[0:n])
return str, nil
}
if err != nil {
// Attempt to fix the connection
Reset()
return "", fmt.Errorf("Read error: %s", err)
}
return "", nil
}
// Reset resets the socket connection
func Reset() error {
if conn != nil {
conn.Close()
}
var err error
conn, err = kbCtx.LoopbackListener.Dial()
if err != nil {
return fmt.Errorf("Socket error: %s", err)
}
return nil
}
// ForceGC Forces a gc
func ForceGC() {
fmt.Printf("Flushing global caches\n")
kbCtx.FlushCaches()
fmt.Printf("Done flushing global caches\n")
fmt.Printf("Starting force gc\n")
debug.FreeOSMemory()
fmt.Printf("Done force gc\n")
}
// Version returns semantic version string
func Version() string {
return libkb.VersionString()
}
func SetAppStateForeground() {
defer kbCtx.Trace("SetAppStateForeground", func() error { return nil })()
kbCtx.AppState.Update(keybase1.AppState_FOREGROUND)
}
func SetAppStateBackground() {
defer kbCtx.Trace("SetAppStateBackground", func() error { return nil })()
kbCtx.AppState.Update(keybase1.AppState_BACKGROUND)
}
func SetAppStateInactive() {
defer kbCtx.Trace("SetAppStateInactive", func() error { return nil })()
kbCtx.AppState.Update(keybase1.AppState_INACTIVE)
}
func SetAppStateBackgroundActive() {
defer kbCtx.Trace("SetAppStateBackgroundActive", func() error { return nil })()
kbCtx.AppState.Update(keybase1.AppState_BACKGROUNDACTIVE)
}
func BackgroundSync() {
defer kbCtx.Trace("BackgroundSync", func() error { return nil })()
// Skip the sync if we aren't in the background
if state := kbCtx.AppState.State(); state != keybase1.AppState_BACKGROUND {
kbCtx.Log.Debug("BackgroundSync: skipping, app not in background state: %v", state)
return
}
nextState := keybase1.AppState_BACKGROUNDACTIVE
kbCtx.AppState.Update(nextState)
doneCh := make(chan struct{})
go func() {
defer func() { close(doneCh) }()
select {
case state := <-kbCtx.AppState.NextUpdate(&nextState):
// if literally anything happens, let's get out of here
kbCtx.Log.Debug("BackgroundSync: bailing out early, appstate change: %v", state)
return
case <-time.After(10 * time.Second):
kbCtx.AppState.Update(keybase1.AppState_BACKGROUND)
return
}
}()
<-doneCh
}
func HandleBackgroundNotification(strConvID string, intMembersType int, intMessageID int,
pushID string, badgeCount int, unixTime int, body string, pusher PushNotifier) (err error) {
gc := globals.NewContext(kbCtx, kbChatCtx)
ctx := chat.Context(context.Background(), gc,
keybase1.TLFIdentifyBehavior_CHAT_GUI, nil, chat.NewCachingIdentifyNotifier(gc))
defer kbCtx.CTrace(ctx, fmt.Sprintf("HandleBackgroundNotification(%s,%d,%d,%s,%d,%d)",
strConvID, intMembersType, intMessageID, pushID, badgeCount, unixTime),
func() error { return err })()
uid := gregor1.UID(kbCtx.Env.GetUID().ToBytes())
if !kbCtx.ActiveDevice.HaveKeys() {
return libkb.LoginRequiredError{}
}
age := time.Since(time.Unix(int64(unixTime), 0))
if age >= 15*time.Second {
kbCtx.Log.CDebugf(ctx, "HandleBackgroundNotification: stale notification: %v", age)
return errors.New("stale notification")
}
bConvID, err := hex.DecodeString(strConvID)
if err != nil {
kbCtx.Log.CDebugf(ctx, "HandleBackgroundNotification: invalid convID: %s msg: %s", strConvID,
err)
return err
}
convID := chat1.ConversationID(bConvID)
membersType := chat1.ConversationMembersType(intMembersType)
msg, err := kbCtx.ChatHelper.UnboxMobilePushNotification(ctx, uid, convID, membersType, []string{pushID},
body)
if err != nil {
kbCtx.Log.CDebugf(ctx, "HandleBackgroundNotification: failed to unbox: %s", err)
return err
}
// Send up the local notification with our message
id := fmt.Sprintf("%s:%d", strConvID, intMessageID)
pusher.LocalNotification(id, msg, badgeCount, "keybasemessage.wav", strConvID, "chat.newmessage")
// Hit the remote server to let it know we succeeded in showing something useful
kbCtx.ChatHelper.AckMobileNotificationSuccess(ctx, []string{pushID})
return nil
}
// AppWillExit is called reliably on iOS when the app is about to terminate
// not as reliably on android
func AppWillExit() {
defer kbCtx.Trace("AppWillExit", func() error { return nil })()
kbCtx.AppState.Update(keybase1.AppState_BACKGROUNDFINAL)
}
// AppDidEnterBackground notifies the service that the app is in the background
// [iOS] returning true will request about ~3mins from iOS to continue execution
func AppDidEnterBackground() bool {
defer kbCtx.Trace("AppDidEnterBackground", func() error { return nil })()
SetAppStateBackground()
return false
}
func startTrace(logFile string) {
if os.Getenv("KEYBASE_TRACE_MOBILE") != "1" {
return
}
tname := filepath.Join(filepath.Dir(logFile), "svctrace.out")
f, err := os.Create(tname)
if err != nil {
fmt.Printf("error creating %s\n", tname)
return
}
fmt.Printf("Go: starting trace %s\n", tname)
trace.Start(f)
go func() {
fmt.Printf("Go: sleeping 30s for trace\n")
time.Sleep(30 * time.Second)
fmt.Printf("Go: stopping trace %s\n", tname)
trace.Stop()
time.Sleep(5 * time.Second)
fmt.Printf("Go: trace stopped\n")
}()
}