From 98d59bc2671396252902909bf247daf5dc8e3ce6 Mon Sep 17 00:00:00 2001 From: Asif Mohammad Mollah Date: Thu, 23 Jan 2025 14:49:39 +0400 Subject: [PATCH 1/2] telepath for windows issue fixes --- config/app.go | 7 ------- config/darwin.go | 7 ------- config/linux.go | 7 ------- config/windows.go | 7 ------- daemon/daemon.go | 19 +++++++++---------- man/telepath.1 | 2 +- utils/darwin.go | 24 ++++++++++++++++++++++++ utils/linux.go | 24 ++++++++++++++++++++++++ utils/windows.go | 29 +++++++++++++++++++++++++++++ 9 files changed, 87 insertions(+), 39 deletions(-) delete mode 100644 config/darwin.go delete mode 100644 config/linux.go delete mode 100644 config/windows.go create mode 100644 utils/darwin.go create mode 100644 utils/linux.go create mode 100644 utils/windows.go diff --git a/config/app.go b/config/app.go index 839437c..1b32975 100644 --- a/config/app.go +++ b/config/app.go @@ -14,7 +14,6 @@ type Configuration interface { LoadConfig() SaveConfig() error Config() *models.Config - IsWindows() bool ConfigDir() string } @@ -23,7 +22,6 @@ type configuration struct { cfgFile string cfgFilePath string config *models.Config - isWindows bool } func (cfg *configuration) Config() *models.Config { @@ -50,10 +48,6 @@ func (cfg *configuration) isConfigExists() { } } -func (cfg *configuration) IsWindows() bool { - return cfg.IsWindows() -} - func (cfg *configuration) ConfigDir() string { return cfg.cfgDir } @@ -99,7 +93,6 @@ func InitConfig() Configuration { Credientials: make(map[string]models.Crediential), Tunnels: make(map[string]models.Tunnel), }, - isWindows: IsWindows(), } cfg.isConfigExists() cfg.LoadConfig() diff --git a/config/darwin.go b/config/darwin.go deleted file mode 100644 index 0db85a6..0000000 --- a/config/darwin.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build darwin - -package config - -func IsWindows() bool { - return false -} diff --git a/config/linux.go b/config/linux.go deleted file mode 100644 index bd89817..0000000 --- a/config/linux.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build linux - -package config - -func IsWindows() bool { - return false -} diff --git a/config/windows.go b/config/windows.go deleted file mode 100644 index a443629..0000000 --- a/config/windows.go +++ /dev/null @@ -1,7 +0,0 @@ -//go:build windows - -package config - -func IsWindows() bool { - return true -} diff --git a/daemon/daemon.go b/daemon/daemon.go index 563046c..49af7b6 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -9,12 +9,10 @@ import ( "log" "net" "os" - "os/exec" "strconv" "strings" "syscall" - "github.com/tech-thinker/telepath/config" "github.com/tech-thinker/telepath/constants" "github.com/tech-thinker/telepath/handler" "github.com/tech-thinker/telepath/models" @@ -61,12 +59,13 @@ func (ps *daemonMgr) RunAsDaemon(ctx context.Context) error { return fmt.Errorf("daemon is already running") } - // Fork the process - cmd := exec.Command(os.Args[0], "daemon", "start", "--daemon-child") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - cmd.Start() - fmt.Printf("Daemon started with PID: %d\n", cmd.Process.Pid) + pid, err := utils.FrokProcess(os.Args[0], "daemon", "start", "--daemon-child") + if err != nil { + // fmt.Println("Failed to start daemon process: ", err) + return fmt.Errorf("failed to start daemon process: %v", err.Error()) + } + + fmt.Printf("Daemon started with PID: %d\n", pid) return nil } @@ -83,7 +82,7 @@ func (ps *daemonMgr) RunDaemonChild(ctx context.Context) (err error) { // Set up the UNIX socket var listener net.Listener - if !config.IsWindows() { + if !utils.IsWindows() { if _, err := os.Stat(ps.socketPath); err == nil { os.Remove(ps.socketPath) } @@ -189,7 +188,7 @@ func (ps *daemonMgr) SendCommandToDaemon(ctx context.Context, packet models.Pack var conn net.Conn var err error - if !config.IsWindows() { + if !utils.IsWindows() { conn, err = net.Dial("unix", ps.socketPath) if err != nil { return fmt.Errorf("failed to connect to daemon: %v", err) diff --git a/man/telepath.1 b/man/telepath.1 index cb36354..d049e60 100644 --- a/man/telepath.1 +++ b/man/telepath.1 @@ -1,4 +1,4 @@ -.TH TELEPATH 1 "BUILDDATE" "Version VERSION" "User Commands" +.TH TELEPATH 1 "2025-01-23" "Version v0.0.0" "User Commands" .SH NAME telepath \- is a powerful CLI tool for secure port forwarding with support for multiple jump hosts and flexible authentication. .SH SYNOPSIS diff --git a/utils/darwin.go b/utils/darwin.go new file mode 100644 index 0000000..75d219f --- /dev/null +++ b/utils/darwin.go @@ -0,0 +1,24 @@ +//go:build darwin + +package utils + +import ( + "os" + "os/exec" +) + +func IsWindows() bool { + return false +} + +func FrokProcess(name string, arg ...string) (int, error) { + // Fork the process + cmd := exec.Command(name, arg...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Start() + if err != nil { + return 0, err + } + return cmd.Process.Pid, nil +} diff --git a/utils/linux.go b/utils/linux.go new file mode 100644 index 0000000..57b9967 --- /dev/null +++ b/utils/linux.go @@ -0,0 +1,24 @@ +//go:build linux + +package utils + +import ( + "os" + "os/exec" +) + +func IsWindows() bool { + return false +} + +func FrokProcess(name string, arg ...string) (int, error) { + // Fork the process + cmd := exec.Command(name, arg...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Start() + if err != nil { + return 0, err + } + return cmd.Process.Pid, nil +} diff --git a/utils/windows.go b/utils/windows.go new file mode 100644 index 0000000..b7b9c53 --- /dev/null +++ b/utils/windows.go @@ -0,0 +1,29 @@ +//go:build windows + +package utils + +import ( + "os" + "os/exec" + "syscall" +) + +func IsWindows() bool { + return true +} + +func FrokProcess(name string, arg ...string) (int, error) { + // Fork the process + cmd := exec.Command(name, arg...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + cmd.SysProcAttr = &syscall.SysProcAttr{ + HideWindow: true, + } + err := cmd.Start() + if err != nil { + return 0, err + } + return cmd.Process.Pid, nil +} From 875cf89755b38d95a13a39fdb04ed9dfb79e9999 Mon Sep 17 00:00:00 2001 From: Injamul Mohammad Mollah Date: Sun, 26 Jan 2025 10:33:38 +0530 Subject: [PATCH 2/2] fix: process finding issue in windows --- daemon/daemon.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/daemon/daemon.go b/daemon/daemon.go index 49af7b6..9ca342c 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -47,6 +47,11 @@ func (ps *daemonMgr) IsDaemonRunning(ctx context.Context) bool { return false } + // For windows, no need to check process signal + if utils.IsWindows() { + return true + } + // Check if the process is alive err = process.Signal(syscall.Signal(0)) return err == nil