-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
context.go
345 lines (302 loc) · 9.54 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
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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package env
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/keybase/client/go/kbconst"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
const (
kbfsSocketFile = "kbfsd.sock"
)
// AppStateUpdater is an interface for things that need to listen to
// app state changes.
type AppStateUpdater interface {
// NextAppStateUpdate returns a channel that app state changes
// are sent to.
NextAppStateUpdate(lastState *keybase1.MobileAppState) <-chan keybase1.MobileAppState
// NextNetworkStateUpdate returns a channel that mobile network
// state changes are sent to.
NextNetworkStateUpdate(lastState *keybase1.MobileNetworkState) <-chan keybase1.MobileNetworkState
}
// EmptyAppStateUpdater is an implementation of AppStateUpdater that
// never returns an update, for testing.
type EmptyAppStateUpdater struct{}
// NextAppStateUpdate implements AppStateUpdater.
func (easu EmptyAppStateUpdater) NextAppStateUpdate(lastState *keybase1.MobileAppState) <-chan keybase1.MobileAppState {
// Receiving on a nil channel blocks forever.
return nil
}
// NextNetworkStateUpdate implements AppStateUpdater.
func (easu EmptyAppStateUpdater) NextNetworkStateUpdate(
lastState *keybase1.MobileNetworkState) <-chan keybase1.MobileNetworkState {
// Receiving on a nil channel blocks forever.
return nil
}
// Context defines the environment for this package
type Context interface {
AppStateUpdater
GetRunMode() kbconst.RunMode
GetLogDir() string
GetDataDir() string
GetEnv() *libkb.Env
GetMountDir() (string, error)
ConfigureSocketInfo() (err error)
CheckService() error
GetSocket(clearError bool) (net.Conn, rpc.Transporter, bool, error)
NewRPCLogFactory() rpc.LogFactory
NewNetworkInstrumenter(keybase1.NetworkSource) rpc.NetworkInstrumenterStorage
GetKBFSSocket(clearError bool) (net.Conn, rpc.Transporter, bool, error)
BindToKBFSSocket() (net.Listener, error)
GetVDebugSetting() string
GetPerfLog() logger.Logger
}
// KBFSContext is an implementation for libkbfs.Context
type KBFSContext struct {
g *libkb.GlobalContext
// protects the socket primitives below
kbfsSocketMtx sync.RWMutex
kbfsSocket libkb.Socket
kbfsSocketWrapper *libkb.SocketWrapper
}
var _ Context = (*KBFSContext)(nil)
func (c *KBFSContext) initKBFSSocket() {
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
log := c.g.Log
bindFile := c.getKBFSSocketFile()
dialFiles := []string{bindFile}
c.kbfsSocket = libkb.NewSocketWithFiles(log, bindFile, dialFiles)
}
// NewContextFromGlobalContext constructs a context
func NewContextFromGlobalContext(g *libkb.GlobalContext) *KBFSContext {
c := &KBFSContext{g: g}
c.initKBFSSocket()
return c
}
func newContextFromG(g *libkb.GlobalContext) *KBFSContext {
err := g.ConfigureConfig()
if err != nil {
panic(err)
}
err = g.ConfigureLogging(nil)
if err != nil {
panic(err)
}
err = g.ConfigureCaches()
if err != nil {
panic(err)
}
err = g.ConfigureMerkleClient()
if err != nil {
panic(err)
}
return NewContextFromGlobalContext(g)
}
// NewContext constructs a context. This should only be called once in
// main functions.
func NewContext() *KBFSContext {
g := libkb.NewGlobalContextInit()
return newContextFromG(g)
}
// NewContextWithPerfLog constructs a context with a specific perf
// log. This should only be called once in main functions.
func NewContextWithPerfLog(logName string) *KBFSContext {
g := libkb.NewGlobalContextInit()
// Override the perf file for this process, before logging is
// initialized.
if os.Getenv("KEYBASE_PERF_LOG_FILE") == "" {
os.Setenv("KEYBASE_PERF_LOG_FILE", filepath.Join(
g.Env.GetLogDir(), logName))
}
return newContextFromG(g)
}
// GetLogDir returns log dir
func (c *KBFSContext) GetLogDir() string {
return c.g.Env.GetLogDir()
}
// GetDataDir returns log dir
func (c *KBFSContext) GetDataDir() string {
return c.g.Env.GetDataDir()
}
// GetMountDir returns mount dir
func (c *KBFSContext) GetMountDir() (string, error) {
return c.g.Env.GetMountDir()
}
// GetEnv returns the global Env
func (c *KBFSContext) GetEnv() *libkb.Env {
return c.g.Env
}
// GetRunMode returns run mode
func (c *KBFSContext) GetRunMode() kbconst.RunMode {
return c.g.GetRunMode()
}
// GetPerfLog returns the perf log.
func (c *KBFSContext) GetPerfLog() logger.Logger {
return c.g.GetPerfLog()
}
// NextAppStateUpdate implements AppStateUpdater.
func (c *KBFSContext) NextAppStateUpdate(lastState *keybase1.MobileAppState) <-chan keybase1.MobileAppState {
if c.g.MobileAppState == nil {
return nil
}
return c.g.MobileAppState.NextUpdate(lastState)
}
// NextNetworkStateUpdate implements AppStateUpdater.
func (c *KBFSContext) NextNetworkStateUpdate(
lastState *keybase1.MobileNetworkState) <-chan keybase1.MobileNetworkState {
if c.g.MobileNetState == nil {
return nil
}
return c.g.MobileNetState.NextUpdate(lastState)
}
// CheckService checks if the service is running and returns nil if
// so, and an error otherwise.
func (c *KBFSContext) CheckService() error {
// Trying to dial the service seems like the best
// platform-agnostic way of seeing if the service is up.
// Stat-ing the socket file, for example, doesn't work for
// Windows named pipes.
s, err := libkb.NewSocket(c.g)
if err != nil {
return err
}
conn, err := s.DialSocket()
if err != nil {
switch libkb.RuntimeGroup() {
case keybase1.RuntimeGroup_DARWINLIKE, keybase1.RuntimeGroup_WINDOWSLIKE:
return errors.New(
"keybase isn't running; open the Keybase app")
default:
return errors.New(
"keybase isn't running; try `run_keybase`")
}
}
err = conn.Close()
if err != nil {
return err
}
return nil
}
// GetSocket returns a socket
func (c *KBFSContext) GetSocket(clearError bool) (
net.Conn, rpc.Transporter, bool, error) {
return c.g.GetSocket(clearError)
}
// ConfigureSocketInfo configures a socket
func (c *KBFSContext) ConfigureSocketInfo() error {
return c.g.ConfigureSocketInfo()
}
// NewRPCLogFactory constructs an RPC logger
func (c *KBFSContext) NewRPCLogFactory() rpc.LogFactory {
return &libkb.RPCLogFactory{Contextified: libkb.NewContextified(c.g)}
}
// NewNetworkInstrumenter constructs an RPC NetworkInstrumenterStorage
func (c *KBFSContext) NewNetworkInstrumenter(src keybase1.NetworkSource) rpc.NetworkInstrumenterStorage {
return libkb.NetworkInstrumenterStorageFromSrc(c.g, src)
}
func (c *KBFSContext) getSandboxSocketFile() string {
sandboxDir := c.g.Env.HomeFinder.SandboxCacheDir()
if sandboxDir == "" {
return ""
}
return filepath.Join(sandboxDir, kbfsSocketFile)
}
func (c *KBFSContext) getKBFSSocketFile() string {
e := c.g.Env
return e.GetString(
c.getSandboxSocketFile,
// TODO: maybe add command-line option here
func() string { return os.Getenv("KBFS_SOCKET_FILE") },
func() string { return filepath.Join(e.GetRuntimeDir(), kbfsSocketFile) },
)
}
func (c *KBFSContext) newTransportFromSocket(s net.Conn) rpc.Transporter {
return rpc.NewTransport(s, c.NewRPCLogFactory(), c.NewNetworkInstrumenter(keybase1.NetworkSource_LOCAL),
libkb.WrapError, rpc.DefaultMaxFrameLength)
}
// GetKBFSSocket dials the socket configured in `c.kbfsSocket`.
// Adapted from github.com/keybase/client/go/libkb.GlobalContext.GetSocket.
func (c *KBFSContext) GetKBFSSocket(clearError bool) (
net.Conn, rpc.Transporter, bool, error) {
var err error
c.g.Trace("GetSocket", &err)()
// Protect all global socket wrapper manipulation with a
// lock to prevent race conditions.
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
needWrapper := false
if c.kbfsSocketWrapper == nil {
needWrapper = true
c.g.Log.Debug("empty socket wrapper; need a new one")
} else if c.kbfsSocketWrapper.Transporter != nil && !c.kbfsSocketWrapper.Transporter.IsConnected() {
// need reconnect
c.g.Log.Debug("rpc transport isn't connected, reconnecting...")
needWrapper = true
}
isNew := false
if needWrapper {
sw := libkb.SocketWrapper{}
if c.kbfsSocket == nil {
sw.Err = fmt.Errorf("Cannot get socket")
} else {
sw.Conn, sw.Err = c.kbfsSocket.DialSocket()
c.g.Log.Debug("DialSocket -> %s", libkb.ErrToOk(sw.Err))
isNew = true
}
if sw.Err == nil {
sw.Transporter = c.newTransportFromSocket(sw.Conn)
}
c.kbfsSocketWrapper = &sw
}
// Return the current error no matter what
sw := c.kbfsSocketWrapper
if sw.Err != nil && clearError {
c.kbfsSocketWrapper = nil
}
return sw.Conn, sw.Transporter, isNew, sw.Err
}
// cleanupSocketFile cleans up the socket file for binding.
func (c *KBFSContext) cleanupSocketFile() error {
switch sock := c.kbfsSocket.(type) {
case libkb.SocketInfo:
sf := sock.GetBindFile()
if exists, err := libkb.FileExists(sf); err != nil {
return err
} else if exists {
c.g.Log.Debug("removing stale socket file: %s", sf)
if err = os.Remove(sf); err != nil {
c.g.Log.Warning("error removing stale socket file: %s", err)
return err
}
}
case nil:
return errors.New("socket not initialized")
default:
return errors.New("invalid socket type")
}
return nil
}
// BindToKBFSSocket binds to the socket configured in `c.kbfsSocket`.
func (c *KBFSContext) BindToKBFSSocket() (net.Listener, error) {
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
err := c.cleanupSocketFile()
if err != nil {
return nil, err
}
return c.kbfsSocket.BindToSocket()
}
// GetVDebugSetting returns the verbose debug logger.
func (c *KBFSContext) GetVDebugSetting() string {
return c.g.Env.GetVDebugSetting()
}