Skip to content

Commit d941132

Browse files
committed
add authMethods option; fix #27
1 parent 03f020e commit d941132

File tree

4 files changed

+206
-167
lines changed

4 files changed

+206
-167
lines changed

conf.go

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"regexp"
8+
"time"
9+
10+
"github.com/aler9/gortsplib"
11+
"gopkg.in/yaml.v2"
12+
)
13+
14+
type ConfPath struct {
15+
Source string `yaml:"source"`
16+
SourceProtocol string `yaml:"sourceProtocol"`
17+
PublishUser string `yaml:"publishUser"`
18+
PublishPass string `yaml:"publishPass"`
19+
PublishIps []string `yaml:"publishIps"`
20+
publishIpsParsed []interface{}
21+
ReadUser string `yaml:"readUser"`
22+
ReadPass string `yaml:"readPass"`
23+
ReadIps []string `yaml:"readIps"`
24+
readIpsParsed []interface{}
25+
}
26+
27+
type conf struct {
28+
Protocols []string `yaml:"protocols"`
29+
protocolsParsed map[streamProtocol]struct{}
30+
RtspPort int `yaml:"rtspPort"`
31+
RtpPort int `yaml:"rtpPort"`
32+
RtcpPort int `yaml:"rtcpPort"`
33+
PreScript string `yaml:"preScript"`
34+
PostScript string `yaml:"postScript"`
35+
ReadTimeout time.Duration `yaml:"readTimeout"`
36+
WriteTimeout time.Duration `yaml:"writeTimeout"`
37+
AuthMethods []string `yaml:"authMethods"`
38+
authMethodsParsed []gortsplib.AuthMethod
39+
Pprof bool `yaml:"pprof"`
40+
Paths map[string]*ConfPath `yaml:"paths"`
41+
}
42+
43+
func loadConf(fpath string, stdin io.Reader) (*conf, error) {
44+
conf := &conf{}
45+
46+
err := func() error {
47+
if fpath == "stdin" {
48+
err := yaml.NewDecoder(stdin).Decode(conf)
49+
if err != nil {
50+
return err
51+
}
52+
53+
return nil
54+
55+
} else {
56+
// conf.yml is optional
57+
if fpath == "conf.yml" {
58+
if _, err := os.Stat(fpath); err != nil {
59+
return nil
60+
}
61+
}
62+
63+
f, err := os.Open(fpath)
64+
if err != nil {
65+
return err
66+
}
67+
defer f.Close()
68+
69+
err = yaml.NewDecoder(f).Decode(conf)
70+
if err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}
76+
}()
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
if len(conf.Protocols) == 0 {
82+
conf.Protocols = []string{"udp", "tcp"}
83+
}
84+
conf.protocolsParsed = make(map[streamProtocol]struct{})
85+
for _, proto := range conf.Protocols {
86+
switch proto {
87+
case "udp":
88+
conf.protocolsParsed[_STREAM_PROTOCOL_UDP] = struct{}{}
89+
90+
case "tcp":
91+
conf.protocolsParsed[_STREAM_PROTOCOL_TCP] = struct{}{}
92+
93+
default:
94+
return nil, fmt.Errorf("unsupported protocol: %s", proto)
95+
}
96+
}
97+
if len(conf.protocolsParsed) == 0 {
98+
return nil, fmt.Errorf("no protocols provided")
99+
}
100+
101+
if conf.RtspPort == 0 {
102+
conf.RtspPort = 8554
103+
}
104+
if conf.RtpPort == 0 {
105+
conf.RtpPort = 8000
106+
}
107+
if (conf.RtpPort % 2) != 0 {
108+
return nil, fmt.Errorf("rtp port must be even")
109+
}
110+
if conf.RtcpPort == 0 {
111+
conf.RtcpPort = 8001
112+
}
113+
if conf.RtcpPort != (conf.RtpPort + 1) {
114+
return nil, fmt.Errorf("rtcp and rtp ports must be consecutive")
115+
}
116+
117+
if conf.ReadTimeout == 0 {
118+
conf.ReadTimeout = 5 * time.Second
119+
}
120+
if conf.WriteTimeout == 0 {
121+
conf.WriteTimeout = 5 * time.Second
122+
}
123+
124+
if len(conf.AuthMethods) == 0 {
125+
conf.AuthMethods = []string{"basic", "digest"}
126+
}
127+
for _, method := range conf.AuthMethods {
128+
switch method {
129+
case "basic":
130+
conf.authMethodsParsed = append(conf.authMethodsParsed, gortsplib.Basic)
131+
132+
case "digest":
133+
conf.authMethodsParsed = append(conf.authMethodsParsed, gortsplib.Digest)
134+
135+
default:
136+
return nil, fmt.Errorf("unsupported authentication method: %s", method)
137+
}
138+
}
139+
140+
if len(conf.Paths) == 0 {
141+
conf.Paths = map[string]*ConfPath{
142+
"all": {},
143+
}
144+
}
145+
146+
for path, pconf := range conf.Paths {
147+
if pconf.Source == "" {
148+
pconf.Source = "record"
149+
}
150+
151+
if pconf.PublishUser != "" {
152+
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.PublishUser) {
153+
return nil, fmt.Errorf("publish username must be alphanumeric")
154+
}
155+
}
156+
if pconf.PublishPass != "" {
157+
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.PublishPass) {
158+
return nil, fmt.Errorf("publish password must be alphanumeric")
159+
}
160+
}
161+
pconf.publishIpsParsed, err = parseIpCidrList(pconf.PublishIps)
162+
if err != nil {
163+
return nil, err
164+
}
165+
166+
if pconf.ReadUser != "" && pconf.ReadPass == "" || pconf.ReadUser == "" && pconf.ReadPass != "" {
167+
return nil, fmt.Errorf("read username and password must be both filled")
168+
}
169+
if pconf.ReadUser != "" {
170+
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.ReadUser) {
171+
return nil, fmt.Errorf("read username must be alphanumeric")
172+
}
173+
}
174+
if pconf.ReadPass != "" {
175+
if !regexp.MustCompile("^[a-zA-Z0-9]+$").MatchString(pconf.ReadPass) {
176+
return nil, fmt.Errorf("read password must be alphanumeric")
177+
}
178+
}
179+
if pconf.ReadUser != "" && pconf.ReadPass == "" || pconf.ReadUser == "" && pconf.ReadPass != "" {
180+
return nil, fmt.Errorf("read username and password must be both filled")
181+
}
182+
pconf.readIpsParsed, err = parseIpCidrList(pconf.ReadIps)
183+
if err != nil {
184+
return nil, err
185+
}
186+
187+
if pconf.Source != "record" {
188+
if path == "all" {
189+
return nil, fmt.Errorf("path 'all' cannot have a RTSP source")
190+
}
191+
192+
if pconf.SourceProtocol == "" {
193+
pconf.SourceProtocol = "udp"
194+
}
195+
}
196+
}
197+
198+
return conf, nil
199+
}

conf.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ postScript:
1515
readTimeout: 5s
1616
# timeout of write operations
1717
writeTimeout: 5s
18+
# supported authentication methods
19+
authMethods: [basic, digest]
1820
# enable pprof on port 9999 to monitor performance
1921
pprof: false
2022

0 commit comments

Comments
 (0)