diff --git a/lib/common/util.go b/lib/common/util.go index d216588..895316f 100755 --- a/lib/common/util.go +++ b/lib/common/util.go @@ -50,6 +50,28 @@ func DomainCheck(domain string) bool { return match } +// 判断是否有有效的账号 +func hasValidAccount(accountMap map[string]string) bool { + if accountMap == nil { + return false + } + + for u, p := range accountMap { + if u != "" && p != "" { + return true + } + } + return false +} + +// 判断是否需要验证 +// user global user +// passwd global passwd +// accountMap enable multi user auth +func HasValid(user, passwd string, accountMap map[string]string) bool { + return hasValidAccount(accountMap) || (user != "" && passwd != "") +} + // CheckAuthWithAccountMap // u current login user // p current login passwd @@ -57,6 +79,11 @@ func DomainCheck(domain string) bool { // passwd global passwd // accountMap enable multi user auth func checkAuthWithAccountMap(u, p, user, passwd string, accountMap map[string]string) bool { + // 是否需要验证 + if !HasValid(user, passwd, accountMap) { + return true + } + // invalid user or passwd if u == "" || p == "" { return false @@ -91,6 +118,11 @@ func CheckAuthWithAccountMap(u, p, user, passwd string, accountMap map[string]st //Check if the Request request is validated func CheckAuth(r *http.Request, user, passwd string, accountMap map[string]string) bool { + // 是否需要验证 + if !HasValid(user, passwd, accountMap) { + return true + } + s := strings.SplitN(r.Header.Get("Authorization"), " ", 2) if len(s) != 2 { s = strings.SplitN(r.Header.Get("Proxy-Authorization"), " ", 2) diff --git a/server/proxy/base.go b/server/proxy/base.go index c51166a..58e240f 100644 --- a/server/proxy/base.go +++ b/server/proxy/base.go @@ -65,7 +65,7 @@ func (s *BaseServer) writeConnFail(c net.Conn) { //auth check func (s *BaseServer) auth(r *http.Request, c *conn.Conn, u, p string, AccountMap map[string]string) error { - if u != "" && p != "" && !common.CheckAuth(r, u, p, AccountMap) { + if !common.CheckAuth(r, u, p, AccountMap) { var resp = common.UnauthorizedBytes resp = strings.ReplaceAll(resp, "\n", "\r\n") c.Write([]byte(resp)) diff --git a/server/proxy/socks5.go b/server/proxy/socks5.go index 795ff7e..52d1c76 100755 --- a/server/proxy/socks5.go +++ b/server/proxy/socks5.go @@ -302,7 +302,13 @@ func (s *Sock5ModeServer) handleConn(c net.Conn) { c.Close() return } - if (s.task.Client.Cnf.U != "" && s.task.Client.Cnf.P != "") || (s.task.MultiAccount != nil && len(s.task.MultiAccount.AccountMap) > 0) { + + var accountMap map[string]string = nil + if s.task.MultiAccount != nil { + accountMap = s.task.MultiAccount.AccountMap + } + + if common.HasValid(s.task.Client.Cnf.U, s.task.Client.Cnf.P, accountMap) { buf[1] = UserPassAuth c.Write(buf) if err := s.Auth(c); err != nil {