|
1 | 1 | package service |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json" |
5 | 6 | "time" |
6 | 7 |
|
7 | 8 | "github.com/1Panel-dev/1Panel/agent/app/dto" |
8 | 9 | "github.com/1Panel-dev/1Panel/agent/buserr" |
| 10 | + "github.com/1Panel-dev/1Panel/agent/utils/encrypt" |
| 11 | + "github.com/1Panel-dev/1Panel/agent/utils/ssh" |
| 12 | + "github.com/jinzhu/copier" |
9 | 13 | ) |
10 | 14 |
|
11 | 15 | type SettingService struct{} |
12 | 16 |
|
13 | 17 | type ISettingService interface { |
14 | 18 | GetSettingInfo() (*dto.SettingInfo, error) |
15 | 19 | Update(key, value string) error |
| 20 | + |
| 21 | + GetSSHInfo() (string, error) |
| 22 | + TestConnByInfo(req dto.SSHConnData) bool |
| 23 | + SaveConnInfo(req dto.SSHConnData) error |
16 | 24 | } |
17 | 25 |
|
18 | 26 | func NewISettingService() ISettingService { |
@@ -44,3 +52,75 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) { |
44 | 52 | func (u *SettingService) Update(key, value string) error { |
45 | 53 | return settingRepo.UpdateOrCreate(key, value) |
46 | 54 | } |
| 55 | + |
| 56 | +func (u *SettingService) GetSSHInfo() (string, error) { |
| 57 | + conn, err := settingRepo.GetValueByKey("LocalSSHConn") |
| 58 | + if err != nil || len(conn) == 0 { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + return encrypt.StringDecrypt(conn) |
| 62 | +} |
| 63 | + |
| 64 | +func (u *SettingService) TestConnByInfo(req dto.SSHConnData) bool { |
| 65 | + if req.AuthMode == "password" && len(req.Password) != 0 { |
| 66 | + password, err := base64.StdEncoding.DecodeString(req.Password) |
| 67 | + if err != nil { |
| 68 | + return false |
| 69 | + } |
| 70 | + req.Password = string(password) |
| 71 | + } |
| 72 | + if req.AuthMode == "key" && len(req.PrivateKey) != 0 { |
| 73 | + privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey) |
| 74 | + if err != nil { |
| 75 | + return false |
| 76 | + } |
| 77 | + req.PrivateKey = string(privateKey) |
| 78 | + } |
| 79 | + |
| 80 | + var connInfo ssh.ConnInfo |
| 81 | + _ = copier.Copy(&connInfo, &req) |
| 82 | + connInfo.PrivateKey = []byte(req.PrivateKey) |
| 83 | + if len(req.PassPhrase) != 0 { |
| 84 | + connInfo.PassPhrase = []byte(req.PassPhrase) |
| 85 | + } |
| 86 | + client, err := ssh.NewClient(connInfo) |
| 87 | + if err != nil { |
| 88 | + return false |
| 89 | + } |
| 90 | + defer client.Close() |
| 91 | + return true |
| 92 | +} |
| 93 | + |
| 94 | +func (u *SettingService) SaveConnInfo(req dto.SSHConnData) error { |
| 95 | + if req.AuthMode == "password" && len(req.Password) != 0 { |
| 96 | + password, err := base64.StdEncoding.DecodeString(req.Password) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + req.Password = string(password) |
| 101 | + } |
| 102 | + if req.AuthMode == "key" && len(req.PrivateKey) != 0 { |
| 103 | + privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + req.PrivateKey = string(privateKey) |
| 108 | + } |
| 109 | + |
| 110 | + var connInfo ssh.ConnInfo |
| 111 | + _ = copier.Copy(&connInfo, &req) |
| 112 | + connInfo.PrivateKey = []byte(req.PrivateKey) |
| 113 | + if len(req.PassPhrase) != 0 { |
| 114 | + connInfo.PassPhrase = []byte(req.PassPhrase) |
| 115 | + } |
| 116 | + client, err := ssh.NewClient(connInfo) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + defer client.Close() |
| 121 | + |
| 122 | + localConn, _ := json.Marshal(&connInfo) |
| 123 | + connAfterEncrypt, _ := encrypt.StringEncrypt(string(localConn)) |
| 124 | + _ = settingRepo.Update("LocalSSHConn", connAfterEncrypt) |
| 125 | + return nil |
| 126 | +} |
0 commit comments