@@ -13,17 +13,13 @@ import (
13
13
lumberjack "gopkg.in/natefinch/lumberjack.v2"
14
14
)
15
15
16
- type BlockedNames struct {
16
+ type PluginBlockName struct {
17
17
allWeeklyRanges * map [string ]WeeklyRanges
18
18
patternMatcher * PatternMatcher
19
19
logger * lumberjack.Logger
20
20
format string
21
21
}
22
22
23
- type PluginBlockName struct {
24
- blockedNames * BlockedNames
25
- }
26
-
27
23
func (plugin * PluginBlockName ) Name () string {
28
24
return "block_name"
29
25
}
@@ -38,10 +34,8 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
38
34
if err != nil {
39
35
return err
40
36
}
41
- blockedNames := BlockedNames {
42
- allWeeklyRanges : proxy .allWeeklyRanges ,
43
- patternMatcher : NewPatternPatcher (),
44
- }
37
+ plugin .allWeeklyRanges = proxy .allWeeklyRanges
38
+ plugin .patternMatcher = NewPatternPatcher ()
45
39
for lineNo , line := range strings .Split (string (bin ), "\n " ) {
46
40
line = strings .TrimFunc (line , unicode .IsSpace )
47
41
if len (line ) == 0 || strings .HasPrefix (line , "#" ) {
@@ -58,24 +52,23 @@ func (plugin *PluginBlockName) Init(proxy *Proxy) error {
58
52
}
59
53
var weeklyRanges * WeeklyRanges
60
54
if len (timeRangeName ) > 0 {
61
- weeklyRangesX , ok := (* blockedNames .allWeeklyRanges )[timeRangeName ]
55
+ weeklyRangesX , ok := (* plugin .allWeeklyRanges )[timeRangeName ]
62
56
if ! ok {
63
57
dlog .Errorf ("Time range [%s] not found at line %d" , timeRangeName , 1 + lineNo )
64
58
} else {
65
59
weeklyRanges = & weeklyRangesX
66
60
}
67
61
}
68
- if err := blockedNames .patternMatcher .Add (line , weeklyRanges , lineNo + 1 ); err != nil {
62
+ if err := plugin .patternMatcher .Add (line , weeklyRanges , lineNo + 1 ); err != nil {
69
63
dlog .Error (err )
70
64
continue
71
65
}
72
66
}
73
67
if len (proxy .blockNameLogFile ) == 0 {
74
68
return nil
75
69
}
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
79
72
80
73
return nil
81
74
}
@@ -97,11 +90,7 @@ func (plugin *PluginBlockName) Eval(pluginsState *PluginsState, msg *dns.Msg) er
97
90
return nil
98
91
}
99
92
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 )
105
94
var weeklyRanges * WeeklyRanges
106
95
if xweeklyRanges != nil {
107
96
weeklyRanges = xweeklyRanges .(* WeeklyRanges )
@@ -111,34 +100,33 @@ func (blockedNames *BlockedNames) check(pluginsState *PluginsState, qName string
111
100
reject = false
112
101
}
113
102
}
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 ) )
140
129
}
141
- blockedNames .logger .Write ([]byte (line ))
142
130
}
143
131
return nil
144
132
}
0 commit comments