-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
141 lines (139 loc) · 3.47 KB
/
context.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package middleware
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"strings"
)
func BuildContextWithMask(next http.Handler, mask func(fieldName, s string) string) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
var ctx context.Context
ctx = r.Context()
if len(fieldConfig.Ip) > 0 {
ip := getRemoteIp(r)
ctx = context.WithValue(ctx, fieldConfig.Ip, ip)
}
if fieldConfig.Constants != nil && len(fieldConfig.Constants) > 0 {
for k, e := range fieldConfig.Constants {
if len(e) > 0 {
ctx = context.WithValue(ctx, k, e)
}
}
}
if fieldConfig.Headers != nil && len(fieldConfig.Headers) > 0 {
for k, e := range fieldConfig.Headers {
if len(e) > 0 {
header := r.Header.Get(e)
ctx = context.WithValue(ctx, k, header)
}
}
}
if fieldConfig.Map != nil && len(fieldConfig.Map) > 0 && r.Body != nil && (r.Method != "GET" || r.Method != "DELETE") {
buf := new(bytes.Buffer)
buf.ReadFrom(r.Body)
r.Body = io.NopCloser(buf)
var v interface{}
er2 := json.NewDecoder(strings.NewReader(buf.String())).Decode(&v)
if er2 != nil {
if len(fieldConfig.Ip) == 0 && fieldConfig.Constants == nil {
next.ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r.WithContext(ctx))
}
} else {
m, ok := v.(map[string]interface{})
if !ok {
if len(fieldConfig.Ip) == 0 && fieldConfig.Constants == nil {
next.ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r.WithContext(ctx))
}
} else {
for k, e := range fieldConfig.Map {
if strings.Index(e, ".") >= 0 {
v3 := ValueOf(v, e)
if v3 != nil {
s3, ok3 := v3.(string)
if ok3 {
if len(s3) > 0 {
if mask != nil && fieldConfig.Masks != nil && len(fieldConfig.Masks) > 0 {
if Include(fieldConfig.Masks, k) {
ctx = context.WithValue(ctx, k, mask(k, s3))
} else {
ctx = context.WithValue(ctx, k, s3)
}
} else {
ctx = context.WithValue(ctx, k, s3)
}
}
} else {
ctx = context.WithValue(ctx, k, v3)
}
}
} else {
x, ok2 := m[e]
if ok2 && x != nil {
s3, ok3 := x.(string)
if ok3 {
if len(s3) > 0 {
if mask != nil && fieldConfig.Masks != nil && len(fieldConfig.Masks) > 0 {
if Include(fieldConfig.Masks, k) {
ctx = context.WithValue(ctx, k, mask(k, s3))
} else {
ctx = context.WithValue(ctx, k, s3)
}
} else {
ctx = context.WithValue(ctx, k, s3)
}
}
} else {
ctx = context.WithValue(ctx, k, x)
}
}
}
}
next.ServeHTTP(w, r.WithContext(ctx))
}
}
} else {
if len(fieldConfig.Ip) == 0 && fieldConfig.Constants == nil && fieldConfig.Headers == nil {
next.ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r.WithContext(ctx))
}
}
}
return http.HandlerFunc(fn)
}
func BuildContext(next http.Handler) http.Handler {
return BuildContextWithMask(next, nil)
}
func Include(vs []string, v string) bool {
for _, s := range vs {
if v == s {
return true
}
}
return false
}
func ValueOf(m interface{}, path string) interface{} {
arr := strings.Split(path, ".")
i := 0
var c interface{}
c = m
l1 := len(arr) - 1
for i < len(arr) {
key := arr[i]
m2, ok := c.(map[string]interface{})
if ok {
c = m2[key]
}
if !ok || i >= l1 {
return c
}
i++
}
return c
}