From c31eba7aadbd5fdf06fb7297238c0cda7ec4f6d8 Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Tue, 6 Dec 2022 14:22:26 +0000 Subject: [PATCH] Add support for listening on a unix socket --- screego.config.example | 5 +++++ server/server.go | 35 ++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/screego.config.example b/screego.config.example index e708b67b..c81a3af9 100644 --- a/screego.config.example +++ b/screego.config.example @@ -19,6 +19,11 @@ SCREEGO_TLS_CERT_FILE= SCREEGO_TLS_KEY_FILE= # The address the http server will listen on. +# Formats: +# - host:port +# Example: 127.0.0.1:5050 +# - unix socket (must be prefixed with unix:) +# Example: unix:/my/file/path.socket SCREEGO_SERVER_ADDRESS=0.0.0.0:5050 # The address the TURN server will listen on. diff --git a/server/server.go b/server/server.go index c6aa8963..0e13fb18 100644 --- a/server/server.go +++ b/server/server.go @@ -2,9 +2,11 @@ package server import ( "context" + "net" "net/http" "os" "os/signal" + "strings" "time" "github.com/rs/zerolog/log" @@ -32,19 +34,34 @@ func startServer(mux *mux.Router, address, cert, key string) (*http.Server, chan shutdown := make(chan error) go func() { - if cert != "" || key != "" { - log.Info().Str("addr", address).Msg("Start HTTP with tls") - err := srv.ListenAndServeTLS(cert, key) - shutdown <- err - } else { - log.Info().Str("addr", address).Msg("Start HTTP") - err := srv.ListenAndServe() - shutdown <- err - } + err := listenAndServe(srv, address, cert, key) + shutdown <- err }() return srv, shutdown } +func listenAndServe(srv *http.Server, address, cert, key string) error { + var err error + var listener net.Listener + + if strings.HasPrefix(address, "unix:") { + listener, err = net.Listen("unix", strings.TrimPrefix(address, "unix:")) + } else { + listener, err = net.Listen("tcp", address) + } + if err != nil { + return err + } + + if cert != "" || key != "" { + log.Info().Str("addr", address).Msg("Start HTTP with tls") + return srv.ServeTLS(listener, cert, key) + } else { + log.Info().Str("addr", address).Msg("Start HTTP") + return srv.Serve(listener) + } +} + func shutdownOnInterruptSignal(server *http.Server, timeout time.Duration, shutdown chan<- error) { interrupt := make(chan os.Signal, 1) notifySignal(interrupt, os.Interrupt)