-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategies.go
190 lines (148 loc) · 4.44 KB
/
strategies.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
package main
import (
"net"
"time"
"strings"
"strconv"
"net/http"
"math/rand"
)
type Strategy interface {
Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool
}
type ShareStrategy struct {
Share int `json:"share"`
}
func (self *ShareStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
if self.Share == 100 {
return true
}
shareCookie, err := req.Cookie("toogles-" + feature.Id + "|share")
var share int
if err != nil {
share := rand.Intn(100)
shareString := strconv.Itoa(int(share))
cookie := http.Cookie{
Name: "toogles-" + feature.Id + "|share",
Value: shareString,
}
http.SetCookie(res, &cookie)
} else {
s, _ := strconv.ParseInt(shareCookie.Value, 10, 64)
share = int(s)
}
return self.Share <= share
}
type FirstStrategy struct {
First int64 `json:"first"`
}
func (self *FirstStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
if feature.GetStat("impressions") < self.First {
return true
}
return false
}
type QueryStrategy struct {
Key string `json:"key"`
Values []string `json:"values"`
}
func (self *QueryStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
URL := req.URL
Query := URL.Query()
Value := Query.Get(self.Key)
if Value != "" {
return sliceContains(self.Values, Value)
}
return false
}
type HeaderStrategy struct {
Header string `json:"header"`
Values []string `json:"values"`
}
func (self *HeaderStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
if _, ok := req.Header[self.Header]; ok == false {
return false
}
if len(req.Header[self.Header]) > 0 {
Value := req.Header[self.Header][0]
return sliceContains(self.Values, Value)
}
return false
}
type IPStrategy struct {
IPs []string `json:"ips"`
}
func (self *IPStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
return false
}
if (sliceContains(self.IPs, ip)) {
return true
}
if forwardedForHeaders, ok := req.Header["X-Forwarded-For"]; ok {
for _, ip := range forwardedForHeaders {
if sliceContains(self.IPs, ip) {
return true
}
}
}
return false
}
type RefererStrategy struct {
Referers []string `json:"referers"`
}
func (self *RefererStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
if referers, ok := req.Header["Referer"]; ok {
for _, referer := range referers {
if sliceContains(self.Referers, referer) {
return true
}
}
}
return false
}
type RetargetStrategy struct {
FeatureIds []string `json:"featureIds"`
}
func (self *RetargetStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
for _, featureId := range self.FeatureIds {
if _, err := req.Cookie("toogles-" + featureId); err != nil {
return true
}
}
return false
}
type UserRecurrencyStrategy struct {
Visits int `json:"visits"`
VisitInterval int `json:"visitInterval"`
}
func (self *UserRecurrencyStrategy) Toggle(feature *Feature, res http.ResponseWriter, req *http.Request) bool {
cookie, err := req.Cookie("toogles-" + feature.Id + "|visits")
visits := 1
now := time.Now().Unix()
if err == nil {
value := strings.Split(cookie.Value, "|")
if len(value) != 2 {
return false
}
visits64, _ := strconv.ParseInt(value[0], 10, 64)
visits = int(visits64)
lastVisit, _ := strconv.ParseInt(value[1], 10, 64)
diff := int(now - lastVisit)
if diff > self.VisitInterval {
visits += 1
}
if visits >= self.Visits {
return true
}
}
visitsString := strconv.Itoa(visits)
nowString := strconv.FormatInt(now, 10)
visitCookie := http.Cookie{
Name: "toogles-" + feature.Id + "|visits",
Value: visitsString + "|" + nowString,
}
http.SetCookie(res, &visitCookie)
return false
}