Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: authentication cannot disable #30

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 15 additions & 2 deletions pkg/credential/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/credential/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
4 changes: 4 additions & 0 deletions pkg/httpproxy/httpproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/socks5/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 7 additions & 9 deletions pkg/socks5/socks5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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{
Expand Down
Loading