forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybase.go
264 lines (226 loc) · 6.56 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
// 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"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"runtime/trace"
"sync"
"time"
"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/keybase1"
"github.com/keybase/client/go/service"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
"github.com/keybase/kbfs/fsrpc"
"github.com/keybase/kbfs/libkbfs"
)
var kbCtx *libkb.GlobalContext
var conn net.Conn
var startOnce sync.Once
var logSendContext libkb.LogSendContext
var kbfsConfig libkbfs.Config
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)
}
startTrace(logFile)
dnsNSFetcher := newDNSNSFetcher(externalDNSNSFetcher)
dnsServers := dnsNSFetcher.GetServers()
for _, srv := range dnsServers {
fmt.Printf("Go: DNS Server: %s\n", srv)
}
kbCtx = libkb.G
kbCtx.Init()
kbCtx.SetServices(externals.GetServices())
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,
}
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)
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() {
kbfsParams := libkbfs.DefaultInitParams(kbCtx)
// Setting this flag will enable KBFS debug logging to always be
// true in a mobile setting. Kill this setting if too spammy.
// (Setting to false now 2017-08-21 PC)
kbfsParams.Debug = false
kbfsParams.Mode = libkbfs.InitMinimalString
kbfsConfig, _ = libkbfs.Init(
context.Background(), kbCtx, 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) {
keybaseService := libkbfs.NewKeybaseDaemonRPC(config, ctx, log, true, nil)
keybaseService.AddProtocols([]rpc.Protocol{
keybase1.FsProtocol(fsrpc.NewFS(config, log)),
})
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
}
// LogSend sends a log to Keybase
func LogSend(status string, feedback string, sendLogs bool, uiLogPath string) (string, error) {
logSendContext.Logs.Desktop = uiLogPath
return logSendContext.LogSend(status, feedback, sendLogs, 5*1024*1024)
}
// 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
}
// Version returns semantic version string
func Version() string {
return libkb.VersionString()
}
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")
}()
}