@@ -28,10 +28,6 @@ const (
2828 filePermissions = fs .FileMode (0666 )
2929)
3030
31- var (
32- infoLogger = log .New (io .Discard , "INFO: GeoBlock: " , log .Ldate | log .Ltime )
33- )
34-
3531// Config the plugin configuration.
3632type Config struct {
3733 SilentStartUp bool `yaml:"silentStartUp"`
@@ -94,10 +90,13 @@ type GeoBlock struct {
9490 logFile * os.File
9591 redirectURLIfDenied string
9692 name string
93+ infoLogger * log.Logger
9794}
9895
9996// New created a new GeoBlock plugin.
10097func New (ctx context.Context , next http.Handler , config * Config , name string ) (http.Handler , error ) {
98+ infoLogger := log .New (io .Discard , "INFO: GeoBlock: " , log .Ldate | log .Ltime )
99+
101100 // check geolocation API uri
102101 if len (config .API ) == 0 || ! strings .Contains (config .API , "{ip}" ) {
103102 return nil , fmt .Errorf ("no api uri given" )
@@ -178,14 +177,15 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
178177 logFile : logFile ,
179178 redirectURLIfDenied : config .RedirectURLIfDenied ,
180179 name : name ,
180+ infoLogger : infoLogger ,
181181 }, nil
182182}
183183
184184func (a * GeoBlock ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
185185 requestIPAddresses , err := a .collectRemoteIP (req )
186186 if err != nil {
187187 // if one of the ip addresses could not be parsed, return status forbidden
188- infoLogger .Println (err )
188+ a . infoLogger .Println (err )
189189 rw .WriteHeader (http .StatusForbidden )
190190 return
191191 }
@@ -216,21 +216,21 @@ func (a *GeoBlock) allowDenyIPAddress(requestIPAddr *net.IP, req *http.Request)
216216 if isPrivateIP (* requestIPAddr , a .privateIPRanges ) {
217217 if a .allowLocalRequests {
218218 if a .logLocalRequests {
219- infoLogger .Printf ("%s: request allowed [%s] since local IP addresses are allowed" , a .name , requestIPAddr )
219+ a . infoLogger .Printf ("%s: request allowed [%s] since local IP addresses are allowed" , a .name , requestIPAddr )
220220 }
221221 return true
222222 }
223223
224224 if a .logLocalRequests {
225- infoLogger .Printf ("%s: request denied [%s] since local IP addresses are denied" , a .name , requestIPAddr )
225+ a . infoLogger .Printf ("%s: request denied [%s] since local IP addresses are denied" , a .name , requestIPAddr )
226226 }
227227 return false
228228 }
229229
230230 // check if the request IP address is explicitly allowed
231231 if ipInSlice (* requestIPAddr , a .allowedIPAddresses ) {
232232 if a .logAllowedRequests {
233- infoLogger .Printf ("%s: request allowed [%s] since the IP address is explicitly allowed" , a .name , requestIPAddr )
233+ a . infoLogger .Printf ("%s: request allowed [%s] since the IP address is explicitly allowed" , a .name , requestIPAddr )
234234 }
235235 return true
236236 }
@@ -239,7 +239,7 @@ func (a *GeoBlock) allowDenyIPAddress(requestIPAddr *net.IP, req *http.Request)
239239 for _ , ipRange := range a .allowedIPRanges {
240240 if ipRange .Contains (* requestIPAddr ) {
241241 if a .logLocalRequests {
242- infoLogger .Printf ("%s: request allowed [%s] since the IP address is explicitly allowed" , a .name , requestIPAddr )
242+ a . infoLogger .Printf ("%s: request allowed [%s] since the IP address is explicitly allowed" , a .name , requestIPAddr )
243243 }
244244 return true
245245 }
@@ -266,7 +266,7 @@ func (a *GeoBlock) allowDenyCachedRequestIP(requestIPAddr *net.IP, req *http.Req
266266 if err != nil && ! (os .IsTimeout (err ) && a .ignoreAPITimeout ) {
267267 return false , ""
268268 } else if os .IsTimeout (err ) && a .ignoreAPITimeout {
269- infoLogger .Printf ("%s: request allowed [%s] due to API timeout" , a .name , requestIPAddr )
269+ a . infoLogger .Printf ("%s: request allowed [%s] due to API timeout" , a .name , requestIPAddr )
270270 // TODO: this was previously an immediate response to the client
271271 return true , ""
272272 }
@@ -275,7 +275,7 @@ func (a *GeoBlock) allowDenyCachedRequestIP(requestIPAddr *net.IP, req *http.Req
275275 }
276276
277277 if a .logAPIRequests {
278- infoLogger .Println ("Loaded from database: " , entry )
278+ a . infoLogger .Println ("Loaded from database: " , entry )
279279 }
280280
281281 // check if existing entry was made more than a month ago, if so update the entry
@@ -292,12 +292,12 @@ func (a *GeoBlock) allowDenyCachedRequestIP(requestIPAddr *net.IP, req *http.Req
292292 (entry .Country == unknownCountryCode && a .allowUnknownCountries )
293293
294294 if ! isAllowed {
295- infoLogger .Printf ("%s: request denied [%s] for country [%s]" , a .name , requestIPAddr , entry .Country )
295+ a . infoLogger .Printf ("%s: request denied [%s] for country [%s]" , a .name , requestIPAddr , entry .Country )
296296 return false , entry .Country
297297 }
298298
299299 if a .logAllowedRequests {
300- infoLogger .Printf ("%s: request allowed [%s] for country [%s]" , a .name , requestIPAddr , entry .Country )
300+ a . infoLogger .Printf ("%s: request allowed [%s] for country [%s]" , a .name , requestIPAddr , entry .Country )
301301 }
302302
303303 return true , entry .Country
@@ -351,7 +351,7 @@ func (a *GeoBlock) createNewIPEntry(req *http.Request, ipAddressString string) (
351351 a .database .Add (ipAddressString , entry )
352352
353353 if a .logAPIRequests {
354- infoLogger .Println ("Added to database: " , entry )
354+ a . infoLogger .Println ("Added to database: " , entry )
355355 }
356356
357357 return entry , nil
@@ -365,7 +365,7 @@ func (a *GeoBlock) getCountryCode(req *http.Request, ipAddressString string) (st
365365 }
366366
367367 if a .logAPIRequests {
368- infoLogger .Print ("Failed to read country from HTTP header field [" ,
368+ a . infoLogger .Print ("Failed to read country from HTTP header field [" ,
369369 a .iPGeolocationHTTPHeaderField ,
370370 "], continuing with API lookup." )
371371 }
@@ -374,7 +374,7 @@ func (a *GeoBlock) getCountryCode(req *http.Request, ipAddressString string) (st
374374 country , err := a .callGeoJS (ipAddressString )
375375 if err != nil {
376376 if ! (os .IsTimeout (err ) || a .ignoreAPITimeout ) {
377- infoLogger .Println (err )
377+ a . infoLogger .Println (err )
378378 }
379379 return "" , err
380380 }
@@ -422,7 +422,7 @@ func (a *GeoBlock) callGeoJS(ipAddress string) (string, error) {
422422 }
423423
424424 if a .logAPIRequests {
425- infoLogger .Printf ("Country [%s] for ip %s fetched from %s" , countryCode , ipAddress , apiURI )
425+ a . infoLogger .Printf ("Country [%s] for ip %s fetched from %s" , countryCode , ipAddress , apiURI )
426426 }
427427
428428 return countryCode , nil
@@ -575,13 +575,13 @@ func initializeLogFile(logFilePath string, logger *log.Logger) (*os.File, error)
575575 return nil , nil
576576 }
577577
578- writable , err := isFolder (logFilePath )
578+ writeable , err := isFolder (logFilePath )
579579 if err != nil {
580580 logger .Println (err )
581581 return nil , err
582- } else if ! writable {
583- logger .Println ("Specified log folder is not writable " )
584- return nil , fmt .Errorf ("folder is not writable : %s" , logFilePath )
582+ } else if ! writeable {
583+ logger .Println ("Specified log folder is not writeable " )
584+ return nil , fmt .Errorf ("folder is not writeable : %s" , logFilePath )
585585 }
586586
587587 logFile , err := os .OpenFile (logFilePath , os .O_RDWR | os .O_CREATE | os .O_APPEND , filePermissions )
0 commit comments