You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gomud/telnet/telnet.go

93 lines
2.0 KiB
Go

package telnet
import (
"bufio"
"context"
"log"
"net"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
)
type TELNETOption struct {
Us, UsQ, Them, ThemQ bool
}
type TELNETOptions map[string]TELNETOption
type TELNETParser struct {
s message.Subscriber
c context.Context
logger watermill.LoggerAdapter
to TELNETOptions
}
func (tp *TELNETParser) Handle() {
messages, err := tp.s.Subscribe(tp.c, "telnet.raw")
if err != nil {
log.Fatalln("couldn't subscribe to telnet.raw")
}
for message := range messages {
fields := make(watermill.LogFields)
fields["body"] = string(message.Payload)
fields["correlation_id"] = string(message.Metadata["correlation_id"])
tp.logger.Trace("received raw TCP line", fields)
message.Ack()
}
}
func NewTELNETParser(c context.Context, s message.Subscriber, wml watermill.LoggerAdapter) *TELNETParser {
return &TELNETParser{
c: c,
s: s,
logger: wml,
}
}
type TELNETServer struct {
l net.Listener
p message.Publisher
c context.Context
logger watermill.LoggerAdapter
to TELNETOptions
}
func NewTELNETServer(c context.Context, l net.Listener, p message.Publisher, wml watermill.LoggerAdapter) *TELNETServer {
ts := TELNETServer{
c: c,
l: l,
p: p,
logger: wml,
}
return &ts
}
func (ts *TELNETServer) Handle(conn net.Conn) {
s := bufio.NewScanner(conn)
defer conn.Close()
// i need to create an ecs Renderer that targets a iw.Writer (net.Conn)
// the renderer
correlationID := watermill.NewUUID()
ts.logger = ts.logger.With(
watermill.LogFields{
"correlation_id": correlationID},
)
for s.Scan() {
t := s.Text()
m := message.NewMessage(watermill.NewUUID(), []byte(t))
middleware.SetCorrelationID(correlationID, m)
err := ts.p.Publish("telnet.raw", m)
if err != nil {
log.Fatalln("couldn't write to telnet.raw")
}
ts.logger.Trace("received bytes over telnet", nil)
}
}