about summary refs log tree commit diff
path: root/src/limit.go
blob: c4befac0ba9bd68841228bf7c194db4f7a9867a7 (plain)
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
package main

import (
	"flag"
	"time"
)

var (
	endTimestamp *int64
	endAfter *int64
	initialLoginTime int64
)

func registerLimitFlags() {
	endTimestamp = flag.Int64("endTimestamp", 0, "Date/Time after which flags are not accepted anymore")
	endAfter = flag.Int64("endAfter", 0, "Seconds after the first login, after which flags are not accepted anymore")
}

func startLimitTimer() {
	now := time.Now().Unix()

	// check if endTimestamp is set
	if *endTimestamp > 0 {
		// Start the endTimestampTimer
		endTimestampTimer := time.NewTimer(time.Duration(*endTimestamp - now))
		go func() {
			<-endTimestampTimer.C
			// Stop all challenge containers if timer hit
			stopAllChallengeContainers()
		}()
	}

	// check if endAfter is set
	if *endAfter > 0 {
		// Start the endAfterTimer
		endAfterTimer := time.NewTimer(time.Duration(*endAfter - now))
		go func() {
			<-endAfterTimer.C
			// Stop all challenge containers if timer hit
			stopAllChallengeContainers()
		}()
	}
}

// Called on every login to set the "initialLoginTime" if it's not already set
func registerLoginForLimiter() {
	if initialLoginTime == 0 {
		initialLoginTime = time.Now().Unix()
	}
}

// Called before starting containers or entering flags
func shouldLimit() (bool) {
	now := time.Now().Unix()
	return (*endTimestamp > 0 && *endTimestamp < now) || (*endAfter > 0 && *endAfter < now - initialLoginTime)
}