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