gnetproxy.go 1.2 KB

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