Improve: HTTP proxy server handler

This commit is contained in:
gVisor bot 2018-08-27 00:06:40 +08:00
parent 2667e519a1
commit 8d91482f7b
10 changed files with 109 additions and 85 deletions

View file

@ -108,8 +108,6 @@ FINAL,,Proxy # note: there is two ","
[riobard/go-shadowsocks2](https://github.com/riobard/go-shadowsocks2) [riobard/go-shadowsocks2](https://github.com/riobard/go-shadowsocks2)
[google/tcpproxy](https://github.com/google/tcpproxy)
## License ## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FDreamacro%2Fclash.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FDreamacro%2Fclash?ref=badge_large) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FDreamacro%2Fclash.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FDreamacro%2Fclash?ref=badge_large)

View file

@ -1,56 +1,18 @@
package adapters package adapters
import ( import (
"bufio"
"bytes"
"io"
"net" "net"
"net/http"
"strings" "strings"
C "github.com/Dreamacro/clash/constant" 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 // HTTPAdapter is a adapter for HTTP connection
type HTTPAdapter struct { type HTTPAdapter struct {
addr *C.Addr addr *C.Addr
conn *PeekedConn conn net.Conn
R *http.Request
} }
// Close HTTP connection // Close HTTP connection
@ -69,14 +31,34 @@ func (h *HTTPAdapter) Conn() net.Conn {
} }
// NewHTTP is HTTPAdapter generator // 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{ return &HTTPAdapter{
addr: parseHTTPAddr(host), addr: parseHTTPAddr(request),
conn: &PeekedConn{ R: request,
Peeked: peeked, conn: conn,
Conn: conn, }
host: host, }
isHTTP: isHTTP,
}, // 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" "github.com/riobard/go-shadowsocks2/socks"
) )
// SocksAdapter is a adapter for socks and redir connection // SocketAdapter is a adapter for socks and redir connection
type SocksAdapter struct { type SocketAdapter struct {
conn net.Conn conn net.Conn
addr *C.Addr addr *C.Addr
} }
// Close socks and redir connection // Close socks and redir connection
func (s *SocksAdapter) Close() { func (s *SocketAdapter) Close() {
s.conn.Close() s.conn.Close()
} }
// Addr return destination address // Addr return destination address
func (s *SocksAdapter) Addr() *C.Addr { func (s *SocketAdapter) Addr() *C.Addr {
return s.addr return s.addr
} }
// Conn return raw net.Conn // Conn return raw net.Conn
func (s *SocksAdapter) Conn() net.Conn { func (s *SocketAdapter) Conn() net.Conn {
return s.conn return s.conn
} }
// NewSocks is SocksAdapter generator // NewSocket is SocketAdapter generator
func NewSocks(target socks.Addr, conn net.Conn) *SocksAdapter { func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter {
return &SocksAdapter{ return &SocketAdapter{
conn: conn, conn: conn,
addr: parseSocksAddr(target), addr: parseSocksAddr(target),
} }

View file

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

View file

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"net" "net"
"net/http" "net/http"
"strings"
"github.com/Dreamacro/clash/adapters/local" "github.com/Dreamacro/clash/adapters/local"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -56,24 +55,20 @@ func NewHttpProxy(addr string) (*C.ProxySignal, error) {
func handleConn(conn net.Conn) { func handleConn(conn net.Conn) {
br := bufio.NewReader(conn) br := bufio.NewReader(conn)
method, hostName := adapters.ParserHTTPHostHeader(br) request, err := http.ReadRequest(br)
if hostName == "" { if err != nil {
conn.Close()
return return
} }
if !strings.Contains(hostName, ":") { if request.Method == http.MethodConnect {
hostName += ":80"
}
var peeked []byte
if method == http.MethodConnect {
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) _, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil { if err != nil {
return return
} }
} else if n := br.Buffered(); n > 0 { tun.Add(adapters.NewHTTPS(request, conn))
peeked, _ = br.Peek(br.Buffered()) return
} }
tun.Add(adapters.NewHTTP(hostName, peeked, method != http.MethodConnect, conn)) tun.Add(adapters.NewHTTP(request, conn))
} }

View file

@ -58,5 +58,5 @@ func handleRedir(conn net.Conn) {
return return
} }
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
tun.Add(adapters.NewSocks(target, conn)) tun.Add(adapters.NewSocket(target, conn))
} }

View file

@ -59,5 +59,5 @@ func handleSocks(conn net.Conn) {
return return
} }
conn.(*net.TCPConn).SetKeepAlive(true) conn.(*net.TCPConn).SetKeepAlive(true)
tun.Add(adapters.NewSocks(target, conn)) tun.Add(adapters.NewSocket(target, conn))
} }

View file

@ -1,7 +1,9 @@
package tunnel package tunnel
import ( import (
"bufio"
"io" "io"
"net/http"
"github.com/Dreamacro/clash/adapters/local" "github.com/Dreamacro/clash/adapters/local"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -9,20 +11,47 @@ import (
func (t *Tunnel) handleHTTP(request *adapters.HTTPAdapter, proxy C.ProxyAdapter) { func (t *Tunnel) handleHTTP(request *adapters.HTTPAdapter, proxy C.ProxyAdapter) {
conn := newTrafficTrack(proxy.Conn(), t.traffic) conn := newTrafficTrack(proxy.Conn(), t.traffic)
req := request.R
host := req.Host
// Before we unwrap src and/or dst, copy any buffered data. for {
if wc, ok := request.Conn().(*adapters.PeekedConn); ok && len(wc.Peeked) > 0 { req.Header.Set("Connection", "close")
if _, err := conn.Write(wc.Peeked); err != nil { req.RequestURI = ""
return adapters.RemoveHopByHopHeaders(req.Header)
err := req.Write(conn)
if err != nil {
break
} }
wc.Peeked = nil br := bufio.NewReader(conn)
} resp, err := http.ReadResponse(br, req)
if err != nil {
break
}
adapters.RemoveHopByHopHeaders(resp.Header)
if resp.ContentLength >= 0 {
resp.Header.Set("Proxy-Connection", "keep-alive")
resp.Header.Set("Connection", "keep-alive")
resp.Header.Set("Keep-Alive", "timeout=4")
resp.Close = false
} else {
resp.Close = true
}
resp.Write(request.Conn())
go io.Copy(request.Conn(), conn) req, err = http.ReadRequest(bufio.NewReader(request.Conn()))
io.Copy(conn, request.Conn()) if err != nil {
break
}
// Sometimes firefox just open a socket to process multiple domains in HTTP
// The temporary solution is close connection when encountering different HOST
if req.Host != host {
break
}
}
} }
func (t *Tunnel) handleSOCKS(request *adapters.SocksAdapter, proxy C.ProxyAdapter) { func (t *Tunnel) handleSOCKS(request *adapters.SocketAdapter, proxy C.ProxyAdapter) {
conn := newTrafficTrack(proxy.Conn(), t.traffic) conn := newTrafficTrack(proxy.Conn(), t.traffic)
go io.Copy(request.Conn(), conn) go io.Copy(request.Conn(), conn)
io.Copy(conn, request.Conn()) io.Copy(conn, request.Conn())

View file

@ -106,7 +106,7 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
switch adapter := localConn.(type) { switch adapter := localConn.(type) {
case *LocalAdapter.HTTPAdapter: case *LocalAdapter.HTTPAdapter:
t.handleHTTP(adapter, remoConn) t.handleHTTP(adapter, remoConn)
case *LocalAdapter.SocksAdapter: case *LocalAdapter.SocketAdapter:
t.handleSOCKS(adapter, remoConn) t.handleSOCKS(adapter, remoConn)
} }
} }