2016-04-08 15:42:33 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2016-05-27 06:52:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2016-04-08 15:42:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var gNetProxyTransport = &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
|
|
|
|
director := func(req *http.Request) {
|
|
|
|
req.URL.Scheme = "https"
|
|
|
|
req.URL.Host = "grafana.net"
|
|
|
|
req.Host = "grafana.net"
|
|
|
|
|
2016-05-27 06:52:19 -05:00
|
|
|
req.URL.Path = util.JoinUrlFragments(setting.GrafanaNetUrl+"/api", proxyPath)
|
2016-04-08 15:42:33 -05:00
|
|
|
|
|
|
|
// clear cookie headers
|
|
|
|
req.Header.Del("Cookie")
|
|
|
|
req.Header.Del("Set-Cookie")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProxyGnetRequest(c *middleware.Context) {
|
|
|
|
proxyPath := c.Params("*")
|
|
|
|
proxy := ReverseProxyGnetReq(proxyPath)
|
|
|
|
proxy.Transport = gNetProxyTransport
|
|
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
|
|
|
c.Resp.Header().Del("Set-Cookie")
|
|
|
|
}
|