Skip to content

Commit

Permalink
Added Caddy IP address header parser, upgraded Go version, updated de…
Browse files Browse the repository at this point in the history
…pendencies.
  • Loading branch information
Kugelschieber committed Aug 26, 2024
1 parent 975f55c commit e1e2e35
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 44 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## Changelog

## 2.4.0

* added Caddy IP address header parser
* upgraded Go version
* updated dependencies

## 2.3.0

* removed DNT
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test deps docker release
.PHONY: test run deps docker release

test:
go test -cover -race github.com/pirsch-analytics/pirsch-go-proxy/pkg/proxy
go test -cover -race ./pkg/...

run:
go run cmd/main.go config/config.toml
Expand Down
3 changes: 2 additions & 1 deletion config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
# This configuration can be used to retreive the real client IP address and set accepted subnets for proxies and load balancers.
# Make sure you use the correct header if you are running the proxy behind another proxy or load balancer, otherwise the statistics will be inaccurate.
[network]
# Parsed in order, allowed values: CF-Connecting-IP, True-Client-IP, X-Forwarded-For, Forwarded, X-Real-IP
# Parsed in order, allowed values: CF-Connecting-IP, True-Client-IP, X-Forwarded-For, Forwarded, X-Real-IP, caddy
# The proxy will use the remote IP if no header is configured.
# For Caddy add "caddy" to the list. It's the same as X-Forwarded-For, but uses the first entry instead of the last.
header = ["CF-Connecting-IP", "True-Client-IP", "X-Forwarded-For", "Forwarded", "X-Real-IP"]

# List of allowed subnets (CIDR).
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/pirsch-analytics/pirsch-go-proxy

go 1.21
go 1.23

require (
github.com/BurntSushi/toml v1.4.0
github.com/emvi/logbuch v1.2.0
github.com/go-chi/chi/v5 v5.0.12
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/cors v1.2.1
github.com/klauspost/compress v1.17.8
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.3.0
github.com/klauspost/compress v1.17.9
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.4.0
github.com/stretchr/testify v1.9.0
)

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ github.com/emvi/logbuch v1.2.0 h1:Bw0jQH1Dbs+oIygZBNx/2Ub1igXRFtKQrIMRrZdVFJM=
github.com/emvi/logbuch v1.2.0/go.mod h1:hFxe0XQOFl76SkE/f0Pt5oQbXRZtyGa8EroBrrbQHuc=
github.com/emvi/null v1.3.1 h1:EQxrthMJnzpozGgKrcCepJmwrzA+UCk7ctBWOZLTW0Y=
github.com/emvi/null v1.3.1/go.mod h1:tEZhBSLFCucrJdwRentxoOLVRpTCy/3aIfiNKE2uuYU=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.3.0 h1:uZRvFSq3ZL8T0o6HoI0lr2+pl1RpORX7w7Xn+nuUSws=
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.3.0/go.mod h1:rsaEOobygzywGmIt+fJ4rkL5bJ20ElcBQOXpgOENbcI=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.4.0 h1:loPzF29uDuNPptr6KEKt3c+GeHv6GWjUmgqLK+djuz8=
github.com/pirsch-analytics/pirsch-go-sdk/v2 v2.4.0/go.mod h1:rsaEOobygzywGmIt+fJ4rkL5bJ20ElcBQOXpgOENbcI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
6 changes: 6 additions & 0 deletions pkg/proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func loadIPHeader(config *Config) {
}
}

if strings.ToLower(header) == "caddy" {
ipHeader = append(ipHeader, xForwardedForCaddy)
found = true
break
}

if !found {
logbuch.Fatal("Header invalid", logbuch.Fields{"header": header})
}
Expand Down
35 changes: 29 additions & 6 deletions pkg/proxy/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

var (
cfConnectingIP = headerParser{"CF-Connecting-IP", parseXForwardedForHeader}
trueClientIP = headerParser{"True-Client-IP", parseXForwardedForHeader}
xForwardedFor = headerParser{"X-Forwarded-For", parseXForwardedForHeader}
forwarded = headerParser{"Forwarded", parseForwardedHeader}
xRealIP = headerParser{"X-Real-IP", parseXRealIPHeader}
allIPHeader = []headerParser{
cfConnectingIP = headerParser{"CF-Connecting-IP", parseXForwardedForHeader}
trueClientIP = headerParser{"True-Client-IP", parseXForwardedForHeader}
xForwardedFor = headerParser{"X-Forwarded-For", parseXForwardedForHeader}
forwarded = headerParser{"Forwarded", parseForwardedHeader}
xRealIP = headerParser{"X-Real-IP", parseXRealIPHeader}
xForwardedForCaddy = headerParser{"X-Forwarded-For", parseXForwardedForHeader}
allIPHeader = []headerParser{
cfConnectingIP,
trueClientIP,
xForwardedFor,
Expand Down Expand Up @@ -119,6 +120,28 @@ func parseXForwardedForHeader(value string) string {
return ""
}

func parseXForwardedForHeaderFirst(value string) string {
parts := strings.Split(value, ",")

if len(parts) > 1 {
ip := parts[0]

if strings.Contains(ip, ":") {
host, _, err := net.SplitHostPort(ip)

if err != nil {
return ip
}

return strings.TrimSpace(host)
}

return strings.TrimSpace(ip)
}

return ""
}

func parseXRealIPHeader(value string) string {
value = cleanIP(strings.TrimSpace(value))

Expand Down
9 changes: 9 additions & 0 deletions pkg/proxy/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ func TestParseXForwardedForHeader(t *testing.T) {
}
}

func TestParseXForwardedForHeaderCaddy(t *testing.T) {
assert.Equal(t, "2003:e1:7f03:8893:5f5e:3681:6c1f:b086", parseXForwardedForHeaderFirst("2003:e1:7f03:8893:5f5e:3681:6c1f:b086, 10.0.1.254"))
assert.Equal(t, "2003:e1:7f03:8893:5f5e:3681:6c1f:b086", parseXForwardedForHeaderFirst("[2003:e1:7f03:8893:5f5e:3681:6c1f:b086]:9876, 10.0.1.254"))
assert.Equal(t, "123.245.221.9", parseXForwardedForHeaderFirst("123.245.221.9:9876, 10.0.1.254"))
assert.Equal(t, "87.171.210.182", parseXForwardedForHeaderFirst("87.171.210.182, 10.0.1.7"))
assert.Empty(t, parseXForwardedForHeaderFirst("2003:e1:7f0c:5700:1e36:e95f:7ef1:401b"))
assert.Empty(t, parseXForwardedForHeaderFirst("123.245.221.9"))
}

func TestParseXRealIPHeader(t *testing.T) {
header := []string{
"",
Expand Down
12 changes: 6 additions & 6 deletions vendor/github.com/go-chi/chi/v5/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions vendor/github.com/go-chi/chi/v5/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions vendor/github.com/klauspost/compress/flate/matchlen_amd64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions vendor/github.com/klauspost/compress/zstd/dict.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions vendor/github.com/klauspost/compress/zstd/matchlen_amd64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/github.com/pirsch-analytics/pirsch-go-sdk/v2/pkg/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e1e2e35

Please sign in to comment.