/
sftp.go
103 lines (88 loc) · 2.16 KB
/
sftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package filesystem
import (
"fmt"
"io/ioutil"
"net"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// SFTP represents a filesystem over an SFTP connection
type SFTP struct {
client *sftp.Client
basePath string
}
// NewSFTP returns a new SFTP filesystem
func NewSFTP(u *url.URL) (FileSystem, error) {
config := &ssh.ClientConfig{
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil },
}
// add user's public key to ssh configuration
usr, pubKey := publicKey()
config.User = usr
if pubKey != nil {
config.Auth = append(config.Auth, pubKey)
}
// add user/password authentication if specified in url
if u.User != nil {
usr = u.User.Username()
config.User = usr
if password, ok := u.User.Password(); ok {
config.Auth = append(config.Auth, ssh.Password(password))
}
}
hp := hostPort(u.Host)
conn, err := ssh.Dial("tcp", hp, config)
if err != nil {
return nil, fmt.Errorf("dial: %s", err)
}
client, err := sftp.NewClient(conn)
if err != nil {
return nil, fmt.Errorf("create sftp client: %s", err)
}
basePath := u.Path
return &SFTP{
client: client,
basePath: basePath,
}, nil
}
func (s *SFTP) ReadDir(dirname string) ([]os.FileInfo, error) {
return s.client.ReadDir(filepath.Join(s.basePath, dirname))
}
func (s *SFTP) Lstat(name string) (os.FileInfo, error) {
return s.client.Lstat(filepath.Join(s.basePath, name))
}
func (s *SFTP) Join(elem ...string) string {
return s.client.Join(elem...)
}
func (s *SFTP) Open(path string) (File, error) {
return s.client.Open(filepath.Join(s.basePath, path))
}
func (s *SFTP) Close() error {
return s.client.Close()
}
func publicKey() (username string, pubKey ssh.AuthMethod) {
usr, err := user.Current()
if err != nil {
return "", nil
}
key, err := ioutil.ReadFile(filepath.Join(usr.HomeDir, ".ssh/id_rsa"))
if err != nil {
return "", nil
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return "", nil
}
return usr.Username, ssh.PublicKeys(signer)
}
func hostPort(host string) string {
if !strings.ContainsRune(host, ':') {
return fmt.Sprintf("%s:%d", host, 22)
}
return host
}