From e348bfc91c9d8fb16dcea58e8bb52de84f67c1f5 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Thu, 1 Dec 2022 11:58:38 +0800 Subject: [PATCH] perf: adaptation v3 --- go-client/cmd/awaken/awaken.go | 3 +- go-client/pkg/awaken/awaken.go | 38 ++++++++++++-------------- go-client/pkg/awaken/awaken_unix.go | 6 ++-- go-client/pkg/awaken/awaken_windows.go | 26 ++++++++++++++---- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/go-client/cmd/awaken/awaken.go b/go-client/cmd/awaken/awaken.go index 9ebe7ab..9be5192 100755 --- a/go-client/cmd/awaken/awaken.go +++ b/go-client/cmd/awaken/awaken.go @@ -9,7 +9,6 @@ import ( "go.uber.org/zap" "net/url" "os" - "runtime" ) func main() { @@ -23,7 +22,7 @@ func main() { infoJson := string(decoded) json.Unmarshal([]byte(infoJson), p) r := awaken.Rouse{ - SysType: runtime.GOOS, + //SysType: runtime.GOOS, Info: *p, } r.Run() diff --git a/go-client/pkg/awaken/awaken.go b/go-client/pkg/awaken/awaken.go index e121ffd..1044ddb 100755 --- a/go-client/pkg/awaken/awaken.go +++ b/go-client/pkg/awaken/awaken.go @@ -1,7 +1,6 @@ package awaken import ( - "encoding/json" "fmt" "go-client/global" "io/ioutil" @@ -12,27 +11,27 @@ import ( ) /*{ - "filename": "{}-{}-jumpserver".format(username, name), + "id": "f", + "value": "q", "protocol": "ssh", - "username": "laoguang", - "token": "xxx", - "config": "full address:s:rdjumpserver.fit2cloud.com:33390" + "command": "xxx" + "file": { + "name": "name", + "content": "content", + } }*/ -type Token struct { - Ip string `json:"ip"` - Port string `json:"port"` - UserName string `json:"username"` - Password string `json:"password"` +type File struct { + Name string `json:"name"` + Content string `json:"content"` } type Info struct { - FileName string `json:"filename"` + ID string `json:"id"` + Value string `json:"value"` Protocol string `json:"protocol"` - UserName string `json:"username"` - Token string `json:"token"` Command string `json:"command"` - Config string `json:"config"` + File `json:"file"` } type DBCommand struct { @@ -44,7 +43,6 @@ type DBCommand struct { } type Rouse struct { - SysType string Info } @@ -61,8 +59,8 @@ func removeCurRdpFile() { func (r *Rouse) HandleRDP() { removeCurRdpFile() - filePath := filepath.Join(filepath.Dir(os.Args[0]), r.Info.FileName+".rdp") - err := ioutil.WriteFile(filePath, []byte(r.Info.Config), os.ModePerm) + filePath := filepath.Join(filepath.Dir(os.Args[0]), r.Name+".rdp") + err := ioutil.WriteFile(filePath, []byte(r.Content), os.ModePerm) if err != nil { global.LOG.Error(err.Error()) return @@ -72,10 +70,8 @@ func (r *Rouse) HandleRDP() { } func (r *Rouse) HandleSSH() { - t := &Token{} - json.Unmarshal([]byte(r.Token), t) currentPath := filepath.Dir(os.Args[0]) - cmd := handleSSH(t, currentPath) + cmd := handleSSH(r.Command, r.Value, currentPath) cmd.Run() } @@ -140,7 +136,7 @@ func (r *Rouse) HandleDB() { cmd.Run() } func (r *Rouse) Run() { - protocol := r.Info.Protocol + protocol := r.Protocol switch protocol { case "rdp": r.HandleRDP() diff --git a/go-client/pkg/awaken/awaken_unix.go b/go-client/pkg/awaken/awaken_unix.go index 432f20a..7328c1b 100755 --- a/go-client/pkg/awaken/awaken_unix.go +++ b/go-client/pkg/awaken/awaken_unix.go @@ -15,11 +15,9 @@ func handleRDP(filePath string) *exec.Cmd { return cmd } -func handleSSH(t *Token, currentPath string) *exec.Cmd { +func handleSSH(c string, secret string, currentPath string) *exec.Cmd { clientPath := filepath.Join(currentPath, "client") - command := fmt.Sprintf( - "%s ssh %s@%s -p %s -P %s", clientPath, t.UserName, t.Ip, t.Port, t.Password, - ) + command := fmt.Sprintf("%s %s -P %s", clientPath, c, secret) cmd := awakenCommand(command) return cmd } diff --git a/go-client/pkg/awaken/awaken_windows.go b/go-client/pkg/awaken/awaken_windows.go index 9b3b09d..1a1ce14 100755 --- a/go-client/pkg/awaken/awaken_windows.go +++ b/go-client/pkg/awaken/awaken_windows.go @@ -2,6 +2,7 @@ package awaken import ( "fmt" + "go-client/global" "os/exec" "path/filepath" "strings" @@ -13,16 +14,29 @@ func handleRDP(filePath string) *exec.Cmd { return cmd } -func handleSSH(t *Token, currentPath string) *exec.Cmd { +func handleSSH(c string, secret string, currentPath string) *exec.Cmd { + command := "" + if strings.HasPrefix(c, "putty.exe") { + command = puttySSH(c, secret, currentPath) + } else if strings.HasPrefix(c, "xshell.exe") { + command = xshellSSH(c) + } else { + global.LOG.Error("Type not supported") + } + return exec.Command(command) +} + +func puttySSH(c string, secret string, currentPath string) string { + c = strings.TrimPrefix(c, "putty.exe ") puttyPath := "putty.exe" if _, err := exec.LookPath("putty.exe"); err != nil { puttyPath = filepath.Join(currentPath, "putty.exe") } - cmd := exec.Command( - puttyPath, "-ssh", - fmt.Sprintf("%s@%s", t.UserName, t.Ip), "-P", t.Port, "-pw", t.Password, - ) - return cmd + return fmt.Sprintf("%s %s -pw %s", puttyPath, c, secret) +} + +func xshellSSH(c string) string { + return c } func structurePostgreSQLCommand(command string) string {