From 08e796079e19d064829af175986be164fe70dd72 Mon Sep 17 00:00:00 2001 From: Tommy Wu Date: Wed, 5 Feb 2025 13:04:21 +0800 Subject: [PATCH] fix http proxy for PuTTY Change-Id: I30dcc7750a6f7b9980655770929c8bd5d2b1b3c8 --- protocol/http/handshake.go | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/protocol/http/handshake.go b/protocol/http/handshake.go index fd5817b7..60d0132c 100644 --- a/protocol/http/handshake.go +++ b/protocol/http/handshake.go @@ -56,9 +56,12 @@ func HandleConnectionEx( } if !authOk { // Since no one else is using the library, use a fixed realm until rewritten - err = responseWith( + err = responseWithBody( request, http.StatusProxyAuthRequired, - "Proxy-Authenticate", `Basic realm="sing-box" charset="UTF-8"`, + "Proxy authentication required", + "Content-Type", "text/plain; charset=utf-8", + "Proxy-Authenticate", `Basic realm="sing-box", charset="UTF-8"`, + "Connection", "close", ).Write(conn) if err != nil { return err @@ -68,7 +71,8 @@ func HandleConnectionEx( } else if authorization != "" { return E.New("http: authentication failed, Proxy-Authorization=", authorization) } else { - return E.New("http: authentication failed, no Proxy-Authorization header") + //return E.New("http: authentication failed, no Proxy-Authorization header") + continue } } } @@ -270,3 +274,30 @@ func responseWith(request *http.Request, statusCode int, headers ...string) *htt Header: header, } } + +func responseWithBody(request *http.Request, statusCode int, body string, headers ...string) *http.Response { + var header http.Header + if len(headers) > 0 { + header = make(http.Header) + for i := 0; i < len(headers); i += 2 { + header.Add(headers[i], headers[i+1]) + } + } + var bodyReadCloser io.ReadCloser + var bodyContentLength = int64(0) + if body != "" { + bodyReadCloser = io.NopCloser(strings.NewReader(body)) + bodyContentLength = int64(len(body)) + } + return &http.Response{ + StatusCode: statusCode, + Status: http.StatusText(statusCode), + Proto: request.Proto, + ProtoMajor: request.ProtoMajor, + ProtoMinor: request.ProtoMinor, + Header: header, + Body: bodyReadCloser, + ContentLength: bodyContentLength, + Close: true, + } +}