/
ssh_config.go
68 lines (58 loc) · 1.84 KB
/
ssh_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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package http
import (
"net/http"
"encoding/json"
"fmt"
"github.com/wanglu119/ng-ssh/lib/config"
)
var getSshConfigs = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
sshConfigs := config.GetSshConfigs()
return renderJSON(w, r, sshConfigs)
})
var getSshConfigNames = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
sshConfigs := config.GetSshConfigs()
configNames := []string{}
for _,conf := range sshConfigs {
configNames = append(configNames, conf.Name)
}
return renderJSON(w, r, configNames)
})
var getSftpConfigNames = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
sshConfigs := config.GetSshConfigs()
configNames := []string{}
for _,conf := range sshConfigs {
if conf.Name != "ngssh-server" {
configNames = append(configNames, conf.Name)
}
}
return renderJSON(w, r, configNames)
})
var addSshConfig = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
sshConfig := &config.SshConfig{}
err := json.NewDecoder(r.Body).Decode(sshConfig)
if err != nil {
log.Error(fmt.Sprintf("%v", err))
return http.StatusInternalServerError, err
}
sshConfig.Type = config.SSH_CONFIG_TYPE_PASSWORD
config.Encode(sshConfig)
sshConfigs := config.GetSshConfigs()
sshConfigs[sshConfig.Name] = sshConfig
config.UpdateConfig()
return 0, nil
})
var deleteSshConfig = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
sshConfig := &config.SshConfig{}
err := json.NewDecoder(r.Body).Decode(sshConfig)
if err != nil {
log.Error(fmt.Sprintf("%v", err))
return http.StatusInternalServerError, err
}
sshConfigs := config.GetSshConfigs()
if _, ok := sshConfigs[sshConfig.Name]; !ok {
return http.StatusBadRequest, nil
}
delete(sshConfigs, sshConfig.Name)
config.UpdateConfig()
return 0, nil
})