ghostream/stream/telnet/telnet.go

42 lines
865 B
Go
Raw Normal View History

// 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-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)
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-17 16:12:25 +00:00
log.Printf("Telnet server listening on %s", cfg.ListenAddress)
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
}
go handleViewer(s, streams, cfg)
}
}