@@ -17,26 +17,26 @@ var (
17
17
RestoreChains = true
18
18
BackupChains = true
19
19
ReloadConf = true
20
-
21
- DefaultCheckInterval = 10 * time .Second
22
- RulesCheckerDisabled = "0s"
23
20
)
24
21
25
22
type (
26
23
callback func ()
27
24
callbackBool func () bool
28
25
26
+ stopChecker struct {
27
+ ch chan bool
28
+ sync.RWMutex
29
+ }
30
+
29
31
// Common holds common fields and functionality of both firewalls,
30
32
// iptables and nftables.
31
33
Common struct {
32
- RulesChecker * time.Ticker
33
- ErrChan chan string
34
- stopChecker chan bool
35
- RulesCheckInterval time.Duration
36
- QueueNum uint16
37
- Running bool
38
- Intercepting bool
39
- FwEnabled bool
34
+ RulesChecker * time.Ticker
35
+ stopCheckerChan * stopChecker
36
+ QueueNum uint16
37
+ Running bool
38
+ Intercepting bool
39
+ FwEnabled bool
40
40
sync.RWMutex
41
41
}
42
42
// FirewallError is a type that holds both IPv4 and IPv6 errors.
@@ -77,46 +77,15 @@ func (s *stopChecker) exit() <-chan bool {
77
77
return s .ch
78
78
}
79
79
80
- // ErrorsChan returns the channel where the errors are sent to.
81
- func (c * Common ) ErrorsChan () <- chan string {
82
- return c .ErrChan
83
- }
84
-
85
- // ErrChanEmpty checks if the errors channel is empty.
86
- func (c * Common ) ErrChanEmpty () bool {
87
- return len (c .ErrChan ) == 0
88
- }
89
-
90
- // SendError sends an error to the channel of errors.
91
- func (c * Common ) SendError (err string ) {
92
- log .Warning ("%s" , err )
80
+ func (s * stopChecker ) stop () {
81
+ s .Lock ()
82
+ defer s .Unlock ()
93
83
94
- if len (c .ErrChan ) >= cap (c .ErrChan ) {
95
- log .Debug ("fw errors channel full, emptying errChan" )
96
- for e := range c .ErrChan {
97
- log .Warning ("%s" , e )
98
- if c .ErrChanEmpty () {
99
- break
100
- }
101
- }
102
- return
103
- }
104
- select {
105
- case c .ErrChan <- err :
106
- case <- time .After (100 * time .Millisecond ):
107
- log .Warning ("SendError() channel locked? REVIEW" )
108
- }
109
- }
110
-
111
- func (c * Common ) SetRulesCheckerInterval (interval string ) {
112
- dur , err := time .ParseDuration (interval )
113
- if err != nil {
114
- log .Warning ("Invalid rules checker interval (falling back to %s): %s" , DefaultCheckInterval , err )
115
- c .RulesCheckInterval = DefaultCheckInterval
116
- return
84
+ if s .ch != nil {
85
+ s .ch <- true
86
+ close (s .ch )
87
+ s .ch = nil
117
88
}
118
-
119
- c .RulesCheckInterval = dur
120
89
}
121
90
122
91
// SetQueueNum sets the queue number used by the firewall.
@@ -128,6 +97,7 @@ func (c *Common) SetQueueNum(qNum *int) {
128
97
if qNum != nil {
129
98
c .QueueNum = uint16 (* qNum )
130
99
}
100
+
131
101
}
132
102
133
103
// IsRunning returns if the firewall is running or not.
@@ -160,38 +130,25 @@ func (c *Common) IsIntercepting() bool {
160
130
func (c * Common ) NewRulesChecker (areRulesLoaded callbackBool , reloadRules callback ) {
161
131
c .Lock ()
162
132
defer c .Unlock ()
163
- if c .RulesCheckInterval . String () == RulesCheckerDisabled {
164
- log . Info ( "Fw rules checker disabled ..." )
165
- return
133
+ if c .stopCheckerChan != nil {
134
+ c . stopCheckerChan . stop ( )
135
+ c . stopCheckerChan = nil
166
136
}
167
137
168
- if c .RulesChecker != nil {
169
- c .RulesChecker .Stop ()
170
- select {
171
- case c .stopChecker <- true :
172
- case <- time .After (5 * time .Millisecond ):
173
- log .Error ("NewRulesChecker: timed out stopping monitor rules" )
174
- }
175
- }
176
- c .stopChecker = make (chan bool , 1 )
177
- log .Info ("Starting new fw checker every %s ..." , c .RulesCheckInterval )
178
- c .RulesChecker = time .NewTicker (c .RulesCheckInterval )
138
+ c .stopCheckerChan = & stopChecker {ch : make (chan bool , 1 )}
139
+ c .RulesChecker = time .NewTicker (time .Second * 15 )
179
140
180
- go startCheckingRules ( c . stopChecker , c . RulesChecker , areRulesLoaded , reloadRules )
141
+ go c . startCheckingRules ( areRulesLoaded , reloadRules )
181
142
}
182
143
183
144
// StartCheckingRules monitors if our rules are loaded.
184
145
// If the rules to intercept traffic are not loaded, we'll try to insert them again.
185
- func startCheckingRules ( exitChan <- chan bool , rulesChecker * time. Ticker , areRulesLoaded callbackBool , reloadRules callback ) {
146
+ func ( c * Common ) startCheckingRules ( areRulesLoaded callbackBool , reloadRules callback ) {
186
147
for {
187
148
select {
188
- case <- exitChan :
149
+ case <- c . stopCheckerChan . exit () :
189
150
goto Exit
190
- case _ , active := <- rulesChecker .C :
191
- if ! active {
192
- goto Exit
193
- }
194
-
151
+ case <- c .RulesChecker .C :
195
152
if areRulesLoaded () == false {
196
153
reloadRules ()
197
154
}
@@ -204,20 +161,14 @@ Exit:
204
161
205
162
// StopCheckingRules stops checking if firewall rules are loaded.
206
163
func (c * Common ) StopCheckingRules () {
207
- c .Lock ()
208
- defer c .Unlock ()
164
+ c .RLock ()
165
+ defer c .RUnlock ()
209
166
210
167
if c .RulesChecker != nil {
211
- select {
212
- case c .stopChecker <- true :
213
- close (c .stopChecker )
214
- case <- time .After (5 * time .Millisecond ):
215
- // We should not arrive here
216
- log .Error ("StopCheckingRules: timed out stopping monitor rules" )
217
- }
218
-
219
168
c .RulesChecker .Stop ()
220
- c .RulesChecker = nil
169
+ }
170
+ if c .stopCheckerChan != nil {
171
+ c .stopCheckerChan .stop ()
221
172
}
222
173
}
223
174
0 commit comments