2016-04-08 15:42:33 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2016-05-27 09:11:05 -05:00
|
|
|
"net/url"
|
2016-04-08 15:42:33 -05:00
|
|
|
"time"
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
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"
|
|
|
|
)
|
|
|
|
|
2017-05-22 07:56:50 -05:00
|
|
|
var grafanaComProxyTransport = &http.Transport{
|
2017-09-28 05:10:59 -05:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2016-04-08 15:42:33 -05:00
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
2017-09-28 00:25:00 -05:00
|
|
|
DualStack: true,
|
2016-04-08 15:42:33 -05:00
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReverseProxyGnetReq(proxyPath string) *httputil.ReverseProxy {
|
2017-05-22 07:56:50 -05:00
|
|
|
url, _ := url.Parse(setting.GrafanaComUrl)
|
2016-05-27 09:11:05 -05:00
|
|
|
|
2016-04-08 15:42:33 -05:00
|
|
|
director := func(req *http.Request) {
|
2016-05-27 09:11:05 -05:00
|
|
|
req.URL.Scheme = url.Scheme
|
|
|
|
req.URL.Host = url.Host
|
|
|
|
req.Host = url.Host
|
2016-04-08 15:42:33 -05:00
|
|
|
|
2016-05-27 09:11:05 -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")
|
2016-10-11 23:54:33 -05:00
|
|
|
req.Header.Del("Authorization")
|
2016-04-08 15:42:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
|
|
}
|
|
|
|
|
2018-03-07 10:54:50 -06:00
|
|
|
func ProxyGnetRequest(c *m.ReqContext) {
|
2016-04-08 15:42:33 -05:00
|
|
|
proxyPath := c.Params("*")
|
|
|
|
proxy := ReverseProxyGnetReq(proxyPath)
|
2017-05-22 07:56:50 -05:00
|
|
|
proxy.Transport = grafanaComProxyTransport
|
2016-04-08 15:42:33 -05:00
|
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
|
|
|
c.Resp.Header().Del("Set-Cookie")
|
|
|
|
}
|