Skip to content

Commit 4df9102

Browse files
author
mirkobrombin
committed
feat: Switch from TCP to Unix sockets
1 parent 7b12c1e commit 4df9102

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ problem on those systems where Flatpak or DBus are not available.
3838

3939
- [x] Add an option to restrict the commands that can be run
4040
- [x] Add support for creating shims for host binaries
41-
- [ ] Switch from TCP to Unix sockets
41+
- [x] Switch from TCP to Unix sockets
4242
- [ ] Code optimization

main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func main() {
3030
helpFlag := flag.Bool("h", false, "Display help")
3131
helpFlagLong := flag.Bool("help", false, "Display help")
3232
startFlag := flag.Bool("start", false, "Start the server")
33+
socketFlag := flag.String("socket", "/tmp/hrun.sock", "Specify an alternative socket path")
3334
allowedCmds := make([]string, 0)
3435
flag.Func("allowed-cmd", "Specify allowed command (can be used multiple times)", func(cmd string) error {
3536
allowedCmds = append(allowedCmds, cmd)
@@ -60,7 +61,7 @@ If no command is provided, it starts a shell on the host.
6061

6162
// Server mode
6263
if *startFlag {
63-
startServer(allowedCmds)
64+
startServer(allowedCmds, socketFlag)
6465
return
6566
}
6667

@@ -72,17 +73,17 @@ If no command is provided, it starts a shell on the host.
7273
command = flag.Args()
7374
}
7475

75-
startClient(command)
76+
startClient(command, socketFlag)
7677
}
7778

78-
func startServer(allowedCmds []string) {
79+
func startServer(allowedCmds []string, socketFlag *string) {
7980
// Create a listener for the server
80-
listener, err := net.Listen("tcp", "127.0.0.1:8080")
81+
listener, err := net.Listen("unix", *socketFlag)
8182
if err != nil {
8283
panic(err)
8384
}
8485
defer listener.Close()
85-
log.Printf("Server is running on 127.0.0.1:8080\n\n")
86+
log.Printf("Server is running on %s\n", listener.Addr())
8687

8788
// Set up a signal handler to shut down the server
8889
doneCh := make(chan struct{})
@@ -270,9 +271,9 @@ func handleConnection(conn net.Conn, allowedCmds []string) {
270271
log.Printf("Connection closed\n\n")
271272
}
272273

273-
func startClient(command []string) {
274+
func startClient(command []string, socketFlag *string) {
274275
// Connect to the server
275-
conn, err := net.Dial("tcp", "127.0.0.1:8080")
276+
conn, err := net.Dial("unix", *socketFlag)
276277
if err != nil {
277278
log.Println("Error connecting to the host:", err)
278279
return

0 commit comments

Comments
 (0)