-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.go
147 lines (128 loc) · 3.03 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
package main
import (
"context"
"fmt"
"io"
"log/slog"
"strings"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
func NewSftpClient(server, user, pass, dir string) (*SftpClient, string, error) {
if !strings.HasSuffix(server, ":22") && !strings.Contains(server, ":") {
server = server + ":22"
}
config := &ssh.ClientConfig{
Timeout: 10 * time.Second,
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pass),
ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
// Just send the password back for all questions
answers := make([]string, len(questions))
for i := range answers {
answers[i] = pass
}
return answers, nil
}),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
//HostKeyCallback: ssh.FixedHostKey(hostKey),
}
sshClient, err := ssh.Dial("tcp", server, config)
if err != nil {
return nil, "", fmt.Errorf("dial %s error %w", server, err)
}
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return nil, "", fmt.Errorf("sftp %s error %w", server, err)
}
if dir == "" {
dir, err = sftpClient.Getwd()
if err != nil {
return nil, "", fmt.Errorf("getwd error %w", err)
}
}
pwd, err := sftpClient.RealPath(dir)
if err != nil {
return nil, "", fmt.Errorf("realpath %s error %w", dir, err)
}
return &SftpClient{
Pwd: pwd,
Client: sftpClient,
}, pwd, nil
}
type SftpClient struct {
Pwd string
*sftp.Client
}
func (c *SftpClient) List(ctx context.Context, prefix, marker string) (data []File, nextMarker string, err error) {
slog.Debug("s3 list",
slog.String("marker", marker),
slog.String("prefix", prefix),
)
if prefix == "" {
prefix = c.Pwd
}
fis, err := c.ReadDir(prefix)
if err != nil {
err = fmt.Errorf("readDir %s error %w", prefix, err)
return
}
data = make([]File, len(fis))
for i, v := range fis {
f := File{
Name: v.Name(),
Type: FileRegular,
Size: v.Size(),
Time: v.ModTime(),
}
if v.IsDir() {
f.Type = FileDir
if !strings.HasSuffix(f.Name, "/") {
f.Name += "/"
}
}
data[i] = f
}
return
}
func (c *SftpClient) Upload(ctx context.Context, rs io.ReadSeeker, key, contentType string) (err error) {
f, err := c.Create(key)
if err != nil {
err = fmt.Errorf("create %s error %w", key, err)
return
}
_, err = io.Copy(f, rs)
return
}
func (c *SftpClient) Download(ctx context.Context, w io.Writer, key string) (err error) {
f, err := c.Open(key)
if err != nil {
err = fmt.Errorf("open %s error %w", key, err)
return err
}
_, err = io.Copy(w, f)
return
}
func (c *SftpClient) Delete(ctx context.Context, key string) (err error) {
return c.Remove(key)
}
func (c *SftpClient) Stat(ctx context.Context, key string) (f File, err error) {
fi, err := c.Client.Stat(key)
if err != nil {
err = fmt.Errorf("stat %s error %w", key, err)
return
}
f.Name = fi.Name()
if fi.IsDir() {
f.Type = FileDir
}
f.Size = fi.Size()
f.Time = fi.ModTime()
return
}
func (c *SftpClient) Close(ctx context.Context) error {
return c.Client.Close()
}