make a util of IP address truncation for logging. (#3148)
* make a util of IP address truncation for logging. * exported method
This commit is contained in:
+4
-21
@@ -18,7 +18,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -55,8 +54,6 @@ const (
|
|||||||
dataForwardLoadBalanceThreshold = 20
|
dataForwardLoadBalanceThreshold = 20
|
||||||
|
|
||||||
simulateDisconnectSignalTimeout = 5 * time.Second
|
simulateDisconnectSignalTimeout = 5 * time.Second
|
||||||
|
|
||||||
minIPTruncateLen = 8
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -1840,25 +1837,11 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
|
|||||||
if c.Trickle {
|
if c.Trickle {
|
||||||
cStr += "[trickle]"
|
cStr += "[trickle]"
|
||||||
}
|
}
|
||||||
remoteAddress := c.Remote.Address()
|
cStr += " " + fmt.Sprintf("%s %s %s:%d", c.Remote.NetworkType(), c.Remote.Type(), MaybeTruncateIP(c.Remote.Address()), c.Remote.Port())
|
||||||
ipAddr := net.ParseIP(remoteAddress)
|
|
||||||
isPrivate := false
|
|
||||||
if ipAddr != nil {
|
|
||||||
isPrivate = ipAddr.IsPrivate()
|
|
||||||
}
|
|
||||||
if !isPrivate && len(remoteAddress) > minIPTruncateLen {
|
|
||||||
remoteAddress = remoteAddress[:len(remoteAddress)-3] + "..."
|
|
||||||
}
|
|
||||||
cStr += " " + fmt.Sprintf("%s %s %s:%d", c.Remote.NetworkType(), c.Remote.Type(), remoteAddress, c.Remote.Port())
|
|
||||||
if relatedAddress := c.Remote.RelatedAddress(); relatedAddress != nil {
|
if relatedAddress := c.Remote.RelatedAddress(); relatedAddress != nil {
|
||||||
ipAddr = net.ParseIP(relatedAddress.Address)
|
relatedAddr := MaybeTruncateIP(relatedAddress.Address)
|
||||||
if ipAddr != nil {
|
if relatedAddr != "" {
|
||||||
isPrivate = ipAddr.IsPrivate()
|
cStr += " " + fmt.Sprintf(" related %s:%d", relatedAddr, relatedAddress.Port)
|
||||||
relatedAddressAddress := relatedAddress.Address
|
|
||||||
if !isPrivate && len(relatedAddressAddress) > minIPTruncateLen {
|
|
||||||
relatedAddressAddress = relatedAddressAddress[:len(relatedAddressAddress)-3] + "..."
|
|
||||||
}
|
|
||||||
cStr += " " + fmt.Sprintf(" related %s:%d", relatedAddressAddress, relatedAddress.Port)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
candidates = append(candidates, cStr)
|
candidates = append(candidates, cStr)
|
||||||
|
|||||||
@@ -150,10 +150,10 @@ func (w wrappedICECandidatePairLogger) MarshalLogObject(e zapcore.ObjectEncoder)
|
|||||||
if w.pair.Remote != nil {
|
if w.pair.Remote != nil {
|
||||||
e.AddString("remoteProtocol", w.pair.Remote.Protocol.String())
|
e.AddString("remoteProtocol", w.pair.Remote.Protocol.String())
|
||||||
e.AddString("remoteCandidateType", w.pair.Remote.Typ.String())
|
e.AddString("remoteCandidateType", w.pair.Remote.Typ.String())
|
||||||
e.AddString("remoteAdddress", w.pair.Remote.Address[:len(w.pair.Remote.Address)-3]+"...")
|
e.AddString("remoteAdddress", MaybeTruncateIP(w.pair.Remote.Address))
|
||||||
e.AddUint16("remotePort", w.pair.Remote.Port)
|
e.AddUint16("remotePort", w.pair.Remote.Port)
|
||||||
if w.pair.Remote.RelatedAddress != "" {
|
if w.pair.Remote.RelatedAddress != "" {
|
||||||
e.AddString("relatedAdddress", w.pair.Remote.RelatedAddress[:len(w.pair.Remote.RelatedAddress)-3]+"...")
|
e.AddString("relatedAdddress", MaybeTruncateIP(w.pair.Remote.RelatedAddress))
|
||||||
e.AddUint16("relatedPort", w.pair.Remote.RelatedPort)
|
e.AddUint16("relatedPort", w.pair.Remote.RelatedPort)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
@@ -28,6 +29,8 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
trackIdSeparator = "|"
|
trackIdSeparator = "|"
|
||||||
|
|
||||||
|
cMinIPTruncateLen = 8
|
||||||
)
|
)
|
||||||
|
|
||||||
func UnpackStreamID(packed string) (participantID livekit.ParticipantID, trackID livekit.TrackID) {
|
func UnpackStreamID(packed string) (participantID livekit.ParticipantID, trackID livekit.TrackID) {
|
||||||
@@ -198,3 +201,16 @@ func LoggerWithCodecMime(l logger.Logger, mime string) logger.Logger {
|
|||||||
}
|
}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MaybeTruncateIP(addr string) string {
|
||||||
|
ipAddr := net.ParseIP(addr)
|
||||||
|
if ipAddr == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
if ipAddr.IsPrivate() || len(addr) <= cMinIPTruncateLen {
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
||||||
|
return addr[:len(addr)-3] + "..."
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user