support multiple network interfaces
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/hosts"
|
||||
@ -13,11 +14,55 @@ import (
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
Retries int
|
||||
Chain Chainer
|
||||
Hosts hosts.HostMapper
|
||||
Resolver resolver.Resolver
|
||||
Logger logger.Logger
|
||||
timeout time.Duration
|
||||
retries int
|
||||
ifceName string
|
||||
chain Chainer
|
||||
resolver resolver.Resolver
|
||||
hosts hosts.HostMapper
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (r *Router) WithTimeout(timeout time.Duration) *Router {
|
||||
r.timeout = timeout
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) WithRetries(retries int) *Router {
|
||||
r.retries = retries
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) WithInterface(ifceName string) *Router {
|
||||
r.ifceName = ifceName
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) WithChain(chain Chainer) *Router {
|
||||
r.chain = chain
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) WithResolver(resolver resolver.Resolver) *Router {
|
||||
r.resolver = resolver
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) WithHosts(hosts hosts.HostMapper) *Router {
|
||||
r.hosts = hosts
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) Hosts() hosts.HostMapper {
|
||||
if r != nil {
|
||||
return r.hosts
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) WithLogger(logger logger.Logger) *Router {
|
||||
r.logger = logger
|
||||
return r
|
||||
}
|
||||
|
||||
func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
|
||||
@ -34,74 +79,76 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
|
||||
}
|
||||
|
||||
func (r *Router) dial(ctx context.Context, network, address string) (conn net.Conn, err error) {
|
||||
count := r.Retries + 1
|
||||
count := r.retries + 1
|
||||
if count <= 0 {
|
||||
count = 1
|
||||
}
|
||||
r.Logger.Debugf("dial %s/%s", address, network)
|
||||
r.logger.Debugf("dial %s/%s", address, network)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
var route *Route
|
||||
if r.Chain != nil {
|
||||
route = r.Chain.Route(network, address)
|
||||
if r.chain != nil {
|
||||
route = r.chain.Route(network, address)
|
||||
}
|
||||
|
||||
if r.Logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
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())
|
||||
r.logger.Debugf("route(retry=%d) %s", i, buf.String())
|
||||
}
|
||||
|
||||
address, err = resolve(ctx, "ip", address, r.Resolver, r.Hosts, r.Logger)
|
||||
address, err = resolve(ctx, "ip", address, r.resolver, r.hosts, r.logger)
|
||||
if err != nil {
|
||||
r.Logger.Error(err)
|
||||
r.logger.Error(err)
|
||||
break
|
||||
}
|
||||
|
||||
if route != nil {
|
||||
route.logger = r.Logger
|
||||
if route == nil {
|
||||
route = &Route{}
|
||||
}
|
||||
route.ifceName = r.ifceName
|
||||
route.logger = r.logger
|
||||
|
||||
conn, err = route.Dial(ctx, network, address)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
r.Logger.Errorf("route(retry=%d) %s", i, err)
|
||||
r.logger.Errorf("route(retry=%d) %s", i, err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *Router) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (ln net.Listener, err error) {
|
||||
count := r.Retries + 1
|
||||
count := r.retries + 1
|
||||
if count <= 0 {
|
||||
count = 1
|
||||
}
|
||||
r.Logger.Debugf("bind on %s/%s", address, network)
|
||||
r.logger.Debugf("bind on %s/%s", address, network)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
var route *Route
|
||||
if r.Chain != nil {
|
||||
route = r.Chain.Route(network, address)
|
||||
if r.chain != nil {
|
||||
route = r.chain.Route(network, address)
|
||||
}
|
||||
|
||||
if r.Logger.IsLevelEnabled(logger.DebugLevel) {
|
||||
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())
|
||||
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)
|
||||
r.logger.Errorf("route(retry=%d) %s", i, err)
|
||||
}
|
||||
|
||||
return
|
||||
|
Reference in New Issue
Block a user