diff --git a/cmd/gost/route.go b/cmd/gost/route.go index a215877d..ff115a8e 100644 --- a/cmd/gost/route.go +++ b/cmd/gost/route.go @@ -3,8 +3,10 @@ package main import ( "crypto/sha256" "crypto/tls" + "encoding/base64" "fmt" "net" + "net/url" "os" "strings" "time" @@ -90,13 +92,29 @@ func parseChainNode(ns string) (nodes []gost.Node, err error) { return } - users, err := parseUsers(node.Get("secrets")) - if err != nil { - return + if auth := node.Get("auth"); auth != "" && node.User == nil { + c, err := base64.StdEncoding.DecodeString(auth) + if err != nil { + return nil, err + } + cs := string(c) + s := strings.IndexByte(cs, ':') + if s < 0 { + node.User = url.User(cs) + } else { + node.User = url.UserPassword(cs[:s], cs[s+1:]) + } } - if node.User == nil && len(users) > 0 { - node.User = users[0] + if node.User == nil { + users, err := parseUsers(node.Get("secrets")) + if err != nil { + return nil, err + } + if len(users) > 0 { + node.User = users[0] + } } + serverName, sport, _ := net.SplitHostPort(node.Addr) if serverName == "" { serverName = "localhost" // default server name @@ -280,6 +298,20 @@ func (r *route) GenRouters() ([]router, error) { if err != nil { return nil, err } + + if auth := node.Get("auth"); auth != "" && node.User == nil { + c, err := base64.StdEncoding.DecodeString(auth) + if err != nil { + return nil, err + } + cs := string(c) + s := strings.IndexByte(cs, ':') + if s < 0 { + node.User = url.User(cs) + } else { + node.User = url.UserPassword(cs[:s], cs[s+1:]) + } + } authenticator, err := parseAuthenticator(node.Get("secrets")) if err != nil { return nil, err diff --git a/http.go b/http.go index 4a03ebf9..979d1e7a 100644 --- a/http.go +++ b/http.go @@ -381,11 +381,9 @@ func (h *httpHandler) forwardRequest(conn net.Conn, req *http.Request, route *Ch var userpass string if user := route.LastNode().User; user != nil { - s := user.String() - if _, set := user.Password(); !set { - s += ":" - } - userpass = base64.StdEncoding.EncodeToString([]byte(s)) + u := user.Username() + p, _ := user.Password() + userpass = base64.StdEncoding.EncodeToString([]byte(u + ":" + p)) } cc, err := route.Conn()