1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-23 00:22:19 +00:00

Remove globals in SRT server

This commit is contained in:
Alexandre Iooss 2020-10-03 17:50:34 +02:00
parent 93ac5a7de1
commit 6172cf89be
No known key found for this signature in database
GPG Key ID: 6C79278F3FCDCC02

View File

@ -12,7 +12,6 @@ import (
"github.com/haivision/srtgo" "github.com/haivision/srtgo"
"gitlab.crans.org/nounous/ghostream/auth" "gitlab.crans.org/nounous/ghostream/auth"
"gitlab.crans.org/nounous/ghostream/auth/bypass"
) )
// Options holds web package configuration // Options holds web package configuration
@ -28,11 +27,6 @@ type Packet struct {
StreamName string StreamName string
} }
var (
authBackend auth.Backend
forwardingChannel chan Packet
)
// Split host and port from listen address // Split host and port from listen address
func splitHostPort(hostport string) (string, uint16) { func splitHostPort(hostport string) (string, uint16) {
host, portS, err := net.SplitHostPort(hostport) host, portS, err := net.SplitHostPort(hostport)
@ -50,16 +44,7 @@ func splitHostPort(hostport string) (string, uint16) {
} }
// Serve SRT server // Serve SRT server
func Serve(cfg *Options, backend auth.Backend, forwarding chan Packet) { func Serve(cfg *Options, authBackend auth.Backend, forwardingChannel chan Packet) {
// If no authentification backend was provided, default to bypass
if backend == nil {
backend, _ = bypass.New()
}
// FIXME: should not use globals
authBackend = backend
forwardingChannel = forwarding
// Start SRT in listen mode // Start SRT in listen mode
log.Printf("SRT server listening on %s", cfg.ListenAddress) log.Printf("SRT server listening on %s", cfg.ListenAddress)
host, port := splitHostPort(cfg.ListenAddress) host, port := splitHostPort(cfg.ListenAddress)
@ -72,9 +57,6 @@ func Serve(cfg *Options, backend auth.Backend, forwarding chan Packet) {
log.Fatal("Unable to listen to SRT clients:", err) log.Fatal("Unable to listen to SRT clients:", err)
} }
// FIXME: See srtgo.SocketOptions and value, err := s.GetSockOptString to get parameters
// http://ffmpeg.org/ffmpeg-protocols.html#srt
// FIXME: Get the stream type // FIXME: Get the stream type
streamStarted := false streamStarted := false
// FIXME Better structure // FIXME Better structure
@ -90,7 +72,7 @@ func Serve(cfg *Options, backend auth.Backend, forwarding chan Packet) {
} }
if !streamStarted { if !streamStarted {
go acceptCallerSocket(s, clientDataChannels, &listeners) go acceptCallerSocket(s, clientDataChannels, &listeners, authBackend, forwardingChannel)
streamStarted = true streamStarted = true
} else { } else {
dataChannel := make(chan Packet, 2048) dataChannel := make(chan Packet, 2048)
@ -103,8 +85,8 @@ func Serve(cfg *Options, backend auth.Backend, forwarding chan Packet) {
sck.Close() sck.Close()
} }
func acceptCallerSocket(s *srtgo.SrtSocket, clientDataChannels []chan Packet, listeners *int) { func acceptCallerSocket(s *srtgo.SrtSocket, clientDataChannels []chan Packet, listeners *int, authBackend auth.Backend, forwardingChannel chan Packet) {
streamName, err := authenticateSocket(s) streamName, err := authenticateSocket(s, authBackend)
if err != nil { if err != nil {
log.Println("Authentication failure:", err) log.Println("Authentication failure:", err)
s.Close() s.Close()
@ -170,7 +152,7 @@ func acceptListeningSocket(s *srtgo.SrtSocket, dataChannel chan Packet) {
} }
} }
func authenticateSocket(s *srtgo.SrtSocket) (string, error) { func authenticateSocket(s *srtgo.SrtSocket, authBackend auth.Backend) (string, error) {
streamID, err := s.GetSockOptString(C.SRTO_STREAMID) streamID, err := s.GetSockOptString(C.SRTO_STREAMID)
if err != nil { if err != nil {
return "", fmt.Errorf("error while fetching stream key: %s", err) return "", fmt.Errorf("error while fetching stream key: %s", err)
@ -182,6 +164,10 @@ func authenticateSocket(s *srtgo.SrtSocket) (string, error) {
splittedStreamID := strings.SplitN(streamID, ":", 2) splittedStreamID := strings.SplitN(streamID, ":", 2)
streamName, password := splittedStreamID[0], splittedStreamID[1] streamName, password := splittedStreamID[0], splittedStreamID[1]
if authBackend == nil {
// Bypass authentification if none provided
return streamName, nil
}
loggedIn, err := authBackend.Login(streamName, password) loggedIn, err := authBackend.Login(streamName, password)
if !loggedIn { if !loggedIn {
return streamID, fmt.Errorf("invalid credentials for stream %s", streamName) return streamID, fmt.Errorf("invalid credentials for stream %s", streamName)