-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
326 lines (293 loc) · 7.65 KB
/
validate.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
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"pro-iris/common"
"pro-iris/datamodels"
"pro-iris/encrypt"
"pro-iris/rabbitmq"
"strconv"
"sync"
"time"
)
// cluster addresses : here internal IPs
//var hostArray = []string{"172.26.194.41", "172.26.194.42"} // for real server
var hostArray = []string{"192.168.68.105", "192.168.68.105"} // for local test
var localHost = ""
// GetOneIp : getOne SLB intranet IP
//var GetOneIp = "172.26.194.43" // for real server
var GetOneIp = "127.0.0.1" // for local test
var port = "8083"
var GetOnePort = "8084"
var hashConsistent *common.Consistent
var rabbitMqValidate *rabbitmq.RabbitMQ
// Set server response interval to be 10 seconds -> Avoid malicious for-loop requests
var interval = 10
type BlackList struct {
listArray map[int]bool
sync.RWMutex
}
var blackList = &BlackList{
listArray: make(map[int]bool),
}
func (m *BlackList) GetBlackListByID(uid int) bool {
m.RLock()
defer m.RUnlock()
return m.listArray[uid]
}
func (m *BlackList) SetBlackListByID(uid int) bool {
m.Lock()
defer m.Unlock()
m.listArray[uid] = true
return true
}
// AccessControl : store control info
type AccessControl struct {
// Store information based on user ID
sourcesArray map[int]time.Time
// Using RW mutex to ensure R/W security of map
sync.RWMutex
}
var accessControl = &AccessControl{
sourcesArray: make(map[int]time.Time),
}
func (m *AccessControl) GetNewRecord(uid int) time.Time {
m.RWMutex.RLock()
defer m.RWMutex.RUnlock()
data := m.sourcesArray[uid]
return data
}
func (m *AccessControl) SetNewRecord(uid int) {
m.RWMutex.Lock()
m.sourcesArray[uid] = time.Now()
m.RWMutex.Unlock()
}
func (m *AccessControl) GetDistributedRight(req *http.Request) bool {
uid, err := req.Cookie("uid")
if err != nil {
return false
}
// consistent hashing
hostRequest, err := hashConsistent.Get(uid.Value)
if err != nil {
return false
}
if hostRequest == localHost {
// Local: data reading and verification
return m.GetDataFromMap(uid.Value)
} else {
// using agent
return GetDataFromOtherMap(hostRequest, req)
}
}
func (m *AccessControl) GetDataFromMap(uid string) bool {
uidInt, err := strconv.Atoi(uid)
if err != nil {
return false
}
if blackList.GetBlackListByID(uidInt) {
return false
}
data := m.GetNewRecord(uidInt)
if !data.IsZero() {
if data.Add(time.Duration(interval) * time.Second).After(time.Now()) {
return false
}
}
m.SetNewRecord(uidInt)
return true
}
func GetDataFromOtherMap(host string, req *http.Request) bool {
hostUrl := "http://" + host + ":" + port + "/checkRight"
response, body, err := GetCurl(hostUrl, req)
if err != nil {
return false
}
if response.StatusCode == 200 {
return string(body) == "true"
} else {
return false
}
}
// Auth : Unified verification filter
// Each interface needs to be verified in advance
func Auth(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8082")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
fmt.Println("run Auth function successfully")
// Add permission verification based on cookie
err := checkUserInfo(r)
if err != nil {
return err
}
return nil
}
// Identity verification
func checkUserInfo(r *http.Request) error {
// get uid from cookie
uidCookie, err := r.Cookie("uid")
if err != nil {
return errors.New("unable to access uid from cookie")
}
// get encrypted userInfo from cookie
signCookie, err := r.Cookie("sign")
if err != nil {
return errors.New("unable to access sign from cookie")
}
// Decrypt sign
signByte, err := encrypt.DePwdCode(signCookie.Value)
if err != nil {
return errors.New("encrypted User information has been tampered with")
}
fmt.Println("Identity verification")
fmt.Println("uid:" + uidCookie.Value)
fmt.Println("decrypted sign:" + string(signByte))
if checkInfo(uidCookie.Value, string(signByte)) {
return nil
} else {
return errors.New("identity verification failed")
}
}
func checkInfo(checkStr string, signStr string) bool {
return checkStr == signStr
}
func CheckRight(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8082")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
right := accessControl.GetDistributedRight(r)
if !right {
w.Write([]byte("false"))
return
}
w.Write([]byte("true"))
return
}
// Check : Execute normal logic
func Check(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "http://localhost:8082")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
fmt.Println("run Check function successfully")
queryForm, err := url.ParseQuery(r.URL.RawQuery)
if err != nil || len(queryForm["productID"]) <= 0 {
w.Write([]byte("false"))
return
}
productString := queryForm["productID"][0]
fmt.Println(productString)
userCookie, err := r.Cookie("uid")
if err != nil {
w.Write([]byte("false"))
return
}
// 1.Distributed permission verification
right := accessControl.GetDistributedRight(r)
if right == false {
w.Write([]byte("false"))
return
}
// 2.getOne quantity control : prevent oversold
hostUrl := "http://" + GetOneIp + ":" + GetOnePort + "/getOne"
responseValidate, bodyValidate, err := GetCurl(hostUrl, r)
if err != nil {
w.Write([]byte("false"))
return
}
// 3.check request status of quantity control
if responseValidate.StatusCode == 200 {
if string(bodyValidate) == "true" {
productID, err := strconv.ParseInt(productString, 10, 64)
if err != nil {
w.Write([]byte("false"))
return
}
userID, err := strconv.ParseInt(userCookie.Value, 10, 64)
if err != nil {
w.Write([]byte("false"))
return
}
message := datamodels.NewMessage(userID, productID)
byteMsg, err := json.Marshal(message)
if err != nil {
w.Write([]byte("false"))
return
}
err = rabbitMqValidate.PublishSimple(string(byteMsg))
if err != nil {
w.Write([]byte("false"))
return
}
w.Write([]byte("true"))
// Every user can buy only once
//blackList.SetBlackListByID(userID)
return
}
}
w.Write([]byte("false"))
return
}
func GetCurl(hostUrl string, request *http.Request) (response *http.Response, body []byte, err error) {
uidPre, err := request.Cookie("uid")
if err != nil {
return
}
uidSign, err := request.Cookie("sign")
if err != nil {
return
}
client := &http.Client{}
req, err := http.NewRequest("GET", hostUrl, nil)
if err != nil {
return
}
cookieUid := &http.Cookie{
Name: "uid",
Value: uidPre.Value,
Path: "/",
}
cookieSign := &http.Cookie{
Name: "sign",
Value: uidSign.Value,
Path: "/",
}
req.AddCookie(cookieUid)
req.AddCookie(cookieSign)
response, err = client.Do(req)
defer response.Body.Close()
if err != nil {
return
}
body, err = ioutil.ReadAll(response.Body)
return
}
func main() {
// LB settings
// Consistent Hashing
hashConsistent = common.NewConsistent()
for _, v := range hostArray {
hashConsistent.Add(v)
}
localIp, err := common.GetIntranetIp()
if err != nil {
fmt.Println(err)
}
localHost = localIp
fmt.Println(localHost)
rabbitMqValidate = rabbitmq.NewRabbitMQSimple("rabbitmqProduct")
defer rabbitMqValidate.Destroy()
// 1. create filter
filter := common.NewFilter()
// 2. register filter
filter.RegisterFilterUri("/check", Auth)
filter.RegisterFilterUri("/checkRight", Auth)
// 3. start service
http.HandleFunc("/check", filter.Handle(Check))
http.HandleFunc("/checkRight", filter.Handle(CheckRight))
http.ListenAndServe(":8083", nil)
}