1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-06-09 04:29:17 -04:00

read original addr from x-forwarded-for header if present

This commit is contained in:
Darien Raymond
2017-12-18 20:34:00 +01:00
parent a0b2c285b2
commit 91ca88bcff
6 changed files with 93 additions and 13 deletions

View File

@@ -0,0 +1,21 @@
package http
import (
"net/http"
"strings"
"v2ray.com/core/common/net"
)
func ParseXForwardedFor(header http.Header) []net.Address {
xff := header.Get("X-Forwarded-For")
if len(xff) == 0 {
return nil
}
list := strings.Split(xff, ",")
addrs := make([]net.Address, 0, len(list))
for _, proxy := range list {
addrs = append(addrs, net.ParseAddress(proxy))
}
return addrs
}

View File

@@ -0,0 +1,20 @@
package http_test
import (
"net/http"
"testing"
. "v2ray.com/core/common/protocol/http"
. "v2ray.com/ext/assert"
)
func TestParseXForwardedFor(t *testing.T) {
assert := With(t)
header := http.Header{}
header.Add("X-Forwarded-For", "129.78.138.66, 129.78.64.103")
addrs := ParseXForwardedFor(header)
assert(len(addrs), Equals, 2)
assert(addrs[0].String(), Equals, "129.78.138.66")
assert(addrs[1].String(), Equals, "129.78.64.103")
}