-
Notifications
You must be signed in to change notification settings - Fork 5
/
block_domains_decider.go
60 lines (48 loc) · 1.47 KB
/
block_domains_decider.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
package blocker
import (
"fmt"
"os"
"time"
)
// BlockDomainsDecider is the interface which must be implemented by any type which intends to
// become a blocker. The purpose of each of the functions is described below.
type BlockDomainsDecider interface {
IsDomainBlocked(domain string) bool
StartBlocklistUpdater(ticker *time.Ticker)
UpdateBlocklist() error
}
type BlocklistType string
const BlocklistType_Hosts BlocklistType = "hosts"
const BlocklistType_ABP BlocklistType = "abp"
// PrepareBlocklist ...
func PrepareBlocklist(filePath string, blocklistUpdateFrequency string, blocklistType string, logger Logger) (BlockDomainsDecider, []func() error, error) {
_, err := os.Stat(filePath)
if err != nil {
return nil, nil, err
}
frequency, err := time.ParseDuration(blocklistUpdateFrequency)
if err != nil {
return nil, nil, err
}
var decider BlockDomainsDecider
switch BlocklistType(blocklistType) {
case BlocklistType_Hosts:
decider = NewBlockDomainsDeciderHosts(filePath, logger)
case BlocklistType_ABP:
decider = NewBlockDomainsDeciderABP(filePath, logger)
}
// Always update the blocklist when the server starts up
decider.UpdateBlocklist()
// Setup periodic updation of the blocklist
ticker := time.NewTicker(frequency)
decider.StartBlocklistUpdater(ticker)
stopTicker := func() error {
fmt.Println("[INFO] Ticker was stopped.")
ticker.Stop()
return nil
}
shutdownHooks := []func() error{
stopTicker,
}
return decider, shutdownHooks, nil
}