fix: make(socks5.StaticCredentials)

This commit is contained in:
Alexander Ng 2024-01-12 22:31:08 -08:00
parent ba68c42b6f
commit 07b5f76b66
No known key found for this signature in database
GPG Key ID: 54CA3BAC2DBFEE4C
2 changed files with 20 additions and 18 deletions

View File

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

View File

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