From eec4966b82cd6b2173c1e4be1f5562dd31c535aa Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Thu, 14 Dec 2023 00:08:50 +0100 Subject: [PATCH] [eventsub] Fix: Clean IPs from eventsub-socket read errors Signed-off-by: Knut Ahlers --- internal/helpers/cleanErrorIPs.go | 33 +++++++++++++++++++++++++ internal/helpers/operr.go | 35 --------------------------- main.go | 2 +- pkg/twitch/eventsubWebsocketClient.go | 2 +- twitchWatcher.go | 2 +- 5 files changed, 36 insertions(+), 38 deletions(-) create mode 100644 internal/helpers/cleanErrorIPs.go delete mode 100644 internal/helpers/operr.go diff --git a/internal/helpers/cleanErrorIPs.go b/internal/helpers/cleanErrorIPs.go new file mode 100644 index 0000000..56439ef --- /dev/null +++ b/internal/helpers/cleanErrorIPs.go @@ -0,0 +1,33 @@ +package helpers + +import ( + "errors" + "net" + "regexp" +) + +var networkArrowErrorPart = regexp.MustCompile(` (?:(?:[0-9]+\.){3}[0-9]+:[0-9]+(?:->)?)+`) + +// CleanNetworkAddressFromError checks whether an IP:Port->IP:port +// information is contained in the error. This is checked by explicitly +// sanitizing *net.OpError instances or by returning a sanitized error +// string without the stack previously present. +// +// As of the loss of information this is only intended to clean up +// logging and not be used in error returns. +func CleanNetworkAddressFromError(err error) error { + if opE, ok := err.(*net.OpError); ok { + // Error in the outmost position is an OpError, lets just patch it + opE.Source = nil + opE.Addr = nil + return opE + } + + if networkArrowErrorPart.FindStringIndex(err.Error()) == nil { + // There is no network address somewhere inside, keep the error as is + return err + } + + // Patch out IP information and create an new error with its message + return errors.New(networkArrowErrorPart.ReplaceAllString(err.Error(), "")) +} diff --git a/internal/helpers/operr.go b/internal/helpers/operr.go deleted file mode 100644 index 5ac52c3..0000000 --- a/internal/helpers/operr.go +++ /dev/null @@ -1,35 +0,0 @@ -package helpers - -import ( - "errors" - "net" - "regexp" -) - -// CleanOpError checks whether a *net.OpError is included in the error -// and if so removes the included address information. This can happen -// in two ways: If the passed error is indeed an OpError the address -// info is just patched out. If the OpError is buried deeper inside -// the wrapped error stack, a new error with patched message is created -// sacrificing the wrapping and possible included stacktrace. -// -// As of the loss of information this is only intended to clean up -// logging and not be used in error returns. -func CleanOpError(err error) error { - if opE, ok := err.(*net.OpError); ok { - // Error in the outmost position is an OpError, lets just patch it - opE.Source = nil - opE.Addr = nil - return opE - } - - var opE *net.OpError - if !errors.As(err, &opE) { - // There is no OpError somewhere inside, keep the error as is - return err - } - - // Patch out IP information and create an new error with its message - return errors.New(regexp.MustCompile(` (?:(?:[0-9]+\.){3}[0-9]+:[0-9]+(?:->)?)+`). - ReplaceAllString(err.Error(), "")) -} diff --git a/main.go b/main.go index ffdd594..67a2395 100644 --- a/main.go +++ b/main.go @@ -307,7 +307,7 @@ func main() { go func() { log.Info("(re-)connecting IRC client") if err := ircHdl.Run(); err != nil { - log.WithError(helpers.CleanOpError(err)).Error("IRC run exited unexpectedly") + log.WithError(helpers.CleanNetworkAddressFromError(err)).Error("IRC run exited unexpectedly") } time.Sleep(ircReconnectDelay) ircDisconnected <- struct{}{} diff --git a/pkg/twitch/eventsubWebsocketClient.go b/pkg/twitch/eventsubWebsocketClient.go index 0424740..a922463 100644 --- a/pkg/twitch/eventsubWebsocketClient.go +++ b/pkg/twitch/eventsubWebsocketClient.go @@ -199,7 +199,7 @@ func (e *EventSubSocketClient) Run() error { defer func() { if err := e.conn.Close(); err != nil { - e.logger.WithError(helpers.CleanOpError(err)).Error("finally closing socket") + e.logger.WithError(helpers.CleanNetworkAddressFromError(err)).Error("finally closing socket") } }() diff --git a/twitchWatcher.go b/twitchWatcher.go index 2c2da11..7384945 100644 --- a/twitchWatcher.go +++ b/twitchWatcher.go @@ -433,7 +433,7 @@ func (t *twitchWatcher) updateChannelFromAPI(channel string) error { log.WithField("channel", channel).Info("watching for eventsub events") go func(storedStatus *twitchChannelState) { if err := storedStatus.esc.Run(); err != nil { - log.WithField("channel", channel).WithError(helpers.CleanOpError(err)).Error("eventsub client caused error") + log.WithField("channel", channel).WithError(helpers.CleanNetworkAddressFromError(err)).Error("eventsub client caused error") } storedStatus.CloseESC() }(storedStatus)