grafana_com_proxy.go 1.1 KB

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