-
Notifications
You must be signed in to change notification settings - Fork 50
/
api_caller.go
175 lines (150 loc) · 3.48 KB
/
api_caller.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
package proxy
import (
"log"
"regexp"
"sort"
"strings"
)
//var API_PREF string = "api_pref"
const (
apiPrefParamName string = "api_pref"
apiPrefTypeReq = "req"
apiPrefTypeCookie = "cookie"
apiPrefTypeHeader = "header"
)
var prefTypes = []string{apiPrefTypeReq, apiPrefTypeCookie, apiPrefTypeHeader}
// Caller caller hosts
type Caller []*CallerItem
// CallerItem caller host
type CallerItem struct {
Note string `json:"note"`
IP string `json:"ip"`
IPReg *regexp.Regexp `json:"-"`
Enable bool `json:"enable"`
Pref []string `json:"pref"`
Ignore []string `json:"ignore"`
}
func newCaller() Caller {
return make([]*CallerItem, 0)
}
func newCallerItem(ip string) (*CallerItem, error) {
item := &CallerItem{
IP: ip,
Pref: make([]string, 0),
Ignore: make([]string, 0),
}
var err error
err = item.init()
return item, err
}
func newCallerItemMust(ip string) *CallerItem {
item, _ := newCallerItem(ip)
item.Enable = true
return item
}
func (citem *CallerItem) init() (err error) {
citem.IPReg, err = regexp.Compile(strings.Replace(strings.Replace(citem.IP, ".", `\.`, -1), "*", `\d+`, -1))
if err != nil {
log.Println("ip wrong:", citem.IP)
}
if citem.Ignore == nil {
citem.Ignore = make([]string, 0)
}
return err
}
func (citem *CallerItem) isHostIgnore(hostHame string, cpf *CallerPrefConf) bool {
isIgnore := InStringSlice(hostHame, citem.Ignore)
if isIgnore && cpf != nil {
hs := cpf.allPrefHosts()
//pref host must not ignore
if InStringSlice(hostHame, hs) {
return false
}
}
return isIgnore
}
const ipAll string = "*.*.*.*"
func (caller *Caller) init() (err error) {
hasAll := false
for _, citem := range *caller {
err := citem.init()
if err != nil {
return err
}
if citem.IP == ipAll {
hasAll = true
}
}
if !hasAll {
citem := newCallerItemMust(ipAll)
citem.Note = "default all"
citem.Enable = true
citem.init()
caller.addNewCallerItem(citem)
}
return nil
}
func (caller *Caller) getPrefHostName(allowNames []string, cpf *CallerPrefConf) string {
if len(allowNames) == 0 || len(*caller) == 0 {
return StrSliceRandItem(allowNames)
}
for _, prefType := range prefTypes {
if len(cpf.prefHostName[prefType]) > 0 {
pref := StrSliceIntersectGetOne(cpf.prefHostName[prefType], allowNames)
if pref != "" {
return pref
}
}
}
item := caller.getCallerItemByIP(cpf.ip)
if item != nil && len(item.Pref) > 0 {
pref := StrSliceIntersectGetOne(item.Pref, allowNames)
if pref != "" {
return pref
}
}
return StrSliceRandItem(allowNames)
}
// Sort sort by host ip
func (caller Caller) Sort() {
sort.Sort(caller)
}
func (caller Caller) Len() int {
return len(caller)
}
/**
*让 127.0.0.1 排在127.0.0.* 前面
*/
func (caller Caller) Less(i, j int) bool {
aPos := strings.Index(caller[i].IP, "*")
if aPos == -1 {
return true
}
bPos := strings.Index(caller[j].IP, "*")
if bPos == -1 {
return false
}
return aPos > bPos
}
func (caller Caller) Swap(i, j int) {
caller[i], caller[j] = caller[j], caller[i]
}
var defaultCaller = &CallerItem{IP: ipAll, Enable: true, Note: "default"}
func init() {
defaultCaller.init()
}
func (caller Caller) getCallerItemByIP(ip string) *CallerItem {
for _, item := range caller {
if !item.Enable {
continue
}
if item.IP == ip || item.IPReg.MatchString(ip) {
return item
}
}
return defaultCaller
}
func (caller *Caller) addNewCallerItem(item *CallerItem) {
*caller = append(*caller, item)
caller.Sort()
}