fix windows id_rsa file

This commit is contained in:
rinetd 2018-12-19 19:58:10 +08:00
parent 08411d78e5
commit ad63cbf4d0
2 changed files with 19 additions and 5 deletions

View File

@ -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

16
util.go
View File

@ -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) {