add auther config

This commit is contained in:
ginuerzh
2022-02-12 00:33:20 +08:00
parent c1bf501734
commit a8a6bbc3a3
37 changed files with 261 additions and 183 deletions

View File

@ -10,16 +10,16 @@ import (
)
type Options struct {
User *url.Userinfo
Auth *url.Userinfo
TLSConfig *tls.Config
Logger logger.Logger
}
type Option func(opts *Options)
func UserOption(user *url.Userinfo) Option {
func AuthOption(auth *url.Userinfo) Option {
return func(opts *Options) {
opts.User = user
opts.Auth = auth
}
}

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"net"
"net/url"
"sync"
"time"
@ -20,7 +19,6 @@ func init() {
}
type sshdDialer struct {
user *url.Userinfo
sessions map[string]*sshSession
sessionMutex sync.Mutex
md metadata
@ -34,7 +32,6 @@ func NewDialer(opts ...dialer.Option) dialer.Dialer {
}
return &sshdDialer{
user: options.User,
sessions: make(map[string]*sshSession),
options: options,
}
@ -167,9 +164,9 @@ func (d *sshdDialer) initSession(ctx context.Context, addr string, conn net.Conn
// Timeout: timeout,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if d.user != nil {
config.User = d.user.Username()
if password, _ := d.user.Password(); password != "" {
if d.options.Auth != nil {
config.User = d.options.Auth.Username()
if password, _ := d.options.Auth.Password(); password != "" {
config.Auth = []ssh.AuthMethod{
ssh.Password(password),
}