/
sftp.go
185 lines (154 loc) · 3.85 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* knoxite
* Copyright (c) 2019, Fabian Siegel <fabians1999@gmail.com>
* 2020, Christian Muehlhaeuser <muesli@gmail.com>
*
* For license see LICENSE
*/
package sftp
import (
"io/ioutil"
"net"
"net/url"
"os"
"os/user"
"path/filepath"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
kh "golang.org/x/crypto/ssh/knownhosts"
"github.com/knoxite/knoxite"
)
type SFTPStorage struct {
url url.URL
ssh *ssh.Client
sftp *sftp.Client
knoxite.StorageFilesystem
}
func init() {
knoxite.RegisterStorageBackend(&SFTPStorage{})
}
func (*SFTPStorage) NewBackend(u url.URL) (knoxite.Backend, error) {
_, port, err := net.SplitHostPort(u.Host)
if err != nil || len(port) == 0 {
port = "22"
u.Host = net.JoinHostPort(u.Host, port)
}
username := u.User.Username()
password, isSet := u.User.Password()
auth := []ssh.AuthMethod{}
if isSet {
auth = append(auth, ssh.Password(password))
} else {
socket := os.Getenv("SSH_AUTH_SOCK")
agent_conn, err := net.Dial("unix", socket)
if err != nil {
return &SFTPStorage{}, knoxite.ErrInvalidPassword
} else {
agentClient := agent.NewClient(agent_conn)
auth = append(auth, ssh.PublicKeysCallback(agentClient.Signers))
}
}
usr, _ := user.Current()
hostKeyCallback, err := kh.New(filepath.Join(usr.HomeDir, ".ssh/known_hosts"))
if err != nil {
// If no hostkey can be found, ignore it for now...
hostKeyCallback = ssh.InsecureIgnoreHostKey()
}
config := &ssh.ClientConfig{
User: username,
Auth: auth,
HostKeyCallback: hostKeyCallback,
}
conn, err := ssh.Dial("tcp", u.Hostname()+":"+u.Port(), config)
if err != nil {
return &SFTPStorage{}, err
}
client, err := sftp.NewClient(conn)
if err != nil {
return &SFTPStorage{}, err
}
backend := SFTPStorage{
url: u,
sftp: client,
ssh: conn,
}
fs, err := knoxite.NewStorageFilesystem(u.Path, &backend)
if err != nil {
return &SFTPStorage{}, err
}
backend.StorageFilesystem = fs
return &backend, nil
}
func (backend *SFTPStorage) Protocols() []string {
return []string{"sftp"}
}
func (backend *SFTPStorage) AvailableSpace() (uint64, error) {
stat, err := backend.sftp.StatVFS(backend.url.Path)
if err != nil || stat == nil {
return 0, knoxite.ErrAvailableSpaceUnknown
}
return stat.FreeSpace(), err
}
func (backend *SFTPStorage) Close() error {
return backend.sftp.Close()
}
func (backend *SFTPStorage) Description() string {
return "SSH/SFTP Storage"
}
func (backend *SFTPStorage) Location() string {
return backend.url.String()
}
func (backend *SFTPStorage) CreatePath(path string) error {
return backend.sftp.MkdirAll(path)
}
func (backend *SFTPStorage) DeleteFile(path string) error {
return backend.sftp.Remove(path)
}
func (backend *SFTPStorage) DeletePath(path string) error {
// fmt.Println("Deleting path", path)
files, err := backend.sftp.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
fpath := backend.sftp.Join(path, file.Name())
if file.IsDir() {
err = backend.DeletePath(fpath)
if err != nil {
return err
}
err = backend.sftp.Remove(fpath)
} else {
err = backend.DeleteFile(fpath)
}
if err != nil {
return err
}
}
return nil
}
func (backend *SFTPStorage) ReadFile(path string) ([]byte, error) {
file, err := backend.sftp.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return ioutil.ReadAll(file)
}
func (backend *SFTPStorage) WriteFile(path string, data []byte) (size uint64, err error) {
file, err := backend.sftp.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
if err != nil {
return 0, err
}
defer file.Close()
length, err := file.Write(data)
return uint64(length), err
}
func (backend *SFTPStorage) Stat(path string) (uint64, error) {
stat, err := backend.sftp.Stat(path)
if err != nil {
return 0, err
}
return uint64(stat.Size()), err
}