grafana_com_proxy.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. DualStack: true,
  18. }).Dial,
  19. TLSHandshakeTimeout: 10 * time.Second,
  20. }
  21. func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
  22. url, _ := url.Parse(setting.GrafanaComUrl)
  23. director := func(req *http.Request) {
  24. req.URL.Scheme = url.Scheme
  25. req.URL.Host = url.Host
  26. req.Host = url.Host
  27. req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath)
  28. // clear cookie headers
  29. req.Header.Del("Cookie")
  30. req.Header.Del("Set-Cookie")
  31. req.Header.Del("Authorization")
  32. }
  33. return &httputil.ReverseProxy{Director: director}
  34. }
  35. func ProxyGnetRequest(c *m.ReqContext) {
  36. proxyPath := c.Params("*")
  37. proxy := ReverseProxyGnetReq(proxyPath)
  38. proxy.Transport = grafanaComProxyTransport
  39. proxy.ServeHTTP(c.Resp, c.Req.Request)
  40. c.Resp.Header().Del("Set-Cookie")
  41. }