-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
104 lines (83 loc) · 2.28 KB
/
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
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
func createProxies() *http.ServeMux {
mux := http.NewServeMux()
for path, hostconfig := range chaosConfig.Paths {
log.Info().Println(path, "->", hostconfig.Host)
url, err := url.Parse(hostconfig.Host)
if err != nil {
log.Error().Println(err, "parsing", hostconfig.Host)
continue
}
p := httputil.NewSingleHostReverseProxy(url)
p.ErrorHandler = errorHandler
p.ModifyResponse = modifyResponse
mux.Handle(path, p)
}
if url, err := url.Parse(chaosConfig.DefaultHost.Host); err != nil {
log.Error().Println(err, "parsing", chaosConfig.DefaultHost.Host)
} else {
p := httputil.NewSingleHostReverseProxy(url)
p.ErrorHandler = errorHandler
p.ModifyResponse = modifyResponse
mux.Handle("/", p)
}
return mux
}
func errorHandler(w http.ResponseWriter, r *http.Request, e error) {
if e.Error() == "HUP" {
hj, ok := w.(http.Hijacker)
if !ok {
log.Error().Println("Connection could not be hijacked")
w.WriteHeader(400)
return
}
conn, _, err := hj.Hijack()
if err != nil {
log.Error().Println("Error hijacking connection", err)
w.WriteHeader(400)
return
}
conn.Close()
log.Info().Println("Connection closed for", r.URL.String())
} else if e.Error() == "5xx" {
if s, ok := e.(random5xx); ok {
statusCode := s.status()
w.WriteHeader(statusCode)
log.Info().Println("Sending status", statusCode, "for", r.URL.String())
} else {
log.Error().Println("errorHandler: Cannot type-cast random5xx error")
w.WriteHeader(http.StatusBadGateway)
}
} else if e.Error() == "TIMEOUT" {
w.WriteHeader(http.StatusGatewayTimeout)
} else {
log.Error().Printf("http: proxy error: %v", e)
w.WriteHeader(http.StatusBadGateway)
}
}
func modifyResponse(res *http.Response) error {
dice := rollDices()
switch dice.kind() {
case "HUP":
return fmt.Errorf("HUP")
case "5xx":
if s, ok := dice.(random5xx); ok {
return s
}
log.Error().Println("modifyResponse: Cannot type-cast random5xx error")
case "DELAY":
if r, ok := dice.(delay); ok {
r.wait(res.Request.URL.String())
return fmt.Errorf("TIMEOUT")
}
log.Error().Println("modifyResponse: Cannot type-cast delay error")
}
log.Info().Println("Passing through for", res.Request.URL.String())
return nil
}