Skip to content

Commit

Permalink
Refactor the Authenticator interface to a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 8, 2023
1 parent 7ae7420 commit a44c4e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
36 changes: 14 additions & 22 deletions common/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package auth

type Authenticator interface {
Verify(user string, pass string) bool
Users() []string
}
import "github.com/sagernet/sing/common"

type User struct {
Username string `json:"username"`
Password string `json:"password"`
}

type inMemoryAuthenticator struct {
storage map[string]string
usernames []string
Username string
Password string
}

func (au *inMemoryAuthenticator) Verify(username string, password string) bool {
realPass, ok := au.storage[username]
return ok && realPass == password
type Authenticator struct {
userMap map[string][]string
}

func (au *inMemoryAuthenticator) Users() []string { return au.usernames }

func NewAuthenticator(users []User) Authenticator {
func NewAuthenticator(users []User) *Authenticator {
if len(users) == 0 {
return nil
}
au := &inMemoryAuthenticator{
storage: make(map[string]string),
usernames: make([]string, 0, len(users)),
au := &Authenticator{
userMap: make(map[string][]string),
}
for _, user := range users {
au.storage[user.Username] = user.Password
au.usernames = append(au.usernames, user.Username)
au.userMap[user.Username] = append(au.userMap[user.Username], user.Password)
}
return au
}

func (au *Authenticator) Verify(username string, password string) bool {
passwordList, ok := au.userMap[username]
return ok && common.Contains(passwordList, password)
}
2 changes: 1 addition & 1 deletion protocol/http/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

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
for {
request, err := ReadRequest(reader)
Expand Down
4 changes: 2 additions & 2 deletions protocol/socks/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func ClientHandshake5(conn io.ReadWriter, command byte, destination M.Socksaddr,
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)
if err != nil {
return err
}
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 {
case socks4.Version:
request, err := socks4.ReadRequest0(conn)
Expand Down

0 comments on commit a44c4e2

Please sign in to comment.