ghostream/stream/telnet/telnet.go

98 lines
1.9 KiB
Go
Raw Normal View History

// Package telnet provides some fancy tools, like an ASCII-art stream.
package telnet
import (
"io"
"log"
"net"
"time"
)
2020-10-12 22:15:23 +00:00
var (
Cfg *Options
currentMessage *string
)
// Options holds telnet package configuration
type Options struct {
Enabled bool
ListenAddress string
Width int
Height int
Delay int
}
2020-10-12 22:15:23 +00:00
func Serve(config *Options) {
Cfg = config
if !Cfg.Enabled {
return
}
listener, err := net.Listen("tcp", Cfg.ListenAddress)
if err != nil {
2020-10-12 22:15:23 +00:00
log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err)
return
}
2020-10-12 22:15:23 +00:00
currentMessage = new(string)
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) {
for {
2020-10-12 22:15:23 +00:00
n, err := s.Write([]byte(*currentMessage))
if err != nil {
log.Printf("Error while sending TCP data: %s", err)
_ = s.Close()
break
}
if n == 0 {
_ = s.Close()
break
}
2020-10-12 22:15:23 +00:00
time.Sleep(time.Duration(Cfg.Delay) * time.Millisecond)
}
}(s)
}
}()
2020-10-12 22:15:23 +00:00
log.Println("Telnet server initialized")
}
func asciiChar(pixel byte) string {
asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "}
return asciiChars[(255-pixel)/23]
}
// ServeAsciiArt starts a telnet server that send all packets as ASCII Art
func ServeAsciiArt(reader io.ReadCloser) {
if !Cfg.Enabled {
_ = reader.Close()
return
}
buff := make([]byte, Cfg.Width*Cfg.Height)
header := "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
for {
n, _ := reader.Read(buff)
if n == 0 {
break
}
imageStr := ""
2020-10-12 22:15:23 +00:00
for j := 0; j < Cfg.Height; j++ {
for i := 0; i < Cfg.Width; i++ {
pixel := buff[Cfg.Width*j+i]
imageStr += asciiChar(pixel) + asciiChar(pixel)
}
imageStr += "\n"
}
2020-10-12 22:15:23 +00:00
*currentMessage = header + imageStr
}
}