Skip to content

Commit f92916b

Browse files
committed
Try to fix issue with IP address look-up
1 parent 689ef69 commit f92916b

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ jobs:
1313
name: Main Process
1414
runs-on: ubuntu-latest
1515
env:
16-
GO_VERSION: 1.19
17-
GOLANGCI_LINT_VERSION: v1.50.0
18-
YAEGI_VERSION: v0.14.2
16+
GO_VERSION: 1.22
17+
GOLANGCI_LINT_VERSION: v1.58.1
18+
YAEGI_VERSION: v0.16.1
1919
CGO_ENABLED: 0
2020
defaults:
2121
run:

.golangci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
# inspired by: https://github.com/golangci/golangci-lint/blob/master/.golangci.yml
22

33
run:
4-
go: "1.18"
4+
go: "1.19"
55

66
linters-settings:
77
# dupl:
88
# threshold: 100
99
funlen:
1010
lines: 100
1111
statements: 50
12-
golint:
13-
min-confidence: 0
1412
lll:
1513
line-length: 125
1614
goconst:
1715
min-len: 2
18-
min-occurences: 2
16+
min-occurrences: 3
1917
misspell:
2018
locale: US
2119

@@ -26,14 +24,12 @@ linters:
2624
# - bidichk
2725
# - bodyclose
2826
# - contextcheck
29-
- deadcode
3027
- depguard
3128
- dogsled
3229
# - dupl
3330
# - durationcheck
3431
- errcheck
3532
# - errname
36-
- exportloopref
3733
- funlen
3834
- gochecknoinits
3935
- goconst
@@ -60,15 +56,13 @@ linters:
6056
# - predeclared
6157
# - revive
6258
- staticcheck
63-
- structcheck
6459
- stylecheck
6560
# - tagliatelle
6661
# - thelper
6762
- typecheck
6863
- unconvert
6964
- unparam
7065
- unused
71-
- varcheck
7266
# - wastedassign
7367
- whitespace
7468

geoblock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func (a *GeoBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
189189

190190
// only keep the first IP address, which should be the client (if the proxy behaves itself), to check if allowed or denied
191191
if a.xForwardedForReverseProxy {
192+
infoLogger.Printf("X-Forwarded-For reverse proxy is enabled. Searching for: %s\n", requestIPAddresses[0])
192193
requestIPAddresses = requestIPAddresses[:1]
193194
}
194195

geoblock_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,33 @@ func TestMultipleAllowedCountry(t *testing.T) {
153153
assertStatusCode(t, recorder.Result(), http.StatusOK)
154154
}
155155

156+
func createMockAPIServer(t *testing.T, ipResponseMap map[string][]byte) *httptest.Server {
157+
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
158+
t.Logf("Intercepted request: %s %s", req.Method, req.URL.String())
159+
t.Logf("Headers: %v", req.Header)
160+
161+
requestedIP := req.URL.String()[1:]
162+
163+
if response, exists := ipResponseMap[requestedIP]; exists {
164+
t.Logf("Matched IP: %s", requestedIP)
165+
rw.WriteHeader(http.StatusOK)
166+
_, _ = rw.Write(response)
167+
} else {
168+
t.Errorf("Unexpected IP: %s", requestedIP)
169+
rw.WriteHeader(http.StatusNotFound)
170+
_, _ = rw.Write([]byte(`{"error": "IP not found"}`))
171+
}
172+
}))
173+
}
174+
156175
func TestMultipleIpAddresses(t *testing.T) {
176+
mockServer := createMockAPIServer(t, map[string][]byte{caExampleIP: []byte(`CA`), chExampleIP: []byte(`CH`)})
177+
defer mockServer.Close()
178+
157179
cfg := createTesterConfig()
158180

159181
cfg.Countries = append(cfg.Countries, "CH")
182+
cfg.API = mockServer.URL + "/{ip}"
160183

161184
ctx := context.Background()
162185
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
@@ -181,9 +204,13 @@ func TestMultipleIpAddresses(t *testing.T) {
181204
}
182205

183206
func TestMultipleIpAddressesReverse(t *testing.T) {
207+
mockServer := createMockAPIServer(t, map[string][]byte{caExampleIP: []byte(`CA`), chExampleIP: []byte(`CH`)})
208+
defer mockServer.Close()
209+
184210
cfg := createTesterConfig()
185211

186212
cfg.Countries = append(cfg.Countries, "CH")
213+
cfg.API = mockServer.URL + "/{ip}"
187214

188215
ctx := context.Background()
189216
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
@@ -208,10 +235,14 @@ func TestMultipleIpAddressesReverse(t *testing.T) {
208235
}
209236

210237
func TestMultipleIpAddressesProxy(t *testing.T) {
238+
mockServer := createMockAPIServer(t, map[string][]byte{caExampleIP: []byte(`CA`)})
239+
defer mockServer.Close()
240+
211241
cfg := createTesterConfig()
212242

213243
cfg.Countries = append(cfg.Countries, "CA")
214244
cfg.XForwardedForReverseProxy = true
245+
cfg.API = mockServer.URL + "/{ip}"
215246

216247
ctx := context.Background()
217248
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
@@ -228,18 +259,24 @@ func TestMultipleIpAddressesProxy(t *testing.T) {
228259
t.Fatal(err)
229260
}
230261

231-
req.Header.Add(xForwardedFor, strings.Join([]string{caExampleIP, chExampleIP}, ","))
262+
forwardedForContent := strings.Join([]string{caExampleIP, chExampleIP}, ",")
263+
req.Header.Add(xForwardedFor, forwardedForContent)
264+
t.Logf("X-Forwarded-For header: %s", forwardedForContent)
232265

233266
handler.ServeHTTP(recorder, req)
234267

235268
assertStatusCode(t, recorder.Result(), http.StatusOK)
236269
}
237270

238271
func TestMultipleIpAddressesProxyReverse(t *testing.T) {
272+
mockServer := createMockAPIServer(t, map[string][]byte{chExampleIP: []byte(`CH`)})
273+
defer mockServer.Close()
274+
239275
cfg := createTesterConfig()
240276

241277
cfg.Countries = append(cfg.Countries, "CA")
242278
cfg.XForwardedForReverseProxy = true
279+
cfg.API = mockServer.URL + "/{ip}"
243280

244281
ctx := context.Background()
245282
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
@@ -256,7 +293,9 @@ func TestMultipleIpAddressesProxyReverse(t *testing.T) {
256293
t.Fatal(err)
257294
}
258295

259-
req.Header.Add(xForwardedFor, strings.Join([]string{chExampleIP, caExampleIP}, ","))
296+
forwardedForContent := strings.Join([]string{chExampleIP, caExampleIP}, ",")
297+
req.Header.Add(xForwardedFor, forwardedForContent)
298+
t.Logf("X-Forwarded-For header: %s", forwardedForContent)
260299

261300
handler.ServeHTTP(recorder, req)
262301

0 commit comments

Comments
 (0)