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"
|
|
|
|
|
2022-04-11 06:17:08 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2016-04-08 15:42:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2022-04-11 06:17:08 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util/proxyutil"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-04-08 15:42:33 -05:00
|
|
|
)
|
|
|
|
|
2017-05-22 07:56:50 -05:00
|
|
|
var grafanaComProxyTransport = &http.Transport{
|
2017-09-28 05:10:59 -05:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2022-04-11 06:17:08 -05:00
|
|
|
DialContext: (&net.Dialer{
|
2016-04-08 15:42:33 -05:00
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
2022-04-11 06:17:08 -05:00
|
|
|
}).DialContext,
|
2016-04-08 15:42:33 -05:00
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2023-01-27 03:20:55 -06:00
|
|
|
func ReverseProxyGnetReq(logger log.Logger, proxyPath string, version string, grafanaComAPIUrl string) *httputil.ReverseProxy {
|
|
|
|
url, _ := url.Parse(grafanaComAPIUrl)
|
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
|
|
|
|
2023-01-27 03:20:55 -06:00
|
|
|
req.URL.Path = util.JoinURLFragments(url.Path, 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")
|
2021-11-12 04:07:12 -06:00
|
|
|
|
|
|
|
// send the current Grafana version for each request proxied to GCOM
|
|
|
|
req.Header.Add("grafana-version", version)
|
2016-04-08 15:42:33 -05:00
|
|
|
}
|
|
|
|
|
2022-04-11 06:17:08 -05:00
|
|
|
return proxyutil.NewReverseProxy(logger, director)
|
2016-04-08 15:42:33 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (hs *HTTPServer) ProxyGnetRequest(c *contextmodel.ReqContext) {
|
2021-10-11 07:30:59 -05:00
|
|
|
proxyPath := web.Params(c.Req)["*"]
|
2023-01-25 07:23:18 -06:00
|
|
|
proxy := ReverseProxyGnetReq(c.Logger, proxyPath, hs.Cfg.BuildVersion, hs.Cfg.GrafanaComAPIURL)
|
2017-05-22 07:56:50 -05:00
|
|
|
proxy.Transport = grafanaComProxyTransport
|
2021-09-01 04:18:30 -05:00
|
|
|
proxy.ServeHTTP(c.Resp, c.Req)
|
2016-04-08 15:42:33 -05:00
|
|
|
}
|