Fix: if http proxy Upgrade failure
This commit is contained in:
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -62,11 +62,12 @@ 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
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp == nil {
|
||||||
RemoveHopByHopHeaders(request.Header)
|
RemoveHopByHopHeaders(request.Header)
|
||||||
RemoveExtraHTTPHostPort(request)
|
RemoveExtraHTTPHostPort(request)
|
||||||
|
|
||||||
@ -78,6 +79,7 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache[string,
|
|||||||
resp = responseWith(request, http.StatusBadGateway)
|
resp = responseWith(request, http.StatusBadGateway)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RemoveHopByHopHeaders(resp.Header)
|
RemoveHopByHopHeaders(resp.Header)
|
||||||
}
|
}
|
||||||
@ -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 {
|
||||||
|
@ -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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusSwitchingProtocols {
|
||||||
removeProxyHeaders(resp.Header)
|
removeProxyHeaders(resp.Header)
|
||||||
|
|
||||||
|
err = conn.SetReadDeadline(time.Time{})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = resp.Write(conn)
|
err = resp.Write(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusSwitchingProtocols {
|
|
||||||
N.Relay(bufferedLeft, conn)
|
N.Relay(bufferedLeft, conn)
|
||||||
|
_ = conn.Close()
|
||||||
|
resp = nil
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
@ -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")
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user