Skip to content

Commit

Permalink
fix(deps): refactor StaticCredentialStore initialization
Browse files Browse the repository at this point in the history
Refactored StaticCredentialStore to use a struct with a map instead
of a plain map, adding methods for better encapsulation. Updated
credentials handling for proper initialization and improved password
storage using bcrypt hashing.
  • Loading branch information
ryanbekhen committed Dec 13, 2024
1 parent 2cd5713 commit 6a96a44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
19 changes: 11 additions & 8 deletions nanoproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ func main() {
time.Local = loc
}

credentials := credential.StaticCredentialStore{}
for _, cred := range cfg.Credentials {
credArr := strings.Split(cred, ":")
if len(credArr) != 2 {
logger.Fatal().Msgf("Invalid credential: %s", cred)
var credentials credential.Store
if len(cfg.Credentials) > 0 {
credentials := credential.NewStaticCredentialStore()
for _, cred := range cfg.Credentials {
credArr := strings.Split(cred, ":")
if len(credArr) != 2 {
logger.Fatal().Msgf("Invalid credential: %s", cred)
}

credentials.Add(credArr[0], credArr[1])
}

credentials[credArr[0]] = credArr[1]
}

dnsResolver := &resolver.DNSResolver{}
Expand Down Expand Up @@ -77,7 +80,7 @@ func main() {
}()
}

if len(credentials) > 0 {
if len(cfg.Credentials) > 0 {
authenticator := &socks5.UserPassAuthenticator{
Credentials: credentials,
}
Expand Down
21 changes: 19 additions & 2 deletions pkg/credential/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,27 @@ type Store interface {
Valid(user, password string) bool
}

type StaticCredentialStore map[string]string
type StaticCredentialStore struct {
store map[string]string
}

func NewStaticCredentialStore() *StaticCredentialStore {
return &StaticCredentialStore{
store: make(map[string]string),
}
}

func (s StaticCredentialStore) Add(user, password string) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return
}

s.store[user] = string(hash)
}

func (s StaticCredentialStore) Valid(user, password string) bool {
pass, ok := s[user]
pass, ok := s.store[user]
if !ok {
return false
}
Expand Down

0 comments on commit 6a96a44

Please sign in to comment.