fix log usage
This commit is contained in:
@ -10,15 +10,18 @@ import (
|
||||
"github.com/go-gost/gost/pkg/common/util/socks"
|
||||
"github.com/go-gost/gost/pkg/common/util/udp"
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
// Bind implements connector.Binder.
|
||||
func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
c.logger = c.logger.WithFields(map[string]interface{}{
|
||||
log := c.options.Logger.WithFields(map[string]interface{}{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
c.logger.Infof("bind: %s/%s", address, network)
|
||||
log.Infof("bind: %s/%s", address, network)
|
||||
|
||||
options := connector.BindOptions{}
|
||||
for _, opt := range opts {
|
||||
@ -28,20 +31,20 @@ func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, addr
|
||||
switch network {
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
if options.Mux {
|
||||
return c.muxBindTCP(ctx, conn, network, address)
|
||||
return c.muxBindTCP(ctx, conn, network, address, log)
|
||||
}
|
||||
return c.bindTCP(ctx, conn, network, address)
|
||||
return c.bindTCP(ctx, conn, network, address, log)
|
||||
case "udp", "udp4", "udp6":
|
||||
return c.bindUDP(ctx, conn, network, address, &options)
|
||||
return c.bindUDP(ctx, conn, network, address, &options, log)
|
||||
default:
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindTCP(ctx context.Context, conn net.Conn, network, address string) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, gosocks5.CmdBind, network, address)
|
||||
func (c *socks5Connector) bindTCP(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, gosocks5.CmdBind, network, address, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -49,12 +52,12 @@ func (c *socks5Connector) bindTCP(ctx context.Context, conn net.Conn, network, a
|
||||
return &tcpListener{
|
||||
addr: laddr,
|
||||
conn: conn,
|
||||
logger: c.logger,
|
||||
logger: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network, address string) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdMuxBind, network, address)
|
||||
func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdMuxBind, network, address, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -67,12 +70,12 @@ func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network
|
||||
return &tcpMuxListener{
|
||||
addr: laddr,
|
||||
session: session,
|
||||
logger: c.logger,
|
||||
logger: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindUDP(ctx context.Context, conn net.Conn, network, address string, opts *connector.BindOptions) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdUDPTun, network, address)
|
||||
func (c *socks5Connector) bindUDP(ctx context.Context, conn net.Conn, network, address string, opts *connector.BindOptions, log logger.Logger) (net.Listener, error) {
|
||||
laddr, err := c.bind(conn, socks.CmdUDPTun, network, address, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -83,19 +86,19 @@ func (c *socks5Connector) bindUDP(ctx context.Context, conn net.Conn, network, a
|
||||
opts.Backlog,
|
||||
opts.UDPDataQueueSize, opts.UDPDataBufferSize,
|
||||
opts.UDPConnTTL,
|
||||
c.logger)
|
||||
log)
|
||||
|
||||
return ln, nil
|
||||
}
|
||||
|
||||
func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string) (net.Addr, error) {
|
||||
func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string, log logger.Logger) (net.Addr, error) {
|
||||
addr := gosocks5.Addr{}
|
||||
addr.ParseFrom(address)
|
||||
req := gosocks5.NewRequest(cmd, &addr)
|
||||
if err := req.Write(conn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.logger.Debug(req)
|
||||
log.Debug(req)
|
||||
|
||||
// first reply, bind status
|
||||
reply, err := gosocks5.ReadReply(conn)
|
||||
@ -103,7 +106,7 @@ func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.logger.Debug(reply)
|
||||
log.Debug(reply)
|
||||
|
||||
if reply.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("bind on %s/%s failed", address, network)
|
||||
@ -121,7 +124,7 @@ func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.logger.Debugf("bind on %s/%s OK", baddr, baddr.Network())
|
||||
log.Debugf("bind on %s/%s OK", baddr, baddr.Network())
|
||||
|
||||
return baddr, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user