[eventsub] Fix: Clean IPs from eventsub-socket read errors

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-14 00:08:50 +01:00
parent 5d0a5322a5
commit eec4966b82
Signed by: luzifer
GPG Key ID: D91C3E91E4CAD6F5
5 changed files with 36 additions and 38 deletions

View File

@ -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(), ""))
}

View File

@ -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(), ""))
}

View File

@ -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{}{}

View File

@ -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")
}
}()

View File

@ -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)