diff --git a/nanoproxy.go b/nanoproxy.go index 5eca6ee..8af27f9 100644 --- a/nanoproxy.go +++ b/nanoproxy.go @@ -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{} @@ -77,7 +80,7 @@ func main() { }() } - if len(credentials) > 0 { + if len(cfg.Credentials) > 0 { authenticator := &socks5.UserPassAuthenticator{ Credentials: credentials, } diff --git a/pkg/credential/credentials.go b/pkg/credential/credentials.go index e6bccea..45ef412 100644 --- a/pkg/credential/credentials.go +++ b/pkg/credential/credentials.go @@ -5,13 +5,26 @@ import ( ) type Store interface { + Add(user, password string) 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) { + s.store[user] = password +} func (s StaticCredentialStore) Valid(user, password string) bool { - pass, ok := s[user] + pass, ok := s.store[user] if !ok { return false } diff --git a/pkg/credential/credentials_test.go b/pkg/credential/credentials_test.go index b65deea..5863f4c 100644 --- a/pkg/credential/credentials_test.go +++ b/pkg/credential/credentials_test.go @@ -7,7 +7,9 @@ import ( func Test_CredentialStore_Valid(t *testing.T) { s := StaticCredentialStore{ - "foo": "$2y$05$Xr4Vj6wbsCuf70.Fif2guuX8Ez97GB0VysyCTRL2EMkIikCpY/ugi", + store: map[string]string{ + "foo": "$2y$05$Xr4Vj6wbsCuf70.Fif2guuX8Ez97GB0VysyCTRL2EMkIikCpY/ugi", + }, } assert.True(t, s.Valid("foo", "bar")) assert.False(t, s.Valid("foo", "baz")) diff --git a/pkg/httpproxy/httpproxy_test.go b/pkg/httpproxy/httpproxy_test.go index 8574b3e..0ba645b 100644 --- a/pkg/httpproxy/httpproxy_test.go +++ b/pkg/httpproxy/httpproxy_test.go @@ -18,6 +18,10 @@ import ( type MockCredentialStore struct{} +func (m *MockCredentialStore) Add(username, password string) { + +} + func (m *MockCredentialStore) Valid(username, password string) bool { return username == "user" && password == "password" } diff --git a/pkg/socks5/auth_test.go b/pkg/socks5/auth_test.go index 29a48a5..d0e634d 100644 --- a/pkg/socks5/auth_test.go +++ b/pkg/socks5/auth_test.go @@ -10,6 +10,10 @@ type mockCredentialStore struct { valid bool } +func (m *mockCredentialStore) Add(user, password string) { + +} + func (m *mockCredentialStore) Valid(user, password string) bool { return m.valid } diff --git a/pkg/socks5/socks5_test.go b/pkg/socks5/socks5_test.go index fc167bc..55bbd50 100644 --- a/pkg/socks5/socks5_test.go +++ b/pkg/socks5/socks5_test.go @@ -48,9 +48,9 @@ func TestListenAndServe(t *testing.T) { }() lAddr := l.Addr().(*net.TCPAddr) - credentials := credential.StaticCredentialStore{ - "foo": "$2y$05$Xr4Vj6wbsCuf70.Fif2guuX8Ez97GB0VysyCTRL2EMkIikCpY/ugi", // foo:bar - } + credentials := credential.NewStaticCredentialStore() + credentials.Add("foo", "$2y$05$Xr4Vj6wbsCuf70.Fif2guuX8Ez97GB0VysyCTRL2EMkIikCpY/ugi") + auth := &UserPassAuthenticator{Credentials: credentials} conf := &Config{ Authentication: []Authenticator{auth}, @@ -112,9 +112,8 @@ func TestListenAndServe_InvalidCredentials(t *testing.T) { lAddr := l.Addr().(*net.TCPAddr) - credentials := credential.StaticCredentialStore{ - "foo": "bar", - } + credentials := credential.NewStaticCredentialStore() + credentials.Add("foo", "bar") auth := &UserPassAuthenticator{Credentials: credentials} conf := &Config{ Authentication: []Authenticator{auth}, @@ -164,9 +163,8 @@ func TestListenAndServe_InvalidAuthType(t *testing.T) { assert.NoError(t, err) lAddr := l.Addr().(*net.TCPAddr) - credentials := credential.StaticCredentialStore{ - "foo": "bar", - } + credentials := credential.NewStaticCredentialStore() + credentials.Add("foo", "bar") auth := &UserPassAuthenticator{Credentials: credentials} conf := &Config{