add binder for connector
This commit is contained in:
@ -3,7 +3,10 @@ package chain
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -93,6 +96,41 @@ 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) {
|
||||
if r.IsEmpty() {
|
||||
return r.bindLocal(ctx, network, address)
|
||||
}
|
||||
|
||||
conn, err := r.Connect(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accepter, err := r.Last().transport.Bind(ctx, conn, network, address)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Route) IsEmpty() bool {
|
||||
return r == nil || len(r.nodes) == 0
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
@ -59,6 +60,35 @@ 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 {
|
||||
|
@ -62,6 +62,13 @@ 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) {
|
||||
if binder, ok := tr.connector.(connector.Binder); ok {
|
||||
return binder.Bind(ctx, conn, network, address, connector.MuxBindOption(true))
|
||||
}
|
||||
return nil, connector.ErrBindUnsupported
|
||||
}
|
||||
|
||||
func (tr *Transport) IsMultiplex() bool {
|
||||
if mux, ok := tr.dialer.(dialer.Multiplexer); ok {
|
||||
return mux.IsMultiplex()
|
||||
|
Reference in New Issue
Block a user