From 45c6b5dba5cf7629da3f5add39549622c4844e95 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Fri, 9 Oct 2020 22:06:30 +0200 Subject: [PATCH] Fix #7: make each module optional --- auth/auth.go | 11 +++++++---- auth/bypass/bypass.go | 21 --------------------- auth/bypass/bypass_test.go | 14 -------------- docs/ghostream.example.yml | 25 +++++++++++++++++++++++-- internal/monitoring/monitoring.go | 6 ++++++ main.go | 9 ++++++++- stream/srt/srt.go | 6 ++++++ stream/webrtc/webrtc.go | 11 ++++++++--- web/web.go | 6 ++++++ 9 files changed, 64 insertions(+), 45 deletions(-) delete mode 100644 auth/bypass/bypass.go delete mode 100644 auth/bypass/bypass_test.go diff --git a/auth/auth.go b/auth/auth.go index d568b31..da66090 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -6,12 +6,12 @@ import ( "strings" "gitlab.crans.org/nounous/ghostream/auth/basic" - "gitlab.crans.org/nounous/ghostream/auth/bypass" "gitlab.crans.org/nounous/ghostream/auth/ldap" ) // Options holds package configuration type Options struct { + Enabled bool Backend string Basic basic.Options LDAP ldap.Options @@ -25,14 +25,17 @@ type Backend interface { // New initialize authentification backend func New(cfg *Options) (Backend, error) { - var backend Backend + var backend Backend = nil var err error + if !cfg.Enabled { + // Authentification is disabled + return nil, nil + } + switch strings.ToLower(cfg.Backend) { case "basic": backend, err = basic.New(&cfg.Basic) - case "bypass": - backend, err = bypass.New() case "ldap": backend, err = ldap.New(&cfg.LDAP) default: diff --git a/auth/bypass/bypass.go b/auth/bypass/bypass.go deleted file mode 100644 index 5abcd25..0000000 --- a/auth/bypass/bypass.go +++ /dev/null @@ -1,21 +0,0 @@ -package bypass - -// ByPass authentification backend -// By pass password check, open your streaming server to everyone! -type ByPass struct { -} - -// Login always return success -func (a ByPass) Login(username string, password string) (bool, error) { - return true, nil -} - -// Close has no connection to close -func (a ByPass) Close() { -} - -// New instanciates a new Basic authentification backend -func New() (ByPass, error) { - backend := ByPass{} - return backend, nil -} diff --git a/auth/bypass/bypass_test.go b/auth/bypass/bypass_test.go deleted file mode 100644 index 58fb015..0000000 --- a/auth/bypass/bypass_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package bypass - -import ( - "testing" -) - -func TestBypassLogin(t *testing.T) { - backend, _ := New() - ok, err := backend.Login("demo", "demo") - if !ok { - t.Error("Error while logging with the bypass authentication:", err) - } - backend.Close() -} diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml index c8c5046..f3dde93 100644 --- a/docs/ghostream.example.yml +++ b/docs/ghostream.example.yml @@ -5,11 +5,14 @@ # e.g. GHOSTREAM_AUTH_BACKEND=ldap will change auth.backend to "ldap" ## Authentification package ## -# Manage incoming stream authentification auth: + # If you disable authentification no more check will be done on incoming + # streams. + # + #enabled: true + # Authentification backend, # can be "basic" to use a list of user:password - # can be "bypass" to bypass the authentification process # can be "ldap" to use a LDAP server # #backend: basic @@ -41,6 +44,10 @@ forwarding: ## Prometheus monitoring ## # Expose a monitoring endpoint for Prometheus monitoring: + # If you disable monitoring module, no more metrics will be exposed. + # + #enabled: true + # You should not expose monitoring metrics to the whole world. # To limit access to only localhost, use 127.0.0.1:2112 #listenAddress: :2112 @@ -48,6 +55,11 @@ monitoring: ## SRT server ## # The SRT server receive incoming stream and can also serve video to clients. srt: + # If you disable SRT module, it will be no more possible to receive incoming + # streams and this whole app will become useless. + # + #enabled: true + # To limit access to only localhost, use 127.0.0.1:9710 #listenAddress: :9710 @@ -57,6 +69,11 @@ srt: ## Web server ## # The web server serves a WebRTC player. web: + # If you disable web module, the stream will be accessible only via SRT or + # via forwarding module (see above). + # + #enabled: true + # Web page favicon, can be .ico, .png or .svg # #favicon: /static/img/favicon.svg @@ -94,6 +111,10 @@ web: ## WebRTC server ## webrtc: + # If you disable webrtc module, the web client won't be able to play streams. + # + #enabled: true + # UDP port range used to stream # This range must be opened in your firewall. # diff --git a/internal/monitoring/monitoring.go b/internal/monitoring/monitoring.go index e3f2d0c..309f7a9 100644 --- a/internal/monitoring/monitoring.go +++ b/internal/monitoring/monitoring.go @@ -11,6 +11,7 @@ import ( // Options holds web package configuration type Options struct { + Enabled bool ListenAddress string } @@ -36,6 +37,11 @@ var ( // Serve monitoring server that expose prometheus metrics func Serve(cfg *Options) { + if !cfg.Enabled { + // Monitoring is not enabled, ignore + return + } + mux := http.NewServeMux() mux.Handle("/metrics", promhttp.Handler()) log.Printf("Monitoring HTTP server listening on %s", cfg.ListenAddress) diff --git a/main.go b/main.go index cd4c73a..8e9e0a9 100644 --- a/main.go +++ b/main.go @@ -45,6 +45,7 @@ func loadConfiguration() { } // Define configuration default values + viper.SetDefault("Auth.Enabled", true) viper.SetDefault("Auth.Backend", "Basic") viper.SetDefault("Auth.Basic.Credentials", map[string]string{ // Demo user with password "demo" @@ -53,15 +54,19 @@ func loadConfiguration() { viper.SetDefault("Auth.LDAP.URI", "ldap://127.0.0.1:389") viper.SetDefault("Auth.LDAP.UserDn", "cn=users,dc=example,dc=com") viper.SetDefault("Forwarding", make(map[string][]string)) + viper.SetDefault("Monitoring.Enabled", true) viper.SetDefault("Monitoring.ListenAddress", ":2112") + viper.SetDefault("Srt.Enabled", true) viper.SetDefault("Srt.ListenAddress", ":9710") viper.SetDefault("Srt.MaxClients", 64) + viper.SetDefault("Web.Enabled", true) viper.SetDefault("Web.Favicon", "/static/img/favicon.svg") viper.SetDefault("Web.Hostname", "localhost") viper.SetDefault("Web.ListenAddress", ":8080") viper.SetDefault("Web.Name", "Ghostream") viper.SetDefault("Web.OneStreamPerDomain", false) viper.SetDefault("Web.ViewersCounterRefreshPeriod", 20000) + viper.SetDefault("WebRTC.Enabled", true) viper.SetDefault("WebRTC.MaxPortUDP", 10005) viper.SetDefault("WebRTC.MinPortUDP", 10000) viper.SetDefault("WebRTC.STUNServers", []string{"stun:stun.l.google.com:19302"}) @@ -101,7 +106,9 @@ func main() { if err != nil { log.Fatalln("Failed to load authentification backend:", err) } - defer authBackend.Close() + if authBackend != nil { + defer authBackend.Close() + } // WebRTC session description channels remoteSdpChan := make(chan struct { diff --git a/stream/srt/srt.go b/stream/srt/srt.go index c9893c1..b47f6ba 100644 --- a/stream/srt/srt.go +++ b/stream/srt/srt.go @@ -19,6 +19,7 @@ var ( // Options holds web package configuration type Options struct { + Enabled bool ListenAddress string MaxClients int } @@ -53,6 +54,11 @@ func GetNumberConnectedSessions(streamID string) int { // Serve SRT server func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel, webrtcChannel chan Packet) { + if !cfg.Enabled { + // SRT is not enabled, ignore + return + } + // Start SRT in listening mode log.Printf("SRT server listening on %s", cfg.ListenAddress) host, port := splitHostPort(cfg.ListenAddress) diff --git a/stream/webrtc/webrtc.go b/stream/webrtc/webrtc.go index 6945d79..585c4d1 100644 --- a/stream/webrtc/webrtc.go +++ b/stream/webrtc/webrtc.go @@ -13,9 +13,9 @@ import ( // Options holds web package configuration type Options struct { - MinPortUDP uint16 - MaxPortUDP uint16 - + Enabled bool + MinPortUDP uint16 + MaxPortUDP uint16 STUNServers []string } @@ -179,6 +179,11 @@ func Serve(remoteSdpChan chan struct { StreamID string RemoteDescription webrtc.SessionDescription }, localSdpChan chan webrtc.SessionDescription, inputChannel chan srt.Packet, cfg *Options) { + if !cfg.Enabled { + // SRT is not enabled, ignore + return + } + log.Printf("WebRTC server using UDP from port %d to %d", cfg.MinPortUDP, cfg.MaxPortUDP) // Allocate memory diff --git a/web/web.go b/web/web.go index f717c22..8c5af5c 100644 --- a/web/web.go +++ b/web/web.go @@ -15,6 +15,7 @@ import ( // Options holds web package configuration type Options struct { + Enabled bool Favicon string Hostname string ListenAddress string @@ -82,6 +83,11 @@ func Serve(rSdpChan chan struct { localSdpChan = lSdpChan cfg = c + if !cfg.Enabled { + // SRT is not enabled, ignore + return + } + // Load templates if err := loadTemplates(); err != nil { log.Fatalln("Failed to load templates:", err)