Skip to content

Improve client ip behavior - #3

Merged
ghosind merged 2 commits into
go-amwk:mainfrom
ghosind:feat/client-ip-improve
Jul 20, 2026
Merged

Improve client ip behavior#3
ghosind merged 2 commits into
go-amwk:mainfrom
ghosind:feat/client-ip-improve

Conversation

@ghosind

@ghosind ghosind commented Jul 20, 2026

Copy link
Copy Markdown
Member

This PR introduces the following changes:

  • Update Context.ClientIP() to return the direct TCP IP address.
  • Add ClientIPs to parse XFF and X-Real-IP from the request headers.

- 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>
@ghosind
ghosind requested a review from Copilot July 20, 2026 07:28
@ghosind ghosind added the enhancement New feature or request label Jul 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the ClientIPs() 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 thread context.go
Comment on lines +159 to +163
ips := make([]string, 0)

xff := ctx.Header("X-Forwarded-For")
if xff != "" {
parts := strings.Split(xff, ",")
Comment thread context.go
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:
//
Comment thread context_test.go
Signed-off-by: Chen Su <ghosind@gmail.com>
@ghosind
ghosind merged commit f410d03 into go-amwk:main Jul 20, 2026
9 checks passed
@ghosind
ghosind deleted the feat/client-ip-improve branch July 24, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants