add sockopts config

This commit is contained in:
ginuerzh
2022-03-29 23:04:12 +08:00
parent 78089d8887
commit 47cfc087e9
11 changed files with 115 additions and 36 deletions

View File

@ -19,24 +19,33 @@ var (
)
type Route struct {
chain *Chain
ifceName string
nodes []*Node
logger logger.Logger
chain *Chain
nodes []*Node
logger logger.Logger
}
func (r *Route) addNode(node *Node) {
r.nodes = append(r.nodes, node)
}
func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, error) {
func (r *Route) Dial(ctx context.Context, network, address string, opts ...DialOption) (net.Conn, error) {
var options DialOptions
for _, opt := range opts {
opt(&options)
}
if r.Len() == 0 {
netd := dialer.NetDialer{
Timeout: 30 * time.Second,
Timeout: options.Timeout,
Interface: options.Interface,
}
if options.SockOpts != nil {
netd.Mark = options.SockOpts.Mark
}
if r != nil {
netd.Interface = r.ifceName
netd.Logger = r.logger
}
return netd.Dial(ctx, network, address)
}
@ -198,3 +207,29 @@ func (r *Route) bindLocal(ctx context.Context, network, address string, opts ...
return nil, err
}
}
type DialOptions struct {
Timeout time.Duration
Interface string
SockOpts *SockOpts
}
type DialOption func(opts *DialOptions)
func TimeoutDialOption(d time.Duration) DialOption {
return func(opts *DialOptions) {
opts.Timeout = d
}
}
func InterfaceDialOption(ifName string) DialOption {
return func(opts *DialOptions) {
opts.Interface = ifName
}
}
func SockOptsDialOption(so *SockOpts) DialOption {
return func(opts *DialOptions) {
opts.SockOpts = so
}
}