-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
host.go
236 lines (220 loc) · 6.42 KB
/
host.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package service
import (
"encoding/base64"
"fmt"
"github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/utils/ssh"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
)
type HostService struct{}
type IHostService interface {
TestLocalConn(id uint) bool
TestByInfo(req dto.HostConnTest) bool
GetHostInfo(id uint) (*model.Host, error)
SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error)
SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error)
Create(hostDto dto.HostOperate) (*dto.HostInfo, error)
Update(id uint, upMap map[string]interface{}) error
Delete(id []uint) error
}
func NewIHostService() IHostService {
return &HostService{}
}
func (u *HostService) TestByInfo(req dto.HostConnTest) bool {
if req.AuthMode == "password" && len(req.Password) != 0 {
password, err := base64.StdEncoding.DecodeString(req.Password)
if err != nil {
return false
}
req.Password = string(password)
}
if req.AuthMode == "key" && len(req.PrivateKey) != 0 {
privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey)
if err != nil {
return false
}
req.PrivateKey = string(privateKey)
}
if len(req.Password) == 0 && len(req.PrivateKey) == 0 {
host, err := hostRepo.Get(hostRepo.WithByAddr(req.Addr))
if err != nil {
return false
}
req.Password = host.Password
req.AuthMode = host.AuthMode
req.PrivateKey = host.PrivateKey
req.PassPhrase = host.PassPhrase
}
var connInfo ssh.ConnInfo
_ = copier.Copy(&connInfo, &req)
connInfo.PrivateKey = []byte(req.PrivateKey)
if len(req.PassPhrase) != 0 {
connInfo.PassPhrase = []byte(req.PassPhrase)
}
client, err := connInfo.NewClient()
if err != nil {
return false
}
defer client.Close()
return true
}
func (u *HostService) TestLocalConn(id uint) bool {
var (
host model.Host
err error
)
if id == 0 {
host, err = hostRepo.Get(hostRepo.WithByAddr("127.0.0.1"))
if err != nil {
return false
}
} else {
host, err = hostRepo.Get(commonRepo.WithByID(id))
if err != nil {
return false
}
}
var connInfo ssh.ConnInfo
if err := copier.Copy(&connInfo, &host); err != nil {
return false
}
connInfo.PrivateKey = []byte(host.PrivateKey)
if len(host.PassPhrase) != 0 {
connInfo.PassPhrase = []byte(host.PassPhrase)
}
client, err := connInfo.NewClient()
if err != nil {
return false
}
defer client.Close()
return true
}
func (u *HostService) GetHostInfo(id uint) (*model.Host, error) {
host, err := hostRepo.Get(commonRepo.WithByID(id))
if err != nil {
return nil, constant.ErrRecordNotFound
}
return &host, err
}
func (u *HostService) SearchWithPage(search dto.SearchHostWithPage) (int64, interface{}, error) {
total, hosts, err := hostRepo.Page(search.Page, search.PageSize, hostRepo.WithByInfo(search.Info), commonRepo.WithByGroupID(search.GroupID))
if err != nil {
return 0, nil, err
}
var dtoHosts []dto.HostInfo
for _, host := range hosts {
var item dto.HostInfo
if err := copier.Copy(&item, &host); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
group, _ := groupRepo.Get(commonRepo.WithByID(host.GroupID))
item.GroupBelong = group.Name
if !item.RememberPassword {
item.Password = ""
item.PrivateKey = ""
item.PassPhrase = ""
}
dtoHosts = append(dtoHosts, item)
}
return total, dtoHosts, err
}
func (u *HostService) SearchForTree(search dto.SearchForTree) ([]dto.HostTree, error) {
hosts, err := hostRepo.GetList(hostRepo.WithByInfo(search.Info))
if err != nil {
return nil, err
}
groups, err := groupRepo.GetList(commonRepo.WithByType("host"))
if err != nil {
return nil, err
}
var datas []dto.HostTree
for _, group := range groups {
var data dto.HostTree
data.ID = group.ID + 10000
data.Label = group.Name
for _, host := range hosts {
label := fmt.Sprintf("%s@%s:%d", host.User, host.Addr, host.Port)
if len(host.Name) != 0 {
label = fmt.Sprintf("%s - %s@%s:%d", host.Name, host.User, host.Addr, host.Port)
}
if host.GroupID == group.ID {
data.Children = append(data.Children, dto.TreeChild{ID: host.ID, Label: label})
}
}
if len(data.Children) != 0 {
datas = append(datas, data)
}
}
return datas, err
}
func (u *HostService) Create(req dto.HostOperate) (*dto.HostInfo, error) {
var host model.Host
if err := copier.Copy(&host, &req); err != nil {
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
if req.GroupID == 0 {
group, err := groupRepo.Get(groupRepo.WithByIsDefault(true))
if err != nil {
return nil, errors.New("get default group failed")
}
host.GroupID = group.ID
req.GroupID = group.ID
}
var sameHostID uint
if req.Addr == "127.0.0.1" {
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr))
sameHostID = hostSame.ID
} else {
hostSame, _ := hostRepo.Get(hostRepo.WithByAddr(req.Addr), hostRepo.WithByUser(req.User), hostRepo.WithByPort(req.Port))
sameHostID = hostSame.ID
}
if sameHostID != 0 {
host.ID = sameHostID
upMap := make(map[string]interface{})
upMap["name"] = req.Name
upMap["group_id"] = req.GroupID
upMap["addr"] = req.Addr
upMap["port"] = req.Port
upMap["user"] = req.User
upMap["auth_mode"] = req.AuthMode
upMap["password"] = req.Password
upMap["private_key"] = req.PrivateKey
upMap["pass_phrase"] = req.PassPhrase
upMap["remember_password"] = req.RememberPassword
upMap["description"] = req.Description
if err := hostRepo.Update(sameHostID, upMap); err != nil {
return nil, err
}
var hostinfo dto.HostInfo
if err := copier.Copy(&hostinfo, &host); err != nil {
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
return &hostinfo, nil
}
if err := hostRepo.Create(&host); err != nil {
return nil, err
}
var hostinfo dto.HostInfo
if err := copier.Copy(&hostinfo, &host); err != nil {
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
return &hostinfo, nil
}
func (u *HostService) Delete(ids []uint) error {
hosts, _ := hostRepo.GetList(commonRepo.WithIdsIn(ids))
for _, host := range hosts {
if host.ID == 0 {
return constant.ErrRecordNotFound
}
if host.Addr == "127.0.0.1" {
return errors.New("the local connection information cannot be deleted!")
}
}
return hostRepo.Delete(commonRepo.WithIdsIn(ids))
}
func (u *HostService) Update(id uint, upMap map[string]interface{}) error {
return hostRepo.Update(id, upMap)
}