cmd/strelaypoolsrv: Handle portless X-Forwarded-For (#4856)

This commit is contained in:
Jakob Borg 2018-04-01 21:29:34 -04:00 committed by GitHub
parent 26d87ec3bb
commit 6982c06261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -319,13 +319,9 @@ func handlePostRequest(w http.ResponseWriter, r *http.Request) {
} }
// Get the IP address of the client // Get the IP address of the client
rhost, _, err := net.SplitHostPort(r.RemoteAddr) rhost := r.RemoteAddr
if err != nil { if host, _, err := net.SplitHostPort(rhost); err == nil {
if debug { rhost = host
log.Println("Failed to split remote address", r.RemoteAddr)
}
http.Error(w, err.Error(), 500)
return
} }
ip := net.ParseIP(host) ip := net.ParseIP(host)
@ -449,13 +445,12 @@ func evict(relay relay) func() {
} }
func limit(addr string, cache *lru.Cache, lock sync.RWMutex, intv time.Duration, burst int) bool { func limit(addr string, cache *lru.Cache, lock sync.RWMutex, intv time.Duration, burst int) bool {
host, _, err := net.SplitHostPort(addr) if host, _, err := net.SplitHostPort(addr); err == nil {
if err != nil { addr = host
return false
} }
lock.RLock() lock.RLock()
bkt, ok := cache.Get(host) bkt, ok := cache.Get(addr)
lock.RUnlock() lock.RUnlock()
if ok { if ok {
bkt := bkt.(*rate.Limiter) bkt := bkt.(*rate.Limiter)
@ -465,7 +460,7 @@ func limit(addr string, cache *lru.Cache, lock sync.RWMutex, intv time.Duration,
} }
} else { } else {
lock.Lock() lock.Lock()
cache.Add(host, rate.NewLimiter(rate.Every(intv), burst)) cache.Add(addr, rate.NewLimiter(rate.Every(intv), burst))
lock.Unlock() lock.Unlock()
} }
return false return false