This is an automated email from the git hooks/post-receive script.
meskio pushed a commit to branch main in repository pluggable-transports/snowflake.
commit 1d592b06e51b42ca4ed13ec219f012a2915c2b1d Author: meskio meskio@torproject.org AuthorDate: Fri May 20 09:43:03 2022 +0200
Implement String() method on events
To make it safe for logging safelog.Scrub function is now public.
Closes: #40141 --- client/pt_event_logger.go | 67 +---------------------------------------------- common/event/interface.go | 36 ++++++++++++++++++++++++- common/safelog/log.go | 4 +-- 3 files changed, 38 insertions(+), 69 deletions(-)
diff --git a/client/pt_event_logger.go b/client/pt_event_logger.go index aae8716..483a3c6 100644 --- a/client/pt_event_logger.go +++ b/client/pt_event_logger.go @@ -1,12 +1,8 @@ package main
import ( - "bytes" - "fmt" pt "git.torproject.org/pluggable-transports/goptlib.git" "git.torproject.org/pluggable-transports/snowflake.git/v2/common/event" - "git.torproject.org/pluggable-transports/snowflake.git/v2/common/safelog" - "strings" )
func NewPTEventLogger() event.SnowflakeEventReceiver { @@ -16,67 +12,6 @@ func NewPTEventLogger() event.SnowflakeEventReceiver { type ptEventLogger struct { }
-type logSeverity int - -const ( - Debug logSeverity = iota - Info - Notice - Warning - Error -) - -func safePTLog(severity logSeverity, format string, a ...interface{}) { - var buff bytes.Buffer - scrubber := &safelog.LogScrubber{Output: &buff} - - // make sure logString ends with exactly one "\n" so it's not stuck in scrubber.Write()'s internal buffer - logString := strings.TrimRight(fmt.Sprintf(format, a...), "\n") + "\n" - scrubber.Write([]byte(logString)) - - // remove newline before calling pt.Log because it adds a newline - msg := strings.TrimRight(buff.String(), "\n") - - switch severity { - case Error: - pt.Log(pt.LogSeverityError, msg) - case Warning: - pt.Log(pt.LogSeverityWarning, msg) - case Notice: - pt.Log(pt.LogSeverityWarning, msg) - case Info: - pt.Log(pt.LogSeverityInfo, msg) - case Debug: - pt.Log(pt.LogSeverityDebug, msg) - default: - pt.Log(pt.LogSeverityNotice, msg) - } -} - func (p ptEventLogger) OnNewSnowflakeEvent(e event.SnowflakeEvent) { - switch e.(type) { - case event.EventOnOfferCreated: - e := e.(event.EventOnOfferCreated) - if e.Error != nil { - safePTLog(Notice, "offer creation failure %v", e.Error.Error()) - } else { - safePTLog(Notice, "offer created") - } - - case event.EventOnBrokerRendezvous: - e := e.(event.EventOnBrokerRendezvous) - if e.Error != nil { - safePTLog(Notice, "broker failure %v", e.Error.Error()) - } else { - safePTLog(Notice, "broker rendezvous peer received") - } - - case event.EventOnSnowflakeConnected: - safePTLog(Notice, "connected") - - case event.EventOnSnowflakeConnectionFailed: - e := e.(event.EventOnSnowflakeConnectionFailed) - safePTLog(Notice, "trying a new proxy: %v", e.Error.Error()) - } - + pt.Log(pt.LogSeverityNotice, e.String()) } diff --git a/common/event/interface.go b/common/event/interface.go index b41d7c3..968b270 100644 --- a/common/event/interface.go +++ b/common/event/interface.go @@ -1,6 +1,11 @@ package event
-import "github.com/pion/webrtc/v3" +import ( + "fmt" + + "git.torproject.org/pluggable-transports/snowflake.git/v2/common/safelog" + "github.com/pion/webrtc/v3" +)
type SnowflakeEvent interface { IsSnowflakeEvent() @@ -13,27 +18,56 @@ type EventOnOfferCreated struct { Error error }
+func (e EventOnOfferCreated) String() string { + if e.Error != nil { + scrubbed := safelog.Scrub([]byte(e.Error.Error())) + return fmt.Sprintf("offer creation failure %s", scrubbed) + } + return "offer created" +} + type EventOnBrokerRendezvous struct { SnowflakeEvent WebRTCRemoteDescription *webrtc.SessionDescription Error error }
+func (e EventOnBrokerRendezvous) String() string { + if e.Error != nil { + scrubbed := safelog.Scrub([]byte(e.Error.Error())) + return fmt.Sprintf("broker failure %s", scrubbed) + } + return "broker rendezvous peer received" +} + type EventOnSnowflakeConnected struct { SnowflakeEvent }
+func (e EventOnSnowflakeConnected) String() string { + return "connected" +} + type EventOnSnowflakeConnectionFailed struct { SnowflakeEvent Error error }
+func (e EventOnSnowflakeConnectionFailed) String() string { + scrubbed := safelog.Scrub([]byte(e.Error.Error())) + return fmt.Sprintf("trying a new proxy: %s", scrubbed) +} + type EventOnProxyConnectionOver struct { SnowflakeEvent InboundTraffic int OutboundTraffic int }
+func (e EventOnProxyConnectionOver) String() string { + return fmt.Sprintf("Proxy connection closed (↑ %d, ↓ %d)", e.InboundTraffic, e.OutboundTraffic) +} + type SnowflakeEventReceiver interface { // OnNewSnowflakeEvent notify receiver about a new event // This method MUST not block diff --git a/common/safelog/log.go b/common/safelog/log.go index 4a135ce..6ca23ee 100644 --- a/common/safelog/log.go +++ b/common/safelog/log.go @@ -38,7 +38,7 @@ type LogScrubber struct { func (ls *LogScrubber) Lock() { (*ls).lock.Lock() } func (ls *LogScrubber) Unlock() { (*ls).lock.Unlock() }
-func scrub(b []byte) []byte { +func Scrub(b []byte) []byte { scrubbedBytes := b for _, pattern := range scrubberPatterns { // this is a workaround since go does not yet support look ahead or look @@ -62,7 +62,7 @@ func (ls *LogScrubber) Write(b []byte) (n int, err error) { return } fullLines := ls.buffer[:i+1] - _, err = ls.Output.Write(scrub(fullLines)) + _, err = ls.Output.Write(Scrub(fullLines)) if err != nil { return }