support multiple network interfaces

This commit is contained in:
ginuerzh
2022-03-01 21:48:50 +08:00
parent 07132d8de7
commit ffdf11e89e
44 changed files with 431 additions and 474 deletions

View File

@ -52,11 +52,6 @@ func (d *sshDialer) Multiplex() bool {
}
func (d *sshDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (conn net.Conn, err error) {
var options dialer.DialOptions
for _, opt := range opts {
opt(&options)
}
d.sessionMutex.Lock()
defer d.sessionMutex.Unlock()
@ -66,7 +61,16 @@ func (d *sshDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOp
ok = false
}
if !ok {
conn, err = d.dial(ctx, "tcp", addr, &options)
var options dialer.DialOptions
for _, opt := range opts {
opt(&options)
}
netd := options.NetDialer
if netd == nil {
netd = dialer.DefaultNetDialer
}
conn, err = netd.Dial(ctx, "tcp", addr)
if err != nil {
return
}
@ -134,34 +138,6 @@ func (d *sshDialer) Handshake(ctx context.Context, conn net.Conn, options ...dia
return ssh_util.NewConn(conn, channel), nil
}
func (d *sshDialer) dial(ctx context.Context, network, addr string, opts *dialer.DialOptions) (net.Conn, error) {
dial := opts.DialFunc
if dial != nil {
conn, err := dial(ctx, addr)
if err != nil {
d.logger.Error(err)
} else {
d.logger.WithFields(map[string]any{
"src": conn.LocalAddr().String(),
"dst": addr,
}).Debug("dial with dial func")
}
return conn, err
}
var netd net.Dialer
conn, err := netd.DialContext(ctx, network, addr)
if err != nil {
d.logger.Error(err)
} else {
d.logger.WithFields(map[string]any{
"src": conn.LocalAddr().String(),
"dst": addr,
}).Debugf("dial direct %s/%s", addr, network)
}
return conn, err
}
func (d *sshDialer) initSession(ctx context.Context, addr string, conn net.Conn) (*sshSession, error) {
config := ssh.ClientConfig{
Timeout: 30 * time.Second,