-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
70 lines (64 loc) · 1.43 KB
/
header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package grpcex
import (
"context"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/peer"
)
const AllowedHeaderPrefix = "x-"
// DefaultHeaderMatcher allows header with "x-" prefix
func DefaultHeaderMatcher(key string) (string, bool) {
newKey, allowed := runtime.DefaultHeaderMatcher(key)
if allowed {
return newKey, allowed
}
key = strings.ToLower(key)
if strings.HasPrefix(key, AllowedHeaderPrefix) {
return runtime.MetadataPrefix + key, true
}
return "", false
}
// GetHeader gets header from GRPC request header or HTTP converted header
func GetHeader(ctx context.Context, key string) string {
var value string
key = strings.ToLower(key)
meta, _ := metadata.FromIncomingContext(ctx)
values, ok := meta[key]
if ok {
if len(values) > 0 {
value = values[0]
}
return value
}
key = runtime.MetadataPrefix + key
values, ok = meta[key]
if ok {
if len(values) > 0 {
value = values[0]
}
}
return value
}
// GetClientIP gets client IP from HTTP forward header or GRPC context
func GetClientIP(ctx context.Context) string {
ip := ""
meta, ok := metadata.FromIncomingContext(ctx)
if ok {
ips := meta.Get("x-forwarded-for")
if len(ips) > 0 {
ip = ips[0]
}
comma := strings.IndexRune(ip, ',')
if comma > 0 {
ip = ip[:comma]
}
}
if ip == "" {
p, ok := peer.FromContext(ctx)
if ok {
ip = p.Addr.String()
}
}
return ip
}