From 6881f35805e12a8339fb7c5e5ef1ecb611be811f Mon Sep 17 00:00:00 2001 From: septs Date: Sun, 8 Oct 2023 11:54:44 +0800 Subject: [PATCH] Improve naive auth logical --- go.mod | 2 +- go.sum | 4 ++-- inbound/naive.go | 12 +++--------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index f8cd20ed..5193a3b0 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/sagernet/gvisor v0.0.0-20230930141345-5fef6f2e17ab github.com/sagernet/quic-go v0.0.0-20231001051131-0fc736a289bb github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 - github.com/sagernet/sing v0.2.14-0.20231006114104-b68485f62e9d + github.com/sagernet/sing v0.2.14-0.20231008040725-e690cb9a7ad2 github.com/sagernet/sing-dns v0.1.10 github.com/sagernet/sing-mux v0.1.3 github.com/sagernet/sing-quic v0.1.3-0.20231006113617-1ea488a34257 diff --git a/go.sum b/go.sum index 4ce81076..44ba0b2c 100644 --- a/go.sum +++ b/go.sum @@ -108,8 +108,8 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU= github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY= github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk= -github.com/sagernet/sing v0.2.14-0.20231006114104-b68485f62e9d h1:vV9APbgB3X+Fz65f7Adg0+KzmFBJOlwAE8818gdiLsk= -github.com/sagernet/sing v0.2.14-0.20231006114104-b68485f62e9d/go.mod h1:AhNEHu0GXrpqkuzvTwvC8+j2cQUU/dh+zLEmq4C99pg= +github.com/sagernet/sing v0.2.14-0.20231008040725-e690cb9a7ad2 h1:fbrU4QJwN6XLLPDwSSLgyUru2niwJRvkpNuHsH51vhs= +github.com/sagernet/sing v0.2.14-0.20231008040725-e690cb9a7ad2/go.mod h1:AhNEHu0GXrpqkuzvTwvC8+j2cQUU/dh+zLEmq4C99pg= github.com/sagernet/sing-dns v0.1.10 h1:iIU7nRBlUYj+fF2TaktGIvRiTFFrHwSMedLQsvlTZCI= github.com/sagernet/sing-dns v0.1.10/go.mod h1:vtUimtf7Nq9EdvD5WTpfCr69KL1M7bcgOVKiYBiAY/c= github.com/sagernet/sing-mux v0.1.3 h1:fAf7PZa2A55mCeh0KKM02f1k2Y4vEmxuZZ/51ahkkLA= diff --git a/inbound/naive.go b/inbound/naive.go index 7542ae0c..0ea2bbb5 100644 --- a/inbound/naive.go +++ b/inbound/naive.go @@ -2,7 +2,6 @@ package inbound import ( "context" - "encoding/base64" "encoding/binary" "io" "math/rand" @@ -139,14 +138,9 @@ func (n *Naive) ServeHTTP(writer http.ResponseWriter, request *http.Request) { n.badRequest(ctx, request, E.New("missing naive padding")) return } - var authOk bool - var userName string - authorization := request.Header.Get("Proxy-Authorization") - if strings.HasPrefix(authorization, "BASIC ") || strings.HasPrefix(authorization, "Basic ") { - userPassword, _ := base64.URLEncoding.DecodeString(authorization[6:]) - userPswdArr := strings.SplitN(string(userPassword), ":", 2) - userName = userPswdArr[0] - authOk = n.authenticator.Verify(userPswdArr[0], userPswdArr[1]) + userName, password, authOk := sHttp.ParseBasicAuth(request.Header.Get("Proxy-Authorization")) + if authOk { + authOk = n.authenticator.Verify(userName, password) } if !authOk { rejectHTTP(writer, http.StatusProxyAuthRequired)