Support time access restrictions in substrings & glob patterns

Improve example
This commit is contained in:
Frank Denis 2018-02-01 09:47:37 +01:00
parent 1a34224c91
commit 107fc35d2a
2 changed files with 44 additions and 15 deletions

View File

@ -315,15 +315,34 @@ cache_neg_ttl = 60
## Time access restrictions (WIP)
## Time access restrictions
##
## One or more weekly schedules can be defined here.
## Patterns in the name-based blocklist can optionally be followed with @schedule_name
## to apply the pattern 'schedule_name' only when it matches a time range of that schedule.
##
## For example, the following rule in a blacklist file:
## *.youtube.* @time-to-sleep
## would block access to Youtube only during the days, and period of the days
## define by the 'time-to-sleep' schedule.
##
## {after='21:00', before= '7:00'} matches 0:00-7:00 and 21:00-0:00
## {after= '9:00', before='18:00'} matches 9:00-18:00
[schedules]
[schedules.'time-to-sleep']
mon = [{after='21:00', before='07:00'}]
tue = [{after='21:00', before='07:00'}]
wed = [{after='21:00', before='07:00'}]
thu = [{after='21:00', before='07:00'}]
fri = [{after='23:00', before='07:00'}]
sat = [{after='23:00', before='07:00'}]
sun = [{after='21:00', before='07:00'}]
mon = [{after='21:00', before='7:00'}]
tue = [{after='21:00', before='7:00'}]
wed = [{after='21:00', before='7:00'}]
thu = [{after='21:00', before='7:00'}]
fri = [{after='23:00', before='7:00'}]
sat = [{after='23:00', before='7:00'}]
sun = [{after='21:00', before='7:00'}]
[schedule.'work']
mon = [{after='9:00', before='18:00'}]
tue = [{after='9:00', before='18:00'}]
wed = [{after='9:00', before='18:00'}]
thu = [{after='9:00', before='18:00'}]
fri = [{after='9:00', before='17:00'}]

View File

@ -30,13 +30,14 @@ const (
type PluginBlockName struct {
sync.Mutex
blockedPrefixes *iradix.Tree
blockedSuffixes *iradix.Tree
blockedSubstrings []string
blockedPatterns []string
outFd *os.File
format string
allWeeklyRanges *map[string]WeeklyRanges
blockedPrefixes *iradix.Tree
blockedSuffixes *iradix.Tree
allWeeklyRanges *map[string]WeeklyRanges
weeklyRangesIndirect map[string]*WeeklyRanges
blockedSubstrings []string
blockedPatterns []string
outFd *os.File
format string
}
type TimeRange struct {
@ -72,6 +73,7 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
return err
}
plugin.allWeeklyRanges = proxy.allWeeklyRanges
plugin.weeklyRangesIndirect = make(map[string]*WeeklyRanges)
plugin.blockedPrefixes = iradix.New()
plugin.blockedSuffixes = iradix.New()
for lineNo, line := range strings.Split(string(bin), "\n") {
@ -136,8 +138,14 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
switch blockType {
case PluginBlockTypeSubstring:
plugin.blockedSubstrings = append(plugin.blockedSubstrings, line)
if weeklyRanges != nil {
plugin.weeklyRangesIndirect[line] = weeklyRanges
}
case PluginBlockTypePattern:
plugin.blockedPatterns = append(plugin.blockedPatterns, line)
if weeklyRanges != nil {
plugin.weeklyRangesIndirect[line] = weeklyRanges
}
case PluginBlockTypePrefix:
plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), weeklyRanges)
case PluginBlockTypeSuffix:
@ -207,6 +215,7 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
for _, substring := range plugin.blockedSubstrings {
if strings.Contains(qName, substring) {
reject, reason = true, "*"+substring+"*"
weeklyRanges = plugin.weeklyRangesIndirect[substring]
break
}
}
@ -215,6 +224,7 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
for _, pattern := range plugin.blockedPatterns {
if found, _ := filepath.Match(pattern, qName); found {
reject, reason = true, pattern
weeklyRanges = plugin.weeklyRangesIndirect[pattern]
break
}
}