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.

72 lines
1.5 KiB
Go

package telnet
import (
"bufio"
"context"
"log"
"net"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message"
1 year ago
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
)
type TELNETServer struct {
1 year ago
l net.Listener
p message.Publisher
c context.Context
logger watermill.LoggerAdapter
to TELNETOptions
}
1 year ago
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.With(
watermill.LogFields{
"name": "TELNETServer",
},
),
}
return &ts
}
func (ts *TELNETServer) Accept() (net.Conn, error) {
conn, err := ts.l.Accept()
ts.logger.Trace("receiving telnet connection", watermill.LogFields{
"remoteAddr": conn.RemoteAddr(),
},
)
return conn, err
}
func (ts *TELNETServer) Handle(wml watermill.LoggerAdapter) func(conn net.Conn) {
logger := wml.With(
1 year ago
watermill.LogFields{
// "correlation_id": correlationID,
},
1 year ago
)
return func(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()
1 year ago
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")
}
logger.Trace("received bytes over telnet", nil)
}
}
}