Skip to content

Commit

Permalink
Added debug logs in websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasbalampekos committed Apr 3, 2024
1 parent 4d50595 commit 473af2d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/mqtt/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (p Proxy) handle(ctx context.Context, inbound net.Conn) {
return
}

if err = session.Stream(ctx, inbound, outbound, p.handler, p.interceptor, clientCert); err != io.EOF {
if err = session.Stream(ctx, inbound, outbound, p.handler, p.interceptor, clientCert, p.logger); err != io.EOF {
p.logger.Warn(err.Error())
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/mqtt/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (p Proxy) pass(ctx context.Context, in *websocket.Conn) {
return
}

err = session.Stream(ctx, inboundConn, outboundConn, p.handler, p.interceptor, clientCert)
err = session.Stream(ctx, inboundConn, outboundConn, p.handler, p.interceptor, clientCert, p.logger)
errc <- err
p.logger.Warn("Broken connection for client", slog.Any("error", err))
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/session/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net"

"github.com/eclipse/paho.mqtt.golang/packets"
Expand All @@ -29,15 +30,15 @@ var (
)

// Stream starts proxy between client and broker.
func Stream(ctx context.Context, in, out net.Conn, h Handler, ic Interceptor, cert x509.Certificate) error {
func Stream(ctx context.Context, in, out net.Conn, h Handler, ic Interceptor, cert x509.Certificate, logger *slog.Logger) error {
s := Session{
Cert: cert,
}
ctx = NewContext(ctx, &s)
errs := make(chan error, 2)

go stream(ctx, Up, in, out, h, ic, errs)
go stream(ctx, Down, out, in, h, ic, errs)
go stream(ctx, Up, in, out, h, ic, errs, logger)
go stream(ctx, Down, out, in, h, ic, errs, logger)

// Handle whichever error happens first.
// The other routine won't be blocked when writing
Expand All @@ -49,10 +50,11 @@ func Stream(ctx context.Context, in, out net.Conn, h Handler, ic Interceptor, ce
return errors.Join(err, disconnectErr)
}

func stream(ctx context.Context, dir Direction, r, w net.Conn, h Handler, ic Interceptor, errs chan error) {
func stream(ctx context.Context, dir Direction, r, w net.Conn, h Handler, ic Interceptor, errs chan error, logger *slog.Logger) {
for {
// Read from one connection.
pkt, err := packets.ReadPacket(r)
logger.Warn("ReadPacket: Received new packet")
if err != nil {
errs <- wrap(ctx, err, dir)
return
Expand Down

0 comments on commit 473af2d

Please sign in to comment.