Fix: if http proxy Upgrade failure

This commit is contained in:
yaling888
2022-04-26 23:58:23 +08:00
parent ca4961a146
commit 7c50c068f5
5 changed files with 50 additions and 38 deletions

View File

@ -12,19 +12,28 @@ import (
func Relay(leftConn, rightConn net.Conn) { func Relay(leftConn, rightConn net.Conn) {
ch := make(chan error) ch := make(chan error)
tcpKeepAlive(leftConn)
tcpKeepAlive(rightConn)
go func() { go func() {
buf := pool.Get(pool.RelayBufferSize) buf := pool.Get(pool.RelayBufferSize)
// Wrapping to avoid using *net.TCPConn.(ReadFrom) // Wrapping to avoid using *net.TCPConn.(ReadFrom)
// See also https://github.com/Dreamacro/clash/pull/1209 // See also https://github.com/Dreamacro/clash/pull/1209
_, err := io.CopyBuffer(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn}, buf) _, err := io.CopyBuffer(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn}, buf)
pool.Put(buf) _ = pool.Put(buf)
leftConn.SetReadDeadline(time.Now()) _ = leftConn.SetReadDeadline(time.Now())
ch <- err ch <- err
}() }()
buf := pool.Get(pool.RelayBufferSize) buf := pool.Get(pool.RelayBufferSize)
io.CopyBuffer(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn}, buf) _, _ = io.CopyBuffer(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn}, buf)
pool.Put(buf) _ = pool.Put(buf)
rightConn.SetReadDeadline(time.Now()) _ = rightConn.SetReadDeadline(time.Now())
<-ch <-ch
} }
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
_ = tcp.SetKeepAlive(true)
}
}

View File

@ -62,20 +62,22 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache[string,
request.RequestURI = "" request.RequestURI = ""
if isUpgradeRequest(request) { if isUpgradeRequest(request) {
handleUpgrade(conn, request, in) if resp = handleUpgrade(conn, request, in); resp == nil {
return // hijack connection
return // hijack connection }
} }
RemoveHopByHopHeaders(request.Header) if resp == nil {
RemoveExtraHTTPHostPort(request) RemoveHopByHopHeaders(request.Header)
RemoveExtraHTTPHostPort(request)
if request.URL.Scheme == "" || request.URL.Host == "" { if request.URL.Scheme == "" || request.URL.Host == "" {
resp = responseWith(request, http.StatusBadRequest) resp = responseWith(request, http.StatusBadRequest)
} else { } else {
resp, err = client.Do(request) resp, err = client.Do(request)
if err != nil { if err != nil {
resp = responseWith(request, http.StatusBadGateway) resp = responseWith(request, http.StatusBadGateway)
}
} }
} }
@ -96,7 +98,7 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache[string,
} }
} }
conn.Close() _ = conn.Close()
} }
func Authenticate(request *http.Request, cache *cache.Cache[string, bool]) *http.Response { func Authenticate(request *http.Request, cache *cache.Cache[string, bool]) *http.Response {

View File

@ -4,6 +4,7 @@ import (
"net" "net"
"net/http" "net/http"
"strings" "strings"
"time"
"github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/adapter/inbound"
N "github.com/Dreamacro/clash/common/net" N "github.com/Dreamacro/clash/common/net"
@ -15,9 +16,7 @@ func isUpgradeRequest(req *http.Request) bool {
return strings.EqualFold(req.Header.Get("Connection"), "Upgrade") return strings.EqualFold(req.Header.Get("Connection"), "Upgrade")
} }
func handleUpgrade(conn net.Conn, request *http.Request, in chan<- C.ConnContext) { func handleUpgrade(conn net.Conn, request *http.Request, in chan<- C.ConnContext) (resp *http.Response) {
defer conn.Close()
removeProxyHeaders(request.Header) removeProxyHeaders(request.Header)
RemoveExtraHTTPHostPort(request) RemoveExtraHTTPHostPort(request)
@ -36,26 +35,36 @@ func handleUpgrade(conn net.Conn, request *http.Request, in chan<- C.ConnContext
in <- inbound.NewHTTP(dstAddr, conn.RemoteAddr(), right) in <- inbound.NewHTTP(dstAddr, conn.RemoteAddr(), right)
bufferedLeft := N.NewBufferedConn(left) bufferedLeft := N.NewBufferedConn(left)
defer bufferedLeft.Close() defer func() {
_ = bufferedLeft.Close()
}()
err := request.Write(bufferedLeft) err := request.Write(bufferedLeft)
if err != nil { if err != nil {
return return
} }
resp, err := http.ReadResponse(bufferedLeft.Reader(), request) resp, err = http.ReadResponse(bufferedLeft.Reader(), request)
if err != nil {
return
}
removeProxyHeaders(resp.Header)
err = resp.Write(conn)
if err != nil { if err != nil {
return return
} }
if resp.StatusCode == http.StatusSwitchingProtocols { if resp.StatusCode == http.StatusSwitchingProtocols {
removeProxyHeaders(resp.Header)
err = conn.SetReadDeadline(time.Time{})
if err != nil {
return
}
err = resp.Write(conn)
if err != nil {
return
}
N.Relay(bufferedLeft, conn) N.Relay(bufferedLeft, conn)
_ = conn.Close()
resp = nil
} }
return
} }

View File

@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
// removeHopByHopHeaders remove Proxy-* headers // removeProxyHeaders remove Proxy-* headers
func removeProxyHeaders(header http.Header) { func removeProxyHeaders(header http.Header) {
header.Del("Proxy-Connection") header.Del("Proxy-Connection")
header.Del("Proxy-Authenticate") header.Del("Proxy-Authenticate")

View File

@ -62,13 +62,5 @@ func handleUDPToLocal(packet C.UDPPacket, pc net.PacketConn, key string, fAddr n
} }
func handleSocket(ctx C.ConnContext, outbound net.Conn) { func handleSocket(ctx C.ConnContext, outbound net.Conn) {
tcpKeepAlive(ctx.Conn())
tcpKeepAlive(outbound)
N.Relay(ctx.Conn(), outbound) N.Relay(ctx.Conn(), outbound)
} }
func tcpKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
}
}