forked from voc/srtrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add flag and config parsing, add static stream auth
- Loading branch information
Showing
16 changed files
with
1,091 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
srtrelay | ||
config.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.