Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: allow to edit properties of path config "all" (#2067) #2075

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func loadFromFile(fpath string, conf *Conf) (bool, error) {
return true, nil
}

func contains(list []headers.AuthMethod, item headers.AuthMethod) bool {
for _, i := range list {
if i == item {
return true
}
}
return false
}

// Conf is a configuration.
type Conf struct {
// general
Expand Down Expand Up @@ -197,15 +206,6 @@ func (conf Conf) Clone() *Conf {
return &dest
}

func contains(list []headers.AuthMethod, item headers.AuthMethod) bool {
for _, i := range list {
if i == item {
return true
}
}
return false
}

// Check checks the configuration for errors.
func (conf *Conf) Check() error {
// general
Expand Down Expand Up @@ -266,12 +266,6 @@ func (conf *Conf) Check() error {
conf.Paths = make(map[string]*PathConf)
}

// "all" is an alias for "~^.*$"
if _, ok := conf.Paths["all"]; ok {
conf.Paths["~^.*$"] = conf.Paths["all"]
delete(conf.Paths, "all")
}

for _, name := range getSortedKeys(conf.Paths) {
pconf := conf.Paths[name]
if pconf == nil {
Expand Down
10 changes: 6 additions & 4 deletions internal/conf/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@ type PathConf struct {
}

func (pconf *PathConf) check(conf *Conf, name string) error {
// normal path
if name == "" || name[0] != '~' {
switch {
case name == "all":
pconf.Regexp = regexp.MustCompile("^.*$")

case name == "" || name[0] != '~': // normal path
err := IsValidPathName(name)
if err != nil {
return fmt.Errorf("invalid path name '%s': %s", name, err)
}

// regular expression path
} else {
default: // regular expression-based path
pathRegexp, err := regexp.Compile(name[1:])
if err != nil {
return fmt.Errorf("invalid regular expression: %s", name[1:])
Expand Down
2 changes: 1 addition & 1 deletion internal/core/hls_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *hlsHTTPServer) onRequest(ctx *gin.Context) {

user, pass, hasCredentials := ctx.Request.BasicAuth()

res := s.pathManager.getPathConf(pathGetPathConfReq{
res := s.pathManager.getConfForPath(pathGetConfForPathReq{
name: dir,
publish: false,
credentials: authCredentials{
Expand Down
6 changes: 3 additions & 3 deletions internal/core/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ type pathPublisherRemoveReq struct {
res chan struct{}
}

type pathGetPathConfRes struct {
type pathGetConfForPathRes struct {
conf *conf.PathConf
err error
}

type pathGetPathConfReq struct {
type pathGetConfForPathReq struct {
name string
publish bool
credentials authCredentials
res chan pathGetPathConfRes
res chan pathGetConfForPathRes
}

type pathDescribeRes struct {
Expand Down
78 changes: 39 additions & 39 deletions internal/core/path_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ func pathConfCanBeUpdated(oldPathConf *conf.PathConf, newPathConf *conf.PathConf
return newPathConf.Equal(clone)
}

func getConfForPath(pathConfs map[string]*conf.PathConf, name string) (string, *conf.PathConf, []string, error) {
err := conf.IsValidPathName(name)
if err != nil {
return "", nil, nil, fmt.Errorf("invalid path name: %s (%s)", err, name)
}

// normal path
if pathConf, ok := pathConfs[name]; ok {
return name, pathConf, nil, nil
}

// regular expression-based path
for pathConfName, pathConf := range pathConfs {
if pathConf.Regexp != nil {
m := pathConf.Regexp.FindStringSubmatch(name)
if m != nil {
return pathConfName, pathConf, m, nil
}
}
}

return "", nil, nil, fmt.Errorf("path '%s' is not configured", name)
}

type pathManagerHLSManager interface {
pathSourceReady(*path)
pathSourceNotReady(*path)
Expand Down Expand Up @@ -64,7 +88,7 @@ type pathManager struct {
chPathClose chan *path
chPathSourceReady chan *path
chPathSourceNotReady chan *path
chPathGetPathConf chan pathGetPathConfReq
chGetConfForPath chan pathGetConfForPathReq
chDescribe chan pathDescribeReq
chReaderAdd chan pathReaderAddReq
chPublisherAdd chan pathPublisherAddReq
Expand Down Expand Up @@ -108,7 +132,7 @@ func newPathManager(
chPathClose: make(chan *path),
chPathSourceReady: make(chan *path),
chPathSourceNotReady: make(chan *path),
chPathGetPathConf: make(chan pathGetPathConfReq),
chGetConfForPath: make(chan pathGetConfForPathReq),
chDescribe: make(chan pathDescribeReq),
chReaderAdd: make(chan pathReaderAddReq),
chPublisherAdd: make(chan pathPublisherAddReq),
Expand Down Expand Up @@ -204,24 +228,24 @@ outer:
pm.hlsManager.pathSourceNotReady(pa)
}

case req := <-pm.chPathGetPathConf:
_, pathConf, _, err := pm.getPathConfInternal(req.name)
case req := <-pm.chGetConfForPath:
_, pathConf, _, err := getConfForPath(pm.pathConfs, req.name)
if err != nil {
req.res <- pathGetPathConfRes{err: err}
req.res <- pathGetConfForPathRes{err: err}
continue
}

err = authenticate(pm.externalAuthenticationURL, pm.authMethods,
req.name, pathConf, req.publish, req.credentials)
if err != nil {
req.res <- pathGetPathConfRes{err: pathErrAuth{wrapped: err}}
req.res <- pathGetConfForPathRes{err: pathErrAuth{wrapped: err}}
continue
}

req.res <- pathGetPathConfRes{conf: pathConf}
req.res <- pathGetConfForPathRes{conf: pathConf}

case req := <-pm.chDescribe:
pathConfName, pathConf, pathMatches, err := pm.getPathConfInternal(req.pathName)
pathConfName, pathConf, pathMatches, err := getConfForPath(pm.pathConfs, req.pathName)
if err != nil {
req.res <- pathDescribeRes{err: err}
continue
Expand All @@ -241,7 +265,7 @@ outer:
req.res <- pathDescribeRes{path: pm.paths[req.pathName]}

case req := <-pm.chReaderAdd:
pathConfName, pathConf, pathMatches, err := pm.getPathConfInternal(req.pathName)
pathConfName, pathConf, pathMatches, err := getConfForPath(pm.pathConfs, req.pathName)
if err != nil {
req.res <- pathReaderSetupPlayRes{err: err}
continue
Expand All @@ -263,7 +287,7 @@ outer:
req.res <- pathReaderSetupPlayRes{path: pm.paths[req.pathName]}

case req := <-pm.chPublisherAdd:
pathConfName, pathConf, pathMatches, err := pm.getPathConfInternal(req.pathName)
pathConfName, pathConf, pathMatches, err := getConfForPath(pm.pathConfs, req.pathName)
if err != nil {
req.res <- pathPublisherAnnounceRes{err: err}
continue
Expand Down Expand Up @@ -354,30 +378,6 @@ func (pm *pathManager) removePath(pa *path) {
delete(pm.paths, pa.name)
}

func (pm *pathManager) getPathConfInternal(name string) (string, *conf.PathConf, []string, error) {
err := conf.IsValidPathName(name)
if err != nil {
return "", nil, nil, fmt.Errorf("invalid path name: %s (%s)", err, name)
}

// normal path
if pathConf, ok := pm.pathConfs[name]; ok {
return name, pathConf, nil, nil
}

// regular expression path
for pathConfName, pathConf := range pm.pathConfs {
if pathConf.Regexp != nil {
m := pathConf.Regexp.FindStringSubmatch(name)
if m != nil {
return pathConfName, pathConf, m, nil
}
}
}

return "", nil, nil, fmt.Errorf("path '%s' is not configured", name)
}

// confReload is called by core.
func (pm *pathManager) confReload(pathConfs map[string]*conf.PathConf) {
select {
Expand Down Expand Up @@ -413,15 +413,15 @@ func (pm *pathManager) onPathClose(pa *path) {
}
}

// getPathConf is called by a reader or publisher.
func (pm *pathManager) getPathConf(req pathGetPathConfReq) pathGetPathConfRes {
req.res = make(chan pathGetPathConfRes)
// getConfForPath is called by a reader or publisher.
func (pm *pathManager) getConfForPath(req pathGetConfForPathReq) pathGetConfForPathRes {
req.res = make(chan pathGetConfForPathRes)
select {
case pm.chPathGetPathConf <- req:
case pm.chGetConfForPath <- req:
return <-req.res

case <-pm.ctx.Done():
return pathGetPathConfRes{err: fmt.Errorf("terminated")}
return pathGetConfForPathRes{err: fmt.Errorf("terminated")}
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/webrtc_http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (s *webRTCHTTPServer) onRequest(ctx *gin.Context) {

user, pass, hasCredentials := ctx.Request.BasicAuth()

res := s.pathManager.getPathConf(pathGetPathConfReq{
res := s.pathManager.getConfForPath(pathGetConfForPathReq{
name: dir,
publish: publish,
credentials: authCredentials{
Expand Down
8 changes: 4 additions & 4 deletions mediamtx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ webrtcICETCPMuxAddress:
# Path parameters

# These settings are path-dependent, and the map key is the name of the path.
# It's possible to use regular expressions by using a tilde as prefix.
# For example, "~^(test1|test2)$" will match both "test1" and "test2".
# For example, "~^prefix" will match all paths that start with "prefix".
# The settings under the path "all" are applied to all paths that do not match
# It's possible to use regular expressions by using a tilde as prefix,
# for example "~^(test1|test2)$" will match both "test1" and "test2",
# for example "~^prefix" will match all paths that start with "prefix".
# Settings under the path "all" are applied to all paths that do not match
# another entry.
paths:
all:
Expand Down