-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package grpcx | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
// GetIncomingMetaData get grpc incoming metadata from grpc context | ||
func GetIncomingMetaData(ctx context.Context) (metadata.MD, bool) { | ||
return metadata.FromIncomingContext(ctx) | ||
} | ||
|
||
// GetMetaDataFirst get metadata first value by key | ||
func GetMetaDataFirst(md metadata.MD, key string) string { | ||
if md == nil { | ||
return "" | ||
} | ||
|
||
if v := md.Get(key); len(v) > 0 { | ||
return v[0] | ||
} | ||
return "" | ||
} | ||
|
||
// GetUserAgent get user agent from grpc context | ||
func GetUserAgent(ctx context.Context) string { | ||
md, _ := GetIncomingMetaData(ctx) | ||
return GetMetaDataFirst(md, "user-agent") | ||
} | ||
|
||
// GetRealIP get user ip from grpc context | ||
func GetRealIP(ctx context.Context) string { | ||
md, _ := GetIncomingMetaData(ctx) | ||
xff := GetMetaDataFirst(md, "x-forwarded-for") | ||
if i := strings.IndexAny(xff, ","); i > 0 { | ||
return strings.TrimSpace(xff[:i]) | ||
} | ||
|
||
if xrip := GetMetaDataFirst(md, "x-real-ip"); len(xrip) > 0 { | ||
return xrip | ||
} | ||
|
||
return "" | ||
} |