Fix: http proxy should response correct http version (#1651)

This commit is contained in:
Kr328 2021-09-30 16:30:07 +08:00 committed by GitHub
parent 9aeb4c8cfe
commit ced9749104
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,7 @@
package http package http
import ( import (
"fmt"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -43,11 +44,8 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
if trusted { if trusted {
if request.Method == http.MethodConnect { if request.Method == http.MethodConnect {
resp = responseWith(200) // Manual writing to support CONNECT for http 1.0 (workaround for uplay client)
resp.Status = "Connection established" if _, err = fmt.Fprintf(conn, "HTTP/%d.%d %03d %s\r\n\r\n", request.ProtoMajor, request.ProtoMinor, http.StatusOK, "Connection established"); err != nil {
resp.ContentLength = -1
if resp.Write(conn) != nil {
break // close connection break // close connection
} }
@ -67,11 +65,11 @@ func HandleConn(c net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
removeExtraHTTPHostPort(request) removeExtraHTTPHostPort(request)
if request.URL.Scheme == "" || request.URL.Host == "" { if request.URL.Scheme == "" || request.URL.Host == "" {
resp = responseWith(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(http.StatusBadGateway) resp = responseWith(request, http.StatusBadGateway)
} }
} }
@ -100,7 +98,7 @@ func authenticate(request *http.Request, cache *cache.Cache) *http.Response {
if authenticator != nil { if authenticator != nil {
credential := parseBasicProxyAuthorization(request) credential := parseBasicProxyAuthorization(request)
if credential == "" { if credential == "" {
resp := responseWith(http.StatusProxyAuthRequired) resp := responseWith(request, http.StatusProxyAuthRequired)
resp.Header.Set("Proxy-Authenticate", "Basic") resp.Header.Set("Proxy-Authenticate", "Basic")
return resp return resp
} }
@ -114,20 +112,20 @@ func authenticate(request *http.Request, cache *cache.Cache) *http.Response {
if !authed.(bool) { if !authed.(bool) {
log.Infoln("Auth failed from %s", request.RemoteAddr) log.Infoln("Auth failed from %s", request.RemoteAddr)
return responseWith(http.StatusForbidden) return responseWith(request, http.StatusForbidden)
} }
} }
return nil return nil
} }
func responseWith(statusCode int) *http.Response { func responseWith(request *http.Request, statusCode int) *http.Response {
return &http.Response{ return &http.Response{
StatusCode: statusCode, StatusCode: statusCode,
Status: http.StatusText(statusCode), Status: http.StatusText(statusCode),
Proto: "HTTP/1.1", Proto: request.Proto,
ProtoMajor: 1, ProtoMajor: request.ProtoMajor,
ProtoMinor: 1, ProtoMinor: request.ProtoMinor,
Header: http.Header{}, Header: http.Header{},
} }
} }