diff --git a/stream/telnet/handler.go b/stream/telnet/handler.go deleted file mode 100644 index 92ecc95..0000000 --- a/stream/telnet/handler.go +++ /dev/null @@ -1,79 +0,0 @@ -package telnet - -import ( - "log" - "net" - "strings" - "time" - - "gitlab.crans.org/nounous/ghostream/stream" -) - -func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) { - // Prompt user about stream name - if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil { - log.Printf("Error while writing to TCP socket: %s", err) - s.Close() - return - } - buff := make([]byte, 255) - n, err := s.Read(buff) - if err != nil { - log.Printf("Error while requesting stream ID to telnet client: %s", err) - s.Close() - return - } - name := strings.TrimSpace(string(buff[:n])) + "@text" - if len(name) < 1 { - // Too short, exit - s.Close() - return - } - - // Wait a bit - time.Sleep(time.Second) - - // Get requested stream - st, ok := streams[name] - if !ok { - log.Println("Stream does not exist, kicking new Telnet viewer") - if _, err := s.Write([]byte("This stream is inactive.\n")); err != nil { - log.Printf("Error while writing to TCP socket: %s", err) - } - s.Close() - return - } - - // Register new client - log.Printf("New Telnet viewer for stream '%s'", name) - c := make(chan []byte, 128) - st.Register(c) - st.IncrementClientCount() - - // Hide terminal cursor - if _, err = s.Write([]byte("\033[?25l")); err != nil { - log.Printf("Error while writing to TCP socket: %s", err) - s.Close() - return - } - - // Receive data and send them - for data := range c { - if len(data) < 1 { - log.Print("Remove Telnet viewer because of end of stream") - break - } - - // Send data - _, err := s.Write(data) - if err != nil { - log.Printf("Remove Telnet viewer because of sending error, %s", err) - break - } - } - - // Close output - st.Unregister(c) - st.DecrementClientCount() - s.Close() -} diff --git a/stream/telnet/telnet.go b/stream/telnet/telnet.go index 5bf1636..5090895 100644 --- a/stream/telnet/telnet.go +++ b/stream/telnet/telnet.go @@ -1,9 +1,11 @@ -// Package telnet provides some fancy tools, like an ASCII-art stream. +// Package telnet expose text version of stream. package telnet import ( "log" "net" + "strings" + "time" "gitlab.crans.org/nounous/ghostream/stream" ) @@ -39,3 +41,72 @@ func Serve(streams map[string]*stream.Stream, cfg *Options) { go handleViewer(s, streams, cfg) } } + +func handleViewer(s net.Conn, streams map[string]*stream.Stream, cfg *Options) { + // Prompt user about stream name + if _, err := s.Write([]byte("[GHOSTREAM]\nEnter stream name: ")); err != nil { + log.Printf("Error while writing to TCP socket: %s", err) + s.Close() + return + } + buff := make([]byte, 255) + n, err := s.Read(buff) + if err != nil { + log.Printf("Error while requesting stream ID to telnet client: %s", err) + s.Close() + return + } + name := strings.TrimSpace(string(buff[:n])) + "@text" + if len(name) < 1 { + // Too short, exit + s.Close() + return + } + + // Wait a bit + time.Sleep(time.Second) + + // Get requested stream + st, ok := streams[name] + if !ok { + log.Println("Stream does not exist, kicking new Telnet viewer") + if _, err := s.Write([]byte("This stream is inactive.\n")); err != nil { + log.Printf("Error while writing to TCP socket: %s", err) + } + s.Close() + return + } + + // Register new client + log.Printf("New Telnet viewer for stream '%s'", name) + c := make(chan []byte, 128) + st.Register(c) + st.IncrementClientCount() + + // Hide terminal cursor + if _, err = s.Write([]byte("\033[?25l")); err != nil { + log.Printf("Error while writing to TCP socket: %s", err) + s.Close() + return + } + + // Receive data and send them + for data := range c { + if len(data) < 1 { + log.Print("Remove Telnet viewer because of end of stream") + break + } + + // Send data + _, err := s.Write(data) + if err != nil { + log.Printf("Remove Telnet viewer because of sending error, %s", err) + break + } + } + + // Close output + st.Unregister(c) + st.DecrementClientCount() + s.Close() +} diff --git a/transcoder/transcoder.go b/transcoder/transcoder.go index 5fda7ef..bf84170 100644 --- a/transcoder/transcoder.go +++ b/transcoder/transcoder.go @@ -1,3 +1,4 @@ +// Package transcoder manages transcoders package transcoder import (