How to get client ip from request metadata with grpc-java

Viewed 15112

I use grpc-java and get the metadata by ServerInterceptor, but i get below information without client ip address, the authority is server ip, how can i get the client ip?Thanks.

Metadata({:scheme=[http], :method=[POST], :path=[/test/test1], :authority=[192.168.199.9:50051], grpc-encoding=[identity], grpc-accept-encoding=[identity,deflate,gzip], te=[trailers], content-type=[application/grpc], user-agent=[grpc-objc/0.13.0 grpc-c/0.13.0 (ios)]})

4 Answers

if in python grpc, you will get client ip address and port using context.peer().

def your_method(self, request, context):
    ...
    context.peer() # return 'ipv4:49.123.106.100:44420'
    ...

Client IP is exposed as gRPC attributes (TRANSPORT_ATTR_REMOTE_ADDR), an example of accessing it is here

 String inetSocketString = serverCallCapture.get().getAttributes()
    .get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString();
Related