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

@ -45,6 +45,7 @@ type Config struct {
MaxClients uint32 `toml:"max_clients"` MaxClients uint32 `toml:"max_clients"`
FallbackResolver string `toml:"fallback_resolver"` FallbackResolver string `toml:"fallback_resolver"`
IgnoreSystemDNS bool `toml:"ignore_system_dns"` IgnoreSystemDNS bool `toml:"ignore_system_dns"`
TimeRanges map[string][]TimeRangeStr `toml:"time_ranges"`
} }
func newConfig() Config { func newConfig() Config {
@ -221,6 +222,8 @@ func ConfigLoad(proxy *Proxy, svcFlag *string) error {
proxy.forwardFile = config.ForwardFile proxy.forwardFile = config.ForwardFile
parseWeeklyRanges(config.TimeRanges)
if err := config.loadSources(proxy); err != nil { if err := config.loadSources(proxy); err != nil {
return err return err
} }

View File

@ -315,3 +315,17 @@ format = 'tsv'
# [static.'google'] # [static.'google']
# stamp = 'sdns://AgEAAAAAAAAAACDyXGrcc5eNecJ8nomJCJ-q6eCLTEn6bHic0hWGUwYQaA5kbnMuZ29vZ2xlLmNvbQ0vZXhwZXJpbWVudGFs' # 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" "net"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -35,6 +36,72 @@ type PluginBlockName struct {
blockedPatterns []string blockedPatterns []string
outFd *os.File outFd *os.File
format string 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 { func (plugin *PluginBlockName) Name() string {
@ -58,6 +125,15 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
if len(line) == 0 || strings.HasPrefix(line, "#") { if len(line) == 0 || strings.HasPrefix(line, "#") {
continue 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, "*") leadingStar := strings.HasPrefix(line, "*")
trailingStar := strings.HasSuffix(line, "*") trailingStar := strings.HasSuffix(line, "*")
blockType := PluginBlockTypeNone 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) dlog.Errorf("Syntax error in block rule at line %d", 1+lineNo)
continue 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) line = strings.ToLower(line)
switch blockType { switch blockType {
case PluginBlockTypeSubstring: case PluginBlockTypeSubstring:
@ -100,9 +185,9 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
case PluginBlockTypePattern: case PluginBlockTypePattern:
plugin.blockedPatterns = append(plugin.blockedPatterns, line) plugin.blockedPatterns = append(plugin.blockedPatterns, line)
case PluginBlockTypePrefix: case PluginBlockTypePrefix:
plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), 0) plugin.blockedPrefixes, _, _ = plugin.blockedPrefixes.Insert([]byte(line), timeRange)
case PluginBlockTypeSuffix: case PluginBlockTypeSuffix:
plugin.blockedSuffixes, _, _ = plugin.blockedSuffixes.Insert([]byte(StringReverse(line)), 0) plugin.blockedSuffixes, _, _ = plugin.blockedSuffixes.Insert([]byte(StringReverse(line)), timeRange)
default: default:
dlog.Fatal("Unexpected block type") dlog.Fatal("Unexpected block type")
} }