-
Notifications
You must be signed in to change notification settings - Fork 680
/
http.go
216 lines (180 loc) · 4.77 KB
/
http.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
package services
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
// HTTP server object (all fields are required).
type HTTP struct {
Port int16
Backend string
SecurePort int16
SecureBackend string
Cert string
Key string
TLSVersion string
}
func getTLSVersion(state *tls.ConnectionState) string {
switch state.Version {
case tls.VersionTLS10:
return "v1.0"
case tls.VersionTLS11:
return "v1.1"
case tls.VersionTLS12:
return "v1.2"
// TLS v1.3 is experimental.
case 0x0304:
return "v1.3"
default:
return "unknown"
}
}
// Start initializes the HTTP server.
func (h *HTTP) Start() <-chan bool {
log.Printf("HTTP: %s listening on %d/%d", h.Backend, h.Port, h.SecurePort)
mux := http.NewServeMux()
mux.HandleFunc("/", h.handler)
exited := make(chan bool)
go func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", h.Port), mux))
close(exited)
}()
go func() {
s := &http.Server{
Addr: fmt.Sprintf(":%v", h.SecurePort),
Handler: mux,
}
log.Fatal(s.ListenAndServeTLS(h.Cert, h.Key))
close(exited)
}()
return exited
}
// Helpers
func lower(m map[string][]string) (result map[string][]string) {
result = make(map[string][]string)
for k, v := range m {
result[strings.ToLower(k)] = v
}
return result
}
func (h *HTTP) handler(w http.ResponseWriter, r *http.Request) {
// Assume we're the clear side of the world.
backend := h.Backend
conntype := "CLR"
var request = make(map[string]interface{})
var url = make(map[string]interface{})
request["url"] = url
url["fragment"] = r.URL.Fragment
url["host"] = r.URL.Host
url["opaque"] = r.URL.Opaque
url["path"] = r.URL.Path
url["query"] = r.URL.Query()
url["rawQuery"] = r.URL.RawQuery
url["scheme"] = r.URL.Scheme
if r.URL.User != nil {
url["username"] = r.URL.User.Username()
pw, ok := r.URL.User.Password()
if ok {
url["password"] = pw
}
}
request["method"] = r.Method
request["headers"] = lower(r.Header)
request["host"] = r.Host
var tlsrequest = make(map[string]interface{})
request["tls"] = tlsrequest
tlsrequest["enabled"] = r.TLS != nil
if r.TLS != nil {
// We're the secure side of the world, I guess.
backend = h.SecureBackend
conntype = "TLS"
tlsrequest["negotiated-protocol"] = r.TLS.NegotiatedProtocol
tlsrequest["server-name"] = r.TLS.ServerName
tlsrequest["negotiated-protocol-version"] = getTLSVersion(r.TLS)
}
// respond with the requested status
status := r.Header.Get("Requested-Status")
if status == "" {
status = "200"
}
statusCode, err := strconv.Atoi(status)
if err != nil {
log.Print(err)
statusCode = 500
}
// copy the requested headers into the response
headers, ok := r.Header["Requested-Header"]
if ok {
for _, header := range headers {
canonical := http.CanonicalHeaderKey(header)
value, ok := r.Header[canonical]
if ok {
w.Header()[canonical] = value
}
}
}
if b, _ := ioutil.ReadAll(r.Body); b != nil {
body := string(b)
log.Printf("received body: %s", body)
w.Header()[http.CanonicalHeaderKey("Auth-Request-Body")] = []string{body}
}
defer r.Body.Close()
cookies, ok := r.Header["Requested-Cookie"]
if ok {
for _, v := range strings.Split(cookies[0], ",") {
val := strings.Trim(v, " ")
http.SetCookie(w, &http.Cookie{
Name: val,
Value: val,
})
}
}
// If they asked for a specific location to be returned, handle that too.
location, ok := r.Header["Requested-Location"]
if ok {
w.Header()[http.CanonicalHeaderKey("Location")] = location
}
addExtauth := os.Getenv("INCLUDE_EXTAUTH_HEADER")
if len(addExtauth) > 0 {
extauth := make(map[string]interface{})
extauth["request"] = request
extauth["resp_headers"] = lower(w.Header())
eaJSON, err := json.Marshal(extauth)
if err != nil {
eaJSON = []byte(fmt.Sprintf("err: %v", err))
}
eaArray := make([]string, 1, 1)
eaArray[0] = string(eaJSON)
w.Header()[http.CanonicalHeaderKey("extauth")] = eaArray
}
// Check header and delay response.
if h, ok := r.Header["Requested-Backend-Delay"]; ok {
if v, err := strconv.Atoi(h[0]); err == nil {
log.Printf("Delaying response by %v ms", v)
time.Sleep(time.Duration(v) * time.Millisecond)
}
}
// Set date response header.
w.Header().Set("Date", time.Now().Format(time.RFC1123))
w.WriteHeader(statusCode)
// Write out all request/response information
var response = make(map[string]interface{})
response["headers"] = lower(w.Header())
var body = make(map[string]interface{})
body["backend"] = backend
body["request"] = request
body["response"] = response
b, err := json.MarshalIndent(body, "", " ")
if err != nil {
b = []byte(fmt.Sprintf("Error: %v", err))
}
log.Printf("%s (%s): writing response HTTP %v", backend, conntype, statusCode)
w.Write(b)
}