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 ( import (
"encoding/json" "encoding/json"
"github.com/armon/go-socks5"
) )
type Credentials struct { type Credentials struct {
@ -9,12 +11,24 @@ type Credentials struct {
Password string `json:"password"` Password string `json:"password"`
} }
func parseCredentials(credsString string) ([]Credentials, error) { func getCredentials(params params) (socks5.StaticCredentials, error) {
var creds []Credentials var creds socks5.StaticCredentials = make(socks5.StaticCredentials)
err := json.Unmarshal([]byte(credsString), &creds)
if params.Creds != "" {
var parsed_env_creds []Credentials
err := json.Unmarshal([]byte(params.Creds), &parsed_env_creds)
if err != nil { if err != nil {
return nil, err 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 return creds, nil
} }

View File

@ -34,20 +34,8 @@ func main() {
log.Printf(cfg.Creds) log.Printf(cfg.Creds)
var creds socks5.StaticCredentials var creds socks5.StaticCredentials
if cfg.Creds != "" { creds, err = getCredentials(cfg)
var uname_pw_pairs, creds_err = parseCredentials(cfg.Creds)
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 { if len(creds) > 0 {
cator := socks5.UserPassAuthenticator{Credentials: creds} cator := socks5.UserPassAuthenticator{Credentials: creds}
socks5conf.AuthMethods = []socks5.Authenticator{cator} socks5conf.AuthMethods = []socks5.Authenticator{cator}