Skip to content

Commit

Permalink
Use NewConn instead of New
Browse files Browse the repository at this point in the history
  • Loading branch information
SmsS4 committed Sep 20, 2023
1 parent 81cf392 commit 160047f
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package goph
import (
"context"
"fmt"
"strconv"
"strings"
"io"
"net"
"os"
Expand Down Expand Up @@ -44,17 +46,39 @@ func GetAuth(host string) (Auth, error) {
return auth, nil
}

func getSshConfig(alias string, key string) string {
// to fix https://github.com/kevinburke/ssh_config/issues/61 issue
return strings.Trim(ssh_config.Get(alias, key), "\"")
}

func NewWithConfigFileHostKeyCallback(name string, callback ssh.HostKeyCallback) (*Client, error){
auth, err := GetAuth(name)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(getSshConfig(name, "Port"))
if err != nil {
return nil, err
}

return NewConn(&Config{
User: getSshConfig(name, "User"),
Addr: getSshConfig(name, "HostName"),
Port: uint(port),
Auth: auth,
Timeout: DefaultTimeout,
Callback: callback,
})

}

// NewWithConfigFile starts a new ssh connection with given config file, the host public key must be in known hosts.
func NewWithConfigFile(name string) (*Client, error) {
auth, err := GetAuth(name)
callback, err := DefaultKnownHosts()
if err != nil {
return nil, err
}
return New(
ssh_config.Get(name, "User"),
ssh_config.Get(name, "HostName"),
auth,
)
return NewWithConfigFileHostKeyCallback(name, callback)
}

// New starts a new ssh connection, the host public key must be in known hosts.
Expand Down Expand Up @@ -92,15 +116,7 @@ func NewUnknown(user string, addr string, auth Auth) (*Client, error) {
}

func NewWithConfigFileUnknown(name string) (*Client, error) {
auth, err := GetAuth(name)
if err != nil {
return nil, err
}
return NewUnknown(
ssh_config.Get(name, "User"),
ssh_config.Get(name, "HostName"),
auth,
)
return NewWithConfigFileHostKeyCallback(name, ssh.InsecureIgnoreHostKey())
}

// NewConn returns new client and error if any.
Expand Down

0 comments on commit 160047f

Please sign in to comment.