From 61592776e2d99570a643c6a0858683148d02d45a Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 1 Feb 2018 01:00:48 +0100 Subject: [PATCH 1/2] time-based access control: done, for prefixes & suffixes rules --- dnscrypt-proxy/plugin_block_name.go | 35 ++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/dnscrypt-proxy/plugin_block_name.go b/dnscrypt-proxy/plugin_block_name.go index 3b430b1e..7f38dd6d 100644 --- a/dnscrypt-proxy/plugin_block_name.go +++ b/dnscrypt-proxy/plugin_block_name.go @@ -48,6 +48,24 @@ type WeeklyRanges struct { ranges [7][]TimeRange } +func (weeklyRanges *WeeklyRanges) Match() bool { + now := time.Now().Local() + day := now.Weekday() + weeklyRange := weeklyRanges.ranges[day] + if len(weeklyRange) == 0 { + return false + } + hour, min, _ := now.Clock() + nowX := (hour*60 + min) * 60 + for _, timeRange := range weeklyRange { + if (timeRange.after > timeRange.before && (nowX >= timeRange.after || nowX <= timeRange.before)) || + (nowX >= timeRange.after && nowX <= timeRange.before) { + return true + } + } + return false +} + type TimeRangeStr struct { After string Before string @@ -131,7 +149,6 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error { } else { weeklyRanges = &weeklyRangesX } - _ = weeklyRanges } line = strings.ToLower(line) switch blockType { @@ -181,16 +198,17 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er } revQname := StringReverse(qName) reject, reason := false, "" + var weeklyRanges *WeeklyRanges if !reject { - if match, _, found := plugin.blockedSuffixes.Root().LongestPrefix([]byte(revQname)); found { + if match, weeklyRangesX, found := plugin.blockedSuffixes.Root().LongestPrefix([]byte(revQname)); found { if len(match) == len(qName) || revQname[len(match)] == '.' { - reject, reason = true, "*."+StringReverse(string(match)) + reject, reason, weeklyRanges = true, "*."+StringReverse(string(match)), weeklyRangesX.(*WeeklyRanges) } else if len(match) < len(revQname) && len(revQname) > 0 { if i := strings.LastIndex(revQname, "."); i > 0 { pName := revQname[:i] if match, _, found := plugin.blockedSuffixes.Root().LongestPrefix([]byte(pName)); found { if len(match) == len(pName) || pName[len(match)] == '.' { - reject, reason = true, "*."+StringReverse(string(match)) + reject, reason, weeklyRanges = true, "*."+StringReverse(string(match)), weeklyRangesX.(*WeeklyRanges) } } } @@ -198,9 +216,9 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er } } if !reject { - match, _, found := plugin.blockedPrefixes.Root().LongestPrefix([]byte(qName)) + match, weeklyRangesX, found := plugin.blockedPrefixes.Root().LongestPrefix([]byte(qName)) if found { - reject, reason = true, string(match)+"*" + reject, reason, weeklyRanges = true, string(match)+"*", weeklyRangesX.(*WeeklyRanges) } } if !reject { @@ -219,6 +237,11 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er } } } + if reject { + if weeklyRanges != nil && !weeklyRanges.Match() { + reject = false + } + } if reject { pluginsState.action = PluginsActionReject if plugin.outFd != nil { From aa34dae308fe75aeee1efa0c21118872fb52b60c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 1 Feb 2018 01:05:23 +0100 Subject: [PATCH 2/2] Move the time check function down, make it more readable --- dnscrypt-proxy/plugin_block_name.go | 39 ++++++++++++++++------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/dnscrypt-proxy/plugin_block_name.go b/dnscrypt-proxy/plugin_block_name.go index 7f38dd6d..01af7ba6 100644 --- a/dnscrypt-proxy/plugin_block_name.go +++ b/dnscrypt-proxy/plugin_block_name.go @@ -48,24 +48,6 @@ type WeeklyRanges struct { ranges [7][]TimeRange } -func (weeklyRanges *WeeklyRanges) Match() bool { - now := time.Now().Local() - day := now.Weekday() - weeklyRange := weeklyRanges.ranges[day] - if len(weeklyRange) == 0 { - return false - } - hour, min, _ := now.Clock() - nowX := (hour*60 + min) * 60 - for _, timeRange := range weeklyRange { - if (timeRange.after > timeRange.before && (nowX >= timeRange.after || nowX <= timeRange.before)) || - (nowX >= timeRange.after && nowX <= timeRange.before) { - return true - } - } - return false -} - type TimeRangeStr struct { After string Before string @@ -344,3 +326,24 @@ func ParseAllWeeklyRanges(allWeeklyRangesStr map[string]WeeklyRangesStr) (*map[s } return &allWeeklyRanges, nil } + +func (weeklyRanges *WeeklyRanges) Match() bool { + now := time.Now().Local() + day := now.Weekday() + weeklyRange := weeklyRanges.ranges[day] + if len(weeklyRange) == 0 { + return false + } + hour, min, _ := now.Clock() + nowX := (hour*60 + min) * 60 + for _, timeRange := range weeklyRange { + if timeRange.after > timeRange.before { + if nowX >= timeRange.after || nowX <= timeRange.before { + return true + } + } else if nowX >= timeRange.after && nowX <= timeRange.before { + return true + } + } + return false +}