support multiple network interfaces
This commit is contained in:
@ -5,9 +5,11 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/common/util/udp"
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/dialer"
|
||||
"github.com/go-gost/gost/pkg/logger"
|
||||
)
|
||||
|
||||
@ -16,8 +18,9 @@ var (
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
nodes []*Node
|
||||
logger logger.Logger
|
||||
nodes []*Node
|
||||
ifceName string
|
||||
logger logger.Logger
|
||||
}
|
||||
|
||||
func (r *Route) addNode(node *Node) {
|
||||
@ -26,7 +29,13 @@ func (r *Route) addNode(node *Node) {
|
||||
|
||||
func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
if r.Len() == 0 {
|
||||
return r.dialDirect(ctx, network, address)
|
||||
netd := dialer.NetDialer{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
if r != nil {
|
||||
netd.Interface = r.ifceName
|
||||
}
|
||||
return netd.Dial(ctx, network, address)
|
||||
}
|
||||
|
||||
conn, err := r.connect(ctx)
|
||||
@ -42,19 +51,6 @@ func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, er
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func (r *Route) dialDirect(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6":
|
||||
if address == "" {
|
||||
return net.ListenUDP(network, nil)
|
||||
}
|
||||
default:
|
||||
}
|
||||
|
||||
d := net.Dialer{}
|
||||
return d.DialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
func (r *Route) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (net.Listener, error) {
|
||||
if r.Len() == 0 {
|
||||
return r.bindLocal(ctx, network, address, opts...)
|
||||
|
@ -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
|
||||
|
@ -3,6 +3,7 @@ package chain
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/gost/pkg/connector"
|
||||
"github.com/go-gost/gost/pkg/dialer"
|
||||
@ -10,6 +11,7 @@ import (
|
||||
|
||||
type Transport struct {
|
||||
addr string
|
||||
ifceName string
|
||||
route *Route
|
||||
dialer dialer.Dialer
|
||||
connector connector.Connector
|
||||
@ -21,6 +23,11 @@ func (tr *Transport) Copy() *Transport {
|
||||
return tr
|
||||
}
|
||||
|
||||
func (tr *Transport) WithInterface(ifceName string) *Transport {
|
||||
tr.ifceName = ifceName
|
||||
return tr
|
||||
}
|
||||
|
||||
func (tr *Transport) WithDialer(dialer dialer.Dialer) *Transport {
|
||||
tr.dialer = dialer
|
||||
return tr
|
||||
@ -32,23 +39,20 @@ func (tr *Transport) WithConnector(connector connector.Connector) *Transport {
|
||||
}
|
||||
|
||||
func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) {
|
||||
return tr.dialer.Dial(ctx, addr, tr.dialOptions()...)
|
||||
}
|
||||
|
||||
func (tr *Transport) dialOptions() []dialer.DialOption {
|
||||
opts := []dialer.DialOption{
|
||||
dialer.HostDialOption(tr.addr),
|
||||
netd := &dialer.NetDialer{
|
||||
Interface: tr.ifceName,
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
if tr.route.Len() > 0 {
|
||||
opts = append(opts,
|
||||
dialer.DialFuncDialOption(
|
||||
func(ctx context.Context, addr string) (net.Conn, error) {
|
||||
return tr.route.Dial(ctx, "tcp", addr)
|
||||
},
|
||||
),
|
||||
)
|
||||
netd.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return tr.route.Dial(ctx, network, addr)
|
||||
}
|
||||
}
|
||||
return opts
|
||||
opts := []dialer.DialOption{
|
||||
dialer.HostDialOption(tr.addr),
|
||||
dialer.NetDialerDialOption(netd),
|
||||
}
|
||||
return tr.dialer.Dial(ctx, addr, opts...)
|
||||
}
|
||||
|
||||
func (tr *Transport) Handshake(ctx context.Context, conn net.Conn) (net.Conn, error) {
|
||||
|
Reference in New Issue
Block a user