2020-10-12 21:39:26 +00:00
|
|
|
// Package telnet provides some fancy tools, like an ASCII-art stream.
|
|
|
|
package telnet
|
|
|
|
|
|
|
|
import (
|
2020-10-16 18:09:59 +00:00
|
|
|
"fmt"
|
2020-10-12 21:39:26 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
2020-10-12 22:52:08 +00:00
|
|
|
"strings"
|
2020-10-12 21:39:26 +00:00
|
|
|
"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
|
2020-10-13 09:17:35 +00:00
|
|
|
currentMessage map[string]*string
|
2020-10-13 09:37:59 +00:00
|
|
|
clientCount map[string]int
|
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 21:39:26 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:17:35 +00:00
|
|
|
currentMessage = make(map[string]*string)
|
2020-10-13 09:37:59 +00:00
|
|
|
clientCount = make(map[string]int)
|
2020-10-12 22:52:08 +00:00
|
|
|
|
2020-10-14 14:44:13 +00:00
|
|
|
listener, err := net.Listen("tcp", config.ListenAddress)
|
2020-10-12 21:39:26 +00:00
|
|
|
if err != nil {
|
2020-10-14 14:44:13 +00:00
|
|
|
log.Printf("Error while listening to the address %s: %s", config.ListenAddress, err)
|
2020-10-12 21:39:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
s, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while accepting TCP socket: %s", s)
|
|
|
|
continue
|
|
|
|
}
|
2020-10-12 22:52:08 +00:00
|
|
|
|
2020-10-12 21:39:26 +00:00
|
|
|
go func(s net.Conn) {
|
2020-10-12 22:52:08 +00:00
|
|
|
streamID := ""
|
|
|
|
// Request for stream ID
|
|
|
|
for {
|
2020-10-14 17:42:23 +00:00
|
|
|
_, err = s.Write([]byte("[GHOSTREAM]\nEnter stream ID: "))
|
2020-10-12 22:52:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:28:29 +00:00
|
|
|
// Avoid bruteforce
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
2020-10-12 22:52:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:37:59 +00:00
|
|
|
clientCount[streamID]++
|
|
|
|
|
2020-10-16 18:09:59 +00:00
|
|
|
// Hide terminal cursor
|
|
|
|
_, _ = s.Write([]byte("\033[?25l"))
|
|
|
|
|
2020-10-12 21:39:26 +00:00
|
|
|
for {
|
2020-10-13 09:17:35 +00:00
|
|
|
n, err := s.Write([]byte(*currentMessage[streamID]))
|
2020-10-12 21:39:26 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while sending TCP data: %s", err)
|
|
|
|
_ = s.Close()
|
2020-10-13 09:37:59 +00:00
|
|
|
clientCount[streamID]--
|
2020-10-12 21:39:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
_ = s.Close()
|
2020-10-13 09:37:59 +00:00
|
|
|
clientCount[streamID]--
|
2020-10-12 21:39:26 +00:00
|
|
|
break
|
|
|
|
}
|
2020-10-14 14:44:13 +00:00
|
|
|
time.Sleep(time.Duration(config.Delay) * time.Millisecond)
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
}(s)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-10-12 22:15:23 +00:00
|
|
|
log.Println("Telnet server initialized")
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:37:59 +00:00
|
|
|
// GetNumberConnectedSessions returns the numbers of clients that are viewing the stream through a telnet shell
|
|
|
|
func GetNumberConnectedSessions(streamID string) int {
|
2020-10-13 16:40:12 +00:00
|
|
|
if Cfg == nil || !Cfg.Enabled {
|
2020-10-13 09:37:59 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return clientCount[streamID]
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:56:49 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:17:35 +00:00
|
|
|
currentMessage[streamID] = new(string)
|
2020-10-14 14:44:13 +00:00
|
|
|
pixelBuff := make([]byte, Cfg.Width*Cfg.Height)
|
|
|
|
textBuff := strings.Builder{}
|
2020-10-12 21:39:26 +00:00
|
|
|
for {
|
2020-10-14 14:44:13 +00:00
|
|
|
n, err := reader.Read(pixelBuff)
|
|
|
|
if err != nil {
|
2020-10-14 19:36:12 +00:00
|
|
|
log.Printf("An error occurred while reading input: %s", err)
|
2020-10-14 14:44:13 +00:00
|
|
|
break
|
|
|
|
}
|
2020-10-12 21:39:26 +00:00
|
|
|
if n == 0 {
|
2020-10-14 14:44:13 +00:00
|
|
|
// Stream is finished
|
2020-10-12 21:39:26 +00:00
|
|
|
break
|
|
|
|
}
|
2020-10-14 14:44:13 +00:00
|
|
|
|
|
|
|
// Header
|
|
|
|
textBuff.Reset()
|
2020-10-16 18:09:59 +00:00
|
|
|
textBuff.Grow((40*Cfg.Width+6)*Cfg.Height + 47)
|
2020-10-14 14:44:13 +00:00
|
|
|
for i := 0; i < 42; i++ {
|
|
|
|
textBuff.WriteByte('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert image to ASCII
|
2020-10-14 20:07:24 +00:00
|
|
|
for i, pixel := range pixelBuff {
|
|
|
|
if i%Cfg.Width == 0 {
|
|
|
|
// New line
|
2020-10-16 18:09:59 +00:00
|
|
|
textBuff.WriteString("\033[49m\n")
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
2020-10-14 20:07:24 +00:00
|
|
|
|
|
|
|
// Print two times the character to make a square
|
2020-10-16 18:09:59 +00:00
|
|
|
text := fmt.Sprintf("\033[48;2;%d;%d;%dm ", pixel, pixel, pixel)
|
|
|
|
textBuff.WriteString(text)
|
|
|
|
textBuff.WriteString(text)
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
2020-10-16 18:09:59 +00:00
|
|
|
textBuff.WriteString("\033[49m")
|
2020-10-14 14:44:13 +00:00
|
|
|
|
|
|
|
*(currentMessage[streamID]) = textBuff.String()
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
}
|