2021-07-24 13:55:42 +00:00
|
|
|
package core
|
2019-12-31 12:48:17 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-10 20:56:46 +00:00
|
|
|
"errors"
|
2019-12-31 12:48:17 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
2020-06-21 09:30:30 +00:00
|
|
|
"time"
|
2019-12-31 12:48:17 +00:00
|
|
|
|
2020-01-20 09:21:05 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2020-11-15 16:56:54 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/auth"
|
|
|
|
"github.com/aler9/gortsplib/pkg/base"
|
|
|
|
"github.com/aler9/gortsplib/pkg/headers"
|
2021-03-21 10:22:49 +00:00
|
|
|
"github.com/aler9/gortsplib/pkg/liberrors"
|
2020-10-13 22:02:55 +00:00
|
|
|
|
2021-09-26 21:06:40 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
2020-11-01 21:56:56 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/externalcmd"
|
2020-12-08 11:21:06 +00:00
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
2019-12-31 12:48:17 +00:00
|
|
|
)
|
|
|
|
|
2020-06-21 09:30:30 +00:00
|
|
|
const (
|
2021-07-24 13:55:42 +00:00
|
|
|
rtspConnPauseAfterAuthError = 2 * time.Second
|
2020-06-21 09:30:30 +00:00
|
|
|
)
|
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
func isTeardownErr(err error) bool {
|
|
|
|
_, ok := err.(liberrors.ErrServerSessionTeardown)
|
|
|
|
return ok
|
2021-01-31 22:11:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:24:00 +00:00
|
|
|
func isTerminatedErr(err error) bool {
|
|
|
|
_, ok := err.(liberrors.ErrServerTerminated)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspConnParent interface {
|
2020-12-08 11:21:06 +00:00
|
|
|
Log(logger.Level, string, ...interface{})
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
type rtspConn struct {
|
2021-04-24 16:25:19 +00:00
|
|
|
rtspAddress string
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods []headers.AuthMethod
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration
|
2020-10-31 15:36:09 +00:00
|
|
|
runOnConnect string
|
|
|
|
runOnConnectRestart bool
|
2021-07-30 18:13:17 +00:00
|
|
|
pathManager *pathManager
|
2020-12-06 17:01:10 +00:00
|
|
|
conn *gortsplib.ServerConn
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspConnParent
|
2020-10-19 20:17:48 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
onConnectCmd *externalcmd.Cmd
|
2021-03-22 20:40:07 +00:00
|
|
|
authUser string
|
|
|
|
authPass string
|
|
|
|
authValidator *auth.Validator
|
|
|
|
authFailures int
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func newRTSPConn(
|
2021-04-24 16:25:19 +00:00
|
|
|
rtspAddress string,
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods []headers.AuthMethod,
|
2021-09-26 21:06:40 +00:00
|
|
|
readTimeout conf.StringDuration,
|
2020-10-19 20:17:48 +00:00
|
|
|
runOnConnect string,
|
2020-10-31 15:36:09 +00:00
|
|
|
runOnConnectRestart bool,
|
2021-07-30 18:13:17 +00:00
|
|
|
pathManager *pathManager,
|
2020-12-06 17:01:10 +00:00
|
|
|
conn *gortsplib.ServerConn,
|
2021-07-24 13:55:42 +00:00
|
|
|
parent rtspConnParent) *rtspConn {
|
|
|
|
c := &rtspConn{
|
2021-04-24 16:25:19 +00:00
|
|
|
rtspAddress: rtspAddress,
|
2021-08-01 14:56:53 +00:00
|
|
|
authMethods: authMethods,
|
2020-10-31 15:36:09 +00:00
|
|
|
readTimeout: readTimeout,
|
|
|
|
runOnConnect: runOnConnect,
|
|
|
|
runOnConnectRestart: runOnConnectRestart,
|
2021-07-30 12:49:09 +00:00
|
|
|
pathManager: pathManager,
|
2020-12-06 17:01:10 +00:00
|
|
|
conn: conn,
|
|
|
|
parent: parent,
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 12:41:18 +00:00
|
|
|
c.log(logger.Info, "opened")
|
2020-10-14 17:35:21 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
if c.runOnConnect != "" {
|
2021-10-03 13:46:06 +00:00
|
|
|
c.log(logger.Info, "runOnConnect command started")
|
2021-05-07 21:07:31 +00:00
|
|
|
_, port, _ := net.SplitHostPort(c.rtspAddress)
|
|
|
|
c.onConnectCmd = externalcmd.New(c.runOnConnect, c.runOnConnectRestart, externalcmd.Environment{
|
|
|
|
Path: "",
|
|
|
|
Port: port,
|
|
|
|
})
|
|
|
|
}
|
2021-01-31 20:42:27 +00:00
|
|
|
|
2020-10-19 20:17:48 +00:00
|
|
|
return c
|
2019-12-31 12:48:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (c *rtspConn) log(level logger.Level, format string, args ...interface{}) {
|
2021-05-09 12:41:18 +00:00
|
|
|
c.parent.Log(level, "[conn %v] "+format, append([]interface{}{c.conn.NetConn().RemoteAddr()}, args...)...)
|
2020-10-19 20:17:48 +00:00
|
|
|
}
|
2020-07-29 21:30:42 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
// Conn returns the RTSP connection.
|
2021-07-24 13:55:42 +00:00
|
|
|
func (c *rtspConn) Conn() *gortsplib.ServerConn {
|
2021-05-07 21:07:31 +00:00
|
|
|
return c.conn
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
func (c *rtspConn) ip() net.IP {
|
2020-05-03 21:26:41 +00:00
|
|
|
return c.conn.NetConn().RemoteAddr().(*net.TCPAddr).IP
|
|
|
|
}
|
|
|
|
|
2021-07-29 13:11:02 +00:00
|
|
|
func (c *rtspConn) validateCredentials(
|
2021-09-27 13:45:51 +00:00
|
|
|
pathUser conf.Credential,
|
|
|
|
pathPass conf.Credential,
|
2021-05-07 21:07:31 +00:00
|
|
|
pathName string,
|
|
|
|
req *base.Request,
|
|
|
|
) error {
|
|
|
|
// reset authValidator every time the credentials change
|
2021-09-27 13:45:51 +00:00
|
|
|
if c.authValidator == nil || c.authUser != string(pathUser) || c.authPass != string(pathPass) {
|
|
|
|
c.authUser = string(pathUser)
|
|
|
|
c.authPass = string(pathPass)
|
|
|
|
c.authValidator = auth.NewValidator(string(pathUser), string(pathPass), c.authMethods)
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VLC strips the control attribute
|
|
|
|
// provide an alternative URL without the control attribute
|
|
|
|
altURL := func() *base.URL {
|
|
|
|
if req.Method != base.Setup {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &base.URL{
|
|
|
|
Scheme: req.URL.Scheme,
|
|
|
|
Host: req.URL.Host,
|
|
|
|
Path: "/" + pathName + "/",
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-05-30 10:57:23 +00:00
|
|
|
err := c.authValidator.ValidateRequest(req, altURL)
|
2021-05-07 21:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.authFailures++
|
|
|
|
|
|
|
|
// vlc with login prompt sends 4 requests:
|
|
|
|
// 1) without credentials
|
|
|
|
// 2) with password but without username
|
|
|
|
// 3) without credentials
|
|
|
|
// 4) with password and username
|
|
|
|
// therefore we must allow up to 3 failures
|
|
|
|
if c.authFailures > 3 {
|
2021-07-31 18:46:06 +00:00
|
|
|
return pathErrAuthCritical{
|
2021-05-07 21:07:31 +00:00
|
|
|
Message: "unauthorized: " + err.Error(),
|
|
|
|
Response: &base.Response{
|
2020-12-13 12:58:56 +00:00
|
|
|
StatusCode: base.StatusUnauthorized,
|
2021-05-07 21:07:31 +00:00
|
|
|
},
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2021-05-07 21:07:31 +00:00
|
|
|
}
|
2020-12-13 12:58:56 +00:00
|
|
|
|
2021-05-07 21:07:31 +00:00
|
|
|
if c.authFailures > 1 {
|
|
|
|
c.log(logger.Debug, "WARN: unauthorized: %s", err)
|
2020-12-13 12:58:56 +00:00
|
|
|
}
|
2021-05-07 21:07:31 +00:00
|
|
|
|
2021-07-31 18:46:06 +00:00
|
|
|
return pathErrAuthNotCritical{
|
2021-05-16 14:25:22 +00:00
|
|
|
Response: &base.Response{
|
|
|
|
StatusCode: base.StatusUnauthorized,
|
|
|
|
Header: base.Header{
|
2021-05-30 10:57:23 +00:00
|
|
|
"WWW-Authenticate": c.authValidator.Header(),
|
2021-05-16 14:25:22 +00:00
|
|
|
},
|
2021-05-07 21:07:31 +00:00
|
|
|
},
|
2021-05-16 14:25:22 +00:00
|
|
|
}
|
2020-07-13 09:12:20 +00:00
|
|
|
}
|
2020-12-13 12:58:56 +00:00
|
|
|
|
|
|
|
// login successful, reset authFailures
|
|
|
|
c.authFailures = 0
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-30 18:13:17 +00:00
|
|
|
|
2021-08-12 08:50:29 +00:00
|
|
|
// OnClose is called by rtspServer.
|
|
|
|
func (c *rtspConn) OnClose(err error) {
|
|
|
|
if err != io.EOF && !isTeardownErr(err) && !isTerminatedErr(err) {
|
|
|
|
c.log(logger.Info, "ERR: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log(logger.Info, "closed")
|
|
|
|
|
|
|
|
if c.onConnectCmd != nil {
|
|
|
|
c.onConnectCmd.Close()
|
2021-10-03 13:46:06 +00:00
|
|
|
c.log(logger.Info, "runOnConnect command stopped")
|
2021-08-12 08:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 18:13:17 +00:00
|
|
|
// OnRequest is called by rtspServer.
|
|
|
|
func (c *rtspConn) OnRequest(req *base.Request) {
|
|
|
|
c.log(logger.Debug, "[c->s] %v", req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnResponse is called by rtspServer.
|
|
|
|
func (c *rtspConn) OnResponse(res *base.Response) {
|
|
|
|
c.log(logger.Debug, "[s->c] %v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnDescribe is called by rtspServer.
|
2021-09-09 21:05:54 +00:00
|
|
|
func (c *rtspConn) OnDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx,
|
|
|
|
) (*base.Response, *gortsplib.ServerStream, error) {
|
2021-07-31 18:46:06 +00:00
|
|
|
res := c.pathManager.OnDescribe(pathDescribeReq{
|
2021-07-30 18:13:17 +00:00
|
|
|
PathName: ctx.Path,
|
|
|
|
URL: ctx.Req.URL,
|
|
|
|
IP: c.ip(),
|
2021-09-27 13:45:51 +00:00
|
|
|
ValidateCredentials: func(pathUser conf.Credential, pathPass conf.Credential) error {
|
2021-08-01 14:56:53 +00:00
|
|
|
return c.validateCredentials(pathUser, pathPass, ctx.Path, ctx.Req)
|
2021-07-30 18:13:17 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if res.Err != nil {
|
|
|
|
switch terr := res.Err.(type) {
|
2021-07-31 18:46:06 +00:00
|
|
|
case pathErrAuthNotCritical:
|
2021-07-30 18:13:17 +00:00
|
|
|
return terr.Response, nil, nil
|
|
|
|
|
2021-07-31 18:46:06 +00:00
|
|
|
case pathErrAuthCritical:
|
2021-07-30 18:13:17 +00:00
|
|
|
// wait some seconds to stop brute force attacks
|
|
|
|
<-time.After(rtspConnPauseAfterAuthError)
|
|
|
|
|
|
|
|
return terr.Response, nil, errors.New(terr.Message)
|
|
|
|
|
2021-07-31 18:46:06 +00:00
|
|
|
case pathErrNoOnePublishing:
|
2021-07-30 18:13:17 +00:00
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusNotFound,
|
|
|
|
}, nil, res.Err
|
|
|
|
|
|
|
|
default:
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusBadRequest,
|
|
|
|
}, nil, res.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Redirect != "" {
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusMovedPermanently,
|
|
|
|
Header: base.Header{
|
|
|
|
"Location": base.HeaderValue{res.Redirect},
|
|
|
|
},
|
|
|
|
}, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &base.Response{
|
|
|
|
StatusCode: base.StatusOK,
|
2021-08-10 16:34:10 +00:00
|
|
|
}, res.Stream.rtspStream, nil
|
2021-07-30 18:13:17 +00:00
|
|
|
}
|