fix socks没密码时提示密码 #789

pull/968/head
ImmortalD 2022-01-03 00:55:47 +08:00
parent 856d881659
commit a58bec2efe
3 changed files with 40 additions and 2 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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 {