forked from raoulh/mc-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moolticute.go
executable file
·313 lines (258 loc) · 7.38 KB
/
moolticute.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
package main
//Moolticute websocket connection
import (
"bytes"
"encoding/base64"
"encoding/gob"
"encoding/json"
"fmt"
"log"
"net/url"
"github.com/gorilla/websocket"
"github.com/satori/go.uuid"
)
const (
MOOLTICUTE_DAEMON_URL = "ws://localhost:30035"
)
type MsgData struct {
Service string `json:"service,omitempty"`
FallbackService string `json:"fallback_service,omitempty"`
NodeData string `json:"node_data,omitempty"`
Failed bool `json:"failed,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
Login string `json:"login,omitempty"`
Password string `json:"password,omitempty"`
Description string `json:"description,omitempty"`
RequestId string `json:"request_id,omitempty"`
ProgressTotal int `json:"progress_total,omitempty"`
ProgressCurrent int `json:"progress_current,omitempty"`
}
type MoolticuteMsg struct {
Msg string `json:"msg"`
Data MsgData `json:"data"`
ClientId string `json:"client_id,omitempty"`
}
type MoolticuteMsgRaw struct {
Msg string `json:"msg"`
Data *json.RawMessage `json:"data"`
ClientId string `json:"client_id,omitempty"`
}
type ProgressCb func(total, current int)
func McSendQuery(m *MoolticuteMsg) (*MsgData, error) {
return McSendQueryProgress(m, func(total, current int) {})
}
func McSendQueryProgress(m *MoolticuteMsg, progress ProgressCb) (*MsgData, error) {
u, err := url.Parse(*mcUrl)
if err != nil {
log.Println("Unable to parse moolticute URL", *mcUrl)
return nil, err
}
log.Printf("Moolticute: connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Print("Moolticute: dial:", err)
return nil, err
}
defer c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
defer c.Close()
if m.ClientId != "" {
m.ClientId = uuid.NewV4().String()
}
data, err := json.Marshal(m)
if err != nil {
return nil, err
}
if err = c.WriteMessage(websocket.TextMessage, data); err != nil {
log.Println("Moolticute: write:", err)
return nil, err
}
//Wait for answer
for {
_, data, err = c.ReadMessage()
if err != nil {
return nil, fmt.Errorf("Moolticute: read: %v", err)
}
log.Println(string(data))
var recv MoolticuteMsgRaw
err = json.Unmarshal(data, &recv)
if err != nil {
return nil, fmt.Errorf("Moolticute: unmarshal error: %v", err)
}
if recv.Msg == "progress" {
var recvData MsgData
err = json.Unmarshal([]byte(*recv.Data), &recvData)
if err != nil {
log.Println("Moolticute: unmarshal error:", err)
return nil, fmt.Errorf("Moolticute: %v", err)
}
progress(recvData.ProgressTotal, recvData.ProgressCurrent)
}
if recv.Msg != m.Msg {
continue
}
var recvData MsgData
err = json.Unmarshal([]byte(*recv.Data), &recvData)
if err != nil {
log.Println("Moolticute: unmarshal error:", err)
return nil, fmt.Errorf("Moolticute: %v", err)
}
if recvData.Failed {
log.Println("Error from moolticute:", recvData.ErrorMessage)
return nil, fmt.Errorf("Moolticute: %v", recvData.ErrorMessage)
}
if m.ClientId == recv.ClientId {
return &recvData, nil
}
log.Println("Should not get here, something is wrong with moolticute answer")
return nil, fmt.Errorf("Something went wrong in Moolticute answer")
}
}
//We store an array of bytes to the device
type McBinKeys [][]byte
func McLoadKeys() (keys *McBinKeys, err error) {
keys = new(McBinKeys)
u, err := url.Parse(*mcUrl)
if err != nil {
log.Println("Unable to parse moolticute URL", *mcUrl)
return
}
log.Printf("Moolticute: connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Print("Moolticute: dial:", err)
return
}
defer c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
defer c.Close()
m := MoolticuteMsg{
Msg: "get_data_node",
ClientId: uuid.NewV4().String(),
Data: MsgData{
Service: "Moolticute SSH Keys",
},
}
data, err := json.Marshal(m)
if err != nil {
return
}
if err = c.WriteMessage(websocket.TextMessage, data); err != nil {
log.Println("Moolticute: write:", err)
return
}
b64data := ""
for {
_, data, err = c.ReadMessage()
if err != nil {
log.Println("Moolticute: read:", err)
return
}
log.Println(string(data))
var recv MoolticuteMsgRaw
err = json.Unmarshal(data, &recv)
if err != nil {
log.Println("Moolticute: unmarshal error:", err)
return
}
if recv.Msg != "get_data_node" {
continue
}
var recvData MsgData
err = json.Unmarshal([]byte(*recv.Data), &recvData)
if err != nil {
log.Println("Moolticute: unmarshal error:", err)
return
}
// keys are not present, this is not an error
if recvData.Failed {
log.Println("Error getting node data from moolticute:", recvData.ErrorMessage)
return keys, nil
}
if m.ClientId == recv.ClientId {
b64data = recvData.NodeData
break
}
log.Println("Should not get here, something is wrong with moolticute answer")
return keys, fmt.Errorf("Something went wrong in Moolticute answer")
}
//try to decode binary data from device
//First Base64 decode
bdec, err := base64.StdEncoding.DecodeString(b64data)
if err != nil {
log.Println("Failed to base64 decode data:", err)
return
}
//To debug
// kf, _ := os.Create("keys.bin")
// kf.Write(bdec)
// kf.Close()
buffer := bytes.NewBuffer(bdec)
binDec := gob.NewDecoder(buffer)
err = binDec.Decode(keys)
if err != nil {
log.Println("Failed to decode encoding/gob:", err)
return
}
return
}
func McSetKeys(keys *McBinKeys) (err error) {
u := url.URL{Scheme: "ws", Host: MOOLTICUTE_DAEMON_URL, Path: "/"}
log.Printf("Moolticute: connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Print("Moolticute: dial:", err)
return
}
defer c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
defer c.Close()
var buffer bytes.Buffer
binEnc := gob.NewEncoder(&buffer)
if err = binEnc.Encode(keys); err != nil {
return fmt.Errorf("Failed to encode with encoding/gob: %v", err)
}
m := MoolticuteMsg{
Msg: "set_data_node",
ClientId: uuid.NewV4().String(),
Data: MsgData{
Service: "Moolticute SSH Keys",
NodeData: base64.StdEncoding.EncodeToString(buffer.Bytes()),
},
}
data, err := json.Marshal(m)
if err != nil {
return
}
if err = c.WriteMessage(websocket.TextMessage, data); err != nil {
log.Println("Moolticute: write:", err)
return
}
for {
_, data, err = c.ReadMessage()
if err != nil {
return fmt.Errorf("Moolticute: read: %v", err)
}
log.Println(string(data))
var recv MoolticuteMsgRaw
err = json.Unmarshal(data, &recv)
if err != nil {
return fmt.Errorf("Moolticute: unmarshal error: %v", err)
}
if recv.Msg != "set_data_node" {
continue
}
var recvData MsgData
err = json.Unmarshal([]byte(*recv.Data), &recvData)
if err != nil {
return fmt.Errorf("Moolticute: unmarshal error: %v", err)
}
// keys are not present, this is not an error
if recvData.Failed {
return fmt.Errorf("Error getting node data from moolticute: %v", err)
}
if m.ClientId == recv.ClientId {
break
}
log.Println("Should not get here, something is wrong with moolticute answer")
return fmt.Errorf("Something went wrong in Moolticute answer")
}
return
}