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:
Raja Subramanian
2024-10-30 19:44:41 +05:30
committed by GitHub
parent 24f3c93204
commit da9bd7f426
3 changed files with 22 additions and 23 deletions
+16
View File
@@ -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] + "..."
}