@@ -13,17 +13,13 @@ import (
1313 lumberjack "gopkg.in/natefinch/lumberjack.v2"
1414)
1515
16- type BlockedNames struct {
16+ type PluginBlockName struct {
1717 allWeeklyRanges * map [string ]WeeklyRanges
1818 patternMatcher * PatternMatcher
1919 logger * lumberjack.Logger
2020 format string
2121}
2222
23- type PluginBlockName struct {
24- blockedNames * BlockedNames
25- }
26-
2723func (plugin * PluginBlockName ) Name () string {
2824 return "block_name"
2925}
@@ -38,10 +34,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
3834 if err != nil {
3935 return err
4036 }
41- blockedNames := BlockedNames {
42- allWeeklyRanges : proxy .allWeeklyRanges ,
43- patternMatcher : NewPatternPatcher (),
44- }
37+ plugin .allWeeklyRanges = proxy .allWeeklyRanges
38+ plugin .patternMatcher = NewPatternPatcher ()
4539 for lineNo , line := range strings .Split (string (bin ), "\n " ) {
4640 line = strings .TrimFunc (line , unicode .IsSpace )
4741 if len (line ) == 0 || strings .HasPrefix (line , "#" ) {
@@ -58,24 +52,23 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
5852 }
5953 var weeklyRanges * WeeklyRanges
6054 if len (timeRangeName ) > 0 {
61- weeklyRangesX , ok := (* blockedNames .allWeeklyRanges )[timeRangeName ]
55+ weeklyRangesX , ok := (* plugin .allWeeklyRanges )[timeRangeName ]
6256 if ! ok {
6357 dlog .Errorf ("Time range [%s] not found at line %d" , timeRangeName , 1 + lineNo )
6458 } else {
6559 weeklyRanges = & weeklyRangesX
6660 }
6761 }
68- if err := blockedNames .patternMatcher .Add (line , weeklyRanges , lineNo + 1 ); err != nil {
62+ if err := plugin .patternMatcher .Add (line , weeklyRanges , lineNo + 1 ); err != nil {
6963 dlog .Error (err )
7064 continue
7165 }
7266 }
7367 if len (proxy .blockNameLogFile ) == 0 {
7468 return nil
7569 }
76- blockedNames .logger = & lumberjack.Logger {LocalTime : true , MaxSize : proxy .logMaxSize , MaxAge : proxy .logMaxAge , MaxBackups : proxy .logMaxBackups , Filename : proxy .blockNameLogFile , Compress : true }
77- blockedNames .format = proxy .blockNameFormat
78- plugin .blockedNames = & blockedNames
70+ plugin .logger = & lumberjack.Logger {LocalTime : true , MaxSize : proxy .logMaxSize , MaxAge : proxy .logMaxAge , MaxBackups : proxy .logMaxBackups , Filename : proxy .blockNameLogFile , Compress : true }
71+ plugin .format = proxy .blockNameFormat
7972
8073 return nil
8174}
@@ -97,11 +90,7 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
9790 return nil
9891 }
9992 qName := strings .ToLower (StripTrailingDot (questions [0 ].Name ))
100- return plugin .blockedNames .check (pluginsState , qName )
101- }
102-
103- func (blockedNames * BlockedNames ) check (pluginsState * PluginsState , qName string ) error {
104- reject , reason , xweeklyRanges := blockedNames .patternMatcher .Eval (qName )
93+ reject , reason , xweeklyRanges := plugin .patternMatcher .Eval (qName )
10594 var weeklyRanges * WeeklyRanges
10695 if xweeklyRanges != nil {
10796 weeklyRanges = xweeklyRanges .(* WeeklyRanges )
@@ -111,34 +100,33 @@ func (blockedNames *BlockedNames) check(pluginsState *PluginsState, qName string
111100 reject = false
112101 }
113102 }
114- if ! reject {
115- return nil
116- }
117- pluginsState . action = PluginsActionReject
118- pluginsState . returnCode = PluginsReturnCodeReject
119- if blockedNames . logger != nil {
120- var clientIPStr string
121- if pluginsState . clientProto == "udp" {
122- clientIPStr = (* pluginsState .clientAddr ).(* net.UDPAddr ).IP .String ()
123- } else {
124- clientIPStr = ( * pluginsState . clientAddr ).( * net. TCPAddr ). IP . String ()
125- }
126- var line string
127- if blockedNames . format == "tsv" {
128- now := time . Now ()
129- year , month , day := now . Date ( )
130- hour , minute , second := now . Clock ( )
131- tsStr := fmt . Sprintf ( "[%d-%02d-%02d %02d:%02d:%02d]" , year , int ( month ), day , hour , minute , second )
132- line = fmt .Sprintf ("%s \t %s\t %s\t %s\n " , tsStr , clientIPStr , StringQuote (qName ), StringQuote (reason ))
133- } else if blockedNames . format == "ltsv" {
134- line = fmt . Sprintf ( "time:%d \t host:%s \t qname:%s \t message:%s \n " , time . Now (). Unix (), clientIPStr , StringQuote ( qName ), StringQuote ( reason ) )
135- } else {
136- dlog . Fatalf ( "Unexpected log format: [%s]" , blockedNames . format )
137- }
138- if blockedNames . logger == nil {
139- return errors . New ( "Log file not initialized" )
103+ if reject {
104+ pluginsState . action = PluginsActionReject
105+ pluginsState . returnCode = PluginsReturnCodeReject
106+ if plugin . logger != nil {
107+ var clientIPStr string
108+ if pluginsState . clientProto == "udp" {
109+ clientIPStr = ( * pluginsState . clientAddr ).( * net. UDPAddr ). IP . String ()
110+ } else {
111+ clientIPStr = (* pluginsState .clientAddr ).(* net.TCPAddr ).IP .String ()
112+ }
113+ var line string
114+ if plugin . format == "tsv" {
115+ now := time . Now ()
116+ year , month , day := now . Date ()
117+ hour , minute , second := now . Clock ()
118+ tsStr := fmt . Sprintf ( "[%d-%02d-%02d %02d:%02d:%02d]" , year , int ( month ) , day , hour , minute , second )
119+ line = fmt . Sprintf ( "%s \t %s \t %s \t %s \n " , tsStr , clientIPStr , StringQuote ( qName ), StringQuote ( reason ) )
120+ } else if plugin . format == "ltsv" {
121+ line = fmt .Sprintf ("time:%d \t host: %s\t qname: %s\t message: %s\n " , time . Now (). Unix () , clientIPStr , StringQuote (qName ), StringQuote (reason ))
122+ } else {
123+ dlog . Fatalf ( "Unexpected log format: [%s] " , plugin . format )
124+ }
125+ if plugin . logger == nil {
126+ return errors . New ( "Log file not initialized" )
127+ }
128+ plugin . logger . Write ([] byte ( line ) )
140129 }
141- blockedNames .logger .Write ([]byte (line ))
142130 }
143131 return nil
144132}
0 commit comments