Skip to content

Commit

Permalink
Remove pessimistic String calls for low level logs
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Apr 19, 2024
1 parent cf40c33 commit 71ef030
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 30 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (c *Client) PerformTransaction(msg *stun.Message, to net.Addr, ignoreResult

c.trMap.Insert(trKey, tr)

c.log.Tracef("Start %s transaction %s to %s", msg.Type, trKey, tr.To.String())
c.log.Tracef("Start %s transaction %s to %s", msg.Type, trKey, tr.To)
_, err := c.conn.WriteTo(tr.Raw, to)
if err != nil {
return client.TransactionResult{}, err
Expand Down Expand Up @@ -524,7 +524,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
return err
}

c.log.Tracef("Data indication received from %s", from.String())
c.log.Tracef("Data indication received from %s", from)

Check warning on line 527 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L527

Added line #L527 was not covered by tests

relayedConn := c.relayedUDPConn()
if relayedConn == nil {
Expand All @@ -548,7 +548,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
return err
}

c.log.Debugf("Connection attempt from %s", addr.String())
c.log.Debugf("Connection attempt from %s", addr)

allocation := c.getTCPAllocation()
if allocation == nil {
Expand All @@ -575,7 +575,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
if !ok {
c.mutexTrMap.Unlock()
// Silently discard
c.log.Debugf("No transaction for %s", msg.String())
c.log.Debugf("No transaction for %s", msg)

Check warning on line 578 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L578

Added line #L578 was not covered by tests
return nil
}

Expand All @@ -589,7 +589,7 @@ func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
From: from,
Retries: tr.Retries(),
}) {
c.log.Debugf("No listener for %s", msg.String())
c.log.Debugf("No listener for %s", msg)

Check warning on line 592 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L592

Added line #L592 was not covered by tests
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestClientWithSTUN(t *testing.T) {

resp, err := c.SendBindingRequest()
assert.NoError(t, err, "should succeed")
log.Debugf("mapped-addr: %s", resp.String())
log.Debugf("mapped-addr: %s", resp)
assert.Equal(t, 0, c.trMap.Size(), "should be no transaction left")
assert.NoError(t, pc.Close())
})
Expand Down
10 changes: 5 additions & 5 deletions internal/allocation/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ func (a *Allocation) packetHandler(m *Manager) {
}

a.log.Debugf("Relay socket %s received %d bytes from %s",
a.RelaySocket.LocalAddr().String(),
a.RelaySocket.LocalAddr(),
n,
srcAddr.String())
srcAddr)

if channel := a.GetChannelByAddr(srcAddr); channel != nil {
channelData := &proto.ChannelData{
Expand Down Expand Up @@ -274,13 +274,13 @@ func (a *Allocation) packetHandler(m *Manager) {
return
}
a.log.Debugf("Relaying message from %s to client at %s",
srcAddr.String(),
a.fiveTuple.SrcAddr.String())
srcAddr,
a.fiveTuple.SrcAddr)
if _, err = a.TurnSocket.WriteTo(msg.Raw, a.fiveTuple.SrcAddr); err != nil {
a.log.Errorf("Failed to send DataIndication from allocation %v %v", srcAddr, err)
}
} else {
a.log.Infof("No Permission or Channel exists for %v on allocation %v", srcAddr, a.RelayAddr.String())
a.log.Infof("No Permission or Channel exists for %v on allocation %v", srcAddr, a.RelayAddr)

Check warning on line 283 in internal/allocation/allocation.go

View check run for this annotation

Codecov / codecov/patch

internal/allocation/allocation.go#L283

Added line #L283 was not covered by tests
}
}
}
2 changes: 1 addition & 1 deletion internal/allocation/allocation_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (m *Manager) CreateAllocation(fiveTuple *FiveTuple, turnSocket net.PacketCo
a.RelaySocket = conn
a.RelayAddr = relayAddr

m.log.Debugf("Listening on relay address: %s", a.RelayAddr.String())
m.log.Debugf("Listening on relay address: %s", a.RelayAddr)

a.lifetimeTimer = time.AfterFunc(lifetime, func() {
m.DeleteAllocation(a.fiveTuple)
Expand Down
2 changes: 1 addition & 1 deletion internal/client/udp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func (c *UDPConn) bind(b *binding) error {
return fmt.Errorf("unexpected response type %s", res.Type) //nolint:goerr113
}

c.log.Debugf("Channel binding successful: %s %d", b.addr.String(), b.number)
c.log.Debugf("Channel binding successful: %s %d", b.addr, b.number)

// Success.
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Request struct {

// HandleRequest processes the give Request
func HandleRequest(r Request) error {
r.Log.Debugf("Received %d bytes of udp from %s on %s", len(r.Buff), r.SrcAddr.String(), r.Conn.LocalAddr().String())
r.Log.Debugf("Received %d bytes of udp from %s on %s", len(r.Buff), r.SrcAddr, r.Conn.LocalAddr())

if proto.IsChannelData(r.Buff) {
return handleDataPacket(r)
Expand Down
2 changes: 1 addition & 1 deletion internal/server/stun.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func handleBindingRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received BindingRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received BindingRequest from %s", r.SrcAddr)

ip, port, err := ipnet.AddrIPPort(r.SrcAddr)
if err != nil {
Expand Down
24 changes: 10 additions & 14 deletions internal/server/turn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const runesAlpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

// See: https://tools.ietf.org/html/rfc5766#section-6.2
func handleAllocateRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received AllocateRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received AllocateRequest from %s", r.SrcAddr)

// 1. The server MUST require that the request be authenticated. This
// authentication MUST be done using the long-term credential
Expand Down Expand Up @@ -183,7 +183,7 @@ func handleAllocateRequest(r Request, m *stun.Message) error {
}

func handleRefreshRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received RefreshRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received RefreshRequest from %s", r.SrcAddr)

messageIntegrity, hasAuth, err := authenticateRequest(r, m, stun.MethodRefresh)
if !hasAuth {
Expand Down Expand Up @@ -217,7 +217,7 @@ func handleRefreshRequest(r Request, m *stun.Message) error {
}

func handleCreatePermissionRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received CreatePermission from %s", r.SrcAddr.String())
r.Log.Debugf("Received CreatePermission from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand All @@ -242,13 +242,12 @@ func handleCreatePermissionRequest(r Request, m *stun.Message) error {
}

if err := r.AllocationManager.GrantPermission(r.SrcAddr, peerAddress.IP); err != nil {
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr.String(),
peerAddress.IP.String())
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr, peerAddress.IP)
return err
}

r.Log.Debugf("Adding permission for %s", fmt.Sprintf("%s:%d",
peerAddress.IP.String(), peerAddress.Port))
peerAddress.IP, peerAddress.Port))

a.AddPermission(allocation.NewPermission(
&net.UDPAddr{
Expand All @@ -272,7 +271,7 @@ func handleCreatePermissionRequest(r Request, m *stun.Message) error {
}

func handleSendIndication(r Request, m *stun.Message) error {
r.Log.Debugf("Received SendIndication from %s", r.SrcAddr.String())
r.Log.Debugf("Received SendIndication from %s", r.SrcAddr)
a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
DstAddr: r.Conn.LocalAddr(),
Expand Down Expand Up @@ -305,7 +304,7 @@ func handleSendIndication(r Request, m *stun.Message) error {
}

func handleChannelBindRequest(r Request, m *stun.Message) error {
r.Log.Debugf("Received ChannelBindRequest from %s", r.SrcAddr.String())
r.Log.Debugf("Received ChannelBindRequest from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand Down Expand Up @@ -334,18 +333,15 @@ func handleChannelBindRequest(r Request, m *stun.Message) error {
}

if err = r.AllocationManager.GrantPermission(r.SrcAddr, peerAddr.IP); err != nil {
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr.String(),
peerAddr.IP.String())
r.Log.Infof("permission denied for client %s to peer %s", r.SrcAddr, peerAddr.IP)

Check warning on line 336 in internal/server/turn.go

View check run for this annotation

Codecov / codecov/patch

internal/server/turn.go#L336

Added line #L336 was not covered by tests

unauthorizedRequestMsg := buildMsg(m.TransactionID,
stun.NewType(stun.MethodChannelBind, stun.ClassErrorResponse),
&stun.ErrorCodeAttribute{Code: stun.CodeUnauthorized})
return buildAndSendErr(r.Conn, r.SrcAddr, err, unauthorizedRequestMsg...)
}

r.Log.Debugf("Binding channel %d to %s",
channel,
fmt.Sprintf("%s:%d", peerAddr.IP.String(), peerAddr.Port))
r.Log.Debugf("Binding channel %d to %s", channel, peerAddr)
err = a.AddChannelBind(allocation.NewChannelBind(
channel,
&net.UDPAddr{IP: peerAddr.IP, Port: peerAddr.Port},
Expand All @@ -359,7 +355,7 @@ func handleChannelBindRequest(r Request, m *stun.Message) error {
}

func handleChannelData(r Request, c *proto.ChannelData) error {
r.Log.Debugf("Received ChannelData from %s", r.SrcAddr.String())
r.Log.Debugf("Received ChannelData from %s", r.SrcAddr)

a := r.AllocationManager.GetAllocation(&allocation.FiveTuple{
SrcAddr: r.SrcAddr,
Expand Down
2 changes: 1 addition & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func TestServerVNet(t *testing.T) {
log.Debug("sending a binding request.")
reflAddr, err := client.SendBindingRequest()
assert.NoError(t, err)
log.Debugf("mapped-address: %v", reflAddr.String())
log.Debugf("mapped-address: %s", reflAddr)
udpAddr, ok := reflAddr.(*net.UDPAddr)
assert.True(t, ok)

Expand Down

0 comments on commit 71ef030

Please sign in to comment.