-
Notifications
You must be signed in to change notification settings - Fork 6
/
p2p.go
267 lines (227 loc) · 8.57 KB
/
p2p.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
package handlers
import (
"errors"
"io/ioutil"
"net/http"
"github.com/buger/jsonparser"
"github.com/gorilla/mux"
"github.com/gladiusio/gladius-common/pkg/blockchain"
"github.com/gladiusio/gladius-p2p/pkg/p2p/message"
"github.com/gladiusio/gladius-p2p/pkg/p2p/peer"
"github.com/gladiusio/gladius-p2p/pkg/p2p/signature"
"github.com/gladiusio/gladius-p2p/pkg/p2p/state"
)
func parseSignedMessageFromBytes(smBytes []byte) (*signature.SignedMessage, error) {
messageBytes, _, _, err := jsonparser.Get(smBytes, "message")
if err != nil {
return nil, errors.New("Can't find `message` in body")
}
hash, err := jsonparser.GetString(smBytes, "hash")
if err != nil {
return nil, errors.New("Can't find `hash` in body")
}
signatureString, err := jsonparser.GetString(smBytes, "signature")
if err != nil {
return nil, errors.New("Could not find `signature` in body")
}
address, err := jsonparser.GetString(smBytes, "address")
if err != nil {
return nil, errors.New("Could not find `address` in body")
}
parsed, err := signature.ParseSignedMessage(string(messageBytes), hash, signatureString, address)
if err != nil {
return nil, errors.New("Couldn't parse body")
}
return parsed, nil
}
// Helper to create signed message from body
func getSignedMessageFromBody(w http.ResponseWriter, r *http.Request) *signature.SignedMessage {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErrorHandler(w, r, "Error decoding body", err, http.StatusBadRequest)
return nil
}
sm, err := parseSignedMessageFromBytes(body)
if err != nil {
ErrorHandler(w, r, "Error parsing signed message", err, http.StatusBadRequest)
}
return sm
}
// Helper to get fields from the json body and verify the signature
func verifyBody(w http.ResponseWriter, r *http.Request) (bool, *signature.SignedMessage) {
parsed := getSignedMessageFromBody(w, r)
if parsed == nil {
return false, nil
}
verified := parsed.IsInPoolAndVerified()
return verified, parsed
}
// Gets a content list from an incoming comparison request
func getContentListFromBody(w http.ResponseWriter, r *http.Request) []string {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErrorHandler(w, r, "Error decoding body", err, http.StatusBadRequest)
return nil
}
s := make([]string, 0)
// Get all content file names passed in
jsonparser.ArrayEach(body, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
s = append(s, string(value))
}, "content")
return s
}
// VerifySignedMessageHandler verifies the incoming message with takes the form
// of:
// {"message": "b64string", "hash": "b64string", "signature": "b64string", "address": ""}
func VerifySignedMessageHandler(w http.ResponseWriter, r *http.Request) {
v, _ := verifyBody(w, r)
if v {
ResponseHandler(w, r, "Message is verified", true, nil, true, nil)
} else {
ResponseHandler(w, r, "Message is not verified", true, nil, false, nil)
}
}
/*******************************************************************************
All methods below use the account generated by the gladius account manager
*******************************************************************************/
// CreateSignedMessageHandler takes the incoming message and returns a signed
// version that includes the timestamp.
func CreateSignedMessageHandler(ga *blockchain.GladiusAccountManager) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErrorHandler(w, r, "Error decoding body", err, http.StatusBadRequest)
return
}
messageBytes, _, _, err := jsonparser.Get(body, "message")
if err != nil {
ErrorHandler(w, r, "Could not find `message` in body", err, http.StatusBadRequest)
return
}
signed, err := signature.CreateSignedMessage(message.New(messageBytes), ga)
if err != nil {
ErrorHandler(w, r, "Could not create sign message. Wallet likely locked.", err, http.StatusBadRequest)
return
}
ResponseHandler(w, r, "Created signed message", true, nil, signed, nil)
}
}
func SetStateDebugHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErrorHandler(w, r, "Error decoding body", err, http.StatusBadRequest)
return
}
toUpdate, _, _, err := jsonparser.Get(body, "state")
if err != nil {
ErrorHandler(w, r, "Could not find `state` in body", err, http.StatusBadRequest)
return
}
s, _ := state.ParseNetworkState(toUpdate)
p.SetState(s)
ResponseHandler(w, r, "Updated state", true, nil, s, nil)
}
}
// PushStateMessageHandler updates state with signed update and pushes state to
// a set of random peers. They then propigate it to their peers until the
// network has a consistent state
func PushStateMessageHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
v, sm := verifyBody(w, r)
if v {
err := p.UpdateAndPushState(sm)
if err != nil {
ErrorHandler(w, r, "Error updating state", err, http.StatusBadRequest)
} else {
ResponseHandler(w, r, "Message was pushed. This does not necessarily mean that the message was recieved by peers.", true, nil, true, nil)
}
} else {
if sm != nil {
ErrorHandler(w, r, "Cannot verifiy signature", errors.New("cannot verifiy signature"), http.StatusBadRequest)
}
}
}
}
func getIntroductionDataFromBody(w http.ResponseWriter, r *http.Request) (ip string) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ErrorHandler(w, r, "Error decoding body", err, http.StatusBadRequest)
return ""
}
ip, err = jsonparser.GetString(body, "ip")
if err != nil {
ErrorHandler(w, r, "Could not find `ip` in body", err, http.StatusBadRequest)
return ""
}
return ip
}
// JoinHandler takes in an IP and tries to join it's cluster
func JoinHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ip := getIntroductionDataFromBody(w, r)
if ip != "" {
err := p.Join([]string{ip})
if err != nil {
ErrorHandler(w, r, "Couldn't join network", err, http.StatusBadRequest)
return
}
ResponseHandler(w, r, "Requested to join pool", true, nil, nil, nil)
return
}
ErrorHandler(w, r, "IP can't be empty", nil, http.StatusBadRequest)
}
}
func LeaveHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := p.StopAndLeave()
if err != nil {
ErrorHandler(w, r, "Couldn't leave network", err, http.StatusBadRequest)
return
}
ResponseHandler(w, r, "Left network", true, nil, nil, nil)
}
}
// GetFullStateHandler gets the current state the node has access to.
func GetFullStateHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
state := p.GetState()
ResponseHandler(w, r, "Got full state", true, nil, state, nil)
}
}
// GetNodeStateHandler gets the current state of a specific node
func GetNodeStateHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
na := vars["node_address"]
nodeState, exists := p.GetState().NodeDataMap[na]
if exists {
ResponseHandler(w, r, "Got state for: "+na, true, nil, nodeState, nil)
return
}
ErrorHandler(w, r, "That node doesn't exist", nil, http.StatusBadRequest)
}
}
// GetSignatureListHandler gets the list of signatures used to create the current
// state
func GetSignatureListHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ResponseHandler(w, r, "Fetched signature list. This can be used to rebuild a verified state.", true, nil, p.GetState().GetSignatureList(), nil)
}
}
// GetContentNeededHandler compares the content provided with what is required by the
// pool and returns the deltas in the format website/<asset or route/filename
func GetContentNeededHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
c := getContentListFromBody(w, r)
ResponseHandler(w, r, "Got needed content", true, nil, p.CompareContent(c), nil)
}
}
// GetContentLinksHandler gets the links to the given list of files from random
// nodes in the network
func GetContentLinksHandler(p *peer.Peer) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
c := getContentListFromBody(w, r)
ResponseHandler(w, r, "Got needed content links", true, nil, p.GetContentLinks(c), nil)
}
}