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
13 changes: 13 additions & 0 deletions config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ type Configuration interface {
LoadConfig()
SaveConfig() error
Config() *models.Config
IsWindows() bool
ConfigDir() string
}

type configuration struct {
cfgDir string
cfgFile string
cfgFilePath string
config *models.Config
isWindows bool
}

func (cfg *configuration) Config() *models.Config {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions config/darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build darwin

package config

func IsWindows() bool {
return false
}
7 changes: 7 additions & 0 deletions config/linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build linux

package config

func IsWindows() bool {
return false
}
7 changes: 7 additions & 0 deletions config/windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build windows

package config

func IsWindows() bool {
return true
}
8 changes: 3 additions & 5 deletions constants/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
44 changes: 34 additions & 10 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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...")
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions handler/tunnelHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"bytes"
"fmt"
"log"
"strconv"
"strings"
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/tech-thinker/telepath/cmd"
"github.com/tech-thinker/telepath/config"
Expand All @@ -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{
Expand Down
13 changes: 13 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package utils

import (
"encoding/json"
"fmt"
"log"
"net"

"github.com/tech-thinker/telepath/models"
)
Expand Down Expand Up @@ -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
}
Loading