-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
config.go
44 lines (40 loc) · 1.42 KB
/
config.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
package sftpmanager
import (
"github.com/admpub/nging/v4/application/library/config"
"github.com/admpub/web-terminal/library/ssh"
"github.com/pkg/sftp"
)
// Config 配置
type Config struct {
Host string `db:"host" bson:"host" comment:"主机名" json:"host" xml:"host"`
Port int `db:"port" bson:"port" comment:"端口" json:"port" xml:"port"`
Username string `db:"username" bson:"username" comment:"用户名" json:"username" xml:"username"`
Password string `db:"password" bson:"password" comment:"密码" json:"password" xml:"password"`
PrivateKey string `db:"private_key" bson:"private_key" comment:"私钥内容" json:"private_key" xml:"private_key"`
Passphrase string `db:"passphrase" bson:"passphrase" comment:"私钥口令" json:"passphrase" xml:"passphrase"`
}
func (c *Config) Connect() (*sftp.Client, error) {
account := &ssh.AccountConfig{
User: c.Username,
Password: config.FromFile().Decode(c.Password),
}
if len(c.PrivateKey) > 0 {
account.PrivateKey = []byte(c.PrivateKey)
}
if len(c.Passphrase) > 0 {
account.Passphrase = []byte(config.FromFile().Decode(c.Passphrase))
}
config, err := ssh.NewSSHConfig(nil, nil, account)
if err != nil {
return nil, err
}
sshClient := ssh.New(config)
err = sshClient.Connect(c.Host, c.Port)
if err != nil {
return nil, err
}
return sftp.NewClient(sshClient.Client)
}
func DefaultConnector(c *Config) (*sftp.Client, error) {
return c.Connect()
}