fix metadata

This commit is contained in:
ginuerzh
2022-04-07 23:03:31 +08:00
parent 1415462d23
commit d011aefefd
10 changed files with 32 additions and 48 deletions

View File

@ -24,17 +24,19 @@ type sshDialer struct {
sessionMutex sync.Mutex
logger logger.Logger
md metadata
options dialer.Options
}
func NewDialer(opts ...dialer.Option) dialer.Dialer {
options := &dialer.Options{}
options := dialer.Options{}
for _, opt := range opts {
opt(options)
opt(&options)
}
return &sshDialer{
sessions: make(map[string]*sshSession),
logger: options.Logger,
options: options,
}
}
@ -139,9 +141,9 @@ func (d *sshDialer) initSession(ctx context.Context, addr string, conn net.Conn)
Timeout: 30 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
if d.md.user != nil {
config.User = d.md.user.Username()
if password, _ := d.md.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),
}

View File

@ -2,8 +2,6 @@ package ssh
import (
"io/ioutil"
"net/url"
"strings"
"time"
mdata "github.com/go-gost/core/metadata"
@ -13,27 +11,16 @@ import (
type metadata struct {
handshakeTimeout time.Duration
user *url.Userinfo
signer ssh.Signer
}
func (d *sshDialer) parseMetadata(md mdata.Metadata) (err error) {
const (
handshakeTimeout = "handshakeTimeout"
user = "user"
privateKeyFile = "privateKeyFile"
passphrase = "passphrase"
)
if v := mdx.GetString(md, user); v != "" {
ss := strings.SplitN(v, ":", 2)
if len(ss) == 1 {
d.md.user = url.User(ss[0])
} else {
d.md.user = url.UserPassword(ss[0], ss[1])
}
}
if key := mdx.GetString(md, privateKeyFile); key != "" {
data, err := ioutil.ReadFile(key)
if err != nil {