-
Notifications
You must be signed in to change notification settings - Fork 2
/
sftpfs.go
276 lines (239 loc) · 5.7 KB
/
sftpfs.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package sftpd
import (
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func NewSftpFile(file *sftp.File) *SftpFile {
return &SftpFile{file: file}
}
func NewSftpDir(client *sftp.Client, path string) *SftpDir {
return &SftpDir{client: client, path: path}
}
func NewSftpFs(client *sftp.Client) *sftpFs {
return &sftpFs{client: client}
}
type SftpFile struct {
file *sftp.File
}
func (sf *SftpFile) Close() error {
return sf.file.Close()
}
func (sf *SftpFile) ReadAt(bs []byte, pos int64) (int, error) {
// 设置光标位置 offset,偏移量, whence,从哪开始:0从头,1当前,2末尾
_, err := sf.file.Seek(pos, 0)
if err != nil {
return 0, err
}
return sf.file.Read(bs)
}
func (sf *SftpFile) WriteAt(bs []byte, pos int64) (int, error) {
// 设置光标位置 offset,偏移量, whence,从哪开始:0从头,1当前,2末尾
_, err := sf.file.Seek(pos, 0)
if err != nil {
return 0, err
}
return sf.file.Write(bs)
}
func (sf *SftpFile) FStat() (*Attr, error) {
var a Attr
fi, e := sf.file.Stat()
if e != nil {
return nil, e
}
a.FillFrom(fi, 2)
return &a, nil
}
func (sf *SftpFile) FSetStat(a *Attr) error {
e := sf.file.Chmod(a.Mode)
if e != nil {
return e
}
sysType := runtime.GOOS
if sysType != "windows" { // windows 不支持 chown 操作
e = sf.file.Chown(int(a.Uid), int(a.Gid))
}
return e
}
type SftpDir struct {
client *sftp.Client
path string
hasRead bool
}
func (sd *SftpDir) Readdir(count int, handles Handles) ([]NamedAttr, error) {
if sd.hasRead {
return nil, io.EOF
}
sd.hasRead = true
fis, e := sd.client.ReadDir(sd.path)
if e != nil {
return nil, e
}
rs := make([]NamedAttr, len(fis))
for i, fi := range fis {
rs[i].Name = fi.Name()
rs[i].FillFrom(fi, 2)
}
return rs, nil
}
func (sd *SftpDir) Close() error {
return nil
}
type sftpFs struct {
client *sftp.Client
}
func (sfs *sftpFs) Stat(path string, isLstat bool) (*Attr, error) {
var fi os.FileInfo
var e error
if isLstat {
fi, e = sfs.client.Lstat(path)
} else {
fi, e = sfs.client.Stat(path)
}
if e != nil {
return nil, e
}
var a Attr
a.FillFrom(fi, 2)
return &a, nil
}
func (sfs *sftpFs) OpenFile(path string, mode uint32, a *Attr) (File, error) {
var (
f *sftp.File
flag int
e error
)
if mode & SSH_FXF_READ != 0 {
flag |= os.O_RDONLY
}
if mode & SSH_FXF_WRITE != 0 {
flag |= os.O_WRONLY
}
if mode & SSH_FXF_APPEND != 0 {
flag |= os.O_APPEND
}
if mode & SSH_FXF_CREAT != 0 {
flag |= os.O_CREATE
}
if mode & SSH_FXF_TRUNC != 0 {
flag |= os.O_TRUNC
}
if mode & SSH_FXF_EXCL != 0 {
flag |= os.O_EXCL
}
f, e = sfs.client.OpenFile(path, flag)
if e != nil {
return nil, e
}
return NewSftpFile(f), nil
}
func (sfs *sftpFs) OpenDir(path string) (Dir, error) {
return NewSftpDir(sfs.client, path), nil
}
func (sfs *sftpFs) Remove(path string) error {
return sfs.client.Remove(path)
}
func (sfs *sftpFs) Rename(oldName, newName string, flag uint32) error {
return sfs.client.Rename(oldName, newName)
}
func (sfs *sftpFs) Mkdir(path string, attr *Attr) error {
return sfs.client.Mkdir(path)
}
func (sfs *sftpFs) Rmdir(path string) error {
return sfs.client.RemoveDirectory(path)
}
func (sfs *sftpFs) SetStat(path string, attr *Attr) error {
e := sfs.client.Chmod(path, attr.Mode)
if e != nil {
return e
}
e = sfs.client.Chown(path, int(attr.Uid), int(attr.Gid))
return e
}
func (sfs *sftpFs) ReadLink(path string) (string, error) {
link, e := sfs.client.ReadLink(path)
if e != nil {
return "", e
}
return link, nil
}
func (sfs *sftpFs) CreateLink(path string, target string, flags uint32) error {
return sfs.client.Symlink(target, path)
}
func (sfs *sftpFs) RealPath(pathX string) (string, error) {
switch pathX {
case "", ".":
pathX = "/"
default:
pathX = path.Clean(pathX)
}
return pathX, nil
}
func publicKeyAuthFunc(pemBytes, keyPassword []byte) (ssh.AuthMethod, error) {
// 通过私钥创建一个 Signer 对象,在根据 Signer 对象获取 AuthMethod 对象
var (
signer ssh.Signer
err error
)
if string(keyPassword) == "" {
signer, err = ssh.ParsePrivateKey(pemBytes)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(pemBytes, keyPassword)
}
if err != nil {
return nil, err
}
return ssh.PublicKeys(signer), nil
}
func NewSshClientConfig(sshUser, sshPassword, sshType, sshKey, sshKeyPassword string, timeout time.Duration) (config *ssh.ClientConfig, err error) {
// 创建 ssh 配置
if sshUser == "" {
return nil, errors.New("ssh_user can not be empty")
}
sshConfig := ssh.Config{
// 兼容交换机等多种设备
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "aes128-gcm@openssh.com", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
}
config = &ssh.ClientConfig{
Config: sshConfig,
Timeout: timeout,
User: sshUser,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
switch sshType {
case "password":
config.Auth = []ssh.AuthMethod{ssh.Password(sshPassword)}
case "key":
key, err := publicKeyAuthFunc([]byte(sshKey), []byte(sshKeyPassword))
if err != nil {
return nil, err
}
config.Auth = []ssh.AuthMethod{key}
default:
return nil, fmt.Errorf("unknow ssh auth type: %s", sshType)
}
return
}
func NewSshUpstream(host, username, password string, timeout time.Duration) (*ssh.Client, *ssh.Session, error) {
// 连接 ssh
clientConfig, err := NewSshClientConfig(username, password, "password", "", "", timeout)
if err != nil {
return nil, nil, err
}
client, err := ssh.Dial("tcp", host, clientConfig)
if err != nil {
return nil, nil, err
}
session, err := client.NewSession()
if err != nil {
_ = client.Close()
return nil, nil, err
}
return client, session, nil
}