grafana/pkg/api/grafana_com_proxy.go

51 lines
1.1 KiB
Go
Raw Normal View History

2016-04-08 15:42:33 -05:00
package api
import (
"net"
"net/http"
"net/http/httputil"
"net/url"
2016-04-08 15:42:33 -05:00
"time"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
2016-04-08 15:42:33 -05:00
"github.com/grafana/grafana/pkg/util"
)
var grafanaComProxyTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
2016-04-08 15:42:33 -05:00
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
2016-04-08 15:42:33 -05:00
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
url, _ := url.Parse(setting.GrafanaComUrl)
2016-04-08 15:42:33 -05:00
director := func(req *http.Request) {
req.URL.Scheme = url.Scheme
req.URL.Host = url.Host
req.Host = url.Host
2016-04-08 15:42:33 -05:00
req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath)
2016-04-08 15:42:33 -05:00
// clear cookie headers
req.Header.Del("Cookie")
req.Header.Del("Set-Cookie")
req.Header.Del("Authorization")
2016-04-08 15:42:33 -05:00
}
return &httputil.ReverseProxy{Director: director}
}
func ProxyGnetRequest(c *m.Context) {
2016-04-08 15:42:33 -05:00
proxyPath := c.Params("*")
proxy := ReverseProxyGnetReq(proxyPath)
proxy.Transport = grafanaComProxyTransport
2016-04-08 15:42:33 -05:00
proxy.ServeHTTP(c.Resp, c.Req.Request)
c.Resp.Header().Del("Set-Cookie")
}