Browse Source

fix

set HTTP_X_FORWARDED_FOR when proxying

https://github.com/chrislusf/seaweedfs/issues/214
pull/228/head
chrislusf 9 years ago
parent
commit
70d050416b
  1. 17
      go/security/guard.go
  2. 8
      go/weed/weed_server/master_server.go

17
go/security/guard.go

@ -6,6 +6,7 @@ import (
"net"
"net/http"
"regexp"
"strings"
"github.com/chrislusf/seaweedfs/go/glog"
)
@ -81,12 +82,26 @@ func (g *Guard) Secure(f func(w http.ResponseWriter, r *http.Request)) func(w ht
}
}
func GetActualRemoteHost(r *http.Request) (host string, err error) {
host = r.Header.Get("HTTP_X_FORWARDED_FOR")
if host == "" {
host = r.Header.Get("X-FORWARDED-FOR")
}
if strings.Contains(host, ",") {
host = host[0:strings.Index(host, ",")]
}
if host == "" {
host, _, err = net.SplitHostPort(r.RemoteAddr)
}
return
}
func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
if len(g.whiteList) == 0 {
return nil
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
host, err := GetActualRemoteHost(r)
if err == nil {
for _, ip := range g.whiteList {

8
go/weed/weed_server/master_server.go

@ -113,6 +113,14 @@ func (ms *MasterServer) proxyToLeader(f func(w http.ResponseWriter, r *http.Requ
}
glog.V(4).Infoln("proxying to leader", ms.Topo.RaftServer.Leader())
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
director := proxy.Director
proxy.Director = func(req *http.Request) {
actualHost, err := security.GetActualRemoteHost(req)
if err == nil {
req.Header.Set(("HTTP_X_FORWARDED_FOR", actualHost)
}
director(req)
}
proxy.Transport = util.Transport
proxy.ServeHTTP(w, r)
} else {

Loading…
Cancel
Save