diff --git a/api.go b/api.go index 35be6df1..9252200d 100644 --- a/api.go +++ b/api.go @@ -2,6 +2,7 @@ package toxiproxy import ( "encoding/json" + "html/template" "fmt" "log" "net" @@ -49,7 +50,7 @@ func (server *ApiServer) PopulateConfig(filename string) { func StopBrowsersMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if strings.HasPrefix(r.UserAgent(), "Mozilla/") { + if strings.HasPrefix(r.UserAgent(), "Mozilla/") && r.URL.Path != "/dashboard" { http.Error(w, "User agent not allowed", 403) } else { h.ServeHTTP(w, r) @@ -74,6 +75,8 @@ func (server *ApiServer) Listen(host string, port string) { r.HandleFunc("/version", server.Version).Methods("GET") + r.HandleFunc("/dashboard", server.Dashboard).Methods("GET") + http.Handle("/", StopBrowsersMiddleware(r)) logrus.WithFields(logrus.Fields{ @@ -389,6 +392,16 @@ func (server *ApiServer) Version(response http.ResponseWriter, request *http.Req } } +func (server *ApiServer) Dashboard(response http.ResponseWriter, request *http.Request) { + var dashboardData []proxyToxics + for _, proxyName := range server.Collection.Proxies() { + dashboardData = append(dashboardData, proxyWithToxics(proxyName)) + } + + tmpl := template.Must(template.ParseFiles("templates/dashboard.tmpl")) + tmpl.Execute(response, dashboardData) +} + type ApiError struct { Message string `json:"error"` StatusCode int `json:"status"` diff --git a/api_test.go b/api_test.go index 488f3d1e..b48ea3e1 100644 --- a/api_test.go +++ b/api_test.go @@ -36,6 +36,19 @@ func WithServer(t *testing.T, f func(string)) { f("http://localhost:8475") } +func TestDashboardIsAccessible(t *testing.T) { + WithServer(t, func(addr string) { + client := http.Client{} + + req, _ := http.NewRequest("GET", "http://localhost:8475/dashboard", nil) + resp, _ := client.Do(req) + + if resp.StatusCode != 200 { + t.Fatal("Dashboard is not accessible at /dashboard") + } + }) +} + func TestBrowserGets403(t *testing.T) { WithServer(t, func(addr string) { client := http.Client{} diff --git a/templates/dashboard.tmpl b/templates/dashboard.tmpl new file mode 100644 index 00000000..29fdbd39 --- /dev/null +++ b/templates/dashboard.tmpl @@ -0,0 +1,55 @@ +
{{ .Proxy.Name }} listen: {{ .Proxy.Listen }} upstream: {{ .Proxy.Upstream }} + Enabled? | +||||||||
+ name: {{ .Name }} + | + type: {{ .Type }} + | + stream: {{ .Stream }} + | + toxicity: {{ .Toxicity }} + | + direction: {{ .Direction }} + | + index: {{ .Index }} + | + buffer: {{ .BufferSize }} + | + |
+ {{ if eq .Type "latency" }}
+ jitter: ms + latency: ms + {{ end }} + + {{ if eq .Type "bandwidth" }} + rate: KB/s + {{ end }} + + {{ if eq .Type "limit_data" }} + bytes: bytes + {{ end }} + + {{ if eq .Type "slicer" }} + average size: bytes + size variation: bytes + delay: microseconds + {{ end }} + + {{ if eq .Type "slow_close" }} + delay: ms + {{ end }} + + {{ if eq .Type "timeout" }} + timeout: ms + {{ end }} + |
+ {{ end }}
+ {{ end }}
+