|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/ThreeDotsLabs/watermill"
|
|
|
|
"github.com/ThreeDotsLabs/watermill/message"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TELNETServer struct {
|
|
|
|
l net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTELNETServer(l net.Listener) *TELNETServer {
|
|
|
|
ts := TELNETServer{
|
|
|
|
l: l,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TELNETServer) Run(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
conn, err := ts.l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(c net.Conn) {
|
|
|
|
s := bufio.NewScanner(c)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
for s.Scan() {
|
|
|
|
t := s.Text()
|
|
|
|
err := ts.Publish("raw_telnet", message.NewMessage(watermill.NewUUID(), []byte(t)))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("couldn't write to raw_telnet queue")
|
|
|
|
}
|
|
|
|
log.Printf("Received user input: %q\n", t)
|
|
|
|
}
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *TELNETServer) Publish(topic string, messages ...*message.Message) error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
l, err := net.Listen("tcp", ":5555")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
ts := NewTELNETServer(l)
|
|
|
|
ts.Run(context.Background())
|
|
|
|
}
|