Skip to content

Commit ccace94

Browse files
authored
Merge pull request #2 from tech-thinker/1-unable-to-start-daemon-on-windows
daemon running issue in windows
2 parents a8bf2d0 + 7753019 commit ccace94

File tree

9 files changed

+95
-16
lines changed

9 files changed

+95
-16
lines changed

config/app.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ type Configuration interface {
1414
LoadConfig()
1515
SaveConfig() error
1616
Config() *models.Config
17+
IsWindows() bool
18+
ConfigDir() string
1719
}
1820

1921
type configuration struct {
2022
cfgDir string
2123
cfgFile string
2224
cfgFilePath string
2325
config *models.Config
26+
isWindows bool
2427
}
2528

2629
func (cfg *configuration) Config() *models.Config {
@@ -47,6 +50,14 @@ func (cfg *configuration) isConfigExists() {
4750
}
4851
}
4952

53+
func (cfg *configuration) IsWindows() bool {
54+
return cfg.IsWindows()
55+
}
56+
57+
func (cfg *configuration) ConfigDir() string {
58+
return cfg.cfgDir
59+
}
60+
5061
func (cfg *configuration) LoadConfig() {
5162
data, err := os.ReadFile(cfg.cfgFilePath)
5263
if err != nil {
@@ -75,6 +86,7 @@ func InitConfig() Configuration {
7586
if err != nil {
7687
log.Fatal("Error getting home directory: ", err)
7788
}
89+
7890
cfgDir := filepath.Join(homeDir, constants.CONFIG_DIR)
7991
cfgFile := constants.CONFIG_FILE
8092
cfgFilePath := filepath.Join(cfgDir, cfgFile)
@@ -87,6 +99,7 @@ func InitConfig() Configuration {
8799
Credientials: make(map[string]models.Crediential),
88100
Tunnels: make(map[string]models.Tunnel),
89101
},
102+
isWindows: IsWindows(),
90103
}
91104
cfg.isConfigExists()
92105
cfg.LoadConfig()

config/darwin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build darwin
2+
3+
package config
4+
5+
func IsWindows() bool {
6+
return false
7+
}

config/linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build linux
2+
3+
package config
4+
5+
func IsWindows() bool {
6+
return false
7+
}

config/windows.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build windows
2+
3+
package config
4+
5+
func IsWindows() bool {
6+
return true
7+
}

constants/app.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package constants
33
const (
44
CONFIG_DIR = ".config/telepath"
55
CONFIG_FILE = "telepath.json"
6-
)
7-
8-
const (
9-
PID_FILE_PATH = "/tmp/telepath.pid"
10-
SOCKET_PATH = "/tmp/telepath.sock"
6+
PID_FILE = "telepath.pid"
7+
SOCKET_FILE = "telepath.sock"
8+
TCP_ADDR = "127.0.0.1:54321"
119
)
1210

1311
const (

daemon/daemon.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"strings"
1515
"syscall"
1616

17+
"github.com/tech-thinker/telepath/config"
18+
"github.com/tech-thinker/telepath/constants"
1719
"github.com/tech-thinker/telepath/handler"
1820
"github.com/tech-thinker/telepath/models"
1921
"github.com/tech-thinker/telepath/utils"
@@ -79,14 +81,25 @@ func (ps *daemonMgr) RunDaemonChild(ctx context.Context) (err error) {
7981
defer os.Remove(ps.pidFilePath)
8082

8183
// Set up the UNIX socket
82-
if _, err := os.Stat(ps.socketPath); err == nil {
83-
os.Remove(ps.socketPath)
84-
}
85-
listener, err := net.Listen("unix", ps.socketPath)
86-
if err != nil {
87-
log.Fatalf("Failed to create UNIX socket: %v", err)
88-
return err
84+
var listener net.Listener
85+
86+
if !config.IsWindows() {
87+
if _, err := os.Stat(ps.socketPath); err == nil {
88+
os.Remove(ps.socketPath)
89+
}
90+
listener, err = net.Listen("unix", ps.socketPath)
91+
if err != nil {
92+
log.Fatalf("Failed to create UNIX socket: %v", err)
93+
return err
94+
}
95+
} else {
96+
listener, err = net.Listen("tcp", constants.TCP_ADDR)
97+
if err != nil {
98+
log.Fatalf("Failed to create TCP socket: %v", err)
99+
return err
100+
}
89101
}
102+
90103
defer listener.Close()
91104

92105
log.Println("Daemon is running...")
@@ -173,9 +186,20 @@ func (ps *daemonMgr) SendCommandToDaemon(ctx context.Context, packet models.Pack
173186
return fmt.Errorf("daemon is not running")
174187
}
175188

176-
conn, err := net.Dial("unix", ps.socketPath)
177-
if err != nil {
178-
return fmt.Errorf("failed to connect to daemon: %v", err)
189+
var conn net.Conn
190+
var err error
191+
192+
if !config.IsWindows() {
193+
conn, err = net.Dial("unix", ps.socketPath)
194+
if err != nil {
195+
return fmt.Errorf("failed to connect to daemon: %v", err)
196+
}
197+
198+
} else {
199+
conn, err = net.Dial("tcp", constants.TCP_ADDR)
200+
if err != nil {
201+
return fmt.Errorf("failed to connect to daemon: %v", err)
202+
}
179203
}
180204
defer conn.Close()
181205

handler/tunnelHandler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handler
22

33
import (
44
"bytes"
5+
"fmt"
56
"log"
67
"strconv"
78
"strings"
@@ -160,6 +161,11 @@ func (h *handler) startTunnel(packet models.Packet) (models.Packet, error) {
160161
return result, nil
161162
}
162163

164+
if !utils.IsPortAvailable(doc.LocalPort) {
165+
result.Data = []byte(fmt.Sprintf("Local port %d is already in use.\n", doc.LocalPort))
166+
return result, nil
167+
}
168+
163169
go h.socketRepo.Start(doc)
164170

165171
result.Data = []byte("Tunnel started successfully.\n")

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
67

78
"github.com/tech-thinker/telepath/cmd"
89
"github.com/tech-thinker/telepath/config"
@@ -21,7 +22,10 @@ var (
2122
func main() {
2223
cfg := config.InitConfig()
2324
handler := handler.NewHandler(cfg)
24-
daemonMgr := daemon.NewDaemonMgr(constants.PID_FILE_PATH, constants.SOCKET_PATH, handler)
25+
26+
pidFilePath := filepath.Join(cfg.ConfigDir(), constants.PID_FILE)
27+
socketPath := filepath.Join(cfg.ConfigDir(), constants.SOCKET_FILE)
28+
daemonMgr := daemon.NewDaemonMgr(pidFilePath, socketPath, handler)
2529
appCmd := cmd.NewApp(daemonMgr)
2630

2731
app := &cli.App{

utils/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package utils
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
7+
"net"
68

79
"github.com/tech-thinker/telepath/models"
810
)
@@ -43,3 +45,14 @@ func ToTunnel(buff []byte) (models.Tunnel, error) {
4345
}
4446
return tunnel, nil
4547
}
48+
49+
// Check if the port is available
50+
func IsPortAvailable(port int) bool {
51+
address := fmt.Sprintf(":%d", port)
52+
listener, err := net.Listen("tcp", address)
53+
if err != nil {
54+
return false // Port is already in use
55+
}
56+
defer listener.Close()
57+
return true // Port is available
58+
}

0 commit comments

Comments
 (0)