Skip to content

Commit ef98da2

Browse files
committed
fix logging to specific log file (fixes #79)
1 parent b546642 commit ef98da2

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

docker/dev-geoblock/docker-compose.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: "3.7"
2-
31
services:
42
traefik:
53
image: traefik:v3.1
@@ -8,8 +6,9 @@ services:
86
- /var/run/docker.sock:/var/run/docker.sock
97
- ./../traefik-config/traefik.yml:/etc/traefik/traefik.yml
108
- ./../traefik-config/dynamic-configuration.yml:/etc/traefik/dynamic-configuration.yml
11-
- ./../..:/plugins-local/src/github.com/PascalMinder/geoblock/
9+
- ./../../../geoblock:/plugins-local/src/github.com/PascalMinder/geoblock
1210
- ./../log:/log
11+
- ./../log/geoblock:/geoblock
1312

1413
labels:
1514
- "traefik.http.routers.dash.rule=Host(`dash.localhost`)"
@@ -34,4 +33,4 @@ services:
3433
- traefik.http.routers.whoami.entrypoints=http
3534
- traefik.http.routers.whoami.rule=Host(`whoami.localhost`)
3635
- traefik.http.services.whoami.loadbalancer.server.port=8000
37-
- traefik.http.routers.whoami.middlewares=my-plugin@file
36+
- traefik.http.routers.whoami.middlewares=my-plugin2@file

docker/traefik-config/dynamic-configuration.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,32 @@ http:
55
geoblock:
66
silentStartUp: false
77
allowLocalRequests: true
8-
logLocalRequests: false
8+
logLocalRequests: true
99
logAllowedRequests: true
10-
logApiRequests: false
10+
logApiRequests: true
1111
api: "https://get.geojs.io/v1/ip/country/{ip}"
1212
cacheSize: 15
1313
forceMonthlyUpdate: true
1414
allowUnknownCountries: false
1515
unknownCountryApiResponse: "nil"
16+
logFilePath: "/geoblock/geoblockB.log"
17+
countries:
18+
- GB
19+
- IS
20+
my-plugin2:
21+
plugin:
22+
geoblock:
23+
silentStartUp: false
24+
allowLocalRequests: true
25+
logLocalRequests: true
26+
logAllowedRequests: true
27+
logApiRequests: true
28+
api: "https://get.geojs.io/v1/ip/country/{ip}"
29+
cacheSize: 15
30+
forceMonthlyUpdate: true
31+
allowUnknownCountries: false
32+
unknownCountryApiResponse: "nil"
33+
logFilePath: "/geoblock/geoblockA.log"
1634
countries:
1735
- GB
1836
- IS

geoblock.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3632
type 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.
10097
func 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

184184
func (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

Comments
 (0)