From ec2595f010e2edb4790ba2c2447a8543182e3012 Mon Sep 17 00:00:00 2001
From: DuFoxit <119046357+DuFoxit@users.noreply.github.com>
Date: Tue, 5 Mar 2024 13:04:20 +0800
Subject: [PATCH] Fix SwitchyOmega authentication failed

---
 protocol/http/handshake.go | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/protocol/http/handshake.go b/protocol/http/handshake.go
index 4fd5b52..d050e46 100644
--- a/protocol/http/handshake.go
+++ b/protocol/http/handshake.go
@@ -50,7 +50,11 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
 				}
 			}
 			if !authOk {
-				err = responseWith(request, http.StatusProxyAuthRequired).Write(conn)
+				// Since no one else is using the library, use a fixed realm until rewritten
+				err = responseWith(
+					request, http.StatusProxyAuthRequired,
+					"Proxy-Authenticate", `Basic realm="sing-box" charset="UTF-8"`,
+				).Write(conn)
 				if err != nil {
 					return err
 				}
@@ -206,13 +210,20 @@ func removeExtraHTTPHostPort(req *http.Request) {
 	req.URL.Host = host
 }
 
-func responseWith(request *http.Request, statusCode int) *http.Response {
+func responseWith(request *http.Request, statusCode int, 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])
+		}
+	}
 	return &http.Response{
 		StatusCode: statusCode,
 		Status:     http.StatusText(statusCode),
 		Proto:      request.Proto,
 		ProtoMajor: request.ProtoMajor,
 		ProtoMinor: request.ProtoMinor,
-		Header:     http.Header{},
+		Header:     header,
 	}
 }