grafana_com_proxy.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package api
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "net/http/httputil"
  7. "net/url"
  8. "time"
  9. "github.com/grafana/grafana/pkg/middleware"
  10. "github.com/grafana/grafana/pkg/setting"
  11. "github.com/grafana/grafana/pkg/util"
  12. )
  13. var grafanaComProxyTransport = &http.Transport{
  14. TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
  15. Proxy: http.ProxyFromEnvironment,
  16. Dial: (&net.Dialer{
  17. Timeout: 30 * time.Second,
  18. KeepAlive: 30 * time.Second,
  19. DualStack: true,
  20. }).Dial,
  21. TLSHandshakeTimeout: 10 * time.Second,
  22. }
  23. func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
  24. url, _ := url.Parse(setting.GrafanaComUrl)
  25. director := func(req *http.Request) {
  26. req.URL.Scheme = url.Scheme
  27. req.URL.Host = url.Host
  28. req.Host = url.Host
  29. req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath)
  30. // clear cookie headers
  31. req.Header.Del("Cookie")
  32. req.Header.Del("Set-Cookie")
  33. req.Header.Del("Authorization")
  34. }
  35. return &httputil.ReverseProxy{Director: director}
  36. }
  37. func ProxyGnetRequest(c *middleware.Context) {
  38. proxyPath := c.Params("*")
  39. proxy := ReverseProxyGnetReq(proxyPath)
  40. proxy.Transport = grafanaComProxyTransport
  41. proxy.ServeHTTP(c.Resp, c.Req.Request)
  42. c.Resp.Header().Del("Set-Cookie")
  43. }