From ad63cbf4d02e03be4305c01289ccbaaa1421671c Mon Sep 17 00:00:00 2001 From: rinetd Date: Wed, 19 Dec 2018 19:58:10 +0800 Subject: [PATCH] fix windows id_rsa file --- client.go | 8 +++----- util.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index e63b1c5..8746911 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net" - "os" "strconv" "time" @@ -35,8 +34,8 @@ func New(cnf *Config) (client *Client, err error) { // 1. privite key file if len(cnf.KeyFiles) == 0 { - keyPath := os.Getenv("HOME") + "/.ssh/id_rsa" - if auth, err := AuthWithPrivateKey(keyPath, cnf.Passphrase); err == nil { + + if auth, err := AuthWithPrivateKey(KeyFile(), cnf.Passphrase); err == nil { clientConfig.Auth = append(clientConfig.Auth, auth) } } else { @@ -128,8 +127,7 @@ func NewWithPrivateKey(Host, Port, User, Passphrase string) (client *Client, err HostKeyCallback: ssh.InsecureIgnoreHostKey(), } // 3. privite key file - keyPath := os.Getenv("HOME") + "/.ssh/id_rsa" - auth, err := AuthWithPrivateKey(keyPath, Passphrase) + auth, err := AuthWithPrivateKey(KeyFile(), Passphrase) if err != nil { fmt.Println(err) return nil, err diff --git a/util.go b/util.go index da26b8b..92b8878 100644 --- a/util.go +++ b/util.go @@ -5,9 +5,25 @@ import ( "crypto" "encoding/hex" "io" + "log" "os" + "path" + "path/filepath" + + "github.com/mitchellh/go-homedir" ) +func KeyFile() string { + + home, err := homedir.Dir() + if err != nil { + println(err.Error()) + return "" + } + key := filepath.ToSlash(path.Join(home, ".ssh/id_rsa")) + log.Println(key) + return key +} func FileExist(file string) bool { if _, err := os.Stat(file); err != nil { if os.IsNotExist(err) {