From dd668e035fcc9ec04627a5d5a179939a1f1b2f5d Mon Sep 17 00:00:00 2001 From: Fijxu Date: Wed, 2 Oct 2024 19:04:19 -0300 Subject: [PATCH] Set unix socket permissions to 777 --- main.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index e1b70b1..82cef82 100644 --- a/main.go +++ b/main.go @@ -310,28 +310,30 @@ func main() { disable_ipv6 = os.Getenv("DISABLE_IPV6") == "1" disable_webp = os.Getenv("DISABLE_WEBP") == "1" - if _, err := os.Stat("socket"); os.IsNotExist(err) { - fmt.Println("socket folder doesn't exists, creating one now.") - err = os.Mkdir("socket", 0755) - if err != nil { - fmt.Println("Failed to create folder, error: ") - log.Fatal(err) - } - } + // if _, err := os.Stat("socket"); os.IsNotExist(err) { + // fmt.Println("socket folder doesn't exists, creating one now.") + // err = os.Mkdir("socket", 0777) + // if err != nil { + // fmt.Println("Failed to create folder, error: ") + // log.Fatal(err) + // } + // } - flag.StringVar(&sock, "s", "http-proxy.sock", "Specify a socket name") + flag.StringVar(&sock, "s", "/run/http-proxy.sock", "Specify a socket name") flag.StringVar(&port, "p", "8080", "Specify a port number") flag.Parse() - socket := "socket" + string(os.PathSeparator) + string(sock) + socket := string(sock) syscall.Unlink(socket) listener, err := net.Listen("unix", socket) + srv := &http.Server{ ReadTimeout: 5 * time.Second, WriteTimeout: 1 * time.Hour, Addr: ":" + string(port), Handler: &requesthandler{}, } + if err != nil { fmt.Println("Failed to bind to UDS, please check the socket name, falling back to TCP/IP") fmt.Println(err.Error()) @@ -342,6 +344,15 @@ func main() { } } else { defer listener.Close() + // To allow everyone to access the socket + err = os.Chmod(socket, 0777) + if err != nil { + fmt.Println("Error setting permissions:", err) + return + } else { + fmt.Println("Setting socket permissions to 777") + } + go srv.Serve(listener) srv.ListenAndServe() } }