mirror of
https://github.com/livekit/livekit.git
synced 2026-10-23 13:02:53 +00:00
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:
1 parent
24f3c93204
commit
da9bd7f426
3 files changed
+22
-23
No files matched your search
+4
-21
@@ -18,7 +18,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -55,8 +54,6 @@ const (
|
||||
dataForwardLoadBalanceThreshold = 20
|
||||
|
||||
simulateDisconnectSignalTimeout = 5 * time.Second
|
||||
|
||||
minIPTruncateLen = 8
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -1840,25 +1837,11 @@ func connectionDetailsFields(infos []*types.ICEConnectionInfo) []interface{} {
|
||||
if c.Trickle {
|
||||
cStr += "[trickle]"
|
||||
}
|
||||
remoteAddress := c.Remote.Address()
|
||||
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())
|
||||
cStr += " " + fmt.Sprintf("%s %s %s:%d", c.Remote.NetworkType(), c.Remote.Type(), MaybeTruncateIP(c.Remote.Address()), c.Remote.Port())
|
||||
if relatedAddress := c.Remote.RelatedAddress(); relatedAddress != nil {
|
||||
ipAddr = net.ParseIP(relatedAddress.Address)
|
||||
if ipAddr != nil {
|
||||
isPrivate = ipAddr.IsPrivate()
|
||||
relatedAddressAddress := relatedAddress.Address
|
||||
if !isPrivate && len(relatedAddressAddress) > minIPTruncateLen {
|
||||
relatedAddressAddress = relatedAddressAddress[:len(relatedAddressAddress)-3] + "..."
|
||||
}
|
||||
cStr += " " + fmt.Sprintf(" related %s:%d", relatedAddressAddress, relatedAddress.Port)
|
||||
relatedAddr := MaybeTruncateIP(relatedAddress.Address)
|
||||
if relatedAddr != "" {
|
||||
cStr += " " + fmt.Sprintf(" related %s:%d", relatedAddr, relatedAddress.Port)
|
||||
}
|
||||
}
|
||||
candidates = append(candidates, cStr)
|
||||
|
||||
@@ -150,10 +150,10 @@ func (w wrappedICECandidatePairLogger) MarshalLogObject(e zapcore.ObjectEncoder)
|
||||
if w.pair.Remote != nil {
|
||||
e.AddString("remoteProtocol", w.pair.Remote.Protocol.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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/pion/webrtc/v3"
|
||||
@@ -28,6 +29,8 @@ import (
|
||||
|
||||
const (
|
||||
trackIdSeparator = "|"
|
||||
|
||||
cMinIPTruncateLen = 8
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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