From 7d846f1405c11a7ea0c69152b5e0ab179be39568 Mon Sep 17 00:00:00 2001 From: Alexander Ng Date: Fri, 12 Jan 2024 03:27:55 -0800 Subject: [PATCH] feat: credentials from JSON --- credentials.go | 27 +++++++++++++++++++++++++++ server.go | 24 ++++++++++++++++-------- 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 credentials.go diff --git a/credentials.go b/credentials.go new file mode 100644 index 0000000..65cf8f9 --- /dev/null +++ b/credentials.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/json" + + "github.com/armon/go-socks5" +) + +type credentials struct { + username string `json:"username"` + password string `json:"password"` +} + +func parseCredentials(credsString string) (socks5.StaticCredentials, error) { + var creds []credentials + err := json.Unmarshal([]byte(credsString), &creds) + if err != nil { + return nil, err + } + + var credsMap socks5.StaticCredentials + for _, cred := range creds { + credsMap[cred.username] = cred.password + } + + return credsMap, nil +} diff --git a/server.go b/server.go index 921f1ac..8c148aa 100644 --- a/server.go +++ b/server.go @@ -10,11 +10,12 @@ import ( ) type params struct { - User string `env:"PROXY_USER" envDefault:""` - Password string `env:"PROXY_PASSWORD" envDefault:""` - Port string `env:"PROXY_PORT" envDefault:"1080"` - AllowedDestFqdn string `env:"ALLOWED_DEST_FQDN" envDefault:""` - AllowedIPs []string `env:"ALLOWED_IPS" envSeparator:"," envDefault:""` + Creds string `env:"PROXY_CREDENTIALS" envDefault:""` + User string `env:"PROXY_USER" envDefault:""` + Password string `env:"PROXY_PASSWORD" envDefault:""` + Port string `env:"PROXY_PORT" envDefault:"1080"` + AllowedDestFqdn string `env:"ALLOWED_DEST_FQDN" envDefault:""` + AllowedIPs []string `env:"ALLOWED_IPS" envSeparator:"," envDefault:""` } func main() { @@ -30,10 +31,17 @@ func main() { Logger: log.New(os.Stdout, "", log.LstdFlags), } - if cfg.User+cfg.Password != "" { - creds := socks5.StaticCredentials{ - os.Getenv("PROXY_USER"): os.Getenv("PROXY_PASSWORD"), + var creds socks5.StaticCredentials + if cfg.Creds != "" { + creds, err = parseCredentials(cfg.Creds) + if err != nil { + log.Printf("%+v\n", err) } + } + if cfg.User+cfg.Password != "" { + creds[cfg.User] = cfg.Password + } + if len(creds) > 0 { cator := socks5.UserPassAuthenticator{Credentials: creds} socks5conf.AuthMethods = []socks5.Authenticator{cator} }