2020-10-12 21:39:26 +00:00
|
|
|
// Package telnet provides some fancy tools, like an ASCII-art stream.
|
|
|
|
package telnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
2020-10-17 16:12:25 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/stream"
|
2020-10-12 22:15:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Options holds telnet package configuration
|
|
|
|
type Options struct {
|
|
|
|
Enabled bool
|
|
|
|
ListenAddress string
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 16:12:25 +00:00
|
|
|
// Serve Telnet server
|
|
|
|
func Serve(streams map[string]*stream.Stream, cfg *Options) {
|
|
|
|
if !cfg.Enabled {
|
|
|
|
// Telnet is not enabled, ignore
|
2020-10-12 22:15:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 16:12:25 +00:00
|
|
|
// Start TCP server
|
|
|
|
listener, err := net.Listen("tcp", cfg.ListenAddress)
|
2020-10-12 21:39:26 +00:00
|
|
|
if err != nil {
|
2020-10-17 16:12:25 +00:00
|
|
|
log.Fatalf("Error while listening to the address %s: %s", cfg.ListenAddress, err)
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
2020-10-17 16:12:25 +00:00
|
|
|
log.Printf("Telnet server listening on %s", cfg.ListenAddress)
|
2020-10-12 21:39:26 +00:00
|
|
|
|
2020-10-17 16:12:25 +00:00
|
|
|
// Handle each new client
|
|
|
|
for {
|
|
|
|
s, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error while accepting TCP socket: %s", s)
|
|
|
|
continue
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 17:40:37 +00:00
|
|
|
go handleViewer(s, streams, cfg)
|
2020-10-12 21:39:26 +00:00
|
|
|
}
|
|
|
|
}
|