Skip to content

Commit

Permalink
fix http proxy for PuTTY
Browse files Browse the repository at this point in the history
Change-Id: I30dcc7750a6f7b9980655770929c8bd5d2b1b3c8
  • Loading branch information
twu2 committed Feb 5, 2025
1 parent 23af22f commit 08e7960
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions protocol/http/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
}
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 08e7960

Please sign in to comment.