Revert "Revert "plugin_block_name: make the blocking code reusable""
This reverts commit 2d00c24f85
.
This commit is contained in:
parent
2d00c24f85
commit
0790328424
|
@ -13,13 +13,17 @@ import (
|
||||||
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PluginBlockName struct {
|
type BlockedNames struct {
|
||||||
allWeeklyRanges *map[string]WeeklyRanges
|
allWeeklyRanges *map[string]WeeklyRanges
|
||||||
patternMatcher *PatternMatcher
|
patternMatcher *PatternMatcher
|
||||||
logger *lumberjack.Logger
|
logger *lumberjack.Logger
|
||||||
format string
|
format string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PluginBlockName struct {
|
||||||
|
blockedNames *BlockedNames
|
||||||
|
}
|
||||||
|
|
||||||
func (plugin *PluginBlockName) Name() string {
|
func (plugin *PluginBlockName) Name() string {
|
||||||
return "block_name"
|
return "block_name"
|
||||||
}
|
}
|
||||||
|
@ -34,8 +38,10 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
plugin.allWeeklyRanges = proxy.allWeeklyRanges
|
blockedNames := BlockedNames{
|
||||||
plugin.patternMatcher = NewPatternPatcher()
|
allWeeklyRanges: proxy.allWeeklyRanges,
|
||||||
|
patternMatcher: NewPatternPatcher(),
|
||||||
|
}
|
||||||
for lineNo, line := range strings.Split(string(bin), "\n") {
|
for lineNo, line := range strings.Split(string(bin), "\n") {
|
||||||
line = strings.TrimFunc(line, unicode.IsSpace)
|
line = strings.TrimFunc(line, unicode.IsSpace)
|
||||||
if len(line) == 0 || strings.HasPrefix(line, "#") {
|
if len(line) == 0 || strings.HasPrefix(line, "#") {
|
||||||
|
@ -52,14 +58,14 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
|
||||||
}
|
}
|
||||||
var weeklyRanges *WeeklyRanges
|
var weeklyRanges *WeeklyRanges
|
||||||
if len(timeRangeName) > 0 {
|
if len(timeRangeName) > 0 {
|
||||||
weeklyRangesX, ok := (*plugin.allWeeklyRanges)[timeRangeName]
|
weeklyRangesX, ok := (*blockedNames.allWeeklyRanges)[timeRangeName]
|
||||||
if !ok {
|
if !ok {
|
||||||
dlog.Errorf("Time range [%s] not found at line %d", timeRangeName, 1+lineNo)
|
dlog.Errorf("Time range [%s] not found at line %d", timeRangeName, 1+lineNo)
|
||||||
} else {
|
} else {
|
||||||
weeklyRanges = &weeklyRangesX
|
weeklyRanges = &weeklyRangesX
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := plugin.patternMatcher.Add(line, weeklyRanges, lineNo+1); err != nil {
|
if err := blockedNames.patternMatcher.Add(line, weeklyRanges, lineNo+1); err != nil {
|
||||||
dlog.Error(err)
|
dlog.Error(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -67,8 +73,9 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
|
||||||
if len(proxy.blockNameLogFile) == 0 {
|
if len(proxy.blockNameLogFile) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
plugin.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.blockNameLogFile, Compress: true}
|
blockedNames.logger = &lumberjack.Logger{LocalTime: true, MaxSize: proxy.logMaxSize, MaxAge: proxy.logMaxAge, MaxBackups: proxy.logMaxBackups, Filename: proxy.blockNameLogFile, Compress: true}
|
||||||
plugin.format = proxy.blockNameFormat
|
blockedNames.format = proxy.blockNameFormat
|
||||||
|
plugin.blockedNames = &blockedNames
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -90,7 +97,11 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
qName := strings.ToLower(StripTrailingDot(questions[0].Name))
|
qName := strings.ToLower(StripTrailingDot(questions[0].Name))
|
||||||
reject, reason, xweeklyRanges := plugin.patternMatcher.Eval(qName)
|
return plugin.blockedNames.check(pluginsState, qName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (blockedNames *BlockedNames) check(pluginsState *PluginsState, qName string) error {
|
||||||
|
reject, reason, xweeklyRanges := blockedNames.patternMatcher.Eval(qName)
|
||||||
var weeklyRanges *WeeklyRanges
|
var weeklyRanges *WeeklyRanges
|
||||||
if xweeklyRanges != nil {
|
if xweeklyRanges != nil {
|
||||||
weeklyRanges = xweeklyRanges.(*WeeklyRanges)
|
weeklyRanges = xweeklyRanges.(*WeeklyRanges)
|
||||||
|
@ -100,33 +111,34 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
|
||||||
reject = false
|
reject = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if reject {
|
if !reject {
|
||||||
pluginsState.action = PluginsActionReject
|
return nil
|
||||||
pluginsState.returnCode = PluginsReturnCodeReject
|
}
|
||||||
if plugin.logger != nil {
|
pluginsState.action = PluginsActionReject
|
||||||
var clientIPStr string
|
pluginsState.returnCode = PluginsReturnCodeReject
|
||||||
if pluginsState.clientProto == "udp" {
|
if blockedNames.logger != nil {
|
||||||
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
|
var clientIPStr string
|
||||||
} else {
|
if pluginsState.clientProto == "udp" {
|
||||||
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
|
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
|
||||||
}
|
} else {
|
||||||
var line string
|
clientIPStr = (*pluginsState.clientAddr).(*net.TCPAddr).IP.String()
|
||||||
if plugin.format == "tsv" {
|
|
||||||
now := time.Now()
|
|
||||||
year, month, day := now.Date()
|
|
||||||
hour, minute, second := now.Clock()
|
|
||||||
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
|
|
||||||
line = fmt.Sprintf("%s\t%s\t%s\t%s\n", tsStr, clientIPStr, StringQuote(qName), StringQuote(reason))
|
|
||||||
} else if plugin.format == "ltsv" {
|
|
||||||
line = fmt.Sprintf("time:%d\thost:%s\tqname:%s\tmessage:%s\n", time.Now().Unix(), clientIPStr, StringQuote(qName), StringQuote(reason))
|
|
||||||
} else {
|
|
||||||
dlog.Fatalf("Unexpected log format: [%s]", plugin.format)
|
|
||||||
}
|
|
||||||
if plugin.logger == nil {
|
|
||||||
return errors.New("Log file not initialized")
|
|
||||||
}
|
|
||||||
plugin.logger.Write([]byte(line))
|
|
||||||
}
|
}
|
||||||
|
var line string
|
||||||
|
if blockedNames.format == "tsv" {
|
||||||
|
now := time.Now()
|
||||||
|
year, month, day := now.Date()
|
||||||
|
hour, minute, second := now.Clock()
|
||||||
|
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
|
||||||
|
line = fmt.Sprintf("%s\t%s\t%s\t%s\n", tsStr, clientIPStr, StringQuote(qName), StringQuote(reason))
|
||||||
|
} else if blockedNames.format == "ltsv" {
|
||||||
|
line = fmt.Sprintf("time:%d\thost:%s\tqname:%s\tmessage:%s\n", time.Now().Unix(), clientIPStr, StringQuote(qName), StringQuote(reason))
|
||||||
|
} else {
|
||||||
|
dlog.Fatalf("Unexpected log format: [%s]", blockedNames.format)
|
||||||
|
}
|
||||||
|
if blockedNames.logger == nil {
|
||||||
|
return errors.New("Log file not initialized")
|
||||||
|
}
|
||||||
|
blockedNames.logger.Write([]byte(line))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue