Improve: HTTP proxy server handler

This commit is contained in:
Dreamacro
2018-08-27 00:06:40 +08:00
parent 2a2e61652f
commit 8ec025b56a
10 changed files with 109 additions and 85 deletions

View File

@ -1,56 +1,18 @@
package adapters
import (
"bufio"
"bytes"
"io"
"net"
"net/http"
"strings"
C "github.com/Dreamacro/clash/constant"
)
// PeekedConn handle http connection and buffed HTTP data
type PeekedConn struct {
net.Conn
Peeked []byte
host string
isHTTP bool
}
func (c *PeekedConn) Read(p []byte) (n int, err error) {
if len(c.Peeked) > 0 {
n = copy(p, c.Peeked)
c.Peeked = c.Peeked[n:]
if len(c.Peeked) == 0 {
c.Peeked = nil
}
return n, nil
}
// Sometimes firefox just open a socket to process multiple domains in HTTP
// The temporary solution is to return io.EOF when encountering different HOST
if c.isHTTP {
br := bufio.NewReader(bytes.NewReader(p))
_, hostName := ParserHTTPHostHeader(br)
if hostName != "" {
if !strings.Contains(hostName, ":") {
hostName += ":80"
}
if hostName != c.host {
return 0, io.EOF
}
}
}
return c.Conn.Read(p)
}
// HTTPAdapter is a adapter for HTTP connection
type HTTPAdapter struct {
addr *C.Addr
conn *PeekedConn
conn net.Conn
R *http.Request
}
// Close HTTP connection
@ -69,14 +31,34 @@ func (h *HTTPAdapter) Conn() net.Conn {
}
// NewHTTP is HTTPAdapter generator
func NewHTTP(host string, peeked []byte, isHTTP bool, conn net.Conn) *HTTPAdapter {
func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter {
return &HTTPAdapter{
addr: parseHTTPAddr(host),
conn: &PeekedConn{
Peeked: peeked,
Conn: conn,
host: host,
isHTTP: isHTTP,
},
addr: parseHTTPAddr(request),
R: request,
conn: conn,
}
}
// 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))
}
}

14
adapters/local/https.go Normal file
View File

@ -0,0 +1,14 @@
package adapters
import (
"net"
"net/http"
)
// NewHTTPS is HTTPAdapter generator
func NewHTTPS(request *http.Request, conn net.Conn) *SocketAdapter {
return &SocketAdapter{
addr: parseHTTPAddr(request),
conn: conn,
}
}

View File

@ -7,30 +7,30 @@ import (
"github.com/riobard/go-shadowsocks2/socks"
)
// SocksAdapter is a adapter for socks and redir connection
type SocksAdapter struct {
// SocketAdapter is a adapter for socks and redir connection
type SocketAdapter struct {
conn net.Conn
addr *C.Addr
}
// Close socks and redir connection
func (s *SocksAdapter) Close() {
func (s *SocketAdapter) Close() {
s.conn.Close()
}
// Addr return destination address
func (s *SocksAdapter) Addr() *C.Addr {
func (s *SocketAdapter) Addr() *C.Addr {
return s.addr
}
// Conn return raw net.Conn
func (s *SocksAdapter) Conn() net.Conn {
func (s *SocketAdapter) Conn() net.Conn {
return s.conn
}
// NewSocks is SocksAdapter generator
func NewSocks(target socks.Addr, conn net.Conn) *SocksAdapter {
return &SocksAdapter{
// NewSocket is SocketAdapter generator
func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
return &SocketAdapter{
conn: conn,
addr: parseSocksAddr(target),
}

View File

@ -40,8 +40,12 @@ func parseSocksAddr(target socks.Addr) *C.Addr {
}
}
func parseHTTPAddr(target string) *C.Addr {
host, port, _ := net.SplitHostPort(target)
func parseHTTPAddr(request *http.Request) *C.Addr {
host := request.URL.Hostname()
port := request.URL.Port()
if port == "" {
port = "80"
}
ipAddr, err := net.ResolveIPAddr("ip", host)
var resolveIP *net.IP
if err == nil {
@ -92,6 +96,7 @@ func ParserHTTPHostHeader(br *bufio.Reader) (method, host string) {
return
}
if bytes.Index(b, crlfcrlf) != -1 || bytes.Index(b, lflf) != -1 {
println(string(b))
req, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(b)))
if err != nil {
return
@ -102,6 +107,7 @@ func ParserHTTPHostHeader(br *bufio.Reader) (method, host string) {
// multiple Host headers?
return
}
println(req.Host)
return req.Method, req.Host
}
}