forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (84 loc) · 2.19 KB
/
main.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package keybase
import (
"encoding/base64"
"fmt"
"net"
"sync"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/service"
)
var con net.Conn
var startOnce sync.Once
// ServerURI should match run mode environment.
func Init(homeDir string, runModeStr string, serverURI string, accessGroupOverride bool) {
startOnce.Do(func() {
g := libkb.G
g.Init()
usage := libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
runMode, err := libkb.StringToRunMode(runModeStr)
if err != nil {
fmt.Println("Error decoding run mode", err, runModeStr)
}
config := libkb.AppConfig{HomeDir: homeDir, RunMode: runMode, Debug: true, LocalRPCDebug: "Acsvip", ServerURI: serverURI, SecurityAccessGroupOverride: accessGroupOverride}
err = libkb.G.Configure(config, usage)
if err != nil {
panic(err)
}
(service.NewService(g, false)).StartLoopbackServer()
Reset()
})
}
// Takes base64 encoded msgpack rpc payload
func WriteB64(str string) bool {
data, err := base64.StdEncoding.DecodeString(str)
if err != nil {
fmt.Println("Base64 decode error:", err, str)
}
n, err := con.Write(data)
if err != nil {
fmt.Println("Write error: ", err)
return false
}
if n != len(data) {
fmt.Println("Did not write all the data")
return false
}
return true
}
// Blocking read, returns base64 encoded msgpack rpc payload
// 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 targetBufferSize = 50 * 1024
const bufferSize = targetBufferSize - (targetBufferSize % 3)
func ReadB64() string {
data := make([]byte, bufferSize)
n, err := con.Read(data)
if n > 0 && err == nil {
str := base64.StdEncoding.EncodeToString(data[0:n])
return str
}
if err != nil {
fmt.Println("read error:", err)
// attempt to fix the connection
Reset()
}
return ""
}
func Reset() {
if con != nil {
con.Close()
}
var err error
libkb.G.SocketWrapper = nil
con, _, _, err = libkb.G.GetSocket(false)
if err != nil {
fmt.Println("loopback socker error:", err)
}
}