Skip to content

Commit

Permalink
add auth parameter (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jan 22, 2020
1 parent 990c7b5 commit 56e2ac3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
42 changes: 37 additions & 5 deletions cmd/gost/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"fmt"
"net"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 56e2ac3

Please sign in to comment.