/
sftpc.go
160 lines (138 loc) · 2.88 KB
/
sftpc.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
package sftpc
import (
"fmt"
"io"
"os"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
type Client struct {
User string
Password string
Host string
Port int
}
// SftpSession sftp会话
type SftpSession struct {
Client *sftp.Client
}
// NewSession 创建一个新的sftp会话
func (s *Client) NewSession() (*SftpSession, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
sshClient *ssh.Client
sftpClient *sftp.Client
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(s.Password))
clientConfig = &ssh.ClientConfig{
User: s.User,
Auth: auth,
Timeout: 10 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
// connet to ssh
addr = fmt.Sprintf("%s:%d", s.Host, s.Port)
if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
// create sftp client
if sftpClient, err = sftp.NewClient(sshClient); err != nil {
return nil, err
}
return &SftpSession{
Client: sftpClient,
}, nil
}
// Upload 上传本地文件到远程
func (s *Client) Upload(session *SftpSession, localFilePath, remotePath string) error {
var err error
var _session *SftpSession
if session == nil {
_session, err = s.NewSession()
if err != nil {
return err
}
defer _session.Client.Close()
} else {
_session = session
}
srcFile, err := os.Open(localFilePath)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := _session.Client.Create(remotePath)
if err != nil {
return err
}
defer dstFile.Close()
buf := make([]byte, 1024)
for {
n, e := srcFile.Read(buf)
if e != nil && e!= io.EOF {
return e
}
if n == 0 {
break
}
if _, e := dstFile.Write(buf[:n]); e != nil {
return e
}
}
return nil
}
// Download 下载远程文件到本地
func (s *Client) Download(session *SftpSession, remotePath, localFilePath string) error {
var err error
var _session *SftpSession
if session == nil {
_session, err = s.NewSession()
if err != nil {
return err
}
defer _session.Client.Close()
} else {
_session = session
}
srcFile, err := _session.Client.Open(remotePath)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(localFilePath)
if err != nil {
return err
}
defer dstFile.Close()
if _, err = srcFile.WriteTo(dstFile); err != nil {
return err
}
return nil
}
func (s *Client) Remove(session *SftpSession, remotePath string) error {
var err error
var _session *SftpSession
if session == nil {
_session, err = s.NewSession()
if err != nil {
return err
}
defer _session.Client.Close()
} else {
_session = session
}
err = _session.Client.Remove(remotePath)
if err != nil {
return err
}
return nil
}
func (s *Client) Close(session *SftpSession) error {
return session.Client.Close()
}