Improve client ip behavior - #3
Merged
Merged
Conversation
- Update ClientIP to return direct TCP IP address. - Add ClientIPs to parse XFF and X-Real-IP header. Signed-off-by: Chen Su <ghosind@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR changes how client IP information is surfaced from Context: ClientIP() now reports only the direct TCP peer IP, while a new ClientIPs() method aggregates proxy header information (X-Forwarded-For / X-Real-IP) plus the direct connection IP for callers that need the full chain.
Changes:
- Updated
Context.ClientIP()semantics to return the direct connection peer IP (ignoring proxy headers). - Added
Context.ClientIPs()to return an ordered list derived from X-Forwarded-For, X-Real-IP, and the direct connection IP. - Expanded context tests to validate the new
ClientIP()behavior and theClientIPs()aggregation logic.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| context.go | Changes ClientIP() semantics and introduces ClientIPs() aggregation API. |
| context_test.go | Updates/expands tests to reflect new ClientIP() behavior and cover ClientIPs() scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+159
to
+163
| ips := make([]string, 0) | ||
|
|
||
| xff := ctx.Header("X-Forwarded-For") | ||
| if xff != "" { | ||
| parts := strings.Split(xff, ",") |
Comment on lines
+149
to
+152
| // ClientIPs collects all available client IP information from the request, | ||
| // combining proxy headers with the direct connection IP. The returned slice | ||
| // is deduplicated and preserves the following priority order: | ||
| // |
Signed-off-by: Chen Su <ghosind@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces the following changes: