-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsftp_client.go
More file actions
45 lines (37 loc) · 802 Bytes
/
sftp_client.go
File metadata and controls
45 lines (37 loc) · 802 Bytes
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
package sshlib
import (
"errors"
"net"
"github.com/pkg/sftp"
)
func (c *Connect) OpenSFTP() (*sftp.Client, error) {
return c.newSFTPClient()
}
func (c *Connect) newSFTPClient() (*sftp.Client, error) {
if c.isControlClient() {
return c.controlClient.newSFTPClient()
}
if c.Client == nil {
return nil, errors.New("ssh client is nil")
}
return sftp.NewClient(c.Client)
}
func (c *controlClient) newSFTPClient() (*sftp.Client, error) {
resp, err := c.request(controlRequest{
Type: controlRequestSubsystem,
Command: "sftp",
})
if err != nil {
return nil, err
}
conn, err := net.Dial("unix", resp.StreamPath)
if err != nil {
return nil, err
}
client, err := sftp.NewClientPipe(conn, conn)
if err != nil {
_ = conn.Close()
return nil, err
}
return client, nil
}