Skip to content

Commit 968f310

Browse files
authored
Merge pull request #3 from tech-thinker/1-unable-to-start-daemon-on-windows
telepath for windows issue fixes
2 parents c042b3d + 875cf89 commit 968f310

File tree

9 files changed

+92
-39
lines changed

9 files changed

+92
-39
lines changed

config/app.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type Configuration interface {
1414
LoadConfig()
1515
SaveConfig() error
1616
Config() *models.Config
17-
IsWindows() bool
1817
ConfigDir() string
1918
}
2019

@@ -23,7 +22,6 @@ type configuration struct {
2322
cfgFile string
2423
cfgFilePath string
2524
config *models.Config
26-
isWindows bool
2725
}
2826

2927
func (cfg *configuration) Config() *models.Config {
@@ -50,10 +48,6 @@ func (cfg *configuration) isConfigExists() {
5048
}
5149
}
5250

53-
func (cfg *configuration) IsWindows() bool {
54-
return cfg.IsWindows()
55-
}
56-
5751
func (cfg *configuration) ConfigDir() string {
5852
return cfg.cfgDir
5953
}
@@ -99,7 +93,6 @@ func InitConfig() Configuration {
9993
Credientials: make(map[string]models.Crediential),
10094
Tunnels: make(map[string]models.Tunnel),
10195
},
102-
isWindows: IsWindows(),
10396
}
10497
cfg.isConfigExists()
10598
cfg.LoadConfig()

config/darwin.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

config/linux.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

config/windows.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

daemon/daemon.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99
"log"
1010
"net"
1111
"os"
12-
"os/exec"
1312
"strconv"
1413
"strings"
1514
"syscall"
1615

17-
"github.com/tech-thinker/telepath/config"
1816
"github.com/tech-thinker/telepath/constants"
1917
"github.com/tech-thinker/telepath/handler"
2018
"github.com/tech-thinker/telepath/models"
@@ -49,6 +47,11 @@ func (ps *daemonMgr) IsDaemonRunning(ctx context.Context) bool {
4947
return false
5048
}
5149

50+
// For windows, no need to check process signal
51+
if utils.IsWindows() {
52+
return true
53+
}
54+
5255
// Check if the process is alive
5356
err = process.Signal(syscall.Signal(0))
5457
return err == nil
@@ -61,12 +64,13 @@ func (ps *daemonMgr) RunAsDaemon(ctx context.Context) error {
6164
return fmt.Errorf("daemon is already running")
6265
}
6366

64-
// Fork the process
65-
cmd := exec.Command(os.Args[0], "daemon", "start", "--daemon-child")
66-
cmd.Stdout = os.Stdout
67-
cmd.Stderr = os.Stderr
68-
cmd.Start()
69-
fmt.Printf("Daemon started with PID: %d\n", cmd.Process.Pid)
67+
pid, err := utils.FrokProcess(os.Args[0], "daemon", "start", "--daemon-child")
68+
if err != nil {
69+
// fmt.Println("Failed to start daemon process: ", err)
70+
return fmt.Errorf("failed to start daemon process: %v", err.Error())
71+
}
72+
73+
fmt.Printf("Daemon started with PID: %d\n", pid)
7074
return nil
7175
}
7276

@@ -83,7 +87,7 @@ func (ps *daemonMgr) RunDaemonChild(ctx context.Context) (err error) {
8387
// Set up the UNIX socket
8488
var listener net.Listener
8589

86-
if !config.IsWindows() {
90+
if !utils.IsWindows() {
8791
if _, err := os.Stat(ps.socketPath); err == nil {
8892
os.Remove(ps.socketPath)
8993
}
@@ -189,7 +193,7 @@ func (ps *daemonMgr) SendCommandToDaemon(ctx context.Context, packet models.Pack
189193
var conn net.Conn
190194
var err error
191195

192-
if !config.IsWindows() {
196+
if !utils.IsWindows() {
193197
conn, err = net.Dial("unix", ps.socketPath)
194198
if err != nil {
195199
return fmt.Errorf("failed to connect to daemon: %v", err)

man/telepath.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.TH TELEPATH 1 "BUILDDATE" "Version VERSION" "User Commands"
1+
.TH TELEPATH 1 "2025-01-23" "Version v0.0.0" "User Commands"
22
.SH NAME
33
telepath \- is a powerful CLI tool for secure port forwarding with support for multiple jump hosts and flexible authentication.
44
.SH SYNOPSIS

utils/darwin.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build darwin
2+
3+
package utils
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func IsWindows() bool {
11+
return false
12+
}
13+
14+
func FrokProcess(name string, arg ...string) (int, error) {
15+
// Fork the process
16+
cmd := exec.Command(name, arg...)
17+
cmd.Stdout = os.Stdout
18+
cmd.Stderr = os.Stderr
19+
err := cmd.Start()
20+
if err != nil {
21+
return 0, err
22+
}
23+
return cmd.Process.Pid, nil
24+
}

utils/linux.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build linux
2+
3+
package utils
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
)
9+
10+
func IsWindows() bool {
11+
return false
12+
}
13+
14+
func FrokProcess(name string, arg ...string) (int, error) {
15+
// Fork the process
16+
cmd := exec.Command(name, arg...)
17+
cmd.Stdout = os.Stdout
18+
cmd.Stderr = os.Stderr
19+
err := cmd.Start()
20+
if err != nil {
21+
return 0, err
22+
}
23+
return cmd.Process.Pid, nil
24+
}

utils/windows.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build windows
2+
3+
package utils
4+
5+
import (
6+
"os"
7+
"os/exec"
8+
"syscall"
9+
)
10+
11+
func IsWindows() bool {
12+
return true
13+
}
14+
15+
func FrokProcess(name string, arg ...string) (int, error) {
16+
// Fork the process
17+
cmd := exec.Command(name, arg...)
18+
cmd.Stdout = os.Stdout
19+
cmd.Stderr = os.Stderr
20+
21+
cmd.SysProcAttr = &syscall.SysProcAttr{
22+
HideWindow: true,
23+
}
24+
err := cmd.Start()
25+
if err != nil {
26+
return 0, err
27+
}
28+
return cmd.Process.Pid, nil
29+
}

0 commit comments

Comments
 (0)