-
Notifications
You must be signed in to change notification settings - Fork 0
/
sftp.go
382 lines (368 loc) · 9.29 KB
/
sftp.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package term
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/handler/caddy"
"github.com/admpub/nging/application/library/charset"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/nging/application/library/filemanager"
"github.com/admpub/nging/application/library/notice"
"github.com/admpub/nging/application/model"
"github.com/admpub/web-terminal/library/ssh"
"github.com/pkg/sftp"
"github.com/webx-top/com"
"github.com/webx-top/echo"
)
func sftpConnect(m *dbschema.SshUser) (*sftp.Client, error) {
account := &ssh.AccountConfig{
User: m.Username,
Password: config.DefaultConfig.Decode(m.Password),
}
if len(m.PrivateKey) > 0 {
account.PrivateKey = []byte(m.PrivateKey)
}
if len(m.Passphrase) > 0 {
account.Passphrase = []byte(config.DefaultConfig.Decode(m.Passphrase))
}
config, err := ssh.NewSSHConfig(nil, nil, account)
if err != nil {
return nil, err
}
sshClient := ssh.New(config)
err = sshClient.Connect(m.Host, m.Port)
if err != nil {
return nil, err
}
return sftp.NewClient(sshClient.Client)
}
func SftpSearch(ctx echo.Context, id uint) error {
m := model.NewSshUser(ctx)
err := m.Get(nil, `id`, id)
if err != nil {
return err
}
mgr, err := sftpConnect(m.SshUser)
if err != nil {
return err
}
defer mgr.Close()
var (
paths []string
prefix string
ppath string
)
query := ctx.Form(`query`)
if strings.HasSuffix(query, `/`) {
ppath = query
} else {
prefix = path.Base(query)
ppath = path.Dir(query)
}
num := ctx.Formx(`size`, `10`).Int()
if num <= 0 {
num = 10
}
if len(ppath) == 0 {
ppath = `/`
}
var onlyDir bool
switch ctx.Form(`type`) {
case `dir`:
onlyDir = true
case `file`:
onlyDir = false
default:
onlyDir = true
}
dirs, _ := mgr.ReadDir(ppath)
for _, d := range dirs {
if onlyDir && d.IsDir() == false {
continue
}
if len(paths) >= num {
break
}
name := d.Name()
if len(prefix) == 0 || strings.HasPrefix(name, prefix) {
paths = append(paths, path.Join(ppath, name)+`/`)
continue
}
}
data := ctx.Data().SetData(paths)
return ctx.JSON(data)
}
func Sftp(ctx echo.Context) error {
ctx.Set(`activeURL`, `/term/account`)
id := ctx.Formx(`id`).Uint()
m := model.NewSshUser(ctx)
err := m.Get(nil, `id`, id)
if err != nil {
return err
}
mgr, err := sftpConnect(m.SshUser)
if err != nil {
return err
}
defer mgr.Close()
ppath := ctx.Form(`path`)
do := ctx.Form(`do`)
parentPath := ppath
if len(ppath) == 0 {
ppath = `/`
} else {
parentPath = path.Dir(ppath)
}
switch do {
case `edit`:
data := ctx.Data()
if _, ok := caddy.Editable(ppath); !ok {
data.SetInfo(ctx.T(`此文件不能在线编辑`), 0)
} else {
content := ctx.Form(`content`)
encoding := ctx.Form(`encoding`)
f, err := mgr.Open(ppath)
if err != nil {
return ctx.JSON(data.SetError(err))
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return ctx.JSON(data.SetError(err))
}
if fi.IsDir() {
return ctx.JSON(data.SetInfo(ctx.T(`不能编辑文件夹`), 0))
}
if config.DefaultConfig.Sys.EditableFileMaxBytes > 0 && fi.Size() > config.DefaultConfig.Sys.EditableFileMaxBytes {
return ctx.JSON(data.SetInfo(ctx.T(`很抱歉,不支持编辑超过%v的文件`, com.FormatByte(config.DefaultConfig.Sys.EditableFileMaxBytes), 0)))
}
encoding = strings.ToLower(encoding)
isUTF8 := len(encoding) == 0 || encoding == `utf-8`
if ctx.IsPost() {
b := []byte(content)
if !isUTF8 {
b, err = charset.Convert(`utf-8`, encoding, b)
if err != nil {
return ctx.JSON(data.SetError(err))
}
}
f.Close()
r := bytes.NewReader(b)
f, err = mgr.OpenFile(ppath, os.O_CREATE|os.O_RDWR|os.O_TRUNC)
if err != nil {
return ctx.JSON(data.SetError(err))
}
_, err = io.Copy(f, r)
if err != nil {
data.SetInfo(ppath+`:`+err.Error(), 0)
} else {
data.SetInfo(ctx.T(`保存成功`), 1)
}
return ctx.JSON(data)
}
dat, err := ioutil.ReadAll(f)
if err == nil && !isUTF8 {
dat, err = charset.Convert(encoding, `utf-8`, dat)
}
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetData(string(dat), 1)
}
}
return ctx.JSON(data)
case `mkdir`:
data := ctx.Data()
newName := ctx.Form(`name`)
if len(newName) == 0 {
data.SetInfo(ctx.T(`请输入文件夹名`), 0)
} else {
dirPath := path.Join(ppath, newName)
if f, err := mgr.Open(dirPath); err == nil {
if finfo, err := f.Stat(); err != nil {
data.SetError(err)
} else if finfo.IsDir() {
data.SetInfo(ctx.T(`已经存在相同名称的文件夹`), 0)
} else {
data.SetInfo(ctx.T(`已经存在相同名称的文件`), 0)
}
} else if !os.IsNotExist(err) {
data.SetError(err)
} else {
err = mgr.Mkdir(dirPath)
if err != nil {
data.SetError(err)
}
}
if data.GetCode() == 1 {
data.SetInfo(ctx.T(`创建成功`))
}
}
return ctx.JSON(data)
case `rename`:
data := ctx.Data()
newName := ctx.Form(`name`)
err = mgr.Rename(ppath, newName)
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetInfo(ctx.T(`重命名成功`), 1)
}
return ctx.JSON(data)
case `chown`:
data := ctx.Data()
uid := ctx.Formx(`uid`).Int()
gid := ctx.Formx(`gid`).Int()
err = mgr.Chown(ppath, uid, gid)
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetInfo(ctx.T(`操作成功`), 1)
}
return ctx.JSON(data)
case `chmod`:
data := ctx.Data()
mode := ctx.Formx(`mode`).Uint32() //0777 etc...
err = mgr.Chmod(ppath, os.FileMode(mode))
if err != nil {
data.SetInfo(err.Error(), 0)
} else {
data.SetInfo(ctx.T(`操作成功`), 1)
}
return ctx.JSON(data)
case `search`:
var paths []string
prefix := ctx.Form(`query`)
num := ctx.Formx(`size`, `10`).Int()
if num <= 0 {
num = 10
}
dirs, _ := mgr.ReadDir(ppath)
for _, d := range dirs {
if len(paths) >= num {
break
}
name := d.Name()
if strings.HasPrefix(name, prefix) {
paths = append(paths, name)
continue
}
}
data := ctx.Data().SetData(paths)
return ctx.JSON(data)
case `delete`:
err = mgr.Remove(ppath)
if err != nil {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(ctx.Referer())
case `upload`:
d, err := mgr.Open(ppath)
if err != nil {
return err
}
defer d.Close()
fi, err := d.Stat()
if !fi.IsDir() {
return ctx.E(`路径不正确`)
}
fileSrc, fileHdr, err := ctx.Request().FormFile(`file`)
if err != nil {
return err
}
defer fileSrc.Close()
// Destination
fileName := fileHdr.Filename
fileDst, err := mgr.Create(path.Join(ppath, fileName))
if err != nil {
return err
}
defer fileDst.Close()
// Copy
_, err = io.Copy(fileDst, fileSrc)
if err != nil {
user := handler.User(ctx)
if user != nil {
notice.OpenMessage(user.Username, `upload`)
notice.Send(user.Username, notice.NewMessageWithValue(`upload`, ctx.T(`文件上传出错`), err.Error()))
}
return ctx.JSON(echo.H{`error`: err.Error()}, 500)
}
return ctx.String(`OK`)
default:
d, err := mgr.Open(ppath)
if err != nil {
return err
}
defer d.Close()
fi, err := d.Stat()
if !fi.IsDir() {
fileName := path.Base(ppath)
return ctx.Attachment(d, fileName)
}
dirs, err := mgr.ReadDir(ppath)
sortBy := ctx.Form(`sortBy`)
switch sortBy {
case `time`:
sort.Sort(filemanager.SortByModTime(dirs))
case `-time`:
sort.Sort(filemanager.SortByModTimeDesc(dirs))
case `name`:
case `-name`:
sort.Sort(filemanager.SortByNameDesc(dirs))
case `type`:
fallthrough
default:
sort.Sort(filemanager.SortByFileType(dirs))
}
ctx.Set(`dirs`, dirs)
}
ctx.Set(`parentPath`, parentPath)
ctx.Set(`path`, ppath)
pathPrefix := ppath
if ppath != `/` {
pathPrefix = ppath + `/`
}
pathSlice := strings.Split(strings.Trim(pathPrefix, `/`), `/`)
pathLinks := make(echo.KVList, len(pathSlice))
encodedSep := filemanager.EncodedSep
urlPrefix := fmt.Sprintf(`/term/sftp?id=%d&path=`, id) + encodedSep
for k, v := range pathSlice {
urlPrefix += com.URLEncode(v)
pathLinks[k] = &echo.KV{K: v, V: urlPrefix}
urlPrefix += encodedSep
}
ctx.Set(`pathLinks`, pathLinks)
ctx.Set(`pathPrefix`, pathPrefix)
ctx.SetFunc(`Editable`, func(fileName string) bool {
_, ok := caddy.Editable(fileName)
return ok
})
ctx.SetFunc(`Playable`, func(fileName string) string {
mime, _ := caddy.Playable(fileName)
return mime
})
ctx.Set(`data`, m.SshUser)
return ctx.Render(`term/sftp`, err)
}