-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbg_handler.go
218 lines (202 loc) · 5.5 KB
/
dbg_handler.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"strconv"
"strings"
)
var oidMap = map[string]string{
"2.5.4.3": "CN",
"2.5.4.5": "SN",
"2.5.4.6": "C",
"2.5.4.7": "L",
"2.5.4.8": "S",
"2.5.4.10": "O",
"2.5.4.11": "OU",
"1.2.840.113549.1.9.1": "eMail",
}
var (
errNotMap = errors.New("value not a Map")
errNotSlice = errors.New("value not a Slice")
)
func parseJSONPath(s string) (path []interface{}) {
for _, p := range strings.Split(s, ".") {
if strings.HasSuffix(p, "]") {
if idx := strings.LastIndex(p, "["); idx != -1 {
if arrayIdx, err := strconv.Atoi(p[idx+1 : len(p)-1]); err == nil {
path = append(path, p[:idx])
path = append(path, arrayIdx)
continue
}
}
}
path = append(path, p)
}
return path
}
func extractJSONPath(source interface{}, path []interface{}) (out interface{}, err error) {
out = source
for _, p := range path {
switch pt := p.(type) {
case string:
switch reflect.TypeOf(out).Kind() {
case reflect.Map:
out = reflect.ValueOf(out).MapIndex(reflect.ValueOf(pt)).Interface()
default:
return source, errNotMap
}
case int:
switch reflect.TypeOf(out).Kind() {
case reflect.Slice:
out = reflect.ValueOf(out).Index(pt).Interface()
default:
return source, errNotSlice
}
}
if out == nil {
break
}
}
return out, nil
}
func init() {
addProtocolHandler("debug", newDebugHandler)
}
func newDebugHandler(urlPath, params string, cfg *serverConfig) (http.Handler, error) {
opts := parseOptString(params)
if opts.IsTrue("json", false) {
var path []interface{}
if pathStr, have := opts["path"]; have {
path = parseJSONPath(pathStr)
}
return &JSONDebugHandler{
tlsCS: opts.IsTrue("tls-cs", false),
headers: opts.IsTrue("hdr", true),
authRoles: opts.IsTrue("auth", true),
path: path,
}, nil
}
return &DebugHandler{}, nil
}
type JSONDebugHandler struct {
tlsCS bool
headers bool
authRoles bool
path []interface{}
}
func (jDbg *JSONDebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var out interface{}
out = map[string]interface{}{}
outMap := out.(map[string]interface{})
if r.TLS != nil {
outMap["tls"] = map[string]interface{}{
"version": r.TLS.Version,
"cipher": r.TLS.CipherSuite,
"resumed": r.TLS.DidResume,
"sni": r.TLS.ServerName,
}
if len(r.TLS.PeerCertificates) > 0 {
peers := make([]interface{}, 0)
for _, cert := range r.TLS.PeerCertificates {
h := sha256.New()
h.Write(cert.Raw)
peerInfo := map[string]interface{}{
"sub": cert.Subject.ToRDNSequence().String(),
"iss": cert.Issuer.ToRDNSequence().String(),
"fp": hex.EncodeToString(h.Sum(nil)),
"sig": cert.Signature,
}
if cert.Subject.CommonName != "" {
peerInfo["cn"] = cert.Subject.CommonName
}
for _, attr := range cert.Subject.Names {
if oidName := attr.Type.String(); oidMap[oidName] == "eMail" {
peerInfo["email"] = attr.Value
}
}
peerInfo["exp"] = cert.NotAfter.Unix()
peerInfo["iat"] = cert.NotBefore.Unix()
peers = append(peers, peerInfo)
}
outMap["tls"].(map[string]interface{})["peers"] = peers
}
if jDbg.tlsCS {
outMap["tls"].(map[string]interface{})["cs"] = r.TLS
}
}
outMap["remote"] = r.RemoteAddr
outMap["method"] = r.Method
outMap["host"] = r.Host
outMap["uri"] = r.RequestURI
outMap["proto"] = r.Proto
if jDbg.headers {
outMap["headers"] = r.Header
}
if jDbg.authRoles {
if auth := r.Context().Value(authRoleContext); auth != nil {
outMap["auth-role"] = auth
}
}
if jDbg.path != nil {
var err error
if out, err = extractJSONPath(out, jDbg.path); err != nil {
logf(r, logLevelWarning, "cannot extract path %#v from %#v: %s", jDbg.path, out, err)
out = nil
}
}
outBytes, err := json.Marshal(out)
if err != nil {
logf(r, logLevelError, "could not marshal debug request (%#v): %s", out, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(outBytes)
}
type DebugHandler struct {
}
// DebugRequest returns debugging information to client
func (dbg *DebugHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
hdrs := make([]string, 0)
for k, v := range r.Header {
for _, vv := range v {
hdrs = append(hdrs, fmt.Sprintf("%s: %s", k, vv))
}
}
metaInfo := []string{fmt.Sprintf("remote=%v", r.RemoteAddr)}
if auth := r.Context().Value(authRoleContext); auth != nil {
metaInfo = append(metaInfo, fmt.Sprintf("auth-role=%#v", auth))
}
if r.TLS != nil {
metaInfo = append(metaInfo, fmt.Sprintf("SSL=0x%04x verified=%d", r.TLS.Version, len(r.TLS.VerifiedChains)))
for _, crt := range r.TLS.PeerCertificates {
subjectName := make([]string, 0)
for _, attr := range crt.Subject.Names {
attrName := attr.Type.String()
if s := oidMap[attrName]; s != "" {
attrName = s
}
subjectName = append(subjectName, fmt.Sprintf("%s=%s", attrName, attr.Value))
}
h := sha256.New()
h.Write(crt.Raw)
metaInfo = append(metaInfo,
fmt.Sprintf("\n# %s %s", hex.EncodeToString(h.Sum(nil)), strings.Join(subjectName, "/")))
}
}
fmt.Fprintf(w, `# %s
%v %v %v
%v
`, strings.Join(metaInfo, " "), r.Method, r.RequestURI, r.Proto, strings.Join(hdrs, "\n"))
if r.ContentLength > 0 {
bodyData := make([]byte, r.ContentLength)
r.Body.Read(bodyData)
w.Write(bodyData)
}
}