Skip to content

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

@Gaojianli

Description

@Gaojianli

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions