perf: hide window on windows when run external command for decode

This commit is contained in:
Lykin 2024-02-20 23:27:31 +08:00
parent 2405a79ace
commit 30835ac469
8 changed files with 67 additions and 40 deletions

View File

@ -128,6 +128,8 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O
ReadTimeout: time.Duration(config.ExecTimeout) * time.Second, ReadTimeout: time.Duration(config.ExecTimeout) * time.Second,
WriteTimeout: time.Duration(config.ExecTimeout) * time.Second, WriteTimeout: time.Duration(config.ExecTimeout) * time.Second,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
DisableIndentity: true,
IdentitySuffix: "tinyrdm_",
} }
if config.Network == "unix" { if config.Network == "unix" {
option.Network = "unix" option.Network = "unix"
@ -138,10 +140,14 @@ func (c *connectionService) buildOption(config types.ConnectionConfig) (*redis.O
} }
} else { } else {
option.Network = "tcp" option.Network = "tcp"
port := 6379
if config.Port > 0 {
port = config.Port
}
if len(config.Addr) <= 0 { if len(config.Addr) <= 0 {
option.Addr = fmt.Sprintf("127.0.0.1:%d", config.Port) option.Addr = fmt.Sprintf("127.0.0.1:%d", port)
} else { } else {
option.Addr = fmt.Sprintf("%s:%d", config.Addr, config.Port) option.Addr = fmt.Sprintf("%s:%d", config.Addr, port)
} }
} }
if sshClient != nil { if sshClient != nil {

View File

@ -2,10 +2,6 @@ package convutil
import ( import (
"encoding/base64" "encoding/base64"
"github.com/vrischmann/userdir"
"os"
"os/exec"
"path"
"strings" "strings"
sliceutil "tinyrdm/backend/utils/slice" sliceutil "tinyrdm/backend/utils/slice"
) )
@ -39,8 +35,7 @@ func (c CmdConvert) Encode(str string) (string, bool) {
if len(args) <= 0 || !containHolder { if len(args) <= 0 || !containHolder {
args = append(args, base64Content) args = append(args, base64Content)
} }
cmd := exec.Command(c.EncodePath, args...) output, err := runCommand(c.EncodePath, args...)
output, err := cmd.Output()
if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" { if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" {
return str, false return str, false
} }
@ -67,8 +62,7 @@ func (c CmdConvert) Decode(str string) (string, bool) {
if len(args) <= 0 || !containHolder { if len(args) <= 0 || !containHolder {
args = append(args, base64Content) args = append(args, base64Content)
} }
cmd := exec.Command(c.DecodePath, args...) output, err := runCommand(c.DecodePath, args...)
output, err := cmd.Output()
if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" { if err != nil || len(output) <= 0 || string(output) == "[RDM-ERROR]" {
return str, false return str, false
} }
@ -80,13 +74,3 @@ func (c CmdConvert) Decode(str string) (string, bool) {
} }
return string(outputContent[:n]), true return string(outputContent[:n]), true
} }
func (c CmdConvert) writeExecuteFile(content []byte, filename string) (string, error) {
filepath := path.Join(userdir.GetConfigHome(), "TinyRDM", "decoder", filename)
_ = os.Mkdir(path.Dir(filepath), 0777)
err := os.WriteFile(filepath, content, 0777)
if err != nil {
return "", err
}
return filepath, nil
}

View File

@ -0,0 +1,17 @@
package convutil
import (
"github.com/vrischmann/userdir"
"os"
"path"
)
func writeExecuteFile(content []byte, filename string) (string, error) {
filepath := path.Join(userdir.GetConfigHome(), "TinyRDM", "decoder", filename)
_ = os.Mkdir(path.Dir(filepath), 0777)
err := os.WriteFile(filepath, content, 0777)
if err != nil {
return "", err
}
return filepath, nil
}

View File

@ -0,0 +1,12 @@
//go:build !windows
package convutil
import (
"os/exec"
)
func runCommand(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
return cmd.Output()
}

View File

@ -0,0 +1,14 @@
//go:build windows
package convutil
import (
"os/exec"
"syscall"
)
func runCommand(name string, arg ...string) ([]byte, error) {
cmd := exec.Command(name, arg...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Output()
}

View File

@ -1,9 +1,5 @@
package convutil package convutil
import (
"os/exec"
)
type PhpConvert struct { type PhpConvert struct {
CmdConvert CmdConvert
} }
@ -51,12 +47,12 @@ func NewPhpConvert() *PhpConvert {
} }
var err error var err error
if err = exec.Command(c.DecodePath, "-v").Err; err != nil { if _, err = runCommand(c.DecodePath, "-v"); err != nil {
return nil return nil
} }
var filepath string var filepath string
if filepath, err = c.writeExecuteFile([]byte(phpDecodeCode), "php_decoder.php"); err != nil { if filepath, err = writeExecuteFile([]byte(phpDecodeCode), "php_decoder.php"); err != nil {
return nil return nil
} }
c.DecodeArgs = []string{filepath, "decode"} c.DecodeArgs = []string{filepath, "decode"}

View File

@ -1,7 +1,5 @@
package convutil package convutil
import "os/exec"
type PickleConvert struct { type PickleConvert struct {
CmdConvert CmdConvert
} }
@ -42,18 +40,18 @@ func NewPickleConvert() *PickleConvert {
} }
c.DecodePath, c.EncodePath = "python3", "python3" c.DecodePath, c.EncodePath = "python3", "python3"
var err error var err error
if err = exec.Command(c.DecodePath, "--version").Err; err != nil { if _, err = runCommand(c.DecodePath, "--version"); err != nil {
c.DecodePath, c.EncodePath = "python", "python" c.DecodePath, c.EncodePath = "python", "python"
if err = exec.Command(c.DecodePath, "--version").Err; err != nil { if _, err = runCommand(c.DecodePath, "--version"); err != nil {
return nil return nil
} }
} }
// check if pickle available // check if pickle available
if err = exec.Command(c.DecodePath, "-c", "import pickle").Err; err != nil { if _, err = runCommand(c.DecodePath, "-c", "import pickle"); err != nil {
return nil return nil
} }
var filepath string var filepath string
if filepath, err = c.writeExecuteFile([]byte(pickleDecodeCode), "pickle_decoder.py"); err != nil { if filepath, err = writeExecuteFile([]byte(pickleDecodeCode), "pickle_decoder.py"); err != nil {
return nil return nil
} }
c.DecodeArgs = []string{filepath, "decode"} c.DecodeArgs = []string{filepath, "decode"}

View File

@ -33,7 +33,7 @@ export const typesColor = {
[types.SET]: '#F59E0B', [types.SET]: '#F59E0B',
[types.ZSET]: '#EF4444', [types.ZSET]: '#EF4444',
[types.STREAM]: '#EC4899', [types.STREAM]: '#EC4899',
[types.JSON]: '#374254', [types.JSON]: '#828766',
} }
/** /**
@ -47,7 +47,7 @@ export const typesBgColor = {
[types.SET]: '#FDF1DF', [types.SET]: '#FDF1DF',
[types.ZSET]: '#FAEAED', [types.ZSET]: '#FAEAED',
[types.STREAM]: '#FDE6F1', [types.STREAM]: '#FDE6F1',
[types.JSON]: '#C1C1D3', [types.JSON]: '#ECECD9',
} }
// export const typesName = Object.fromEntries(Object.entries(types).map(([key, value]) => [key, value.name])) // export const typesName = Object.fromEntries(Object.entries(types).map(([key, value]) => [key, value.name]))