Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClientIP() not parse 'X-Forwarded-For' #1263

Closed
Gaojianli opened this issue Jan 20, 2025 · 0 comments · Fixed by #1264
Closed

ClientIP() not parse 'X-Forwarded-For' #1263

Gaojianli opened this issue Jan 20, 2025 · 0 comments · Fixed by #1264
Assignees

Comments

@Gaojianli
Copy link

Describe the bug
ctx.ClientIP() can't parse X-Forwarded-For while using Unix Domain Sock.

To Reproduce

import (
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/config"
)

func main() {
	options := []config.Option{
		server.WithHostPorts("ingress.sock"),
		server.WithNetwork("unix"),
	}
	h := server.Default(options...)
	h.GET("/hello", func(c context.Context, ctx *app.RequestContext) {
		clientIp := ctx.ClientIP()
		ctx.JSON(200, map[string]any{
			"header":    ctx.Request.Header.Get("X-Forwarded-For"),
			"client_ip": clientIp,
		})
	})
	h.Spin()
}

run socat to emulate a nginx:

socat TCP-LISTEN:12345 UNIX-CONNECT:ingress.sock

Expected behavior

header = client_ip in the response body.

Screenshots

Image

Hertz version:

v0.9.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants