Skip to content

Commit

Permalink
feat: ✨ Add caddy client_ip variable parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienyhuel authored Dec 17, 2024
1 parent e2e2d4f commit d7a1ee8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/corazawaf/coraza/v3/types"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)

// Copied from https://github.com/corazawaf/coraza/blob/main/http/middleware.go
Expand All @@ -27,6 +28,16 @@ func processRequest(tx types.Transaction, req *http.Request) (*types.Interruptio
client = req.RemoteAddr[:idx]
cport, _ = strconv.Atoi(req.RemoteAddr[idx+1:])
}
address := caddyhttp.GetVar(req.Context(), caddyhttp.ClientIPVarKey).(string)
clientIp, clientPort, _ := net.SplitHostPort(address)
if clientIp != "" {
client = clientIp
} else if address != "" {
client = address
}
if clientPort != "" {
cport, _ = strconv.Atoi(clientPort)
}

var in *types.Interruption
// There is no socket access in the request object, so we neither know the server client nor port.
Expand Down

0 comments on commit d7a1ee8

Please sign in to comment.