Skip to content

Commit

Permalink
Reverted logging changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasbalampekos committed May 24, 2024
1 parent f647e08 commit d3d8489
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 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, p.logger); err != io.EOF {
if err = session.Stream(ctx, inbound, outbound, p.handler, p.interceptor, clientCert); 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 @@ -103,7 +103,7 @@ func (p Proxy) pass(in *websocket.Conn) {
return
}

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

"github.com/eclipse/paho.mqtt.golang/packets"
Expand All @@ -31,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, logger *slog.Logger) error {
func Stream(ctx context.Context, in, out net.Conn, h Handler, ic Interceptor, cert x509.Certificate) error {
s := Session{
Cert: cert,
}
ctx = NewContext(ctx, &s)
errs := make(chan error, 2)

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

// Handle whichever error happens first.
// The other routine won't be blocked when writing
Expand All @@ -51,19 +50,17 @@ 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, logger *slog.Logger) {
func stream(ctx context.Context, dir Direction, r, w net.Conn, h Handler, ic Interceptor, errs chan error) {
for {
// Read from one connection.
pkt, err := packets.ReadPacket(r)
logger.Warn(fmt.Sprintf("ReadPacket: Received %s new packet. Type: %T", fmt.Sprint(dir), pkt))
if err != nil {
errs <- wrap(ctx, err, dir)
return
}

if dir == Up {
if err = authorize(ctx, pkt, h); err != nil {
logger.Warn(fmt.Sprintf("Authorize returned error: %s", err.Error()))
errs <- wrap(ctx, err, dir)
return
}
Expand All @@ -79,15 +76,13 @@ func stream(ctx context.Context, dir Direction, r, w net.Conn, h Handler, ic Int

// Send to another.
if err := pkt.Write(w); err != nil {
logger.Warn(fmt.Sprintf("Write returned error: %s", err.Error()))
errs <- wrap(ctx, err, dir)
return
}

// Notify only for packets sent from client to broker (incoming packets).
if dir == Up {
if err := notify(ctx, pkt, h); err != nil {
logger.Warn(fmt.Sprintf("Notify returned error: %s", err.Error()))
errs <- wrap(ctx, err, dir)
}
}
Expand Down

0 comments on commit d3d8489

Please sign in to comment.