From 07b5f76b66eaa213ec15eea5f0a9e79c4305f1f4 Mon Sep 17 00:00:00 2001 From: Alexander Ng Date: Fri, 12 Jan 2024 22:31:08 -0800 Subject: [PATCH] fix: make(socks5.StaticCredentials) --- credentials.go | 24 +++++++++++++++++++----- server.go | 14 +------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/credentials.go b/credentials.go index 5f3c039..de889ca 100644 --- a/credentials.go +++ b/credentials.go @@ -2,6 +2,8 @@ package main import ( "encoding/json" + + "github.com/armon/go-socks5" ) type Credentials struct { @@ -9,11 +11,23 @@ type Credentials struct { Password string `json:"password"` } -func parseCredentials(credsString string) ([]Credentials, error) { - var creds []Credentials - err := json.Unmarshal([]byte(credsString), &creds) - if err != nil { - return nil, err +func getCredentials(params params) (socks5.StaticCredentials, error) { + var creds socks5.StaticCredentials = make(socks5.StaticCredentials) + + if params.Creds != "" { + var parsed_env_creds []Credentials + err := json.Unmarshal([]byte(params.Creds), &parsed_env_creds) + if err != nil { + return nil, err + } + + for _, kv := range parsed_env_creds { + creds[kv.Username] = kv.Password + } + } + + if params.User+params.Password != "" { + creds[params.User] = params.Password } return creds, nil diff --git a/server.go b/server.go index f171d9f..8292a44 100644 --- a/server.go +++ b/server.go @@ -34,20 +34,8 @@ func main() { log.Printf(cfg.Creds) var creds socks5.StaticCredentials - if cfg.Creds != "" { - var uname_pw_pairs, creds_err = parseCredentials(cfg.Creds) + creds, err = getCredentials(cfg) - if creds_err != nil { - log.Printf("%+v\n", err) - } - - for _, kv := range uname_pw_pairs { - creds[kv.Username] = kv.Password - } - } - if cfg.User+cfg.Password != "" { - creds[cfg.User] = cfg.Password - } if len(creds) > 0 { cator := socks5.UserPassAuthenticator{Credentials: creds} socks5conf.AuthMethods = []socks5.Authenticator{cator}