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

X-Forwarded-For support #37

Open
sandersaares opened this issue Oct 24, 2018 · 2 comments
Open

X-Forwarded-For support #37

sandersaares opened this issue Oct 24, 2018 · 2 comments

Comments

@sandersaares
Copy link

I request support for X-Forwarded-For style information about the original client (I wish to know the IP address). I see a comment in the code also expressing this as a todo:

// TODO(mwitkow): Add a forwarded header to metadata, https://en.wikipedia.org/wiki/X-Forwarded-For.

@ghost
Copy link

ghost commented May 17, 2019

I have this in my interceptor:

mdIn, _ := metadata.FromIncomingContext(ctx)

		// requests going through the grpc gateway will have "pipe" as peer address.
		// so we will first look for grpc headers, if they are not present we consider
		// the request as direct gRPC request and take address from the peer.
		// if we cannot determine the hostname, we will return error.
		var hostname string

		if headerVals := mdIn.Get("x-forwarded-for"); len(headerVals) > 0 {
			hostname = headerVals[0]
		}

		if hostname == "" {
			if p, ok := peer.FromContext(ctx); ok {
				hostname = p.Addr.String()
			}
		}

		if hostname == "" {
			return nil, status.Error(codes.Unauthenticated, "no peer found")
		}

		// remove port from the hostname
		if host, _, err := net.SplitHostPort(hostname); err == nil {
			hostname = host
		}

should be just a matter of adding the forwarded header and calling metadata.NewIncomingContext

@vgough
Copy link

vgough commented Dec 8, 2019

FYI, x-forwarded-for is supported in the fork https://github.com/vgough/grpc-proxy

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

No branches or pull requests

2 participants