Skip to content

Commit

Permalink
add flag and config parsing, add static stream auth
Browse files Browse the repository at this point in the history
  • Loading branch information
iSchluff committed Nov 16, 2020
1 parent 2f25141 commit a4af775
Show file tree
Hide file tree
Showing 16 changed files with 1,091 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
srtrelay
config.toml
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,40 @@ Streaming-Relay for the SRT-protocol

**EXPERIMENTAL AT BEST, use at your own risk**

## Dependencies
**Debian 10**:
- apt install libsrt1-openssl

**Gentoo**:
- emerge net-libs/srt

## Build
```
go build
```

## Usage
```bash
# relay
./srtrelay

# publisher
ffmpeg -i test.mp4 -c copy -f mpegts srt://localhost:8090?streamid=test/publish
ffmpeg -i test.mp4 -c copy -f mpegts srt://localhost:1337?streamid=publish/test

# subscriber
ffplay srt://localhost:8090?streamid=test/play
ffplay srt://localhost:1337?streamid=play/test
```

## Dependencies
### Commandline Flags
```bash
# List available flags
./srtrelay -h
```

**Debian 10**:
- apt install libsrt1-openssl
### Configuration
Please take a look at [config.toml.example](config.toml.example) to learn more about configuring srtrelay.

## Build
```
go build
```
The configuration file can be placed under *config.toml* in the current working directory, at */etc/srtrelay/config.toml* or at a custom location specified via the *-config* flag.

## Design Ideas
- Just a 1:n relay, one publisher (push), multiple subscribers (pull)
Expand Down
7 changes: 7 additions & 0 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package auth

import "github.com/voc/srtrelay/stream"

type Authenticator interface {
Authenticate(stream.StreamID) bool
}
23 changes: 23 additions & 0 deletions auth/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package auth

import "github.com/voc/srtrelay/stream"

type httpAuth struct {
url string
}

type HttpAuthConfig struct {
URL string
}

func NewHttpAuth(config HttpAuthConfig) *httpAuth {
return &httpAuth{
url: config.URL,
}
}

// Implement Authenticator
func (h *httpAuth) Authenticate(streamid stream.StreamID) bool {
// TODO: implement post request
return false
}
32 changes: 32 additions & 0 deletions auth/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package auth

import (
"log"

"github.com/voc/srtrelay/stream"
)

type staticAuth struct {
allow []string
}

type StaticAuthConfig struct {
Allow []string
}

func NewStaticAuth(config StaticAuthConfig) *staticAuth {
return &staticAuth{
allow: config.Allow,
}
}

// Implement Authenticator
func (auth *staticAuth) Authenticate(streamid stream.StreamID) bool {
for _, allowed := range auth.allow {
log.Println("test", streamid, allowed)
if streamid.Match(allowed) {
return true
}
}
return false
}
37 changes: 37 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[app]
# Relay listen address
#address = "127.0.0.1"

# Relay listen port
#port = 1337

# SRT protocol latency in ms
# This should be a multiple of your expected RTT because SRT needs some time
# to send and receive acknowledgements and retransmits to be effective.
#latency = 300

# Relay buffer size in bytes, 384000 -> 1 second @ 3Mbits
# This determines the maximum delay tolerance for connected clients.
# Clients which are more than buffersize behind the live edge are disconnected.
#buffersize = 384000

[auth]
# Choose between available auth types (static and http)
# for further config options see below
#type = "static"

[auth.static]
# Streams are authenticated using a static list of allowed streamids
# Each pattern is matched to the client streamid
# in the form of <mode>/<stream-name>/<password>
# Allows using * as wildcard (will match across slashes)
#allow = ["*", "publish/foo/bar", "play/*"]

[auth.http]
# Streams are authenticated using HTTP callbacks
# nginx-rtmp on_publish/on_subscribe compatible http auth
#url = "http://localhost/auth"

# static
# the full url with be <url>/<app>/<stream-name>
#app = "stream"
111 changes: 111 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package config

import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"

"github.com/pelletier/go-toml"
"github.com/voc/srtrelay/auth"
)

type Config struct {
App AppConfig
Auth AuthConfig
}

type AppConfig struct {
Address string
Port uint
Latency uint
Buffersize uint
}

type AuthType int

const (
AuthTypeStatic AuthType = iota
AuthTypeHttp
)

func (a *AuthType) UnmarshalTOML(src interface{}) error {
log.Println("got", src)
switch v := src.(type) {
case string:
if v == "static" {
*a = AuthTypeStatic
} else if v == "http" {
*a = AuthTypeHttp
} else {
return fmt.Errorf("Unknown type '%s'", v)
}
return nil
default:
}
return errors.New("Unknown type")
}

type AuthConfig struct {
Type AuthType
Static auth.StaticAuthConfig
Http auth.HttpAuthConfig
}

func GetAuthenticator(conf AuthConfig) auth.Authenticator {
if conf.Type == AuthTypeHttp {
return auth.NewHttpAuth(conf.Http)
}

return auth.NewStaticAuth(conf.Static)
}

// Parse tries to find and parse config from paths in order
func Parse(paths []string) (*Config, error) {
// set defaults
config := Config{
App: AppConfig{
// TODO: see if we can make IPv6 or even dual socket work
Address: "127.0.0.1",
Port: 1337,
Latency: 300,
Buffersize: 384000,
},
Auth: AuthConfig{
Type: AuthTypeStatic,
Static: auth.StaticAuthConfig{
Allow: []string{"*"},
},
},
}

var data []byte
var err error

// try to read file from given paths
for _, path := range paths {
data, err = ioutil.ReadFile(path)
if err == nil {
log.Println("Read config from", path)
break
} else {
if os.IsNotExist(err) {
continue
}
return nil, err
}
}

// parse toml
if data != nil {
err = toml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
} else {
log.Println("Config file not found, using defaults")
}

return &config, nil
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/voc/srtrelay

go 1.13

require github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a
require (
github.com/haivision/srtgo v0.0.0-20201031140249-2b8b6466e6f0
github.com/minio/minio v0.0.0-20201115213412-598ca0569c58
github.com/pelletier/go-toml v1.8.1
golang.org/x/sys v0.0.0-20201113233024-12cec1faf1ba // indirect
)
Loading

0 comments on commit a4af775

Please sign in to comment.