Refactor the Authenticator interface to a struct

This commit is contained in:
世界 2023-12-08 14:52:58 +08:00
parent 231d7607bc
commit 0f7de716ac
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 17 additions and 25 deletions

View file

@ -1,38 +1,30 @@
package auth package auth
type Authenticator interface { import "github.com/sagernet/sing/common"
Verify(user string, pass string) bool
Users() []string
}
type User struct { type User struct {
Username string `json:"username"` Username string
Password string `json:"password"` Password string
} }
type inMemoryAuthenticator struct { type Authenticator struct {
storage map[string]string userMap map[string][]string
usernames []string
} }
func (au *inMemoryAuthenticator) Verify(username string, password string) bool { func NewAuthenticator(users []User) *Authenticator {
realPass, ok := au.storage[username]
return ok && realPass == password
}
func (au *inMemoryAuthenticator) Users() []string { return au.usernames }
func NewAuthenticator(users []User) Authenticator {
if len(users) == 0 { if len(users) == 0 {
return nil return nil
} }
au := &inMemoryAuthenticator{ au := &Authenticator{
storage: make(map[string]string), userMap: make(map[string][]string),
usernames: make([]string, 0, len(users)),
} }
for _, user := range users { for _, user := range users {
au.storage[user.Username] = user.Password au.userMap[user.Username] = append(au.userMap[user.Username], user.Password)
au.usernames = append(au.usernames, user.Username)
} }
return au return au
} }
func (au *Authenticator) Verify(username string, password string) bool {
passwordList, ok := au.userMap[username]
return ok && common.Contains(passwordList, password)
}

View file

@ -21,7 +21,7 @@ import (
type Handler = N.TCPConnectionHandler type Handler = N.TCPConnectionHandler
func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator *auth.Authenticator, handler Handler, metadata M.Metadata) error {
var httpClient *http.Client var httpClient *http.Client
for { for {
request, err := ReadRequest(reader) request, err := ReadRequest(reader)

View file

@ -93,7 +93,7 @@ func ClientHandshake5(conn io.ReadWriter, command byte, destination M.Socksaddr,
return response, err return response, err
} }
func HandleConnection(ctx context.Context, conn net.Conn, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { func HandleConnection(ctx context.Context, conn net.Conn, authenticator *auth.Authenticator, handler Handler, metadata M.Metadata) error {
version, err := rw.ReadByte(conn) version, err := rw.ReadByte(conn)
if err != nil { if err != nil {
return err return err
@ -101,7 +101,7 @@ func HandleConnection(ctx context.Context, conn net.Conn, authenticator auth.Aut
return HandleConnection0(ctx, conn, version, authenticator, handler, metadata) return HandleConnection0(ctx, conn, version, authenticator, handler, metadata)
} }
func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { func HandleConnection0(ctx context.Context, conn net.Conn, version byte, authenticator *auth.Authenticator, handler Handler, metadata M.Metadata) error {
switch version { switch version {
case socks4.Version: case socks4.Version:
request, err := socks4.ReadRequest0(conn) request, err := socks4.ReadRequest0(conn)