2017-12-18 14:34:00 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-12-10 17:08:16 -05:00
|
|
|
"strconv"
|
2017-12-18 14:34:00 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
)
|
|
|
|
|
2019-02-22 18:29:09 -05:00
|
|
|
// ParseXForwardedFor parses X-Forwarded-For header in http headers, and return the IP list in it.
|
2017-12-18 14:34:00 -05:00
|
|
|
func ParseXForwardedFor(header http.Header) []net.Address {
|
|
|
|
xff := header.Get("X-Forwarded-For")
|
2019-06-14 09:47:28 -04:00
|
|
|
if xff == "" {
|
2017-12-18 14:34:00 -05:00
|
|
|
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
|
|
|
|
}
|
2017-12-18 14:59:43 -05:00
|
|
|
|
2019-02-22 18:29:09 -05:00
|
|
|
// RemoveHopByHopHeaders remove hop by hop headers in http header list.
|
2017-12-18 14:59:43 -05:00
|
|
|
func RemoveHopByHopHeaders(header http.Header) {
|
|
|
|
// Strip hop-by-hop header based on RFC:
|
|
|
|
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.5.1
|
|
|
|
// https://www.mnot.net/blog/2011/07/11/what_proxies_must_do
|
|
|
|
|
|
|
|
header.Del("Proxy-Connection")
|
|
|
|
header.Del("Proxy-Authenticate")
|
|
|
|
header.Del("Proxy-Authorization")
|
|
|
|
header.Del("TE")
|
|
|
|
header.Del("Trailers")
|
|
|
|
header.Del("Transfer-Encoding")
|
|
|
|
header.Del("Upgrade")
|
|
|
|
|
|
|
|
connections := header.Get("Connection")
|
|
|
|
header.Del("Connection")
|
2019-06-14 09:47:28 -04:00
|
|
|
if connections == "" {
|
2017-12-18 14:59:43 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, h := range strings.Split(connections, ",") {
|
|
|
|
header.Del(strings.TrimSpace(h))
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 17:08:16 -05:00
|
|
|
|
|
|
|
// ParseHost splits host and port from a raw string. Default port is used when raw string doesn't contain port.
|
|
|
|
func ParseHost(rawHost string, defaultPort net.Port) (net.Destination, error) {
|
|
|
|
port := defaultPort
|
|
|
|
host, rawPort, err := net.SplitHostPort(rawHost)
|
|
|
|
if err != nil {
|
|
|
|
if addrError, ok := err.(*net.AddrError); ok && strings.Contains(addrError.Err, "missing port") {
|
|
|
|
host = rawHost
|
|
|
|
} else {
|
|
|
|
return net.Destination{}, err
|
|
|
|
}
|
|
|
|
} else if len(rawPort) > 0 {
|
|
|
|
intPort, err := strconv.Atoi(rawPort)
|
|
|
|
if err != nil {
|
|
|
|
return net.Destination{}, err
|
|
|
|
}
|
|
|
|
port = net.Port(intPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.TCPDestination(net.ParseAddress(host), port), nil
|
|
|
|
}
|