2020-10-12 21:39:26 +00:00
|
|
|
// 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 21:39:26 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2020-10-12 21:39:26 +00:00
|
|
|
if err != nil {
|
2020-10-12 22:15:23 +00:00
|
|
|
log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err)
|
2020-10-12 21:39:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-12 22:15:23 +00:00
|
|
|
currentMessage = new(string)
|
2020-10-12 21:39:26 +00:00
|
|
|
|
|
|
|
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))
|
2020-10-12 21:39:26 +00:00
|
|
|
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)
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
}(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)
|
2020-10-12 21:39:26 +00:00
|
|
|
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]
|
2020-10-12 21:39:26 +00:00
|
|
|
imageStr += asciiChar(pixel) + asciiChar(pixel)
|
|
|
|
}
|
|
|
|
imageStr += "\n"
|
|
|
|
}
|
2020-10-12 22:15:23 +00:00
|
|
|
*currentMessage = header + imageStr
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
}
|