-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Modify the terminal connection method #8415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,26 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "encoding/json" | ||
| "time" | ||
|
|
||
| "github.com/1Panel-dev/1Panel/agent/app/dto" | ||
| "github.com/1Panel-dev/1Panel/agent/buserr" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/encrypt" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/ssh" | ||
| "github.com/jinzhu/copier" | ||
| ) | ||
|
|
||
| type SettingService struct{} | ||
|
|
||
| type ISettingService interface { | ||
| GetSettingInfo() (*dto.SettingInfo, error) | ||
| Update(key, value string) error | ||
|
|
||
| GetSSHInfo() (string, error) | ||
| TestConnByInfo(req dto.SSHConnData) bool | ||
| SaveConnInfo(req dto.SSHConnData) error | ||
| } | ||
|
|
||
| func NewISettingService() ISettingService { | ||
|
|
@@ -44,3 +52,75 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) { | |
| func (u *SettingService) Update(key, value string) error { | ||
| return settingRepo.UpdateOrCreate(key, value) | ||
| } | ||
|
|
||
| func (u *SettingService) GetSSHInfo() (string, error) { | ||
| conn, err := settingRepo.GetValueByKey("LocalSSHConn") | ||
| if err != nil || len(conn) == 0 { | ||
| return "", err | ||
| } | ||
| return encrypt.StringDecrypt(conn) | ||
| } | ||
|
|
||
| func (u *SettingService) TestConnByInfo(req dto.SSHConnData) 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) | ||
| } | ||
|
|
||
| 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 := ssh.NewClient(connInfo) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| defer client.Close() | ||
| return true | ||
| } | ||
|
|
||
| func (u *SettingService) SaveConnInfo(req dto.SSHConnData) error { | ||
| if req.AuthMode == "password" && len(req.Password) != 0 { | ||
| password, err := base64.StdEncoding.DecodeString(req.Password) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.Password = string(password) | ||
| } | ||
| if req.AuthMode == "key" && len(req.PrivateKey) != 0 { | ||
| privateKey, err := base64.StdEncoding.DecodeString(req.PrivateKey) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.PrivateKey = string(privateKey) | ||
| } | ||
|
|
||
| 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 := ssh.NewClient(connInfo) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer client.Close() | ||
|
|
||
| localConn, _ := json.Marshal(&connInfo) | ||
| connAfterEncrypt, _ := encrypt.StringEncrypt(string(localConn)) | ||
| _ = settingRepo.Update("LocalSSHConn", connAfterEncrypt) | ||
| return nil | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code provided contains several minor improvements and corrections to handle SSH connection encryption, decryption, serialization, and deserialization in addition to setting management: Minor Changes and Corrections:
Additional Features:
Overall, these changes improve the security and functionality of the SSH settings management service within your application. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code changes seem mostly correct, but there are a few areas where improvements can be made:
Error Handling: In
loadLocalConn, ensure that all errors are checked immediately after each operation to avoid cascading errors.Database Error Handling: Make sure database operations return appropriate errors and handle them appropriately.
User Home Dir Check: Ensure that the directory exists before attempting to read from it, especially if it contains sensitive information like SSH keys.
Code DRYness: Consider extracting common logic into utility functions to reduce redundancy and improve readability.
Here's an improved version of the function with some minor corrections:
Key Changes Made:
defer).