fix udp bind

This commit is contained in:
ginuerzh
2021-11-22 17:52:38 +08:00
parent 3fd3b5e801
commit 8861644bbc
18 changed files with 477 additions and 613 deletions

View File

@ -6,7 +6,9 @@ import (
"fmt"
"net"
"github.com/go-gost/gost/pkg/common/util/udp"
"github.com/go-gost/gost/pkg/connector"
"github.com/go-gost/gost/pkg/logger"
)
var (
@ -96,9 +98,9 @@ func (r *Route) dialDirect(ctx context.Context, network, address string) (net.Co
return d.DialContext(ctx, network, address)
}
func (r *Route) Bind(ctx context.Context, network, address string) (connector.Accepter, error) {
func (r *Route) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
if r.IsEmpty() {
return r.bindLocal(ctx, network, address)
return r.bindLocal(ctx, network, address, opts...)
}
conn, err := r.Connect(ctx)
@ -106,29 +108,13 @@ func (r *Route) Bind(ctx context.Context, network, address string) (connector.Ac
return nil, err
}
accepter, err := r.Last().transport.Bind(ctx, conn, network, address)
ln, err := r.Last().transport.Bind(ctx, conn, network, address, opts...)
if err != nil {
conn.Close()
return nil, err
}
return accepter, nil
}
func (r *Route) bindLocal(ctx context.Context, network, address string) (connector.Accepter, error) {
switch network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
return net.ListenTCP(network, addr)
case "udp", "udp4", "udp6":
return nil, nil
default:
err := fmt.Errorf("network %s unsupported", network)
return nil, err
}
return ln, nil
}
func (r *Route) IsEmpty() bool {
@ -155,3 +141,39 @@ func (r *Route) Path() (path []*Node) {
}
return
}
func (r *Route) bindLocal(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
options := connector.BindOptions{}
for _, opt := range opts {
opt(&options)
}
switch network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
return net.ListenTCP(network, addr)
case "udp", "udp4", "udp6":
addr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
conn, err := net.ListenUDP(network, addr)
if err != nil {
return nil, err
}
logger := logger.Default().WithFields(map[string]interface{}{
"network": network,
"address": address,
})
ln := udp.NewListener(conn, addr,
options.Backlog, options.UDPDataQueueSize, options.UDPDataBufferSize,
options.UDPConnTTL, logger)
return ln, err
default:
err := fmt.Errorf("network %s unsupported", network)
return nil, err
}
}

View File

@ -60,35 +60,6 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
return
}
func (r *Router) Bind(ctx context.Context, network, address string) (accepter connector.Accepter, err error) {
count := r.retries + 1
if count <= 0 {
count = 1
}
r.logger.Debugf("bind: %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
if r.logger.IsLevelEnabled(logger.DebugLevel) {
buf := bytes.Buffer{}
for _, node := range route.Path() {
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
}
accepter, err = route.Bind(ctx, network, address)
if err == nil {
break
}
r.logger.Errorf("route(retry=%d): %s", i, err)
}
return
}
func (r *Router) Connect(ctx context.Context) (conn net.Conn, err error) {
count := r.retries + 1
if count <= 0 {
@ -115,3 +86,32 @@ func (r *Router) Connect(ctx context.Context) (conn net.Conn, err error) {
return
}
func (r *Router) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (ln net.Listener, err error) {
count := r.retries + 1
if count <= 0 {
count = 1
}
r.logger.Debugf("bind: %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
if r.logger.IsLevelEnabled(logger.DebugLevel) {
buf := bytes.Buffer{}
for _, node := range route.Path() {
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
}
ln, err = route.Bind(ctx, network, address, opts...)
if err == nil {
break
}
r.logger.Errorf("route(retry=%d): %s", i, err)
}
return
}

View File

@ -62,9 +62,9 @@ func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, addres
return tr.connector.Connect(ctx, conn, network, address)
}
func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address string) (connector.Accepter, error) {
func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
if binder, ok := tr.connector.(connector.Binder); ok {
return binder.Bind(ctx, conn, network, address, connector.MuxBindOption(true))
return binder.Bind(ctx, conn, network, address, opts...)
}
return nil, connector.ErrBindUnsupported
}