This commit is contained in:
Frank Denis 2018-01-31 22:18:11 +01:00
parent fe8ff9dcbb
commit d575ec8beb
3 changed files with 123 additions and 21 deletions

View File

@ -26,25 +26,26 @@ type Config struct {
CertIgnoreTimestamp bool `toml:"cert_ignore_timestamp"`
BlockIPv6 bool `toml:"block_ipv6"`
Cache bool
CacheSize int `toml:"cache_size"`
CacheNegTTL uint32 `toml:"cache_neg_ttl"`
CacheMinTTL uint32 `toml:"cache_min_ttl"`
CacheMaxTTL uint32 `toml:"cache_max_ttl"`
QueryLog QueryLogConfig `toml:"query_log"`
NxLog NxLogConfig `toml:"nx_log"`
BlockName BlockNameConfig `toml:"blacklist"`
BlockIP BlockIPConfig `toml:"ip_blacklist"`
ForwardFile string `toml:"forwarding_rules"`
ServersConfig map[string]StaticConfig `toml:"static"`
SourcesConfig map[string]SourceConfig `toml:"sources"`
SourceRequireDNSSEC bool `toml:"require_dnssec"`
SourceRequireNoLog bool `toml:"require_nolog"`
SourceRequireNoFilter bool `toml:"require_nofilter"`
SourceIPv4 bool `toml:"ipv4_servers"`
SourceIPv6 bool `toml:"ipv6_servers"`
MaxClients uint32 `toml:"max_clients"`
FallbackResolver string `toml:"fallback_resolver"`
IgnoreSystemDNS bool `toml:"ignore_system_dns"`
CacheSize int `toml:"cache_size"`
CacheNegTTL uint32 `toml:"cache_neg_ttl"`
CacheMinTTL uint32 `toml:"cache_min_ttl"`
CacheMaxTTL uint32 `toml:"cache_max_ttl"`
QueryLog QueryLogConfig `toml:"query_log"`
NxLog NxLogConfig `toml:"nx_log"`
BlockName BlockNameConfig `toml:"blacklist"`
BlockIP BlockIPConfig `toml:"ip_blacklist"`
ForwardFile string `toml:"forwarding_rules"`
ServersConfig map[string]StaticConfig `toml:"static"`
SourcesConfig map[string]SourceConfig `toml:"sources"`
SourceRequireDNSSEC bool `toml:"require_dnssec"`
SourceRequireNoLog bool `toml:"require_nolog"`
SourceRequireNoFilter bool `toml:"require_nofilter"`
SourceIPv4 bool `toml:"ipv4_servers"`
SourceIPv6 bool `toml:"ipv6_servers"`
MaxClients uint32 `toml:"max_clients"`
FallbackResolver string `toml:"fallback_resolver"`
IgnoreSystemDNS bool `toml:"ignore_system_dns"`
TimeRanges map[string][]TimeRangeStr `toml:"time_ranges"`
}
func newConfig() Config {
@ -221,6 +222,8 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
proxy.forwardFile = config.ForwardFile
parseWeeklyRanges(config.TimeRanges)
if err := config.loadSources(proxy); err != nil {
return err
}

View File

@ -315,3 +315,17 @@ format = 'tsv'
# [static.'google']
# stamp = 'sdns://AgEAAAAAAAAAACDyXGrcc5eNecJ8nomJCJ-q6eCLTEn6bHic0hWGUwYQaA5kbnMuZ29vZ2xlLmNvbQ0vZXhwZXJpbWVudGFs'
[time_ranges]
[time_ranges.'time-to-sleep']
mon = [{after="22:00", before="07:00"}]
# mon = [{after="22:00", before="07:00"}]
# tue = [{after="22:00", before="07:00"}]
# wed = [{after="22:00", before="07:00"}]
# thu = [{after="22:00", before="07:00"}]
# fri = [{after="22:00", before="07:00"}]
# sat = [{after="22:00", before="07:00"}]
# sun = [{after="26:00", before="07:00"}]

View File

@ -7,6 +7,7 @@ import (
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
@ -35,6 +36,72 @@ type PluginBlockName struct {
blockedPatterns []string
outFd *os.File
format string
timeRanges map[string]*WeeklyRanges
}
type TimeRange struct {
start int
end int
}
type WeeklyRanges struct {
ranges [7][]TimeRange
}
type TimeRangeStr struct {
After string
Before string
}
func daySecsFromStr(str string) (int, error) {
parts := strings.Split(str, ":")
if len(parts) != 2 {
return -1, fmt.Errorf("Syntax error in a time expression: [%s]", str)
}
hours, err := strconv.Atoi(parts[0])
if err != nil || hours < 0 || hours > 23 {
return -1, fmt.Errorf("Syntax error in a time expression: [%s]", str)
}
minutes, err := strconv.Atoi(parts[1])
if err != nil || minutes < 0 || minutes > 59 {
return -1, fmt.Errorf("Syntax error in a time expression: [%s]", str)
}
return (hours*60 + minutes) * 60, nil
}
func parseTimeRanges(timeRangesStr []TimeRangeStr) ([]TimeRange, error) {
timeRanges := []TimeRange{}
for _, timeRangeStr := range timeRangesStr {
after, err := daySecsFromStr(timeRangeStr.After)
if err != nil {
return timeRanges, err
}
before, err := daySecsFromStr(timeRangeStr.Before)
if err != nil {
return timeRanges, err
}
if after == before {
after, before = -1, 86402
}
}
return timeRanges, nil
}
func parseWeeklyRanges(weeklyRangeStr map[string][]TimeRangeStr) (WeeklyRanges, error) {
weeklyRanges := WeeklyRanges{}
daysStr := []string{"sun", "mon", "tue", "wed", "thu", "fri", "sat"}
for day, dayStr := range daysStr {
timeRangesStr, ok := weeklyRangeStr[dayStr]
if !ok {
continue
}
timeRanges, err := parseTimeRanges(timeRangesStr)
if err != nil {
return weeklyRanges, err
}
weeklyRanges.ranges[day] = timeRanges
}
return weeklyRanges, nil
}
func (plugin *PluginBlockName) Name() string {
@ -58,6 +125,15 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
parts := strings.Split(line, "@")
timeRangeName := ""
if len(parts) == 2 {
line = strings.TrimFunc(parts[0], unicode.IsSpace)
timeRangeName = strings.TrimFunc(parts[1], unicode.IsSpace)
} else if len(parts) > 2 {
dlog.Errorf("Syntax error in block rules at line %d -- Unexpected @ character", 1+lineNo)
continue
}
leadingStar := strings.HasPrefix(line, "*")
trailingStar := strings.HasSuffix(line, "*")
blockType := PluginBlockTypeNone
@ -93,6 +169,15 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
dlog.Errorf("Syntax error in block rule at line %d", 1+lineNo)
continue
}
var timeRange *TimeRange
if len(timeRangeName) > 0 {
timeRange, ok := plugin.timeRanges[timeRangeName]
if !ok {
dlog.Errorf("Time range [%s] not found at line %d", timeRangeName, 1+lineNo)
timeRange = nil
}
_ = timeRange
}
line = strings.ToLower(line)
switch blockType {
case PluginBlockTypeSubstring:
@ -100,9 +185,9 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
case PluginBlockTypePattern:
plugin.blockedPatterns = append(plugin.blockedPatterns, line)
case PluginBlockTypePrefix:
plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), 0)
plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), timeRange)
case PluginBlockTypeSuffix:
plugin.blockedSuffixes, _, _ = plugin.blockedSuffixes.Insert([]byte(StringReverse(line)), 0)
plugin.blockedSuffixes, _, _ = plugin.blockedSuffixes.Insert([]byte(StringReverse(line)), timeRange)
default:
dlog.Fatal("Unexpected block type")
}