-
Notifications
You must be signed in to change notification settings - Fork 8
/
moolticute.go
executable file
·241 lines (197 loc) · 5.3 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
package main
//Moolticute websocket connection
import (
"bytes"
"encoding/base64"
"encoding/gob"
"encoding/json"
"fmt"
"log"
"net/url"
"os"
"github.com/google/uuid"
"github.com/gorilla/websocket"
)
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"`
}
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"`
}
//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()
client_uuid := uuid.New()
m := MoolticuteMsg{
Msg: "get_data_node",
ClientId: client_uuid.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 == "progress" || recv.Msg == "progress_detailed") && *outputProgress == true {
fmt.Println(string(data))
os.Stdout.Sync()
}
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, 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()
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)
}
client_uuid := uuid.New()
m := MoolticuteMsg{
Msg: "set_data_node",
ClientId: client_uuid.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 == "progress" || recv.Msg == "progress_detailed") && *outputProgress == true {
fmt.Println(string(data))
os.Stdout.Sync()
}
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
}