From 885be2b2ab22bb353a5885bcfca8304996fddb5c Mon Sep 17 00:00:00 2001 From: Fijxu Date: Fri, 4 Oct 2024 20:56:01 -0300 Subject: [PATCH] Built-in SSL to get rid of NGINX --- main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 82cef82..9eacb3b 100644 --- a/main.go +++ b/main.go @@ -304,6 +304,8 @@ func RelativeUrl(in string) (newurl string) { func main() { var sock string var port string + var cert string + var key string path_prefix = os.Getenv("PREFIX_PATH") @@ -319,14 +321,13 @@ func main() { // } // } - flag.StringVar(&sock, "s", "/run/http-proxy.sock", "Specify a socket name") + flag.StringVar(&cert, "tls-cert", "", "TLS Certificate path") + flag.StringVar(&key, "tls-key", "", "TLS Certificate Key path") + var https = flag.Bool("https", false, "Use built-in https server") + flag.StringVar(&sock, "s", "/tmp/http-ytproxy.sock", "Specify a socket name") flag.StringVar(&port, "p", "8080", "Specify a port number") flag.Parse() - socket := string(sock) - syscall.Unlink(socket) - listener, err := net.Listen("unix", socket) - srv := &http.Server{ ReadTimeout: 5 * time.Second, WriteTimeout: 1 * time.Hour, @@ -334,6 +335,10 @@ func main() { Handler: &requesthandler{}, } + socket := string(sock) + syscall.Unlink(socket) + listener, err := net.Listen("unix", socket) + if err != nil { fmt.Println("Failed to bind to UDS, please check the socket name, falling back to TCP/IP") fmt.Println(err.Error()) @@ -353,6 +358,14 @@ func main() { fmt.Println("Setting socket permissions to 777") } go srv.Serve(listener) - srv.ListenAndServe() + if *https { + if err := srv.ListenAndServeTLS(cert, key); err != nil { + log.Fatal(err) + } + fmt.Println("Serving HTTPS") + } else { + fmt.Println("Serving HTTP") + srv.ListenAndServe() + } } }