Skip to content

Commit

Permalink
Fix: some HTTP proxy request broken
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamacro committed Mar 10, 2021
1 parent 5acdd72 commit f7f97ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions adapters/inbound/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ func RemoveHopByHopHeaders(header http.Header) {
header.Del(strings.TrimSpace(h))
}
}

// RemoveExtraHTTPHostPort remove extra host port (example.com:80 --> example.com)
// It resolves the behavior of some HTTP servers that do not handle host:80 (e.g. baidu.com)
func RemoveExtraHTTPHostPort(req *http.Request) {
host := req.Host
if host == "" {
host = req.URL.Host
}

if pHost, port, err := net.SplitHostPort(host); err == nil && port == "80" {
host = pHost
}

req.Host = host
req.URL.Host = host
}
4 changes: 3 additions & 1 deletion tunnel/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
func handleHTTP(ctx *context.HTTPContext, outbound net.Conn) {
req := ctx.Request()
conn := ctx.Conn()
host := req.Host

inboundReader := bufio.NewReader(conn)
outboundReader := bufio.NewReader(outbound)

inbound.RemoveExtraHTTPHostPort(req)
host := req.Host
for {
keepAlive := strings.TrimSpace(strings.ToLower(req.Header.Get("Proxy-Connection"))) == "keep-alive"

Expand Down Expand Up @@ -79,6 +80,7 @@ func handleHTTP(ctx *context.HTTPContext, outbound net.Conn) {
break
}

inbound.RemoveExtraHTTPHostPort(req)
// 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 {
Expand Down

0 comments on commit f7f97ef

Please sign in to comment.