mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Add locks to safelog
The safelog Write function can be called from multiple go routines, and it was not thread safe. These locks in particular allow us to pass the logscrubber's output io.Writer to other libraries, such as pion.
This commit is contained in:
parent
f3be34a459
commit
3c28380bc6
1 changed files with 9 additions and 0 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const ipv4Address = `\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`
|
||||
|
@ -30,8 +31,13 @@ var addressRegexp = regexp.MustCompile(addressPattern)
|
|||
type LogScrubber struct {
|
||||
Output io.Writer
|
||||
buffer []byte
|
||||
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (ls *LogScrubber) Lock() { (*ls).lock.Lock() }
|
||||
func (ls *LogScrubber) Unlock() { (*ls).lock.Unlock() }
|
||||
|
||||
func scrub(b []byte) []byte {
|
||||
scrubbedBytes := b
|
||||
for _, pattern := range scrubberPatterns {
|
||||
|
@ -45,6 +51,9 @@ func scrub(b []byte) []byte {
|
|||
}
|
||||
|
||||
func (ls *LogScrubber) Write(b []byte) (n int, err error) {
|
||||
ls.Lock()
|
||||
defer ls.Unlock()
|
||||
|
||||
n = len(b)
|
||||
ls.buffer = append(ls.buffer, b...)
|
||||
for {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue