forked from fagongzi/manba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
243 lines (198 loc) · 5.48 KB
/
api.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
package model
import (
"encoding/json"
"io"
"regexp"
"strings"
"github.com/valyala/fasthttp"
)
const (
// APIStatusDown down status
APIStatusDown = iota
//APIStatusUp up status
APIStatusUp
)
// Node api dispatch node
type Node struct {
ClusterName string `json:"clusterName, omitempty"`
Rewrite string `json:"rewrite, omitempty"`
AttrName string `json:"attrName, omitempty"`
Validations []*Validation `json:"validations, omitempty"`
}
type ipSegment struct {
value []string
}
func parseFrom(value string) *ipSegment {
ip := &ipSegment{}
ip.value = strings.Split(value, ".")
return ip
}
func (ip *ipSegment) matches(value string) bool {
tmp := strings.Split(value, ".")
for index, v := range ip.value {
if v != "*" && v != tmp[index] {
return false
}
}
return true
}
// AccessControl access control
type AccessControl struct {
Whitelist []string `json:"whitelist, omitempty"`
Blacklist []string `json:"blacklist, omitempty"`
parsedWhitelist []*ipSegment
parsedBlacklist []*ipSegment
}
// Mock mock
type Mock struct {
Value string `json:"value"`
ContentType string `json:"contentType, omitempty"`
Headers []*MockHeader `json:"headers, omitempty"`
Cookies []string `json:"cookies, omitempty"`
ParsedCookies []*fasthttp.Cookie `json:"-"`
}
// MockHeader header
type MockHeader struct {
Name string `json:"name"`
Value string `json:"value"`
}
// API a api define
type API struct {
Name string `json:"name, omitempty"`
URL string `json:"url"`
Method string `json:"method"`
Domain string `json:"domain, omitempty"`
Status int `json:"status, omitempty"`
AccessControl *AccessControl `json:"accessControl, omitempty"`
Mock *Mock `json:"mock, omitempty"`
Nodes []*Node `json:"nodes"`
Desc string `json:"desc, omitempty"`
Pattern *regexp.Regexp `json:"-"`
}
// UnMarshalAPI unmarshal
func UnMarshalAPI(data []byte) *API {
v := &API{}
json.Unmarshal(data, v)
if v.Mock != nil && v.Mock.Value == "" {
v.Mock = nil
}
return v
}
// UnMarshalAPIFromReader unmarshal from reader
func UnMarshalAPIFromReader(r io.Reader) (*API, error) {
v := &API{}
decoder := json.NewDecoder(r)
err := decoder.Decode(v)
if v.Mock != nil && v.Mock.Value == "" {
v.Mock = nil
}
return v, err
}
// NewAPI create a API
func NewAPI(url string, nodes []*Node) *API {
return &API{
URL: url,
Nodes: nodes,
}
}
// Parse parse
func (a *API) Parse() {
a.Pattern = regexp.MustCompile(a.URL)
for _, n := range a.Nodes {
if nil != n.Validations {
for _, v := range n.Validations {
v.ParseValidation()
}
}
}
if nil != a.Mock && nil != a.Mock.Cookies && len(a.Mock.Cookies) > 0 {
a.Mock.ParsedCookies = make([]*fasthttp.Cookie, len(a.Mock.Cookies))
for index, c := range a.Mock.Cookies {
ck := &fasthttp.Cookie{}
ck.Parse(c)
a.Mock.ParsedCookies[index] = ck
}
}
if nil != a.AccessControl {
if a.AccessControl.Blacklist != nil {
a.AccessControl.parsedBlacklist = make([]*ipSegment, len(a.AccessControl.Blacklist))
for index, ip := range a.AccessControl.Blacklist {
a.AccessControl.parsedBlacklist[index] = parseFrom(ip)
}
}
if a.AccessControl.Whitelist != nil {
a.AccessControl.parsedWhitelist = make([]*ipSegment, len(a.AccessControl.Whitelist))
for index, ip := range a.AccessControl.Whitelist {
a.AccessControl.parsedWhitelist[index] = parseFrom(ip)
}
}
}
}
// AccessCheckBlacklist check blacklist
func (a *API) AccessCheckBlacklist(ip string) bool {
if a.AccessControl == nil || a.AccessControl.parsedBlacklist == nil {
return false
}
for _, i := range a.AccessControl.parsedBlacklist {
if i.matches(ip) {
return true
}
}
return false
}
// AccessCheckWhitelist check whitelist
func (a *API) AccessCheckWhitelist(ip string) bool {
if a.AccessControl == nil || a.AccessControl.parsedWhitelist == nil {
return true
}
for _, i := range a.AccessControl.parsedWhitelist {
if i.matches(ip) {
return true
}
}
return false
}
// RenderMock dender mock response
func (a *API) RenderMock(ctx *fasthttp.RequestCtx) {
if a.Mock == nil {
return
}
ctx.Response.Header.SetContentType(a.Mock.ContentType)
if a.Mock.Headers != nil && len(a.Mock.Headers) > 0 {
for _, header := range a.Mock.Headers {
ctx.Response.Header.Add(header.Name, header.Value)
}
}
if a.Mock.ParsedCookies != nil && len(a.Mock.ParsedCookies) > 0 {
for _, ck := range a.Mock.ParsedCookies {
ctx.Response.Header.SetCookie(ck)
}
}
ctx.WriteString(a.Mock.Value)
}
// Marshal marshal
func (a *API) Marshal() []byte {
v, _ := json.Marshal(a)
return v
}
func (a *API) getNodeURL(req *fasthttp.Request, node *Node) string {
if node.Rewrite == "" {
return ""
}
return a.Pattern.ReplaceAllString(string(req.URI().RequestURI()), node.Rewrite)
}
func (a *API) matches(req *fasthttp.Request) bool {
return a.isUp() && (a.isDomainMatches(req) || (a.isMethodMatches(req) && a.isURIMatches(req)))
}
func (a *API) isUp() bool {
return a.Status == APIStatusUp
}
func (a *API) isMethodMatches(req *fasthttp.Request) bool {
return a.Method == "*" || strings.ToUpper(string(req.Header.Method())) == a.Method
}
func (a *API) isURIMatches(req *fasthttp.Request) bool {
return a.Pattern.Match(req.URI().RequestURI())
}
func (a *API) isDomainMatches(req *fasthttp.Request) bool {
return a.Domain != "" && string(req.Header.Host()) == a.Domain
}