7
votes

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

4 Answers

7
votes

Client IP is not provided in Metadata. But you can call ServerCall.attributes() and get the Grpc.TRANSPORT_ATTR_REMOTE_ADDR.

Please note that the API is unstable and may change.

3
votes

In latest (1.2.0) gRPC use io.grpc.Grpc.TRANSPORT_ATTR_REMOTE_ADDR attribute in interceptor to get remote address.

3
votes

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'
    ...
0
votes

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();