add dialer for connector
This commit is contained in:
		| @ -71,7 +71,9 @@ func (r *route) Dial(ctx context.Context, network, address string, opts ...DialO | |||||||
|  |  | ||||||
| 	cc, err := r.GetNode(r.Len()-1).transport.Connect(ctx, conn, network, address) | 	cc, err := r.GetNode(r.Len()-1).transport.Connect(ctx, conn, network, address) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | 		if conn != nil { | ||||||
| 			conn.Close() | 			conn.Close() | ||||||
|  | 		} | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return cc, nil | 	return cc, nil | ||||||
|  | |||||||
| @ -17,6 +17,7 @@ type Transport struct { | |||||||
| 	route     Route | 	route     Route | ||||||
| 	dialer    dialer.Dialer | 	dialer    dialer.Dialer | ||||||
| 	connector connector.Connector | 	connector connector.Connector | ||||||
|  | 	timeout   time.Duration | ||||||
| } | } | ||||||
|  |  | ||||||
| func (tr *Transport) Copy() *Transport { | func (tr *Transport) Copy() *Transport { | ||||||
| @ -45,10 +46,15 @@ func (tr *Transport) WithConnector(connector connector.Connector) *Transport { | |||||||
| 	return tr | 	return tr | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func (tr *Transport) WithTimeout(d time.Duration) *Transport { | ||||||
|  | 	tr.timeout = d | ||||||
|  | 	return tr | ||||||
|  | } | ||||||
|  |  | ||||||
| func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) { | func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error) { | ||||||
| 	netd := &net_dialer.NetDialer{ | 	netd := &net_dialer.NetDialer{ | ||||||
| 		Interface: tr.ifceName, | 		Interface: tr.ifceName, | ||||||
| 		Timeout:   15 * time.Second, | 		Timeout:   tr.timeout, | ||||||
| 	} | 	} | ||||||
| 	if tr.sockOpts != nil { | 	if tr.sockOpts != nil { | ||||||
| 		netd.Mark = tr.sockOpts.Mark | 		netd.Mark = tr.sockOpts.Mark | ||||||
| @ -81,7 +87,16 @@ func (tr *Transport) Handshake(ctx context.Context, conn net.Conn) (net.Conn, er | |||||||
| } | } | ||||||
|  |  | ||||||
| func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error) { | func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error) { | ||||||
| 	return tr.connector.Connect(ctx, conn, network, address) | 	netd := &net_dialer.NetDialer{ | ||||||
|  | 		Interface: tr.ifceName, | ||||||
|  | 		Timeout:   tr.timeout, | ||||||
|  | 	} | ||||||
|  | 	if tr.sockOpts != nil { | ||||||
|  | 		netd.Mark = tr.sockOpts.Mark | ||||||
|  | 	} | ||||||
|  | 	return tr.connector.Connect(ctx, conn, network, address, | ||||||
|  | 		connector.NetDialerConnectOption(netd), | ||||||
|  | 	) | ||||||
| } | } | ||||||
|  |  | ||||||
| func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) { | func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) { | ||||||
|  | |||||||
| @ -46,7 +46,9 @@ func (d *NetDialer) Dial(ctx context.Context, network, addr string) (net.Conn, e | |||||||
| 	if d.DialFunc != nil { | 	if d.DialFunc != nil { | ||||||
| 		return d.DialFunc(ctx, network, addr) | 		return d.DialFunc(ctx, network, addr) | ||||||
| 	} | 	} | ||||||
|  | 	if ifceName != "" { | ||||||
| 		log.Debugf("interface: %s %v/%s", ifceName, ifAddr, network) | 		log.Debugf("interface: %s %v/%s", ifceName, ifAddr, network) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	switch network { | 	switch network { | ||||||
| 	case "udp", "udp4", "udp6": | 	case "udp", "udp4", "udp6": | ||||||
|  | |||||||
| @ -5,6 +5,7 @@ import ( | |||||||
| 	"net/url" | 	"net/url" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/go-gost/core/common/net/dialer" | ||||||
| 	"github.com/go-gost/core/logger" | 	"github.com/go-gost/core/logger" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @ -35,10 +36,17 @@ func LoggerOption(logger logger.Logger) Option { | |||||||
| } | } | ||||||
|  |  | ||||||
| type ConnectOptions struct { | type ConnectOptions struct { | ||||||
|  | 	NetDialer *dialer.NetDialer | ||||||
| } | } | ||||||
|  |  | ||||||
| type ConnectOption func(opts *ConnectOptions) | type ConnectOption func(opts *ConnectOptions) | ||||||
|  |  | ||||||
|  | func NetDialerConnectOption(netd *dialer.NetDialer) ConnectOption { | ||||||
|  | 	return func(opts *ConnectOptions) { | ||||||
|  | 		opts.NetDialer = netd | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
| type BindOptions struct { | type BindOptions struct { | ||||||
| 	Mux               bool | 	Mux               bool | ||||||
| 	Backlog           int | 	Backlog           int | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user