add port range support for service
This commit is contained in:
105
chain/route.go
105
chain/route.go
@ -2,6 +2,8 @@ package chain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
@ -10,9 +12,86 @@ import (
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/core/metrics"
|
||||
"github.com/go-gost/core/selector"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
"github.com/go-gost/x/internal/net/dialer"
|
||||
"github.com/go-gost/x/internal/net/udp"
|
||||
xmetrics "github.com/go-gost/x/metrics"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrEmptyRoute = errors.New("empty route")
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultRoute chain.Route = &defaultRoute{}
|
||||
)
|
||||
|
||||
// defaultRoute is a Route without nodes.
|
||||
type defaultRoute struct{}
|
||||
|
||||
func (*defaultRoute) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (net.Conn, error) {
|
||||
var options chain.DialOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
netd := dialer.Dialer{
|
||||
Interface: options.Interface,
|
||||
Netns: options.Netns,
|
||||
Logger: options.Logger,
|
||||
}
|
||||
if options.SockOpts != nil {
|
||||
netd.Mark = options.SockOpts.Mark
|
||||
}
|
||||
|
||||
return netd.Dial(ctx, network, address)
|
||||
}
|
||||
|
||||
func (*defaultRoute) Bind(ctx context.Context, network, address string, opts ...chain.BindOption) (net.Listener, error) {
|
||||
var options chain.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]any{
|
||||
"network": network,
|
||||
"address": address,
|
||||
})
|
||||
ln := udp.NewListener(conn, &udp.ListenConfig{
|
||||
Backlog: options.Backlog,
|
||||
ReadQueueSize: options.UDPDataQueueSize,
|
||||
ReadBufferSize: options.UDPDataBufferSize,
|
||||
TTL: options.UDPConnTTL,
|
||||
KeepAlive: true,
|
||||
Logger: logger,
|
||||
})
|
||||
return ln, err
|
||||
default:
|
||||
err := fmt.Errorf("network %s unsupported", network)
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (r *defaultRoute) Nodes() []*chain.Node {
|
||||
return nil
|
||||
}
|
||||
|
||||
type RouteOptions struct {
|
||||
Chain chain.Chainer
|
||||
}
|
||||
@ -25,12 +104,12 @@ func ChainRouteOption(c chain.Chainer) RouteOption {
|
||||
}
|
||||
}
|
||||
|
||||
type route struct {
|
||||
type chainRoute struct {
|
||||
nodes []*chain.Node
|
||||
options RouteOptions
|
||||
}
|
||||
|
||||
func NewRoute(opts ...RouteOption) *route {
|
||||
func NewRoute(opts ...RouteOption) *chainRoute {
|
||||
var options RouteOptions
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
@ -38,18 +117,18 @@ func NewRoute(opts ...RouteOption) *route {
|
||||
}
|
||||
}
|
||||
|
||||
return &route{
|
||||
return &chainRoute{
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *route) addNode(nodes ...*chain.Node) {
|
||||
func (r *chainRoute) addNode(nodes ...*chain.Node) {
|
||||
r.nodes = append(r.nodes, nodes...)
|
||||
}
|
||||
|
||||
func (r *route) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (net.Conn, error) {
|
||||
func (r *chainRoute) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (net.Conn, error) {
|
||||
if len(r.Nodes()) == 0 {
|
||||
return chain.DefaultRoute.Dial(ctx, network, address, opts...)
|
||||
return DefaultRoute.Dial(ctx, network, address, opts...)
|
||||
}
|
||||
|
||||
var options chain.DialOptions
|
||||
@ -73,9 +152,9 @@ func (r *route) Dial(ctx context.Context, network, address string, opts ...chain
|
||||
return cc, nil
|
||||
}
|
||||
|
||||
func (r *route) Bind(ctx context.Context, network, address string, opts ...chain.BindOption) (net.Listener, error) {
|
||||
func (r *chainRoute) Bind(ctx context.Context, network, address string, opts ...chain.BindOption) (net.Listener, error) {
|
||||
if len(r.Nodes()) == 0 {
|
||||
return chain.DefaultRoute.Bind(ctx, network, address, opts...)
|
||||
return DefaultRoute.Bind(ctx, network, address, opts...)
|
||||
}
|
||||
|
||||
var options chain.BindOptions
|
||||
@ -106,7 +185,7 @@ func (r *route) Bind(ctx context.Context, network, address string, opts ...chain
|
||||
return ln, nil
|
||||
}
|
||||
|
||||
func (r *route) connect(ctx context.Context, logger logger.Logger) (conn net.Conn, err error) {
|
||||
func (r *chainRoute) connect(ctx context.Context, logger logger.Logger) (conn net.Conn, err error) {
|
||||
network := "ip"
|
||||
node := r.nodes[0]
|
||||
|
||||
@ -138,7 +217,7 @@ func (r *route) connect(ctx context.Context, logger logger.Logger) (conn net.Con
|
||||
}
|
||||
}()
|
||||
|
||||
addr, err := chain.Resolve(ctx, network, node.Addr, node.Options().Resolver, node.Options().HostMapper, logger)
|
||||
addr, err := xnet.Resolve(ctx, network, node.Addr, node.Options().Resolver, node.Options().HostMapper, logger)
|
||||
marker := node.Marker()
|
||||
if err != nil {
|
||||
if marker != nil {
|
||||
@ -182,7 +261,7 @@ func (r *route) connect(ctx context.Context, logger logger.Logger) (conn net.Con
|
||||
preNode := node
|
||||
for _, node := range r.nodes[1:] {
|
||||
marker := node.Marker()
|
||||
addr, err = chain.Resolve(ctx, network, node.Addr, node.Options().Resolver, node.Options().HostMapper, logger)
|
||||
addr, err = xnet.Resolve(ctx, network, node.Addr, node.Options().Resolver, node.Options().HostMapper, logger)
|
||||
if err != nil {
|
||||
cn.Close()
|
||||
if marker != nil {
|
||||
@ -218,14 +297,14 @@ func (r *route) connect(ctx context.Context, logger logger.Logger) (conn net.Con
|
||||
return
|
||||
}
|
||||
|
||||
func (r *route) getNode(index int) *chain.Node {
|
||||
func (r *chainRoute) getNode(index int) *chain.Node {
|
||||
if r == nil || len(r.Nodes()) == 0 || index < 0 || index >= len(r.Nodes()) {
|
||||
return nil
|
||||
}
|
||||
return r.nodes[index]
|
||||
}
|
||||
|
||||
func (r *route) Nodes() []*chain.Node {
|
||||
func (r *chainRoute) Nodes() []*chain.Node {
|
||||
if r != nil {
|
||||
return r.nodes
|
||||
}
|
||||
|
Reference in New Issue
Block a user