-
Notifications
You must be signed in to change notification settings - Fork 21
/
peer_mesh.go
101 lines (84 loc) · 2.35 KB
/
peer_mesh.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
package api
import (
"encoding/json"
"github.com/evilsocket/islazy/log"
"github.com/evilsocket/pwngrid/mesh"
"github.com/go-chi/chi"
"io/ioutil"
"net/http"
"sort"
)
// GET /api/v1/mesh/peers
func (api *API) PeerGetPeers(w http.ResponseWriter, r *http.Request) {
peers := make([]*mesh.Peer, 0)
mesh.Peers.Range(func(key, value interface{}) bool {
peers = append(peers, value.(*mesh.Peer))
return true
})
// closer first
sort.Slice(peers, func(i, j int) bool {
return peers[i].RSSI > peers[j].RSSI
})
JSON(w, http.StatusOK, peers)
}
// GET /api/v1/mesh/memory
func (api *API) PeerGetMemory(w http.ResponseWriter, r *http.Request) {
peers := api.Mesh.Memory()
// higher number of encounters first
sort.Slice(peers, func(i, j int) bool {
return peers[i].Encounters > peers[j].Encounters
})
JSON(w, http.StatusOK, peers)
}
// GET /api/v1/mesh/memory/<fingerprint>
func (api *API) PeerGetMemoryOf(w http.ResponseWriter, r *http.Request) {
fingerprint := chi.URLParam(r, "fingerprint")
peer := api.Mesh.MemoryOf(fingerprint)
if peer == nil {
ERROR(w, http.StatusNotFound, ErrEmpty)
return
}
JSON(w, http.StatusOK, peer)
}
// GET /api/v1/mesh/<status>
func (api *API) PeerSetSignaling(w http.ResponseWriter, r *http.Request) {
status := chi.URLParam(r, "status")
if status == "enabled" || status == "true" {
api.Peer.Advertise(true)
} else if status == "disabled" || status == "false" {
api.Peer.Advertise(false)
} else {
ERROR(w, http.StatusNotFound, ErrEmpty)
return
}
JSON(w, http.StatusOK, map[string]interface{}{
"success": true,
})
}
// GET /api/v1/mesh/data
func (api *API) PeerGetMeshData(w http.ResponseWriter, r *http.Request) {
JSON(w, http.StatusOK, api.Peer.Data())
}
// POST /api/v1/mesh/data
func (api *API) PeerSetMeshData(w http.ResponseWriter, r *http.Request) {
var newData map[string]interface{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
log.Debug("%s", body)
if err = json.Unmarshal(body, &newData); err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
// this makes sure that the pwngrid server receives advertisements
api.Client.SetData(map[string]interface{}{
"advertisement": newData,
})
// update mesh advertisement data
api.Peer.SetData(newData)
JSON(w, http.StatusOK, map[string]interface{}{
"success": true,
})
}