feat: credentials from JSON

This commit is contained in:
Alexander Ng 2024-01-12 03:27:55 -08:00
parent d0347549a4
commit 7d846f1405
No known key found for this signature in database
GPG Key ID: 54CA3BAC2DBFEE4C
2 changed files with 43 additions and 8 deletions

27
credentials.go Normal file
View File

@ -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
}

View File

@ -10,6 +10,7 @@ import (
) )
type params struct { type params struct {
Creds string `env:"PROXY_CREDENTIALS" envDefault:""`
User string `env:"PROXY_USER" envDefault:""` User string `env:"PROXY_USER" envDefault:""`
Password string `env:"PROXY_PASSWORD" envDefault:""` Password string `env:"PROXY_PASSWORD" envDefault:""`
Port string `env:"PROXY_PORT" envDefault:"1080"` Port string `env:"PROXY_PORT" envDefault:"1080"`
@ -30,10 +31,17 @@ func main() {
Logger: log.New(os.Stdout, "", log.LstdFlags), Logger: log.New(os.Stdout, "", log.LstdFlags),
} }
if cfg.User+cfg.Password != "" { var creds socks5.StaticCredentials
creds := socks5.StaticCredentials{ if cfg.Creds != "" {
os.Getenv("PROXY_USER"): os.Getenv("PROXY_PASSWORD"), 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} cator := socks5.UserPassAuthenticator{Credentials: creds}
socks5conf.AuthMethods = []socks5.Authenticator{cator} socks5conf.AuthMethods = []socks5.Authenticator{cator}
} }