Skip to content

Commit 8b4df5f

Browse files
committed
Code refactoring for #1841
1 parent 4577390 commit 8b4df5f

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

internal/api/ws/ws.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"strings"
99
"sync"
1010
"time"
11-
"regexp"
1211

1312
"github.com/AlexxIT/go2rtc/internal/api"
1413
"github.com/AlexxIT/go2rtc/internal/app"
14+
"github.com/AlexxIT/go2rtc/pkg/core"
1515
"github.com/gorilla/websocket"
1616
"github.com/rs/zerolog"
1717
)
@@ -133,10 +133,7 @@ func apiWS(w http.ResponseWriter, r *http.Request) {
133133
if handler := wsHandlers[msg.Type]; handler != nil {
134134
go func() {
135135
if err = handler(tr, msg); err != nil {
136-
// Some streams such as ffmpeg might return credentials on error messages
137-
errMsg := err.Error()
138-
sanitizer := regexp.MustCompile(`(\w+)://(.*)@`)
139-
errMsg = sanitizer.ReplaceAllString(errMsg, "$1://******@")
136+
errMsg := core.StripUserinfo(err.Error())
140137
tr.Write(&Message{Type: "error", Value: msg.Type + ": " + errMsg})
141138
}
142139
}()

pkg/core/core_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,17 @@ func TestName(t *testing.T) {
118118
// stage3
119119
_ = prod2.Stop()
120120
}
121+
122+
func TestStripUserinfo(t *testing.T) {
123+
s := `streams:
124+
test:
125+
- ffmpeg:rtsp://username:[email protected]:554/stream1
126+
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
127+
`
128+
s = StripUserinfo(s)
129+
require.Equal(t, `streams:
130+
test:
131+
- ffmpeg:rtsp://***@10.1.2.3:554/stream1
132+
- ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy
133+
`, s)
134+
}

pkg/core/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package core
22

33
import (
44
"crypto/rand"
5+
"regexp"
56
"runtime"
67
"strconv"
78
"strings"
@@ -77,3 +78,14 @@ func Caller() string {
7778
_, file, line, _ := runtime.Caller(1)
7879
return file + ":" + strconv.Itoa(line)
7980
}
81+
82+
const (
83+
unreserved = `A-Za-z0-9-._~`
84+
subdelims = `!$&'()*+,;=`
85+
userinfo = unreserved + subdelims + `%:`
86+
)
87+
88+
func StripUserinfo(s string) string {
89+
sanitizer := regexp.MustCompile(`://[` + userinfo + `]+@`)
90+
return sanitizer.ReplaceAllString(s, `://***@`)
91+
}

0 commit comments

Comments
 (0)