Add support auth for RTSP server

This commit is contained in:
Alexey Khit
2022-11-27 10:08:37 +03:00
parent 5678121c50
commit 1b518b94fd
3 changed files with 44 additions and 4 deletions
+16
View File
@@ -99,6 +99,11 @@ func NewServer(conn net.Conn) *Conn {
return c
}
func (c *Conn) Auth(username, password string) {
info := url.UserPassword(username, password)
c.auth = tcp.NewAuth(info)
}
func (c *Conn) parseURI() (err error) {
c.URL, err = url.Parse(c.uri)
if err != nil {
@@ -490,6 +495,17 @@ func (c *Conn) Accept() error {
c.Fire(req)
if !c.auth.Validate(req) {
res := &tcp.Response{
Status: "401 Unauthorized",
Header: map[string][]string{"Www-Authenticate": {`Basic realm="go2rtc"`}},
}
if err = c.Response(res); err != nil {
return err
}
continue
}
// Receiver: OPTIONS > DESCRIBE > SETUP... > PLAY > TEARDOWN
// Sender: OPTIONS > ANNOUNCE > SETUP... > RECORD > TEARDOWN
switch req.Method {