fix udp bind
This commit is contained in:
@ -17,7 +17,7 @@ type Accepter interface {
|
||||
}
|
||||
|
||||
type Binder interface {
|
||||
Bind(ctx context.Context, conn net.Conn, network, address string, opts ...BindOption) (Accepter, error)
|
||||
Bind(ctx context.Context, conn net.Conn, network, address string, opts ...BindOption) (net.Listener, error)
|
||||
}
|
||||
|
||||
type AcceptError struct {
|
@ -1,6 +1,8 @@
|
||||
package connector
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
@ -22,7 +24,11 @@ type ConnectOptions struct {
|
||||
type ConnectOption func(opts *ConnectOptions)
|
||||
|
||||
type BindOptions struct {
|
||||
Mux bool
|
||||
Mux bool
|
||||
Backlog int
|
||||
UDPDataQueueSize int
|
||||
UDPDataBufferSize int
|
||||
UDPConnTTL time.Duration
|
||||
}
|
||||
|
||||
type BindOption func(opts *BindOptions)
|
||||
@ -32,3 +38,27 @@ func MuxBindOption(mux bool) BindOption {
|
||||
opts.Mux = mux
|
||||
}
|
||||
}
|
||||
|
||||
func BacklogBindOption(backlog int) BindOption {
|
||||
return func(opts *BindOptions) {
|
||||
opts.Backlog = backlog
|
||||
}
|
||||
}
|
||||
|
||||
func UDPDataQueueSizeBindOption(size int) BindOption {
|
||||
return func(opts *BindOptions) {
|
||||
opts.UDPDataQueueSize = size
|
||||
}
|
||||
}
|
||||
|
||||
func UDPDataBufferSizeBindOption(size int) BindOption {
|
||||
return func(opts *BindOptions) {
|
||||
opts.UDPDataBufferSize = size
|
||||
}
|
||||
}
|
||||
|
||||
func UDPConnTTLBindOption(ttl time.Duration) BindOption {
|
||||
return func(opts *BindOptions) {
|
||||
opts.UDPConnTTL = ttl
|
||||
}
|
||||
}
|
||||
|
@ -1,191 +0,0 @@
|
||||
package v5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/common/bufpool"
|
||||
"github.com/go-gost/gost/pkg/common/util/mux"
|
||||
"github.com/go-gost/gost/pkg/common/util/udp"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type tcpAccepter struct {
|
||||
addr net.Addr
|
||||
conn net.Conn
|
||||
logger logger.Logger
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case <-p.done:
|
||||
return nil, io.EOF
|
||||
default:
|
||||
close(p.done)
|
||||
}
|
||||
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(p.conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("peer connect failed")
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: p.conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpAccepter) Close() error {
|
||||
return p.conn.Close()
|
||||
}
|
||||
|
||||
type tcpMuxAccepter struct {
|
||||
addr net.Addr
|
||||
session *mux.Session
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Accept() (net.Conn, error) {
|
||||
cc, err := p.session.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn, err := p.getPeerConn(cc)
|
||||
if err != nil {
|
||||
cc.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) getPeerConn(conn net.Conn) (net.Conn, error) {
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
err = fmt.Errorf("peer connect failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpMuxAccepter) Close() error {
|
||||
return p.session.Close()
|
||||
}
|
||||
|
||||
type udpAccepter struct {
|
||||
addr net.Addr
|
||||
conn net.PacketConn
|
||||
cqueue chan net.Conn
|
||||
connPool *udp.ConnPool
|
||||
readQueueSize int
|
||||
readBufferSize int
|
||||
closed chan struct{}
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Accept() (conn net.Conn, err error) {
|
||||
select {
|
||||
case conn = <-p.cqueue:
|
||||
return
|
||||
case <-p.closed:
|
||||
return nil, net.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
func (p *udpAccepter) acceptLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-p.closed:
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
||||
b := bufpool.Get(p.readBufferSize)
|
||||
|
||||
n, raddr, err := p.conn.ReadFrom(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
c := p.getConn(raddr)
|
||||
if c == nil {
|
||||
bufpool.Put(b)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := c.WriteQueue(b[:n]); err != nil {
|
||||
p.logger.Warn("data discarded: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *udpAccepter) Close() error {
|
||||
select {
|
||||
case <-p.closed:
|
||||
default:
|
||||
close(p.closed)
|
||||
p.connPool.Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *udpAccepter) getConn(raddr net.Addr) *udp.Conn {
|
||||
c, ok := p.connPool.Get(raddr.String())
|
||||
if !ok {
|
||||
c = udp.NewConn(p.conn, p.addr, raddr, p.readQueueSize)
|
||||
select {
|
||||
case p.cqueue <- c:
|
||||
p.connPool.Set(raddr.String(), c)
|
||||
default:
|
||||
c.Close()
|
||||
p.logger.Warnf("connection queue is full, client %s discarded", raddr)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// Bind implements connector.Binder.
|
||||
func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (connector.Accepter, error) {
|
||||
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{}{
|
||||
"network": network,
|
||||
"address": address,
|
||||
@ -32,7 +32,7 @@ func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, addr
|
||||
}
|
||||
return c.bindTCP(ctx, conn, network, address)
|
||||
case "udp", "udp4", "udp6":
|
||||
return c.bindUDP(ctx, conn, network, address)
|
||||
return c.bindUDP(ctx, conn, network, address, &options)
|
||||
default:
|
||||
err := fmt.Errorf("network %s is unsupported", network)
|
||||
c.logger.Error(err)
|
||||
@ -40,21 +40,20 @@ func (c *socks5Connector) Bind(ctx context.Context, conn net.Conn, network, addr
|
||||
}
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindTCP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tcpAccepter{
|
||||
return &tcpListener{
|
||||
addr: laddr,
|
||||
conn: conn,
|
||||
logger: c.logger,
|
||||
done: make(chan struct{}),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -65,42 +64,33 @@ func (c *socks5Connector) muxBindTCP(ctx context.Context, conn net.Conn, network
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tcpMuxAccepter{
|
||||
return &tcpMuxListener{
|
||||
addr: laddr,
|
||||
session: session,
|
||||
logger: c.logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *socks5Connector) bindUDP(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accepter := &udpAccepter{
|
||||
addr: laddr,
|
||||
conn: socks.UDPTunClientPacketConn(conn),
|
||||
cqueue: make(chan net.Conn, c.md.backlog),
|
||||
connPool: udp.NewConnPool(c.md.ttl).WithLogger(c.logger),
|
||||
readQueueSize: c.md.readQueueSize,
|
||||
readBufferSize: c.md.readBufferSize,
|
||||
closed: make(chan struct{}),
|
||||
logger: c.logger,
|
||||
}
|
||||
go accepter.acceptLoop()
|
||||
ln := udp.NewListener(
|
||||
socks.UDPTunClientPacketConn(conn),
|
||||
laddr,
|
||||
opts.Backlog,
|
||||
opts.UDPDataQueueSize, opts.UDPDataBufferSize,
|
||||
opts.UDPConnTTL,
|
||||
c.logger)
|
||||
|
||||
return accepter, nil
|
||||
return ln, nil
|
||||
}
|
||||
|
||||
func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string) (net.Addr, error) {
|
||||
laddr, err := net.ResolveTCPAddr(network, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addr := gosocks5.Addr{}
|
||||
addr.ParseFrom(laddr.String())
|
||||
addr.ParseFrom(address)
|
||||
req := gosocks5.NewRequest(cmd, &addr)
|
||||
if err := req.Write(conn); err != nil {
|
||||
return nil, err
|
||||
@ -116,7 +106,7 @@ func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string
|
||||
l.logger.Debug(reply)
|
||||
|
||||
if reply.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("bind on %s/%s failed", laddr, laddr.Network())
|
||||
return nil, fmt.Errorf("bind on %s/%s failed", address, network)
|
||||
}
|
||||
|
||||
var baddr net.Addr
|
||||
@ -133,5 +123,5 @@ func (l *socks5Connector) bind(conn net.Conn, cmd uint8, network, address string
|
||||
}
|
||||
l.logger.Debugf("bind on %s/%s OK", baddr, baddr.Network())
|
||||
|
||||
return laddr, nil
|
||||
return baddr, nil
|
||||
}
|
||||
|
102
pkg/connector/socks/v5/listener.go
Normal file
102
pkg/connector/socks/v5/listener.go
Normal file
@ -0,0 +1,102 @@
|
||||
package v5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gosocks5"
|
||||
"github.com/go-gost/gost/pkg/common/util/mux"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
type tcpListener struct {
|
||||
addr net.Addr
|
||||
conn net.Conn
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *tcpListener) Accept() (net.Conn, error) {
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(p.conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
return nil, fmt.Errorf("peer connect failed")
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: p.conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpListener) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpListener) Close() error {
|
||||
return p.conn.Close()
|
||||
}
|
||||
|
||||
type tcpMuxListener struct {
|
||||
addr net.Addr
|
||||
session *mux.Session
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (p *tcpMuxListener) Accept() (net.Conn, error) {
|
||||
cc, err := p.session.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn, err := p.getPeerConn(cc)
|
||||
if err != nil {
|
||||
cc.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxListener) getPeerConn(conn net.Conn) (net.Conn, error) {
|
||||
// second reply, peer connected
|
||||
rep, err := gosocks5.ReadReply(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.logger.Debug(rep)
|
||||
|
||||
if rep.Rep != gosocks5.Succeeded {
|
||||
err = fmt.Errorf("peer connect failed")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", rep.Addr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &bindConn{
|
||||
Conn: conn,
|
||||
localAddr: p.addr,
|
||||
remoteAddr: raddr,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *tcpMuxListener) Addr() net.Addr {
|
||||
return p.addr
|
||||
}
|
||||
|
||||
func (p *tcpMuxListener) Close() error {
|
||||
return p.session.Close()
|
||||
}
|
@ -9,23 +9,11 @@ import (
|
||||
md "github.com/go-gost/gost/pkg/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultTTL = 60 * time.Second
|
||||
defaultReadBufferSize = 4096
|
||||
defaultReadQueueSize = 128
|
||||
defaultBacklog = 128
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
connectTimeout time.Duration
|
||||
User *url.Userinfo
|
||||
tlsConfig *tls.Config
|
||||
noTLS bool
|
||||
|
||||
ttl time.Duration
|
||||
readBufferSize int
|
||||
readQueueSize int
|
||||
backlog int
|
||||
}
|
||||
|
||||
func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
@ -33,11 +21,6 @@ func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
connectTimeout = "timeout"
|
||||
auth = "auth"
|
||||
noTLS = "notls"
|
||||
|
||||
ttl = "ttl"
|
||||
readBufferSize = "readBufferSize"
|
||||
readQueueSize = "readQueueSize"
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
if v := md.GetString(auth); v != "" {
|
||||
@ -52,23 +35,5 @@ func (c *socks5Connector) parseMetadata(md md.Metadata) (err error) {
|
||||
c.md.connectTimeout = md.GetDuration(connectTimeout)
|
||||
c.md.noTLS = md.GetBool(noTLS)
|
||||
|
||||
c.md.ttl = md.GetDuration(ttl)
|
||||
if c.md.ttl <= 0 {
|
||||
c.md.ttl = defaultTTL
|
||||
}
|
||||
c.md.readBufferSize = md.GetInt(readBufferSize)
|
||||
if c.md.readBufferSize <= 0 {
|
||||
c.md.readBufferSize = defaultReadBufferSize
|
||||
}
|
||||
|
||||
c.md.readQueueSize = md.GetInt(readQueueSize)
|
||||
if c.md.readQueueSize <= 0 {
|
||||
c.md.readQueueSize = defaultReadQueueSize
|
||||
}
|
||||
|
||||
c.md.backlog = md.GetInt(backlog)
|
||||
if c.md.backlog <= 0 {
|
||||
c.md.backlog = defaultBacklog
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user