feat: credentials from JSON
This commit is contained in:
parent
d0347549a4
commit
7d846f1405
27
credentials.go
Normal file
27
credentials.go
Normal 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
|
||||||
|
}
|
14
server.go
14
server.go
@ -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}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user