add binder for connector

This commit is contained in:
ginuerzh
2021-11-20 23:41:11 +08:00
parent c5df25e84d
commit 3fd3b5e801
29 changed files with 1396 additions and 284 deletions

View File

@ -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 {