Skip to content

Commit

Permalink
Tried to add syscall forward to udptunnel from tunnelexec
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi-Pizzolito committed Jan 11, 2024
1 parent 3c2551c commit dfa9a4b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
19 changes: 19 additions & 0 deletions holepunchudptunnel/tunnelman/tunnelexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"syscall"
"path/filepath"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -156,3 +157,21 @@ func removeCopiedFile(l *zap.Logger, filePath string) {
}
l.Info("Copied file removed successfully.")
}

func forwardSignalToChild(l *zap.Logger, sig os.Signal) {
// Get PID of child executable
pid := os.Getpid()

// Create a process group with the same process group ID as the parent
err := syscall.Setpgid(pid, pid)
if err != nil {
l.Fatal("Error setting process group ID:"+err.Error())
return
}

// Forward the signal to the child process group
err = syscall.Kill(-pid, sig.(syscall.Signal))
if err != nil {
l.Error("Error forwarding signal to child process:"+err.Error())
}
}
20 changes: 17 additions & 3 deletions holepunchudptunnel/tunnelman/tunnelman.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"strings"
"syscall"
"os/signal"
"golang.org/x/term"
"io/ioutil"
"os"
Expand Down Expand Up @@ -97,10 +98,23 @@ func (m* TunnelManager) OpenTunnel(Self, SelfPort, Client string) {
}
m.l.Info("Wrote configuration file for UDP tunnel")

//? Next: summon tunnel UDP with sudo
// Summon tunnel UDP with sudo
m.l.Info("Starting Tunnel Daemon now")
executeEmbeddedBinaryAsSudo(m.l, embeddedExecutable, "udptunnel", "udptunnel_config.json", passwd, "udptunnel_config.json")


// Create channel to recieve signals
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
// Execute embedded binary with sudo in a separate goroutine
go executeEmbeddedBinaryAsSudo(m.l, embeddedExecutable, "udptunnel", "udptunnel_config.json", passwd, "udptunnel_config.json")
// Wait for signals
select {
case sig := <-signalChan:
m.l.Warn("Recieved signal: "+sig.String())
// Forward signal to child process
forwardSignalToChild(m.l, sig)
}

// if tunnels are leftover due to improper teardown, remove with: sudo ip link delete tun0

}

Expand Down

0 comments on commit dfa9a4b

Please sign in to comment.