From c0b0b23c41fffb33e5f6ca9ca320bee65f0fbc96 Mon Sep 17 00:00:00 2001 From: Liwei Date: Wed, 5 Aug 2020 04:11:16 +0800 Subject: [PATCH 1/2] expose request path to runOn commands as an environment variable --- client.go | 6 ++++++ main.go | 5 ++++- path.go | 3 +++ rtsp-simple-server.yml | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 994f6be2..e762b364 100644 --- a/client.go +++ b/client.go @@ -829,6 +829,9 @@ func (c *client) runPlay(path string) { var onReadCmd *exec.Cmd if confp.RunOnRead != "" { onReadCmd = exec.Command("/bin/sh", "-c", confp.RunOnRead) + onReadCmd.Env = append(os.Environ(), + "RTSP_SERVER_PATH="+path, + ) onReadCmd.Stdout = os.Stdout onReadCmd.Stderr = os.Stderr err := onReadCmd.Start() @@ -928,6 +931,9 @@ func (c *client) runRecord(path string) { var onPublishCmd *exec.Cmd if confp.RunOnPublish != "" { onPublishCmd = exec.Command("/bin/sh", "-c", confp.RunOnPublish) + onPublishCmd.Env = append(os.Environ(), + "RTSP_SERVER_PATH="+path, + ) onPublishCmd.Stdout = os.Stdout onPublishCmd.Stderr = os.Stderr err := onPublishCmd.Start() diff --git a/main.go b/main.go index 1a86ea5d..fe6c254a 100644 --- a/main.go +++ b/main.go @@ -260,9 +260,12 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { return nil, err } - for _, confp := range conf.Paths { + for path, confp := range conf.Paths { if confp.RunOnInit != "" { onInitCmd := exec.Command("/bin/sh", "-c", confp.RunOnInit) + onInitCmd.Env = append(os.Environ(), + "RTSP_SERVER_PATH="+path, + ) onInitCmd.Stdout = os.Stdout onInitCmd.Stderr = os.Stderr err := onInitCmd.Start() diff --git a/path.go b/path.go index cf07be9d..2326e371 100644 --- a/path.go +++ b/path.go @@ -122,6 +122,9 @@ func (pa *path) describe(client *client) { pa.lastActivation = time.Now() pa.onDemandCmd = exec.Command("/bin/sh", "-c", pa.confp.RunOnDemand) + pa.onDemandCmd.Env = append(os.Environ(), + "RTSP_SERVER_PATH="+pa.id, + ) pa.onDemandCmd.Stdout = os.Stdout pa.onDemandCmd.Stderr = os.Stderr err := pa.onDemandCmd.Start() diff --git a/rtsp-simple-server.yml b/rtsp-simple-server.yml index 67db2919..d30a0229 100644 --- a/rtsp-simple-server.yml +++ b/rtsp-simple-server.yml @@ -43,19 +43,26 @@ paths: # command to run when this path is loaded by the program. # this can be used, for example, to publish a stream and keep it always opened. # This is terminated with SIGINT when the program closes. + # The path is available as an environment variable $RTSP_SERVER_PATH runOnInit: # command to run when this path is requested. # This can be used, for example, to publish a stream on demand. # This is terminated with SIGINT when the path is not requested anymore. + # The actual path from the request (useful for wildcard paths) is available as an + # environment variable $RTSP_SERVER_PATH runOnDemand: # command to run when a client starts publishing. # This is terminated with SIGINT when a client stops publishing. + # The actual path from the client (useful for wildcard paths) is available as an + # environment variable $RTSP_SERVER_PATH runOnPublish: # command to run when a clients starts reading. # This is terminated with SIGINT when a client stops reading. + # The actual path from the client (useful for wildcard paths) is available as an + # environment variable $RTSP_SERVER_PATH runOnRead: # username required to publish From 3a508b1d42f975e2e0ee2cdd19904baf059a8b33 Mon Sep 17 00:00:00 2001 From: Liwei Date: Wed, 5 Aug 2020 04:44:48 +0800 Subject: [PATCH 2/2] add configuration item to specify name of variable used to pass path --- client.go | 4 ++-- conf.go | 9 +++++++++ main.go | 2 +- path.go | 2 +- rtsp-simple-server.yml | 10 ++++++---- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index e762b364..1491d4f9 100644 --- a/client.go +++ b/client.go @@ -830,7 +830,7 @@ func (c *client) runPlay(path string) { if confp.RunOnRead != "" { onReadCmd = exec.Command("/bin/sh", "-c", confp.RunOnRead) onReadCmd.Env = append(os.Environ(), - "RTSP_SERVER_PATH="+path, + c.p.conf.PathEnvVariable+"="+path, ) onReadCmd.Stdout = os.Stdout onReadCmd.Stderr = os.Stderr @@ -932,7 +932,7 @@ func (c *client) runRecord(path string) { if confp.RunOnPublish != "" { onPublishCmd = exec.Command("/bin/sh", "-c", confp.RunOnPublish) onPublishCmd.Env = append(os.Environ(), - "RTSP_SERVER_PATH="+path, + c.p.conf.PathEnvVariable+"="+path, ) onPublishCmd.Stdout = os.Stdout onPublishCmd.Stderr = os.Stderr diff --git a/conf.go b/conf.go index 991706b5..db7e8dc3 100644 --- a/conf.go +++ b/conf.go @@ -41,6 +41,7 @@ type conf struct { RunOnConnect string `yaml:"runOnConnect"` ReadTimeout time.Duration `yaml:"readTimeout"` WriteTimeout time.Duration `yaml:"writeTimeout"` + PathEnvVariable string `yaml:"pathEnvVariable"` AuthMethods []string `yaml:"authMethods"` authMethodsParsed []gortsplib.AuthMethod `` Metrics bool `yaml:"metrics"` @@ -132,6 +133,14 @@ func loadConf(fpath string, stdin io.Reader) (*conf, error) { conf.WriteTimeout = 5 * time.Second } + if conf.PathEnvVariable == "" { + conf.PathEnvVariable = "RTSP_SERVER_PATH" + } + re := regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$") + if !re.MatchString(conf.PathEnvVariable) { + return nil, fmt.Errorf("pathEnvVariable must consist of only alphanumerics and underscores, and should not begin with a digit") + } + if len(conf.AuthMethods) == 0 { conf.AuthMethods = []string{"basic", "digest"} } diff --git a/main.go b/main.go index fe6c254a..5f71d304 100644 --- a/main.go +++ b/main.go @@ -264,7 +264,7 @@ func newProgram(args []string, stdin io.Reader) (*program, error) { if confp.RunOnInit != "" { onInitCmd := exec.Command("/bin/sh", "-c", confp.RunOnInit) onInitCmd.Env = append(os.Environ(), - "RTSP_SERVER_PATH="+path, + conf.PathEnvVariable+"="+path, ) onInitCmd.Stdout = os.Stdout onInitCmd.Stderr = os.Stderr diff --git a/path.go b/path.go index 2326e371..caa22f1f 100644 --- a/path.go +++ b/path.go @@ -123,7 +123,7 @@ func (pa *path) describe(client *client) { pa.lastActivation = time.Now() pa.onDemandCmd = exec.Command("/bin/sh", "-c", pa.confp.RunOnDemand) pa.onDemandCmd.Env = append(os.Environ(), - "RTSP_SERVER_PATH="+pa.id, + pa.p.conf.PathEnvVariable+"="+pa.id, ) pa.onDemandCmd.Stdout = os.Stdout pa.onDemandCmd.Stderr = os.Stderr diff --git a/rtsp-simple-server.yml b/rtsp-simple-server.yml index d30a0229..3e887e08 100644 --- a/rtsp-simple-server.yml +++ b/rtsp-simple-server.yml @@ -14,6 +14,8 @@ runOnConnect: readTimeout: 10s # timeout of write operations writeTimeout: 5s +# name of environment variable used to pass the path to runOnInit/Demand/Publish/Read +pathEnvVariable: RTSP_SERVER_PATH # supported authentication methods # WARNING: both methods are insecure, use RTSP inside a VPN to enforce security. authMethods: [basic, digest] @@ -43,26 +45,26 @@ paths: # command to run when this path is loaded by the program. # this can be used, for example, to publish a stream and keep it always opened. # This is terminated with SIGINT when the program closes. - # The path is available as an environment variable $RTSP_SERVER_PATH + # The path is available as an environment variable configured by pathEnvVariable runOnInit: # command to run when this path is requested. # This can be used, for example, to publish a stream on demand. # This is terminated with SIGINT when the path is not requested anymore. # The actual path from the request (useful for wildcard paths) is available as an - # environment variable $RTSP_SERVER_PATH + # environment variable configured by pathEnvVariable runOnDemand: # command to run when a client starts publishing. # This is terminated with SIGINT when a client stops publishing. # The actual path from the client (useful for wildcard paths) is available as an - # environment variable $RTSP_SERVER_PATH + # environment variable configured by pathEnvVariable runOnPublish: # command to run when a clients starts reading. # This is terminated with SIGINT when a client stops reading. # The actual path from the client (useful for wildcard paths) is available as an - # environment variable $RTSP_SERVER_PATH + # environment variable configured by pathEnvVariable runOnRead: # username required to publish