Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

grpc

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)]})

like image 702
Wayne Avatar asked Mar 21 '16 06:03

Wayne


3 Answers

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.

like image 112
Eric Anderson Avatar answered Oct 19 '22 04:10

Eric Anderson


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

like image 5
odiszapc Avatar answered Oct 19 '22 04:10

odiszapc


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'
    ...
like image 5
Eric Mo Avatar answered Oct 19 '22 02:10

Eric Mo