Store the clients that are connected to a telnet shell in the connected viewers stats

This commit is contained in:
Yohann D'ANELLO 2020-10-13 11:37:59 +02:00
parent 88c4a037cb
commit 51d38f6fec
2 changed files with 18 additions and 1 deletions

View File

@ -14,6 +14,7 @@ var (
// TODO Config should not be exported // TODO Config should not be exported
Cfg *Options Cfg *Options
currentMessage map[string]*string currentMessage map[string]*string
clientCount map[string]int
) )
// Options holds telnet package configuration // Options holds telnet package configuration
@ -34,6 +35,7 @@ func Serve(config *Options) {
} }
currentMessage = make(map[string]*string) currentMessage = make(map[string]*string)
clientCount = make(map[string]int)
listener, err := net.Listen("tcp", Cfg.ListenAddress) listener, err := net.Listen("tcp", Cfg.ListenAddress)
if err != nil { if err != nil {
@ -94,15 +96,19 @@ func Serve(config *Options) {
} }
} }
clientCount[streamID]++
for { for {
n, err := s.Write([]byte(*currentMessage[streamID])) n, err := s.Write([]byte(*currentMessage[streamID]))
if err != nil { if err != nil {
log.Printf("Error while sending TCP data: %s", err) log.Printf("Error while sending TCP data: %s", err)
_ = s.Close() _ = s.Close()
clientCount[streamID]--
break break
} }
if n == 0 { if n == 0 {
_ = s.Close() _ = s.Close()
clientCount[streamID]--
break break
} }
time.Sleep(time.Duration(Cfg.Delay) * time.Millisecond) time.Sleep(time.Duration(Cfg.Delay) * time.Millisecond)
@ -114,6 +120,14 @@ func Serve(config *Options) {
log.Println("Telnet server initialized") 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.Enabled {
return 0
}
return clientCount[streamID]
}
func asciiChar(pixel byte) string { func asciiChar(pixel byte) string {
asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "} asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "}
return asciiChars[(255-pixel)/22] return asciiChars[(255-pixel)/22]

View File

@ -4,6 +4,7 @@ package web
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"gitlab.crans.org/nounous/ghostream/stream/telnet"
"html/template" "html/template"
"log" "log"
"net" "net"
@ -144,7 +145,9 @@ func statisticsHandler(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w) enc := json.NewEncoder(w)
err := enc.Encode(struct { err := enc.Encode(struct {
ConnectedViewers int ConnectedViewers int
}{webrtc.GetNumberConnectedSessions(streamID) + srt.GetNumberConnectedSessions(streamID)}) }{webrtc.GetNumberConnectedSessions(streamID) +
srt.GetNumberConnectedSessions(streamID) +
telnet.GetNumberConnectedSessions(streamID)})
if err != nil { if err != nil {
http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError) http.Error(w, "Failed to generate JSON.", http.StatusInternalServerError)
log.Printf("Failed to generate JSON: %s", err) log.Printf("Failed to generate JSON: %s", err)