mihomo/adapters/inbound/http.go

67 lines
1.4 KiB
Go
Raw Normal View History

package adapters
import (
2018-08-11 22:51:30 +08:00
"net"
2018-08-27 00:06:40 +08:00
"net/http"
"strings"
C "github.com/Dreamacro/clash/constant"
)
2018-08-12 16:18:58 +08:00
// HTTPAdapter is a adapter for HTTP connection
type HTTPAdapter struct {
2018-09-30 12:25:52 +08:00
metadata *C.Metadata
conn net.Conn
R *http.Request
}
2018-08-12 16:18:58 +08:00
// Close HTTP connection
func (h *HTTPAdapter) Close() {
2018-08-11 22:51:30 +08:00
h.conn.Close()
}
2018-09-30 12:25:52 +08:00
// Metadata return destination metadata
func (h *HTTPAdapter) Metadata() *C.Metadata {
return h.metadata
}
2018-08-12 16:18:58 +08:00
// Conn return raw net.Conn of HTTP
func (h *HTTPAdapter) Conn() net.Conn {
2018-08-11 22:51:30 +08:00
return h.conn
}
2018-08-12 16:18:58 +08:00
// NewHTTP is HTTPAdapter generator
2018-08-27 00:06:40 +08:00
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
metadata := parseHTTPAddr(request)
metadata.SourceIP = parseSourceIP(conn)
2018-08-12 16:18:58 +08:00
return &HTTPAdapter{
metadata: metadata,
2018-09-30 12:25:52 +08:00
R: request,
conn: conn,
2018-08-27 00:06:40 +08: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 22:51:30 +08:00
}
}