Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions go-client/pkg/awaken/awaken.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ func (r *Rouse) getName() string {
return replacer.Replace(name)
}

// reportError 统一处理错误输出:记录日志并输出到 stderr
func reportError(msg string) {
global.LOG.Error(msg)
fmt.Fprintf(os.Stderr, "Error: %s\n", msg)
}

// reportErrorf 格式化错误消息并统一处理
func reportErrorf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
reportError(msg)
}

func removeCurRdpVncFile() {
re := regexp.MustCompile(`(?i)\.(rdp|vncpaxx)$`)
dir, _ := os.UserConfigDir()
Expand All @@ -101,62 +113,60 @@ func (r *Rouse) HandleRDP(appConfig *config.AppConfig) {
filePath := filepath.Join(dir, "jumpserver-client", replacer.Replace(fileName)+".rdp")
err := ioutil.WriteFile(filePath, []byte(r.Content), os.ModePerm)
if err != nil {
errorMsg := err.Error()
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError(err.Error())
return
}
cmd := handleRDP(r, filePath, appConfig)
if cmd != nil {
cmd.Run()
if err := cmd.Run(); err != nil {
reportErrorf("Failed to execute RDP application: %v", err)
}
} else {
errorMsg := "No RDP application configured or found"
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError("No RDP application configured or found")
}
}

func (r *Rouse) HandleVNC(appConfig *config.AppConfig) {
cmd := handleVNC(r, appConfig)
if cmd != nil {
cmd.Run()
if err := cmd.Run(); err != nil {
reportErrorf("Failed to execute VNC application: %v", err)
}
} else {
errorMsg := "No VNC application configured or found"
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError("No VNC application configured or found")
}
}

func (r *Rouse) HandleSSH(appConfig *config.AppConfig) {
cmd := handleSSH(r, appConfig)
if cmd != nil {
cmd.Run()
if err := cmd.Run(); err != nil {
reportErrorf("Failed to execute SSH application: %v", err)
}
} else {
errorMsg := "No SSH application configured or found"
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError("No SSH application configured or found")
}
}

func (r *Rouse) HandleDB(appConfig *config.AppConfig) {
cmd := handleDB(r, appConfig)
if cmd != nil {
cmd.Run()
if err := cmd.Run(); err != nil {
reportErrorf("Failed to execute database application: %v", err)
}
} else {
errorMsg := "No database application configured or found"
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError("No database application configured or found")
}
}

func (r *Rouse) HandleCommand(appConfig *config.AppConfig) {
cmd := handleCommand(r, appConfig)
if cmd != nil {
cmd.Run()
if err := cmd.Run(); err != nil {
reportErrorf("Failed to execute command: %v", err)
}
} else {
errorMsg := "No command application configured or found"
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportError("No command application configured or found")
}
}

Expand All @@ -174,9 +184,7 @@ func (r *Rouse) Run() {
case "mysql", "mariadb", "postgresql", "redis", "oracle", "sqlserver", "mongodb":
r.HandleDB(&appConfig)
default:
errorMsg := fmt.Sprintf("Unsupported protocol: %s", protocol)
global.LOG.Error(errorMsg)
fmt.Fprintf(os.Stderr, "Error: %s\n", errorMsg)
reportErrorf("Unsupported protocol: %s", protocol)
}
} else {
r.HandleCommand(&appConfig)
Expand Down
28 changes: 25 additions & 3 deletions go-client/pkg/awaken/awaken_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func getCommandFromArgs(connectInfo map[string]string, argFormat string) string
return argFormat
}

// validateAppPath checks if the application path exists
func validateAppPath(appPath string) error {
if appPath == "" {
return fmt.Errorf("application path is empty")
}
// Check if path exists
if _, err := os.Stat(appPath); os.IsNotExist(err) {
return fmt.Errorf("application path does not exist: %s", appPath)
}
return nil
}

func awakenRDPCommand(filePath string, cfg *config.AppConfig) *exec.Cmd {
global.LOG.Debug(filePath)
cmd := exec.Command("open", filePath)
Expand All @@ -44,6 +56,10 @@ func awakenVNCCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
"host": r.Host,
"port": strconv.Itoa(r.Port),
}
if err := validateAppPath(appItem.Path); err != nil {
global.LOG.Error(err.Error())
return nil
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
cmd := exec.Command(appItem.Path, strings.Split(commands, " ")...)
// 设置环境变量(只对这个子进程有效)
Expand Down Expand Up @@ -108,10 +124,12 @@ func awakenSSHCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
}

} else {
var appPath string
appPath = appItem.Path
appPath := appItem.Path
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
appPath = appItem.Path
cmd = exec.Command(appPath, strings.Split(commands, " ")...)
}
return cmd
Expand Down Expand Up @@ -165,6 +183,10 @@ func awakenDBCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
)
return cmd
} else {
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
if r.Protocol == "sqlserver" {
connectMap["protocol"] = "mssql_jdbc_ms_new"
}
Expand Down
30 changes: 26 additions & 4 deletions go-client/pkg/awaken/awaken_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func getCommandFromArgs(connectInfo map[string]string, argFormat string) string
return argFormat
}

// validateAppPath checks if the application path exists
func validateAppPath(appPath string) error {
if appPath == "" {
return fmt.Errorf("application path is empty")
}
// Check if path exists
if _, err := os.Stat(appPath); os.IsNotExist(err) {
return fmt.Errorf("application path does not exist: %s", appPath)
}
return nil
}

func awakenRDPCommand(filePath string, cfg *config.AppConfig) *exec.Cmd {
global.LOG.Debug(filePath)
var appItem *config.AppItem
Expand Down Expand Up @@ -57,6 +69,10 @@ func awakenVNCCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
"port": strconv.Itoa(r.Port),
}

if err := validateAppPath(appItem.Path); err != nil {
global.LOG.Error(err.Error())
return nil
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
cmd := exec.Command(appItem.Path, strings.Split(commands, " ")...)
// 设置环境变量(只对这个子进程有效)
Expand Down Expand Up @@ -138,10 +154,12 @@ func awakenSSHCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
if r.Protocol == "sqlserver" {
connectMap["protocol"] = "mssql_jdbc_ms_new"
}
var appPath string
appPath = appItem.Path
appPath := appItem.Path
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
appPath = appItem.Path
cmd = exec.Command(appPath, strings.Split(commands, " ")...)
}
return cmd
Expand Down Expand Up @@ -208,7 +226,11 @@ func awakenDBCommand(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
}
return cmd
} else {
appPath = appItem.Path
appPath := appItem.Path
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
commands := getCommandFromArgs(connectMap, appItem.ArgFormat)
return exec.Command(appPath, strings.Split(commands, " ")...)
}
Expand Down
28 changes: 28 additions & 0 deletions go-client/pkg/awaken/awaken_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ func getCommandFromArgs(connectInfo map[string]string, argFormat string) string
return argFormat
}

// validateAppPath checks if the application path exists
func validateAppPath(appPath string) error {
if appPath == "" {
return fmt.Errorf("application path is empty")
}
// Check if path exists
if _, err := os.Stat(appPath); os.IsNotExist(err) {
return fmt.Errorf("application path does not exist: %s", appPath)
}
return nil
}

func handleRDP(r *Rouse, filePath string, cfg *config.AppConfig) *exec.Cmd {
var appItem *config.AppItem
appLst := cfg.Windows.RemoteDesktop
Expand All @@ -73,6 +85,10 @@ func handleRDP(r *Rouse, filePath string, cfg *config.AppConfig) *exec.Cmd {
return nil
}
appPath := appItem.Path
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
connectMap := map[string]string{
"file": filePath,
"name": r.getName(),
Expand Down Expand Up @@ -105,6 +121,10 @@ func handleVNC(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
if appItem == nil {
return nil
}
if err := validateAppPath(appItem.Path); err != nil {
global.LOG.Error(err.Error())
return nil
}
connectMap := map[string]string{
"name": r.getName(),
"protocol": r.Protocol,
Expand Down Expand Up @@ -215,6 +235,10 @@ func handleSSH(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
} else {
appPath = appItem.Path
}
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}
connectMap := map[string]string{
"name": r.getName(),
"protocol": protocol,
Expand Down Expand Up @@ -246,6 +270,10 @@ func handleDB(r *Rouse, cfg *config.AppConfig) *exec.Cmd {
return nil
}
appPath := appItem.Path
if err := validateAppPath(appPath); err != nil {
global.LOG.Error(err.Error())
return nil
}

connectMap := map[string]string{
"name": r.getName(),
Expand Down
11 changes: 8 additions & 3 deletions src-tauri/src/commands/get_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ pub async fn get_connect_token(
// "get-token-success",
// json!({ "status": url_data.status, "data": from_str::<Value>(&url_data.data).unwrap() }),
// );
pull_up(
app,
if let Err(e) = pull_up(
app.clone(),
url_json.get("url").unwrap().as_str().unwrap().to_string(),
);
) {
let _ = app.emit(
"pull-up-failure",
json!({ "error": e }),
);
}
} else {
let _ = app.emit(
"get-token-failure",
Expand Down
Loading