add network for bypass

This commit is contained in:
ginuerzh
2023-09-30 17:48:40 +08:00
parent b0bd45c1b8
commit bf3b76a273
4 changed files with 41 additions and 9 deletions

View File

@ -2,10 +2,22 @@ package bypass
import "context" import "context"
type Options struct {
Host string
}
type Option func(opts *Options)
func WithHostOpton(host string) Option {
return func(opts *Options) {
opts.Host = host
}
}
// Bypass is a filter of address (IP or domain). // Bypass is a filter of address (IP or domain).
type Bypass interface { type Bypass interface {
// Contains reports whether the bypass includes addr. // Contains reports whether the bypass includes addr.
Contains(ctx context.Context, addr string) bool Contains(ctx context.Context, network, addr string, opts ...Option) bool
} }
type bypassGroup struct { type bypassGroup struct {
@ -18,9 +30,9 @@ func BypassGroup(bypasses ...Bypass) Bypass {
} }
} }
func (p *bypassGroup) Contains(ctx context.Context, addr string) bool { func (p *bypassGroup) Contains(ctx context.Context, network, addr string, opts ...Option) bool {
for _, bypass := range p.bypasses { for _, bypass := range p.bypasses {
if bypass != nil && bypass.Contains(ctx, addr) { if bypass != nil && bypass.Contains(ctx, network, addr, opts...) {
return true return true
} }
} }

View File

@ -4,6 +4,18 @@ import (
"context" "context"
) )
type Chainer interface { type RouteOptions struct {
Route(ctx context.Context, network, address string) Route Host string
}
type RouteOption func(opts *RouteOptions)
func WithHostRouteOption(host string) RouteOption {
return func(opts *RouteOptions) {
opts.Host = host
}
}
type Chainer interface {
Route(ctx context.Context, network, address string, opts ...RouteOption) Route
} }

View File

@ -155,7 +155,8 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
r.options.Logger.Debugf("dial %s/%s", address, network) r.options.Logger.Debugf("dial %s/%s", address, network)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
address, err = Resolve(ctx, "ip", address, r.options.Resolver, r.options.HostMapper, r.options.Logger) var ipAddr string
ipAddr, err = Resolve(ctx, "ip", address, r.options.Resolver, r.options.HostMapper, r.options.Logger)
if err != nil { if err != nil {
r.options.Logger.Error(err) r.options.Logger.Error(err)
break break
@ -163,7 +164,7 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
var route Route var route Route
if r.options.Chain != nil { if r.options.Chain != nil {
route = r.options.Chain.Route(ctx, network, address) route = r.options.Chain.Route(ctx, network, ipAddr, WithHostRouteOption(address))
} }
if r.options.Logger.IsLevelEnabled(logger.DebugLevel) { if r.options.Logger.IsLevelEnabled(logger.DebugLevel) {
@ -171,14 +172,14 @@ func (r *Router) dial(ctx context.Context, network, address string) (conn net.Co
for _, node := range routePath(route) { for _, node := range routePath(route) {
fmt.Fprintf(&buf, "%s@%s > ", node.Name, node.Addr) fmt.Fprintf(&buf, "%s@%s > ", node.Name, node.Addr)
} }
fmt.Fprintf(&buf, "%s", address) fmt.Fprintf(&buf, "%s", ipAddr)
r.options.Logger.Debugf("route(retry=%d) %s", i, buf.String()) r.options.Logger.Debugf("route(retry=%d) %s", i, buf.String())
} }
if route == nil { if route == nil {
route = DefaultRoute route = DefaultRoute
} }
conn, err = route.Dial(ctx, network, address, conn, err = route.Dial(ctx, network, ipAddr,
InterfaceDialOption(r.options.IfceName), InterfaceDialOption(r.options.IfceName),
SockOptsDialOption(r.options.SockOpts), SockOptsDialOption(r.options.SockOpts),
LoggerDialOption(r.options.Logger), LoggerDialOption(r.options.Logger),

View File

@ -7,6 +7,7 @@ import (
) )
type SelectOptions struct { type SelectOptions struct {
Network string
Addr string Addr string
Host string Host string
Protocol string Protocol string
@ -14,6 +15,12 @@ type SelectOptions struct {
type SelectOption func(*SelectOptions) type SelectOption func(*SelectOptions)
func NetworkSelectOption(network string) SelectOption {
return func(so *SelectOptions) {
so.Network = network
}
}
func AddrSelectOption(addr string) SelectOption { func AddrSelectOption(addr string) SelectOption {
return func(o *SelectOptions) { return func(o *SelectOptions) {
o.Addr = addr o.Addr = addr