7
7
"google.golang.org/protobuf/reflect/protoreflect"
8
8
)
9
9
10
- func (s * CGrpcServer ) getcontext (c context.Context , path string , peername string , peeraddr string , metadata map [string ]string , handlers []OutsideHandler , d func (interface {}) error ) * Context {
10
+ func (s * CGrpcServer ) getcontext (c context.Context , path string , peername string , remoteaddr string , metadata map [string ]string , handlers []OutsideHandler , d func (interface {}) error ) * Context {
11
11
ctx , ok := s .ctxpool .Get ().(* Context )
12
12
if ! ok {
13
13
ctx = & Context {
@@ -16,7 +16,7 @@ func (s *CGrpcServer) getcontext(c context.Context, path string, peername string
16
16
handlers : handlers ,
17
17
path : path ,
18
18
peername : peername ,
19
- peeraddr : peeraddr ,
19
+ remoteaddr : remoteaddr ,
20
20
metadata : metadata ,
21
21
resp : nil ,
22
22
e : nil ,
@@ -32,7 +32,7 @@ func (s *CGrpcServer) getcontext(c context.Context, path string, peername string
32
32
ctx .handlers = handlers
33
33
ctx .path = path
34
34
ctx .peername = peername
35
- ctx .peeraddr = peeraddr
35
+ ctx .remoteaddr = remoteaddr
36
36
if metadata != nil {
37
37
ctx .metadata = metadata
38
38
}
@@ -54,7 +54,7 @@ type Context struct {
54
54
handlers []OutsideHandler
55
55
path string
56
56
peername string
57
- peeraddr string
57
+ remoteaddr string
58
58
metadata map [string ]string
59
59
resp interface {}
60
60
e * cerror.Error
@@ -96,8 +96,17 @@ func (c *Context) GetPath() string {
96
96
func (c * Context ) GetPeerName () string {
97
97
return c .peername
98
98
}
99
- func (c * Context ) GetPeerAddr () string {
100
- return c .peeraddr
99
+
100
+ //get the direct peer's addr(maybe a proxy)
101
+ func (c * Context ) GetRemoteAddr () string {
102
+ return c .remoteaddr
103
+ }
104
+
105
+ //this function try to return the first caller's ip(mostly time it will be the user's ip)
106
+ //if can't get the first caller's ip,try to return the real peer's ip which will not be confused by proxy
107
+ //if failed,the direct peer's ip will be returned(maybe a proxy)
108
+ func (c * Context ) GetClientIp () string {
109
+ return c .metadata ["Client-IP" ]
101
110
}
102
111
func (c * Context ) GetMetadata () map [string ]string {
103
112
return c .metadata
0 commit comments