add auther config

This commit is contained in:
ginuerzh
2022-02-12 00:33:20 +08:00
parent c1bf501734
commit a8a6bbc3a3
37 changed files with 261 additions and 183 deletions

View File

@ -23,7 +23,6 @@ func init() {
}
type httpConnector struct {
user *url.Userinfo
md metadata
options connector.Options
}
@ -35,7 +34,6 @@ func NewConnector(opts ...connector.Option) connector.Connector {
}
return &httpConnector{
user: options.User,
options: options,
}
}
@ -67,7 +65,7 @@ func (c *httpConnector) Connect(ctx context.Context, conn net.Conn, network, add
}
req.Header.Set("Proxy-Connection", "keep-alive")
if user := c.user; user != nil {
if user := c.options.Auth; user != nil {
u := user.Username()
p, _ := user.Password()
req.Header.Set("Proxy-Authorization",

View File

@ -24,7 +24,6 @@ func init() {
}
type http2Connector struct {
user *url.Userinfo
md metadata
options connector.Options
}
@ -36,7 +35,6 @@ func NewConnector(opts ...connector.Option) connector.Connector {
}
return &http2Connector{
user: options.User,
options: options,
}
}
@ -76,7 +74,7 @@ func (c *http2Connector) Connect(ctx context.Context, conn net.Conn, network, ad
req.Header.Set("User-Agent", c.md.UserAgent)
}
if user := c.user; user != nil {
if user := c.options.Auth; user != nil {
u := user.Username()
p, _ := user.Password()
req.Header.Set("Proxy-Authorization",

View File

@ -9,16 +9,16 @@ import (
)
type Options struct {
User *url.Userinfo
Auth *url.Userinfo
TLSConfig *tls.Config
Logger logger.Logger
}
type Option func(opts *Options)
func UserOption(user *url.Userinfo) Option {
func AuthOption(auth *url.Userinfo) Option {
return func(opts *Options) {
opts.User = user
opts.Auth = auth
}
}

View File

@ -82,10 +82,10 @@ func (c *relayConnector) bind(conn net.Conn, cmd uint8, network, address string)
Flags: cmd,
}
if c.user != nil {
pwd, _ := c.user.Password()
if c.options.Auth != nil {
pwd, _ := c.options.Auth.Password()
req.Features = append(req.Features, &relay.UserAuthFeature{
Username: c.user.Username(),
Username: c.options.Auth.Username(),
Password: pwd,
})
}

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net"
"net/url"
"time"
"github.com/go-gost/gost/pkg/common/util/socks"
@ -19,7 +18,6 @@ func init() {
}
type relayConnector struct {
user *url.Userinfo
md metadata
options connector.Options
}
@ -31,7 +29,6 @@ func NewConnector(opts ...connector.Option) connector.Connector {
}
return &relayConnector{
user: options.User,
options: options,
}
}
@ -73,10 +70,10 @@ func (c *relayConnector) Connect(ctx context.Context, conn net.Conn, network, ad
}
}
if c.user != nil {
pwd, _ := c.user.Password()
if c.options.Auth != nil {
pwd, _ := c.options.Auth.Password()
req.Features = append(req.Features, &relay.UserAuthFeature{
Username: c.user.Username(),
Username: c.options.Auth.Username(),
Password: pwd,
})
}

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"time"
@ -21,7 +20,6 @@ func init() {
}
type socks4Connector struct {
user *url.Userinfo
md metadata
options connector.Options
}
@ -33,7 +31,6 @@ func NewConnector(opts ...connector.Option) connector.Connector {
}
return &socks4Connector{
user: options.User,
options: options,
}
}
@ -99,8 +96,8 @@ func (c *socks4Connector) Connect(ctx context.Context, conn net.Conn, network, a
}
var userid []byte
if c.user != nil && c.user.Username() != "" {
userid = []byte(c.user.Username())
if c.options.Auth != nil {
userid = []byte(c.options.Auth.Username())
}
req := gosocks4.NewRequest(gosocks4.CmdConnect, addr, userid)
if err := req.Write(conn); err != nil {

View File

@ -48,7 +48,7 @@ func (c *socks5Connector) Init(md md.Metadata) (err error) {
gosocks5.MethodNoAuth,
gosocks5.MethodUserPass,
},
User: c.options.User,
User: c.options.Auth,
TLSConfig: c.options.TLSConfig,
logger: c.options.Logger,
}

View File

@ -41,9 +41,9 @@ func (c *ssConnector) Init(md md.Metadata) (err error) {
return
}
if c.options.User != nil {
method := c.options.User.Username()
password, _ := c.options.User.Password()
if c.options.Auth != nil {
method := c.options.Auth.Username()
password, _ := c.options.Auth.Password()
c.cipher, err = ss.ShadowCipher(method, password, c.md.key)
}

View File

@ -40,9 +40,9 @@ func (c *ssuConnector) Init(md md.Metadata) (err error) {
return
}
if c.options.User != nil {
method := c.options.User.Username()
password, _ := c.options.User.Password()
if c.options.Auth != nil {
method := c.options.Auth.Username()
password, _ := c.options.Auth.Password()
c.cipher, err = ss.ShadowCipher(method, password, c.md.key)
}