From 7753019f5350bc3b2e17ca8c43b53213d64b431d Mon Sep 17 00:00:00 2001 From: Asif Mohammad Mollah Date: Thu, 23 Jan 2025 00:58:36 +0400 Subject: [PATCH] daemon running issue in windows --- config/app.go | 13 ++++++++++++ config/darwin.go | 7 +++++++ config/linux.go | 7 +++++++ config/windows.go | 7 +++++++ constants/app.go | 8 +++----- daemon/daemon.go | 44 +++++++++++++++++++++++++++++++--------- handler/tunnelHandler.go | 6 ++++++ main.go | 6 +++++- utils/utils.go | 13 ++++++++++++ 9 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 config/darwin.go create mode 100644 config/linux.go create mode 100644 config/windows.go diff --git a/config/app.go b/config/app.go index 48c9866..839437c 100644 --- a/config/app.go +++ b/config/app.go @@ -14,6 +14,8 @@ type Configuration interface { LoadConfig() SaveConfig() error Config() *models.Config + IsWindows() bool + ConfigDir() string } type configuration struct { @@ -21,6 +23,7 @@ type configuration struct { cfgFile string cfgFilePath string config *models.Config + isWindows bool } func (cfg *configuration) Config() *models.Config { @@ -47,6 +50,14 @@ func (cfg *configuration) isConfigExists() { } } +func (cfg *configuration) IsWindows() bool { + return cfg.IsWindows() +} + +func (cfg *configuration) ConfigDir() string { + return cfg.cfgDir +} + func (cfg *configuration) LoadConfig() { data, err := os.ReadFile(cfg.cfgFilePath) if err != nil { @@ -75,6 +86,7 @@ func InitConfig() Configuration { if err != nil { log.Fatal("Error getting home directory: ", err) } + cfgDir := filepath.Join(homeDir, constants.CONFIG_DIR) cfgFile := constants.CONFIG_FILE cfgFilePath := filepath.Join(cfgDir, cfgFile) @@ -87,6 +99,7 @@ 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 new file mode 100644 index 0000000..0db85a6 --- /dev/null +++ b/config/darwin.go @@ -0,0 +1,7 @@ +//go:build darwin + +package config + +func IsWindows() bool { + return false +} diff --git a/config/linux.go b/config/linux.go new file mode 100644 index 0000000..bd89817 --- /dev/null +++ b/config/linux.go @@ -0,0 +1,7 @@ +//go:build linux + +package config + +func IsWindows() bool { + return false +} diff --git a/config/windows.go b/config/windows.go new file mode 100644 index 0000000..a443629 --- /dev/null +++ b/config/windows.go @@ -0,0 +1,7 @@ +//go:build windows + +package config + +func IsWindows() bool { + return true +} diff --git a/constants/app.go b/constants/app.go index ac76b96..1f5bef7 100644 --- a/constants/app.go +++ b/constants/app.go @@ -3,11 +3,9 @@ package constants const ( CONFIG_DIR = ".config/telepath" CONFIG_FILE = "telepath.json" -) - -const ( - PID_FILE_PATH = "/tmp/telepath.pid" - SOCKET_PATH = "/tmp/telepath.sock" + PID_FILE = "telepath.pid" + SOCKET_FILE = "telepath.sock" + TCP_ADDR = "127.0.0.1:54321" ) const ( diff --git a/daemon/daemon.go b/daemon/daemon.go index 87f9739..563046c 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -14,6 +14,8 @@ import ( "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" "github.com/tech-thinker/telepath/utils" @@ -79,14 +81,25 @@ func (ps *daemonMgr) RunDaemonChild(ctx context.Context) (err error) { defer os.Remove(ps.pidFilePath) // Set up the UNIX socket - if _, err := os.Stat(ps.socketPath); err == nil { - os.Remove(ps.socketPath) - } - listener, err := net.Listen("unix", ps.socketPath) - if err != nil { - log.Fatalf("Failed to create UNIX socket: %v", err) - return err + var listener net.Listener + + if !config.IsWindows() { + if _, err := os.Stat(ps.socketPath); err == nil { + os.Remove(ps.socketPath) + } + listener, err = net.Listen("unix", ps.socketPath) + if err != nil { + log.Fatalf("Failed to create UNIX socket: %v", err) + return err + } + } else { + listener, err = net.Listen("tcp", constants.TCP_ADDR) + if err != nil { + log.Fatalf("Failed to create TCP socket: %v", err) + return err + } } + defer listener.Close() log.Println("Daemon is running...") @@ -173,9 +186,20 @@ func (ps *daemonMgr) SendCommandToDaemon(ctx context.Context, packet models.Pack return fmt.Errorf("daemon is not running") } - conn, err := net.Dial("unix", ps.socketPath) - if err != nil { - return fmt.Errorf("failed to connect to daemon: %v", err) + var conn net.Conn + var err error + + if !config.IsWindows() { + conn, err = net.Dial("unix", ps.socketPath) + if err != nil { + return fmt.Errorf("failed to connect to daemon: %v", err) + } + + } else { + conn, err = net.Dial("tcp", constants.TCP_ADDR) + if err != nil { + return fmt.Errorf("failed to connect to daemon: %v", err) + } } defer conn.Close() diff --git a/handler/tunnelHandler.go b/handler/tunnelHandler.go index 5c330e9..eb50c01 100644 --- a/handler/tunnelHandler.go +++ b/handler/tunnelHandler.go @@ -2,6 +2,7 @@ package handler import ( "bytes" + "fmt" "log" "strconv" "strings" @@ -160,6 +161,11 @@ func (h *handler) startTunnel(packet models.Packet) (models.Packet, error) { return result, nil } + if !utils.IsPortAvailable(doc.LocalPort) { + result.Data = []byte(fmt.Sprintf("Local port %d is already in use.\n", doc.LocalPort)) + return result, nil + } + go h.socketRepo.Start(doc) result.Data = []byte("Tunnel started successfully.\n") diff --git a/main.go b/main.go index 4ab8c78..44a940d 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "path/filepath" "github.com/tech-thinker/telepath/cmd" "github.com/tech-thinker/telepath/config" @@ -21,7 +22,10 @@ var ( func main() { cfg := config.InitConfig() handler := handler.NewHandler(cfg) - daemonMgr := daemon.NewDaemonMgr(constants.PID_FILE_PATH, constants.SOCKET_PATH, handler) + + pidFilePath := filepath.Join(cfg.ConfigDir(), constants.PID_FILE) + socketPath := filepath.Join(cfg.ConfigDir(), constants.SOCKET_FILE) + daemonMgr := daemon.NewDaemonMgr(pidFilePath, socketPath, handler) appCmd := cmd.NewApp(daemonMgr) app := &cli.App{ diff --git a/utils/utils.go b/utils/utils.go index 0a5542b..3017e76 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,7 +2,9 @@ package utils import ( "encoding/json" + "fmt" "log" + "net" "github.com/tech-thinker/telepath/models" ) @@ -43,3 +45,14 @@ func ToTunnel(buff []byte) (models.Tunnel, error) { } return tunnel, nil } + +// Check if the port is available +func IsPortAvailable(port int) bool { + address := fmt.Sprintf(":%d", port) + listener, err := net.Listen("tcp", address) + if err != nil { + return false // Port is already in use + } + defer listener.Close() + return true // Port is available +}