mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-15 04:40:59 +08:00
Add support auth for RTSP server
This commit is contained in:
@@ -15,6 +15,8 @@ func Init() {
|
|||||||
var conf struct {
|
var conf struct {
|
||||||
Mod struct {
|
Mod struct {
|
||||||
Listen string `yaml:"listen"`
|
Listen string `yaml:"listen"`
|
||||||
|
Username string `yaml:"username"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
} `yaml:"rtsp"`
|
} `yaml:"rtsp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +54,12 @@ func Init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go tcpHandler(conn)
|
|
||||||
|
c := rtsp.NewServer(conn)
|
||||||
|
if conf.Mod.Username != "" {
|
||||||
|
c.Auth(conf.Mod.Username, conf.Mod.Password)
|
||||||
|
}
|
||||||
|
go tcpHandler(c)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
@@ -121,13 +128,12 @@ func rtspHandler(url string) (streamer.Producer, error) {
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpHandler(c net.Conn) {
|
func tcpHandler(conn *rtsp.Conn) {
|
||||||
var name string
|
var name string
|
||||||
var closer func()
|
var closer func()
|
||||||
|
|
||||||
trace := log.Trace().Enabled()
|
trace := log.Trace().Enabled()
|
||||||
|
|
||||||
conn := rtsp.NewServer(c)
|
|
||||||
conn.Listen(func(msg interface{}) {
|
conn.Listen(func(msg interface{}) {
|
||||||
if trace {
|
if trace {
|
||||||
switch msg := msg.(type) {
|
switch msg := msg.(type) {
|
||||||
|
@@ -99,6 +99,11 @@ func NewServer(conn net.Conn) *Conn {
|
|||||||
return c
|
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) {
|
func (c *Conn) parseURI() (err error) {
|
||||||
c.URL, err = url.Parse(c.uri)
|
c.URL, err = url.Parse(c.uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -490,6 +495,17 @@ func (c *Conn) Accept() error {
|
|||||||
|
|
||||||
c.Fire(req)
|
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
|
// Receiver: OPTIONS > DESCRIBE > SETUP... > PLAY > TEARDOWN
|
||||||
// Sender: OPTIONS > ANNOUNCE > SETUP... > RECORD > TEARDOWN
|
// Sender: OPTIONS > ANNOUNCE > SETUP... > RECORD > TEARDOWN
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
|
@@ -80,6 +80,24 @@ func (a *Auth) Write(req *Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Auth) Validate(req *Request) bool {
|
||||||
|
if a == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
header := req.Header.Get("Authorization")
|
||||||
|
if header == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Method == AuthUnknown {
|
||||||
|
a.Method = AuthBasic
|
||||||
|
a.header = "Basic " + B64(a.user, a.pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
return header == a.header
|
||||||
|
}
|
||||||
|
|
||||||
func Between(s, sub1, sub2 string) string {
|
func Between(s, sub1, sub2 string) string {
|
||||||
i := strings.Index(s, sub1)
|
i := strings.Index(s, sub1)
|
||||||
if i < 0 {
|
if i < 0 {
|
||||||
|
Reference in New Issue
Block a user