mediamtx/conf.go

320 lines
9.0 KiB
Go
Raw Normal View History

2020-07-12 11:16:33 +00:00
package main
import (
"fmt"
"io"
"net/url"
2020-07-12 11:16:33 +00:00
"os"
"regexp"
"time"
"github.com/aler9/gortsplib"
"gopkg.in/yaml.v2"
)
2020-09-19 15:13:45 +00:00
type pathConf struct {
regexp *regexp.Regexp
2020-07-30 11:43:10 +00:00
Source string `yaml:"source"`
sourceUrl *url.URL ``
SourceProtocol string `yaml:"sourceProtocol"`
sourceProtocolParsed gortsplib.StreamProtocol ``
SourceOnDemand bool `yaml:"sourceOnDemand"`
2020-07-31 16:12:42 +00:00
RunOnInit string `yaml:"runOnInit"`
2020-07-30 11:43:10 +00:00
RunOnDemand string `yaml:"runOnDemand"`
RunOnPublish string `yaml:"runOnPublish"`
RunOnRead string `yaml:"runOnRead"`
PublishUser string `yaml:"publishUser"`
PublishPass string `yaml:"publishPass"`
PublishIps []string `yaml:"publishIps"`
publishIpsParsed []interface{} ``
ReadUser string `yaml:"readUser"`
ReadPass string `yaml:"readPass"`
ReadIps []string `yaml:"readIps"`
readIpsParsed []interface{} ``
2020-07-12 11:16:33 +00:00
}
type conf struct {
Protocols []string `yaml:"protocols"`
protocolsParsed map[gortsplib.StreamProtocol]struct{} ``
RtspPort int `yaml:"rtspPort"`
RtpPort int `yaml:"rtpPort"`
RtcpPort int `yaml:"rtcpPort"`
RunOnConnect string `yaml:"runOnConnect"`
ReadTimeout time.Duration `yaml:"readTimeout"`
WriteTimeout time.Duration `yaml:"writeTimeout"`
AuthMethods []string `yaml:"authMethods"`
authMethodsParsed []gortsplib.AuthMethod ``
Metrics bool `yaml:"metrics"`
Pprof bool `yaml:"pprof"`
LogDestinations []string `yaml:"logDestinations"`
logDestinationsParsed map[logDestination]struct{} ``
LogFile string `yaml:"logFile"`
2020-09-19 15:13:45 +00:00
Paths map[string]*pathConf `yaml:"paths"`
2020-07-12 11:16:33 +00:00
}
func loadConf(fpath string, stdin io.Reader) (*conf, error) {
conf := &conf{}
err := func() error {
if fpath == "stdin" {
err := yaml.NewDecoder(stdin).Decode(conf)
if err != nil {
return err
}
return nil
} else {
// rtsp-simple-server.yml is optional
if fpath == "rtsp-simple-server.yml" {
2020-07-12 11:16:33 +00:00
if _, err := os.Stat(fpath); err != nil {
return nil
}
}
f, err := os.Open(fpath)
if err != nil {
return err
}
defer f.Close()
err = yaml.NewDecoder(f).Decode(conf)
if err != nil {
return err
}
return nil
}
}()
if err != nil {
return nil, err
}
if len(conf.Protocols) == 0 {
conf.Protocols = []string{"udp", "tcp"}
}
2020-07-19 15:54:31 +00:00
conf.protocolsParsed = make(map[gortsplib.StreamProtocol]struct{})
2020-07-12 11:16:33 +00:00
for _, proto := range conf.Protocols {
switch proto {
case "udp":
2020-09-05 11:19:55 +00:00
conf.protocolsParsed[gortsplib.StreamProtocolUDP] = struct{}{}
2020-07-12 11:16:33 +00:00
case "tcp":
2020-09-05 11:19:55 +00:00
conf.protocolsParsed[gortsplib.StreamProtocolTCP] = struct{}{}
2020-07-12 11:16:33 +00:00
default:
return nil, fmt.Errorf("unsupported protocol: %s", proto)
}
}
if len(conf.protocolsParsed) == 0 {
return nil, fmt.Errorf("no protocols provided")
}
if conf.RtspPort == 0 {
conf.RtspPort = 8554
}
if conf.RtpPort == 0 {
conf.RtpPort = 8000
}
if (conf.RtpPort % 2) != 0 {
return nil, fmt.Errorf("rtp port must be even")
}
if conf.RtcpPort == 0 {
conf.RtcpPort = 8001
}
if conf.RtcpPort != (conf.RtpPort + 1) {
return nil, fmt.Errorf("rtcp and rtp ports must be consecutive")
}
if conf.ReadTimeout == 0 {
2020-07-19 16:43:59 +00:00
conf.ReadTimeout = 10 * time.Second
2020-07-12 11:16:33 +00:00
}
if conf.WriteTimeout == 0 {
conf.WriteTimeout = 5 * time.Second
}
if len(conf.AuthMethods) == 0 {
conf.AuthMethods = []string{"basic", "digest"}
}
for _, method := range conf.AuthMethods {
switch method {
case "basic":
conf.authMethodsParsed = append(conf.authMethodsParsed, gortsplib.Basic)
case "digest":
conf.authMethodsParsed = append(conf.authMethodsParsed, gortsplib.Digest)
default:
return nil, fmt.Errorf("unsupported authentication method: %s", method)
}
}
if len(conf.LogDestinations) == 0 {
conf.LogDestinations = []string{"stdout"}
}
conf.logDestinationsParsed = make(map[logDestination]struct{})
for _, dest := range conf.LogDestinations {
switch dest {
case "stdout":
conf.logDestinationsParsed[logDestinationStdout] = struct{}{}
case "file":
conf.logDestinationsParsed[logDestinationFile] = struct{}{}
2020-09-19 12:48:29 +00:00
case "syslog":
conf.logDestinationsParsed[logDestinationSyslog] = struct{}{}
default:
return nil, fmt.Errorf("unsupported log destination: %s", dest)
}
}
if conf.LogFile == "" {
conf.LogFile = "rtsp-simple-server.log"
}
2020-07-12 11:16:33 +00:00
if len(conf.Paths) == 0 {
2020-09-19 15:13:45 +00:00
conf.Paths = map[string]*pathConf{
2020-07-12 11:16:33 +00:00
"all": {},
}
}
2020-09-19 15:13:45 +00:00
// "all" is an alias for "~^.*$"
if _, ok := conf.Paths["all"]; ok {
conf.Paths["~^.*$"] = conf.Paths["all"]
delete(conf.Paths, "all")
}
for name, pconf := range conf.Paths {
if pconf == nil {
conf.Paths[name] = &pathConf{}
pconf = conf.Paths[name]
}
2020-09-19 15:13:45 +00:00
if name == "" {
return nil, fmt.Errorf("path name can not be empty")
}
// normal path
if name[0] != '~' {
err := checkPathName(name)
if err != nil {
return nil, fmt.Errorf("invalid path name: %s (%s)", err, name)
}
// regular expression path
} else {
pathRegexp, err := regexp.Compile(name[1:])
if err != nil {
return nil, fmt.Errorf("invalid regular expression: %s", name[1:])
}
pconf.regexp = pathRegexp
2020-08-05 10:17:04 +00:00
}
2020-09-19 15:13:45 +00:00
if pconf.Source == "" {
pconf.Source = "record"
2020-07-12 11:16:33 +00:00
}
2020-09-19 15:13:45 +00:00
if pconf.Source != "record" {
if pconf.regexp != nil {
2020-09-19 21:52:06 +00:00
return nil, fmt.Errorf("a path with a regular expression (or path 'all') cannot have a RTSP source; use another path")
2020-07-30 11:31:18 +00:00
}
2020-09-19 15:13:45 +00:00
pconf.sourceUrl, err = url.Parse(pconf.Source)
2020-07-30 11:31:18 +00:00
if err != nil {
2020-09-19 15:13:45 +00:00
return nil, fmt.Errorf("'%s' is not a valid RTSP url", pconf.Source)
2020-07-30 11:31:18 +00:00
}
2020-09-19 15:13:45 +00:00
if pconf.sourceUrl.Scheme != "rtsp" {
return nil, fmt.Errorf("'%s' is not a valid RTSP url", pconf.Source)
2020-07-30 11:31:18 +00:00
}
2020-09-19 15:13:45 +00:00
if pconf.sourceUrl.Port() == "" {
pconf.sourceUrl.Host += ":554"
2020-07-30 11:31:18 +00:00
}
2020-09-19 15:13:45 +00:00
if pconf.sourceUrl.User != nil {
pass, _ := pconf.sourceUrl.User.Password()
user := pconf.sourceUrl.User.Username()
2020-07-30 11:31:18 +00:00
if user != "" && pass == "" ||
user == "" && pass != "" {
fmt.Errorf("username and password must be both provided")
}
}
2020-09-19 15:13:45 +00:00
if pconf.SourceProtocol == "" {
pconf.SourceProtocol = "udp"
2020-08-30 11:31:46 +00:00
}
2020-09-19 15:13:45 +00:00
switch pconf.SourceProtocol {
2020-07-30 11:31:18 +00:00
case "udp":
2020-09-19 15:13:45 +00:00
pconf.sourceProtocolParsed = gortsplib.StreamProtocolUDP
2020-07-30 11:31:18 +00:00
case "tcp":
2020-09-19 15:13:45 +00:00
pconf.sourceProtocolParsed = gortsplib.StreamProtocolTCP
2020-07-30 11:31:18 +00:00
default:
2020-09-19 15:13:45 +00:00
return nil, fmt.Errorf("unsupported protocol '%s'", pconf.SourceProtocol)
2020-07-30 11:31:18 +00:00
}
}
2020-09-19 15:13:45 +00:00
if pconf.PublishUser != "" {
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.PublishUser) {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("publish username must be alphanumeric")
}
}
2020-09-19 15:13:45 +00:00
if pconf.PublishPass != "" {
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.PublishPass) {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("publish password must be alphanumeric")
}
}
2020-09-19 15:13:45 +00:00
pconf.publishIpsParsed, err = parseIpCidrList(pconf.PublishIps)
2020-07-12 11:16:33 +00:00
if err != nil {
return nil, err
}
2020-09-19 15:13:45 +00:00
if pconf.ReadUser != "" && pconf.ReadPass == "" || pconf.ReadUser == "" && pconf.ReadPass != "" {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("read username and password must be both filled")
}
2020-09-19 15:13:45 +00:00
if pconf.ReadUser != "" {
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.ReadUser) {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("read username must be alphanumeric")
}
}
2020-09-19 15:13:45 +00:00
if pconf.ReadPass != "" {
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.ReadPass) {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("read password must be alphanumeric")
}
}
2020-09-19 15:13:45 +00:00
if pconf.ReadUser != "" && pconf.ReadPass == "" || pconf.ReadUser == "" && pconf.ReadPass != "" {
2020-07-12 11:16:33 +00:00
return nil, fmt.Errorf("read username and password must be both filled")
}
2020-09-19 15:13:45 +00:00
pconf.readIpsParsed, err = parseIpCidrList(pconf.ReadIps)
2020-07-12 11:16:33 +00:00
if err != nil {
return nil, err
}
2020-09-19 15:13:45 +00:00
if pconf.regexp != nil && pconf.RunOnInit != "" {
return nil, fmt.Errorf("a path with a regular expression does not support option 'runOnInit'; use another path")
}
2020-07-12 11:16:33 +00:00
}
return conf, nil
}
2020-09-19 15:13:45 +00:00
func (conf *conf) checkPathNameAndFindConf(name string) (*pathConf, error) {
err := checkPathName(name)
if err != nil {
return nil, fmt.Errorf("invalid path name: %s (%s)", err, name)
}
// normal path
if pconf, ok := conf.Paths[name]; ok {
return pconf, nil
}
// regular expression path
for _, pconf := range conf.Paths {
if pconf.regexp != nil && pconf.regexp.MatchString(name) {
return pconf, nil
}
}
return nil, fmt.Errorf("unable to find a valid configuration for path '%s'", name)
}