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:
21
common/protocol/http/headers.go
Normal file
21
common/protocol/http/headers.go
Normal 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
|
||||
}
|
||||
20
common/protocol/http/headers_test.go
Normal file
20
common/protocol/http/headers_test.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user