mirror of
https://github.com/grafana/grafana.git
synced 2024-11-29 20:24:18 -06:00
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
"github.com/grafana/grafana/pkg/util"
|
|
)
|
|
|
|
var grafanaComProxyTransport = &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 {
|
|
url, _ := url.Parse(setting.GrafanaComUrl)
|
|
|
|
director := func(req *http.Request) {
|
|
req.URL.Scheme = url.Scheme
|
|
req.URL.Host = url.Host
|
|
req.Host = url.Host
|
|
|
|
req.URL.Path = util.JoinUrlFragments(url.Path+"/api", proxyPath)
|
|
|
|
// clear cookie headers
|
|
req.Header.Del("Cookie")
|
|
req.Header.Del("Set-Cookie")
|
|
req.Header.Del("Authorization")
|
|
}
|
|
|
|
return &httputil.ReverseProxy{Director: director}
|
|
}
|
|
|
|
func ProxyGnetRequest(c *middleware.Context) {
|
|
proxyPath := c.Params("*")
|
|
proxy := ReverseProxyGnetReq(proxyPath)
|
|
proxy.Transport = grafanaComProxyTransport
|
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
|
c.Resp.Header().Del("Set-Cookie")
|
|
}
|