forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
51 lines (43 loc) · 1.59 KB
/
main.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
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)
func main() {
origin := os.Getenv("ORIGIN_SERVER")
if origin == "" {
// it is never not-set, the default is in the `.env` file
log.Fatalf("missing env-variable ORIGIN_SERVER")
}
sleepDurationStr := os.Getenv("SLEEP_DURATION")
if sleepDurationStr == "" {
// it is never not-set, the default is in the `.env` file
log.Fatalf("missing env-variable SLEEP_DURATION")
}
sleep, err := time.ParseDuration(sleepDurationStr)
if err != nil {
log.Fatalf("failed to parse SLEEP_DURATION: %v", err)
}
originURL, _ := url.Parse(origin)
proxy := httputil.NewSingleHostReverseProxy(originURL)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
safeSleep := strings.ReplaceAll(sleep.String(), "\n", "")
safeRequestUri := strings.ReplaceAll(r.RequestURI, "\n", "")
log.Printf("sleeping for %s then proxying request: url '%s'", safeSleep, safeRequestUri)
// This is commented out as CodeQL flags this as vulnerability CWE-117 (https://cwe.mitre.org/data/definitions/117.html)
// If you need to debug and log the headers then use the line below instead of the log.Printf statement above
// The docker container will then need to be rebuilt after the change is made:
// Run `make devenv sources=slow_proxy`
// or run `docker-compose build` in the devenv folder
//
// log.Printf("sleeping for %s then proxying request: url '%s', headers: '%v'", safeSleep, safeRequestUri, r.Header)
<-time.After(sleep)
proxy.ServeHTTP(w, r)
})
log.Fatal(http.ListenAndServe(":3011", nil))
}