@@ -17,26 +17,26 @@ var (
1717 RestoreChains = true
1818 BackupChains = true
1919 ReloadConf = true
20-
21- DefaultCheckInterval = 10 * time .Second
22- RulesCheckerDisabled = "0s"
2320)
2421
2522type (
2623 callback func ()
2724 callbackBool func () bool
2825
26+ stopChecker struct {
27+ ch chan bool
28+ sync.RWMutex
29+ }
30+
2931 // Common holds common fields and functionality of both firewalls,
3032 // iptables and nftables.
3133 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
4040 sync.RWMutex
4141 }
4242 // FirewallError is a type that holds both IPv4 and IPv6 errors.
@@ -77,46 +77,15 @@ func (s *stopChecker) exit() <-chan bool {
7777 return s .ch
7878}
7979
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 ()
9383
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
11788 }
118-
119- c .RulesCheckInterval = dur
12089}
12190
12291// SetQueueNum sets the queue number used by the firewall.
@@ -128,6 +97,7 @@ func (c *Common) SetQueueNum(qNum *int) {
12897 if qNum != nil {
12998 c .QueueNum = uint16 (* qNum )
13099 }
100+
131101}
132102
133103// IsRunning returns if the firewall is running or not.
@@ -160,38 +130,25 @@ func (c *Common) IsIntercepting() bool {
160130func (c * Common ) NewRulesChecker (areRulesLoaded callbackBool , reloadRules callback ) {
161131 c .Lock ()
162132 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
166136 }
167137
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 )
179140
180- go startCheckingRules ( c . stopChecker , c . RulesChecker , areRulesLoaded , reloadRules )
141+ go c . startCheckingRules ( areRulesLoaded , reloadRules )
181142}
182143
183144// StartCheckingRules monitors if our rules are loaded.
184145// 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 ) {
186147 for {
187148 select {
188- case <- exitChan :
149+ case <- c . stopCheckerChan . exit () :
189150 goto Exit
190- case _ , active := <- rulesChecker .C :
191- if ! active {
192- goto Exit
193- }
194-
151+ case <- c .RulesChecker .C :
195152 if areRulesLoaded () == false {
196153 reloadRules ()
197154 }
@@ -204,20 +161,14 @@ Exit:
204161
205162// StopCheckingRules stops checking if firewall rules are loaded.
206163func (c * Common ) StopCheckingRules () {
207- c .Lock ()
208- defer c .Unlock ()
164+ c .RLock ()
165+ defer c .RUnlock ()
209166
210167 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-
219168 c .RulesChecker .Stop ()
220- c .RulesChecker = nil
169+ }
170+ if c .stopCheckerChan != nil {
171+ c .stopCheckerChan .stop ()
221172 }
222173}
223174
0 commit comments