forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: http proxy Upgrade behavior (#2097)
- Loading branch information
Showing
5 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"time" | ||
|
||
"github.com/Dreamacro/clash/common/pool" | ||
) | ||
|
||
// Relay copies between left and right bidirectionally. | ||
func Relay(leftConn, rightConn net.Conn) { | ||
ch := make(chan error) | ||
|
||
go func() { | ||
buf := pool.Get(pool.RelayBufferSize) | ||
// Wrapping to avoid using *net.TCPConn.(ReadFrom) | ||
// See also https://github.com/Dreamacro/clash/pull/1209 | ||
_, err := io.CopyBuffer(WriteOnlyWriter{Writer: leftConn}, ReadOnlyReader{Reader: rightConn}, buf) | ||
pool.Put(buf) | ||
leftConn.SetReadDeadline(time.Now()) | ||
ch <- err | ||
}() | ||
|
||
buf := pool.Get(pool.RelayBufferSize) | ||
io.CopyBuffer(WriteOnlyWriter{Writer: rightConn}, ReadOnlyReader{Reader: leftConn}, buf) | ||
pool.Put(buf) | ||
rightConn.SetReadDeadline(time.Now()) | ||
<-ch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package http | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/Dreamacro/clash/adapter/inbound" | ||
N "github.com/Dreamacro/clash/common/net" | ||
C "github.com/Dreamacro/clash/constant" | ||
"github.com/Dreamacro/clash/transport/socks5" | ||
) | ||
|
||
func isUpgradeRequest(req *http.Request) bool { | ||
return strings.EqualFold(req.Header.Get("Connection"), "Upgrade") | ||
} | ||
|
||
func handleUpgrade(conn net.Conn, request *http.Request, in chan<- C.ConnContext) { | ||
defer conn.Close() | ||
|
||
removeProxyHeaders(request.Header) | ||
removeExtraHTTPHostPort(request) | ||
|
||
address := request.Host | ||
if _, _, err := net.SplitHostPort(address); err != nil { | ||
address = net.JoinHostPort(address, "80") | ||
} | ||
|
||
dstAddr := socks5.ParseAddr(address) | ||
if dstAddr == nil { | ||
return | ||
} | ||
|
||
left, right := net.Pipe() | ||
|
||
in <- inbound.NewHTTP(dstAddr, conn.RemoteAddr(), right) | ||
|
||
bufferedLeft := N.NewBufferedConn(left) | ||
defer bufferedLeft.Close() | ||
|
||
err := request.Write(bufferedLeft) | ||
if err != nil { | ||
return | ||
} | ||
|
||
resp, err := http.ReadResponse(bufferedLeft.Reader(), request) | ||
if err != nil { | ||
return | ||
} | ||
|
||
removeProxyHeaders(resp.Header) | ||
|
||
err = resp.Write(conn) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if resp.StatusCode == http.StatusSwitchingProtocols { | ||
N.Relay(bufferedLeft, conn) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters