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

remote peer address is lost in ctx - always resolves to localhost #173

Closed
mikeatlas opened this issue Jun 3, 2016 · 2 comments
Closed

Comments

@mikeatlas
Copy link

mikeatlas commented Jun 3, 2016

Since grpc-gateway creates a HTTP REST service that then subsequently calls a gRPC client, the loopback network hop causes all requests at the gRPC service to appear to originate from ::1 (localhost in IPv6).

Example gRPC service:

func (r *myService) MyRPCEndpoint(
    ctx context.Context, msg *grpcapi.Message) (*grpcapi.Message, error) {
    p, _ := peer.FromContext(ctx)
    remoteAddr := p.Addr.String() // ALWAYS localhost when incoming from grp-gateway :(
        // ... etc
}

It would be nice if X-Forwarded-For (or the newer Forwarded RFC header) were populated with the original external address, but since gRPC is not another HTTP endpoint, we need some other context attribute set such as ProxyRemoteAddr (or whatever) in the forwarded gRPC request.

I may make a PR for this as it's something I need right away, and don't see an obvious workaround.

@mikeatlas
Copy link
Author

mikeatlas commented Jun 3, 2016

One way I've found is to inject the HTTP req.RemoteAddr into the ctx:

ctx = metadata.NewContext(ctx, metadata.Pairs("RemoteAddr", req.RemoteAddr))

Then, in the server side gRPC service method:

md, _ := metadata.FromContext(ctx)

// this returns a IP string of "[1.2.3.4:5678]"
remoteAddr := md["remoteaddr"] //  - why the "[ ]" wrap??

So, it's doable, kind of ugly but not terrible on the gRPC server side. Any thoughts?

@mikeatlas
Copy link
Author

mikeatlas commented Jun 3, 2016

Pull request attached ✅

@yugui yugui closed this as completed Jun 6, 2016
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