Skip to content

Commit

Permalink
perf: adaptation v3
Browse files Browse the repository at this point in the history
  • Loading branch information
feng626 committed Dec 1, 2022
1 parent 507902b commit e348bfc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
3 changes: 1 addition & 2 deletions go-client/cmd/awaken/awaken.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"go.uber.org/zap"
"net/url"
"os"
"runtime"
)

func main() {
Expand All @@ -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()
Expand Down
38 changes: 17 additions & 21 deletions go-client/pkg/awaken/awaken.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package awaken

import (
"encoding/json"
"fmt"
"go-client/global"
"io/ioutil"
Expand All @@ -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 {
Expand All @@ -44,7 +43,6 @@ type DBCommand struct {
}

type Rouse struct {
SysType string
Info
}

Expand All @@ -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
Expand All @@ -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()
}

Expand Down Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions go-client/pkg/awaken/awaken_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 20 additions & 6 deletions go-client/pkg/awaken/awaken_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package awaken

import (
"fmt"
"go-client/global"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -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 {
Expand Down

0 comments on commit e348bfc

Please sign in to comment.