-
Notifications
You must be signed in to change notification settings - Fork 5
/
gibbonapi.go
262 lines (231 loc) · 5.53 KB
/
gibbonapi.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
package api
import (
"encoding/json"
"fmt"
log "github.com/cihub/seelog"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/chenyf/gibbon/comet"
"github.com/chenyf/gibbon/devcenter"
)
type CommandRequest struct {
Uid string `json:"uid"`
Cmd string `json:"cmd"`
}
type CommandResponse struct {
Status int `json:"status"`
Error string `json:"error"`
}
type RouterInfo struct {
Rid string `json:"rid"`
Rname string `json:"rname"`
}
type ResponseRouterList struct {
Status int `json:"status"`
Descr string `json:"descr"`
List []RouterInfo `json:"list"`
}
func checkAuthz(uid string, devid string) bool {
log.Tracef("checkAuthz")
devices, err := devcenter.GetDevices(uid, devcenter.DEV_ROUTER)
if err != nil {
log.Errorf("GetDevices failed: %s", err.Error())
return false
}
for _, dev := range devices {
if devid == dev.Id {
return true
}
}
return false
}
func getStatus(w http.ResponseWriter, r *http.Request) {
size := comet.DevMap.Size()
fmt.Fprintf(w, "total register device: %d\n", size)
}
func postRouterCommand(w http.ResponseWriter, r *http.Request) {
log.Tracef("postRouterCommand")
log.Debugf("Request from RemoterAddr: %s", r.RemoteAddr)
var (
uid string
rid string
response CommandResponse
client *comet.Client
body []byte
err error
cmdRequest CommandRequest
bCmd []byte
reply chan *comet.Message
)
response.Status = 1
if r.Method != "POST" {
response.Error = "must using 'POST' method\n"
goto resp
}
r.ParseForm()
rid = r.FormValue("rid")
if rid == "" {
response.Error = "missing 'rid'"
goto resp
}
uid = r.FormValue("uid")
if uid == "" {
response.Error = "missing 'uid'"
goto resp
}
if !checkAuthz(uid, rid) {
log.Warnf("auth failed. uid: %s, rid: %s", uid, rid)
response.Error = "authorization failed"
goto resp
}
/*
uid := r.FormValue("uid")
if uid == "" { fmt.Fprintf(w, "missing 'uid'\n"); return; }
tid := r.FormValue("tid")
if tid == "" { fmt.Fprintf(w, "missing 'tid'\n"); return; }
sign := r.FormValue("sign")
if sign == "" { fmt.Fprintf(w, "missing 'sign'\n"); return; }
tm := r.FormValue("tm")
if tm == "" { fmt.Fprintf(w, "missing 'tm'\n"); return; }
pmtt := r.FormValue("pmtt")
if pmtt == "" { fmt.Fprintf(w, "missing 'pmtt'\n"); return; }
query := map[string]string {
"uid" : uid,
"rid" : rid,
"tid" : tid,
"src" : "letv",
"tm" : tm,
"pmtt" : pmtt,
}
path := "/router/command"
mysign := sign(path, query)
if mysign != sign {
response.Error = "sign valication failed"
b, _ := json.Marshal(response)
fmt.Fprintf(w, string(b))
return
}
*/
if r.Body == nil {
response.Error = "missing POST data"
goto resp
}
if !comet.DevMap.Check(rid) {
response.Error = fmt.Sprintf("device (%s) offline", rid)
goto resp
}
client = comet.DevMap.Get(rid).(*comet.Client)
body, err = ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
response.Error = "invalid POST body"
goto resp
}
cmdRequest = CommandRequest{
Uid: uid,
Cmd: string(body),
}
bCmd, _ = json.Marshal(cmdRequest)
reply = make(chan *comet.Message)
client.SendMessage(comet.MSG_REQUEST, bCmd, reply)
select {
case msg := <-reply:
w.Write(msg.Data)
case <-time.After(10 * time.Second):
response.Error = "recv response timeout"
goto resp
}
return
resp:
b, _ := json.Marshal(response)
log.Debugf("postRouterCommand write: %s", string(b))
w.Write(b)
}
func getRouterList(w http.ResponseWriter, r *http.Request) {
log.Tracef("getRouterList")
var (
uid string
tid string
response ResponseRouterList
router RouterInfo
devices map[string]devcenter.Device
err error
)
response.Status = -1
if r.Method != "GET" {
response.Descr = "must using 'GET' method\n"
goto resp
}
r.ParseForm()
uid = r.FormValue("uid")
if uid == "" {
response.Descr = "missing 'uid'"
goto resp
}
tid = r.FormValue("tid")
if tid == "" {
response.Descr = "missing 'tid'"
goto resp
}
devices, err = devcenter.GetDevices(uid, devcenter.DEV_ROUTER)
if err != nil {
log.Errorf("GetDevices failed: %s", err.Error())
response.Descr = err.Error()
goto resp
}
for _, dev := range devices {
router = RouterInfo{
Rid: dev.Id,
Rname: dev.Title,
}
response.List = append(response.List, router)
}
//router = RouterInfo{
// Rid: "c80e774a1e73",
// Rname: "router1",
//}
//response.List = append(response.List, router)
response.Status = 0
response.Descr = "OK"
resp:
b, _ := json.Marshal(response)
log.Debugf("getRoutelist write: %s", string(b))
w.Write(b)
}
func getCommand(w http.ResponseWriter, r *http.Request) {
log.Tracef("getCommand")
r.ParseForm()
devid := r.FormValue("devid")
if devid == "" {
fmt.Fprintf(w, "missing devid\n")
return
}
if !comet.DevMap.Check(devid) {
fmt.Fprintf(w, "(%s) not register\n", devid)
return
}
cmd := r.FormValue("cmd")
client := comet.DevMap.Get(devid).(*comet.Client)
reply := make(chan *comet.Message)
client.SendMessage(comet.MSG_REQUEST, []byte(cmd), reply)
select {
case msg := <-reply:
fmt.Fprintf(w, "recv reply (%s)\n", string(msg.Data))
case <-time.After(10 * time.Second):
fmt.Fprintf(w, "recv timeout\n")
}
}
func StartHttp(addr string) {
log.Infof("Starting HTTP server on %s", addr)
http.HandleFunc("/router/command", postRouterCommand)
http.HandleFunc("/router/list", getRouterList)
http.HandleFunc("/command", getCommand)
http.HandleFunc("/status", getStatus)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Criticalf("http listen: ", err)
os.Exit(1)
}
}