diff --git a/proxy/http/server.go b/proxy/http/server.go index 8eecf1e7c..7cf2b1c18 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -72,21 +72,29 @@ func canActivate(loginStr string, authenticator auth.Authenticator, cache *cache func HandleConn(conn net.Conn, cache *cache.Cache) { br := bufio.NewReader(conn) + +keepAlive: request, err := http.ReadRequest(br) if err != nil || request.URL.Host == "" { conn.Close() return } + keepAlive := strings.TrimSpace(strings.ToLower(request.Header.Get("Proxy-Connection"))) == "keep-alive" authenticator := authStore.Authenticator() if authenticator != nil { if authStrings := strings.Split(request.Header.Get("Proxy-Authorization"), " "); len(authStrings) != 2 { conn.Write([]byte("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic\r\n\r\n")) - conn.Close() + if keepAlive { + goto keepAlive + } return } else if !canActivate(authStrings[1], authenticator, cache) { conn.Write([]byte("HTTP/1.1 403 Forbidden\r\n\r\n")) log.Infoln("Auth failed from %s", conn.RemoteAddr().String()) + if keepAlive { + goto keepAlive + } conn.Close() return } @@ -95,6 +103,7 @@ func HandleConn(conn net.Conn, cache *cache.Cache) { if request.Method == http.MethodConnect { _, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) if err != nil { + conn.Close() return } tunnel.Add(adapters.NewHTTPS(request, conn))