Use linkname

This commit is contained in:
世界 2023-10-08 11:54:04 +08:00
parent 2258cd248b
commit d273c2f39d
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4

View File

@ -10,6 +10,7 @@ import (
"os" "os"
"strings" "strings"
"time" "time"
_ "unsafe"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/tls" "github.com/sagernet/sing-box/common/tls"
@ -127,6 +128,9 @@ func (n *Naive) Close() error {
) )
} }
//go:linkname parseBasicAuth http.parseBasicAuth
func parseBasicAuth(auth string) (username, password string, ok bool)
func (n *Naive) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (n *Naive) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
ctx := log.ContextWithNewID(request.Context()) ctx := log.ContextWithNewID(request.Context())
if request.Method != "CONNECT" { if request.Method != "CONNECT" {
@ -138,13 +142,11 @@ func (n *Naive) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
n.badRequest(ctx, request, E.New("missing naive padding")) n.badRequest(ctx, request, E.New("missing naive padding"))
return return
} }
// HACK: reuse go builtin basic auth parsing userName, password, authOk := parseBasicAuth(request.Header.Get("Proxy-Authorization"))
request.Header.Set("Authorization", request.Header.Get("Proxy-Authorization")) if authOk {
userName, password, ok := request.BasicAuth() authOk = n.authenticator.Verify(userName, password)
if ok {
ok = n.authenticator.Verify(userName, password)
} }
if !ok { if !authOk {
rejectHTTP(writer, http.StatusProxyAuthRequired) rejectHTTP(writer, http.StatusProxyAuthRequired)
n.badRequest(ctx, request, E.New("authorization failed")) n.badRequest(ctx, request, E.New("authorization failed"))
return return