2019-12-08 04:17:24 +00:00
|
|
|
package inbound
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
import (
|
2018-08-11 14:51:30 +00:00
|
|
|
"net"
|
2018-08-26 16:06:40 +00:00
|
|
|
"net/http"
|
2018-08-12 04:14:59 +00:00
|
|
|
"strings"
|
2018-07-25 16:04:59 +00:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2018-08-12 08:18:58 +00:00
|
|
|
// HTTPAdapter is a adapter for HTTP connection
|
|
|
|
type HTTPAdapter struct {
|
2019-04-23 15:29:36 +00:00
|
|
|
net.Conn
|
2018-09-30 04:25:52 +00:00
|
|
|
metadata *C.Metadata
|
|
|
|
R *http.Request
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 04:25:52 +00:00
|
|
|
// Metadata return destination metadata
|
|
|
|
func (h *HTTPAdapter) Metadata() *C.Metadata {
|
|
|
|
return h.metadata
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 08:18:58 +00:00
|
|
|
// NewHTTP is HTTPAdapter generator
|
2018-08-26 16:06:40 +00:00
|
|
|
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
|
2019-02-02 13:03:13 +00:00
|
|
|
metadata := parseHTTPAddr(request)
|
2019-10-27 13:44:07 +00:00
|
|
|
metadata.Type = C.HTTP
|
2019-05-09 13:00:29 +00:00
|
|
|
if ip, port, err := parseAddr(conn.RemoteAddr().String()); err == nil {
|
|
|
|
metadata.SrcIP = ip
|
|
|
|
metadata.SrcPort = port
|
|
|
|
}
|
2018-08-12 08:18:58 +00:00
|
|
|
return &HTTPAdapter{
|
2019-02-02 13:03:13 +00:00
|
|
|
metadata: metadata,
|
2018-09-30 04:25:52 +00:00
|
|
|
R: request,
|
2019-04-23 15:29:36 +00:00
|
|
|
Conn: conn,
|
2018-08-26 16:06:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveHopByHopHeaders remove hop-by-hop header
|
|
|
|
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")
|
|
|
|
if len(connections) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, h := range strings.Split(connections, ",") {
|
|
|
|
header.Del(strings.TrimSpace(h))
|
2018-08-11 14:51:30 +00:00
|
|
|
}
|
2018-07-25 16:04:59 +00:00
|
|
|
}
|