Skip to content

Commit

Permalink
fix socks没密码时提示密码 ehang-io#789
Browse files Browse the repository at this point in the history
  • Loading branch information
ImmortalD committed Jan 2, 2022
1 parent 856d881 commit a58bec2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
32 changes: 32 additions & 0 deletions lib/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,40 @@ 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
// user global user
// 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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/proxy/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 7 additions & 1 deletion server/proxy/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a58bec2

Please sign in to comment.