ghostream/stream/telnet/telnet.go

173 lines
3.9 KiB
Go
Raw Normal View History

// Package telnet provides some fancy tools, like an ASCII-art stream.
package telnet
import (
"io"
"log"
"net"
"strings"
"time"
)
2020-10-12 22:15:23 +00:00
var (
2020-10-12 22:31:47 +00:00
// Cfg contains the different options of the telnet package, see below
2020-10-12 23:41:17 +00:00
// TODO Config should not be exported
2020-10-12 22:15:23 +00:00
Cfg *Options
currentMessage map[string]*string
clientCount map[string]int
ASCIICHARS = []byte{' ', '.', ',', ':', ';', '+', '*', '?', '%', '$', '#', '@'}
2020-10-12 22:15:23 +00:00
)
// Options holds telnet package configuration
type Options struct {
Enabled bool
ListenAddress string
Width int
Height int
Delay int
}
2020-10-12 22:31:47 +00:00
// Serve starts the telnet server and listen to clients
2020-10-12 22:15:23 +00:00
func Serve(config *Options) {
Cfg = config
2020-10-14 14:44:13 +00:00
if !config.Enabled {
2020-10-12 22:15:23 +00:00
return
}
currentMessage = make(map[string]*string)
clientCount = make(map[string]int)
2020-10-14 14:44:13 +00:00
listener, err := net.Listen("tcp", config.ListenAddress)
if err != nil {
2020-10-14 14:44:13 +00:00
log.Printf("Error while listening to the address %s: %s", config.ListenAddress, err)
return
}
go func() {
for {
s, err := listener.Accept()
if err != nil {
log.Printf("Error while accepting TCP socket: %s", s)
continue
}
go func(s net.Conn) {
streamID := ""
// Request for stream ID
for {
_, _ = s.Write([]byte("[GHOSTREAM]\n"))
_, err = s.Write([]byte("Enter stream ID: "))
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
buff := make([]byte, 255)
n, err := s.Read(buff)
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
// Avoid bruteforce
time.Sleep(3 * time.Second)
streamID = string(buff[:n])
streamID = strings.Replace(streamID, "\r", "", -1)
streamID = strings.Replace(streamID, "\n", "", -1)
if len(streamID) > 0 {
if strings.ToLower(streamID) == "exit" {
_, _ = s.Write([]byte("Goodbye!\n"))
_ = s.Close()
return
}
if _, ok := currentMessage[streamID]; !ok {
_, err = s.Write([]byte("Unknown stream ID.\n"))
if err != nil {
log.Println("Error while requesting stream ID to telnet client")
_ = s.Close()
return
}
continue
}
break
}
}
clientCount[streamID]++
for {
n, err := s.Write([]byte(*currentMessage[streamID]))
if err != nil {
log.Printf("Error while sending TCP data: %s", err)
_ = s.Close()
clientCount[streamID]--
break
}
if n == 0 {
_ = s.Close()
clientCount[streamID]--
break
}
2020-10-14 14:44:13 +00:00
time.Sleep(time.Duration(config.Delay) * time.Millisecond)
}
}(s)
}
}()
2020-10-12 22:15:23 +00:00
log.Println("Telnet server initialized")
}
// GetNumberConnectedSessions returns the numbers of clients that are viewing the stream through a telnet shell
func GetNumberConnectedSessions(streamID string) int {
if Cfg == nil || !Cfg.Enabled {
return 0
}
return clientCount[streamID]
}
// StartASCIIArtStream send all packets received by ffmpeg as ASCII Art to telnet clients
func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
2020-10-12 22:15:23 +00:00
if !Cfg.Enabled {
_ = reader.Close()
return
}
currentMessage[streamID] = new(string)
2020-10-14 14:44:13 +00:00
pixelBuff := make([]byte, Cfg.Width*Cfg.Height)
textBuff := strings.Builder{}
for {
2020-10-14 14:44:13 +00:00
n, err := reader.Read(pixelBuff)
if err != nil {
log.Printf("An error occured while reading input: %s", err)
break
}
if n == 0 {
2020-10-14 14:44:13 +00:00
// Stream is finished
break
}
2020-10-14 14:44:13 +00:00
// Header
textBuff.Reset()
2020-10-14 14:46:39 +00:00
textBuff.Grow((2*Cfg.Width + 1) * Cfg.Height)
2020-10-14 14:44:13 +00:00
for i := 0; i < 42; i++ {
textBuff.WriteByte('\n')
}
// Convert image to ASCII
2020-10-12 22:15:23 +00:00
for j := 0; j < Cfg.Height; j++ {
for i := 0; i < Cfg.Width; i++ {
textBuff.WriteByte(ASCIICHARS[pixelBuff[Cfg.Width*j+i]/22])
textBuff.WriteByte(ASCIICHARS[pixelBuff[Cfg.Width*j+i]/22])
}
2020-10-14 14:44:13 +00:00
textBuff.WriteByte('\n')
}
2020-10-14 14:44:13 +00:00
*(currentMessage[streamID]) = textBuff.String()
}
}