/
ssh.go
171 lines (155 loc) · 3.66 KB
/
ssh.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
package ssh
import (
"Cube-back/log"
"Cube-back/models/common/configure"
"fmt"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"path"
"time"
)
var SftpClient *sftp.Client
var config = map[string]interface{}{}
type Ssh struct {
FileServerIp string
FileServerPort int
FileServerUser string
FileServerPassword string
}
func connect(user, password, host string, port int) (*ssh.Client, error) {
var (
auth []ssh.AuthMethod
addr string
clientConfig *ssh.ClientConfig
sshClient *ssh.Client
err error
)
// get auth method
auth = make([]ssh.AuthMethod, 0)
auth = append(auth, ssh.Password(password))
clientConfig = &ssh.ClientConfig{
User: user,
Auth: auth,
Timeout: 30 * time.Second,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //ssh.FixedHostKey(hostKey),
}
// connet to ssh
addr = fmt.Sprintf("%s:%d", host, port)
if sshClient, err = ssh.Dial("tcp", addr, clientConfig); err != nil {
return nil, err
}
return sshClient, nil
}
func sftpClientCreate(sshClient *ssh.Client) (*sftp.Client, error) {
// create sftp client
sftpClient, err := sftp.NewClient(sshClient)
if err != nil {
return nil, err
}
return sftpClient, nil
}
func sessionCreate(sshClient *ssh.Client) (*ssh.Session, error) {
var session *ssh.Session
session, err := sshClient.NewSession()
if err != nil {
return nil, err
}
return session, nil
}
func CommandExecute(command string) {
session, err := createSession()
if err != nil {
log.Error(err)
}
defer session.Close()
session.Run(command)
}
func UploadFile(filename string, remotePath string, data []uint8) bool {
sftpClient, err := createSftpClient()
if err != nil {
log.Error(err)
return false
}
defer sftpClient.Close()
err = sftpClient.MkdirAll(remotePath)
if err != nil {
log.Error(err)
return false
}
dstFile, err := sftpClient.Create(path.Join(remotePath, filename))
if err != nil {
log.Error(err)
return false
}
_, error := dstFile.Write(data)
if error != nil {
log.Error(error)
return false
}
dstFile.Close()
return true
}
func RemoveFile(path string) {
sftpClient, err := createSftpClient()
if err != nil {
log.Error(err)
}
defer sftpClient.Close()
err = sftpClient.Remove(path)
if err != nil {
log.Error(err)
}
}
func RemoveDirectory(remotePath string) *sftp.Client {
sftpClient, err := createSftpClient()
if err != nil {
log.Error(err)
}
defer sftpClient.Close()
remoteFiles, err := sftpClient.ReadDir(remotePath)
if err != nil {
return sftpClient
}
defer sftpClient.RemoveDirectory(remotePath)
for _, backupDir := range remoteFiles {
remoteFilePath := path.Join(remotePath, backupDir.Name())
if backupDir.IsDir() {
RemoveDirectory(remoteFilePath)
} else {
sftpClient.Remove(path.Join(remoteFilePath))
}
}
return sftpClient
}
func createSftpClient() (*sftp.Client, error) {
sshClient, err := connect(
config["FileServerUser"].(string),
config["FileServerPassword"].(string),
config["FileServerIp"].(string),
config["FileServerPort"].(int))
if sshClient != nil {
return sftpClientCreate(sshClient)
} else {
return nil, err
}
}
func createSession() (*ssh.Session, error) {
sshClient, err := connect(
config["FileServerUser"].(string),
config["FileServerPassword"].(string),
config["FileServerIp"].(string),
config["FileServerPort"].(int))
if sshClient != nil {
return sessionCreate(sshClient)
} else {
return nil, err
}
}
func init() {
conf := new(Ssh)
configure.Get(&conf)
config["FileServerUser"] = conf.FileServerUser
config["FileServerPassword"] = conf.FileServerPassword
config["FileServerIp"] = conf.FileServerIp
config["FileServerPort"] = conf.FileServerPort
}