-
Notifications
You must be signed in to change notification settings - Fork 72
/
whitelist.go
188 lines (156 loc) · 5.65 KB
/
whitelist.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
package main
import (
"fmt"
"strings"
"time"
"github.com/google/go-intervals/timespanset"
)
const (
// whitelistStartPrefix in `YYYY-MM-DDT` format, can be anthing
whitelistStartPrefix = "2000-01-01T"
// whitelistEndPrefix in `YYYY-MM-DDT` format, has to be whitelistStartPrefix plus one day
whitelistEndPrefix = "2000-01-02T"
// whitelistTimePostfix in `:ssZ` format, can be anything
whitelistTimePostfix = ":00Z"
)
var (
whitelistStart time.Time
whitelistEnd time.Time
)
func init() {
var err error
// whitelistStart is the start of the day
whitelistStart, err = time.Parse(time.RFC3339, whitelistStartPrefix+"00:00"+whitelistTimePostfix)
if err != nil {
panic("whitelistStart parse error")
}
// whitelistEnd is the start of the next day
whitelistEnd, err = time.Parse(time.RFC3339, whitelistEndPrefix+"00:00"+whitelistTimePostfix)
if err != nil {
panic("whitelistEnd parse error")
}
}
// WhitelistInstance is resposible for one processing of whitelist and blacklist hours
type WhitelistInstance struct {
// blacklist contains blacklists as passed arguments
blacklist string
// whitelist contains whitelists as passed arguments
whitelist string
// whitelistHours are whitelist periods
whitelistHours *timespanset.Set
// whitelistSecondCount is the total number of seconds cumulated from all the whitelist periods combined
whitelistSecondCount int
}
// initializeWhitelistHours initializes data structures by taking command line arguments into account.
func (w *WhitelistInstance) initialize() {
w.whitelistHours = timespanset.Empty()
w.whitelistSecondCount = 0
}
func (w *WhitelistInstance) parseArguments() {
w.initialize()
if len(w.whitelist) == 0 {
// If there's no whitelist, than the maximum range has to be allowed so that any blacklist
// might be subtracted from it.
w.processHours("00:00 - 12:00, 12:00 - 00:00", "+")
} else {
w.processHours(w.whitelist, "+")
}
w.processHours(w.blacklist, "-")
w.whitelistHours.IntervalsBetween(whitelistStart, whitelistEnd, w.updateWhitelistSecondCount)
}
// getExpiryDate calculates the expiry date of a node.
func (w *WhitelistInstance) getExpiryDate(t time.Time, timeToBeAdded time.Duration) (expiryDatetime time.Time) {
truncatedCreationTime := t.Truncate(24 * time.Hour)
projectedCreation := whitelistStart.Add(t.Sub(truncatedCreationTime))
first := true
for timeToBeAdded > 0 {
// For all whitelisted intervals...
w.whitelistHours.IntervalsBetween(whitelistStart, whitelistEnd, func(start, end time.Time) bool {
// For the first iteration only...
if first {
// If the current interval ends before the creation...
if end.Before(projectedCreation) {
// Skip for now.
return true
}
// If creation is in the middle of the current interval...
if start.Before(projectedCreation) {
// Start with creation.
start = projectedCreation
}
}
// If expiry time has been reached...
intervalDuration := end.Sub(start)
if timeToBeAdded <= intervalDuration {
// This is it, project it back to real time.
expiryDatetime = truncatedCreationTime.Add(start.Add(timeToBeAdded).Sub(whitelistStart))
// But if expiryDatetime is before creation...
fmt.Println("before creation? " + expiryDatetime.String() + " " + t.String())
if expiryDatetime.Before(t) {
// Simply add 24h.
expiryDatetime = expiryDatetime.Add(24 * time.Hour)
}
}
// Consume this interval.
timeToBeAdded -= intervalDuration
// Do we want another interval?
return timeToBeAdded > 0
})
first = false
}
return expiryDatetime
}
// mergeTimespans merges time intervals.
func (w *WhitelistInstance) mergeTimespans(start time.Time, end time.Time, direction string) {
if direction == "+" {
w.whitelistHours.Insert(start, end)
} else if direction == "-" {
subtrahend := timespanset.Empty()
subtrahend.Insert(start, end)
w.whitelistHours.Sub(subtrahend)
} else {
panic(fmt.Sprintf("mergeTimespans(): direction expected to be + or - but got '%v'", direction))
}
}
// processHours parses time stamps and passes them to mergeTimespans(), direction can be "+" or "-".
func (w *WhitelistInstance) processHours(input string, direction string) {
// Time not specified, continue with no restrictions.
if len(input) == 0 {
return
}
// Split in intervals.
input = strings.Replace(input, " ", "", -1)
intervals := strings.Split(input, ",")
for _, timeInterval := range intervals {
times := strings.Split(timeInterval, "-")
// Check format.
if len(times) != 2 {
panic(fmt.Sprintf("processHours(): interval '%v' should be of the form `09:00 - 11:00[, 21:00 - 23:00[, ...]]`", timeInterval))
}
// Start time
start, err := time.Parse(time.RFC3339, whitelistStartPrefix+times[0]+whitelistTimePostfix)
if err != nil {
panic(fmt.Sprintf("processHours(): %v cannot be parsed: %v", times[0], err))
}
// End time
end, err := time.Parse(time.RFC3339, whitelistStartPrefix+times[1]+whitelistTimePostfix)
if err != nil {
panic(fmt.Sprintf("processHours(): %v cannot be parsed: %v", times[1], err))
}
// If end is before start it means it contains midnight, so split in two.
if end.Before(start) {
w.mergeTimespans(whitelistStart, end, direction)
end = whitelistEnd
}
// Merge timespans.
w.mergeTimespans(start, end, direction)
}
}
// updateWhitelistSecondCount adds the difference between two times to an accumulator.
func (w *WhitelistInstance) updateWhitelistSecondCount(start, end time.Time) bool {
if end.Before(start) {
panic(fmt.Sprintf("updateWhitelistSecondCount(): go-intervals timespanset is acting up providing reverse intervals: %v - %v", start, end))
}
w.whitelistSecondCount += int(end.Sub(start).Seconds())
return true
}