Skip to content

Commit

Permalink
add publicAddress for configuring url in API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
iSchluff committed Dec 15, 2021
1 parent 423bb89 commit 43eaaed
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
4 changes: 4 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
# listen on localhost
addresses = ["localhost:1337"]

# Set public address for use in API responses
# by default local FQDN + listen address port is used
#publicAddress = "hostname: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.
Expand Down
41 changes: 35 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"io/ioutil"
"log"
"os"
"strings"
"time"

"github.com/Showmax/go-fqdn"
"github.com/pelletier/go-toml"
"github.com/voc/srtrelay/auth"
)
Expand All @@ -18,12 +20,13 @@ type Config struct {
}

type AppConfig struct {
Address string
Addresses []string
Latency uint
Buffersize uint
SyncClients bool
LossMaxTTL uint
Address string
Addresses []string
PublicAddress string
Latency uint
Buffersize uint
SyncClients bool
LossMaxTTL uint
}

type AuthConfig struct {
Expand All @@ -50,6 +53,22 @@ func GetAuthenticator(conf AuthConfig) (auth.Authenticator, error) {
}
}

func getHostname() string {
name, err := fqdn.FqdnHostname()
if err != nil {
log.Println("fqdn:", err)
if err != fqdn.ErrFqdnNotFound {
return name
}

name, err = os.Hostname()
if err != nil {
log.Println("hostname:", err)
}
}
return name
}

// Parse tries to find and parse config from paths in order
func Parse(paths []string) (*Config, error) {
// set defaults
Expand Down Expand Up @@ -113,5 +132,15 @@ func Parse(paths []string) (*Config, error) {
config.App.Addresses = []string{config.App.Address}
}

// guess public address if not set
if config.App.PublicAddress == "" {
split := strings.Split(config.App.Addresses[0], ":")
if len(split) < 2 {
log.Fatal("Invalid address: ", config.App.Addresses[0])
}
config.App.PublicAddress = fmt.Sprintf("%s:%s", getHostname(), split[len(split)-1])
log.Println("Note: assuming public address", config.App.PublicAddress)
}

return &config, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/voc/srtrelay
go 1.13

require (
github.com/Showmax/go-fqdn v1.0.0 // indirect
github.com/haivision/srtgo v0.0.0-20210708214141-ea719a26f9f2
github.com/minio/minio v0.0.0-20201124200415-f96ed3769f87
github.com/pelletier/go-toml v1.9.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/DataDog/datadog-go v2.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/Shopify/sarama v1.24.1/go.mod h1:fGP8eQ6PugKEI0iUETYYtnP6d1pH/bdDMTel1X5ajsU=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM=
github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/alecthomas/participle v0.2.1/go.mod h1:SW6HZGeZgSIpcUWX3fXpfZhuaWHnmoD5KCVaqSaNTkk=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ func main() {

serverConfig := srt.Config{
Server: srt.ServerConfig{
Addresses: conf.App.Addresses,
Latency: conf.App.Latency,
LossMaxTTL: conf.App.LossMaxTTL,
SyncClients: conf.App.SyncClients,
Auth: auth,
Addresses: conf.App.Addresses,
PublicAddress: conf.App.PublicAddress,
Latency: conf.App.Latency,
LossMaxTTL: conf.App.LossMaxTTL,
SyncClients: conf.App.SyncClients,
Auth: auth,
},
Relay: relay.RelayConfig{
Buffersize: conf.App.Buffersize, // 1s @ 3Mbits/
Expand Down
13 changes: 7 additions & 6 deletions srt/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type Config struct {
}

type ServerConfig struct {
Addresses []string
Latency uint
LossMaxTTL uint
Auth auth.Authenticator
SyncClients bool
Addresses []string
PublicAddress string
Latency uint
LossMaxTTL uint
Auth auth.Authenticator
SyncClients bool
}

// Server is an interface for a srt relay server
Expand Down Expand Up @@ -306,7 +307,7 @@ func (s *ServerImpl) registerForStats(ctx context.Context, conn *srtConn) {
func (s *ServerImpl) GetStatistics() []*relay.StreamStatistics {
streams := s.relay.GetStatistics()
for _, stream := range streams {
stream.URL = fmt.Sprintf("srt://%s?streamid=play/%s", s.config.Addresses[0], stream.Name)
stream.URL = fmt.Sprintf("srt://%s?streamid=play/%s", s.config.PublicAddress, stream.Name)
}
return streams
}
Expand Down
4 changes: 2 additions & 2 deletions srt/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func TestServerImpl_GetStatistics(t *testing.T) {
r := relay.NewRelay(&relay.RelayConfig{})
s := &ServerImpl{
relay: r,
config: &ServerConfig{Addresses: []string{"127.0.0.1:1337", "[::1]:1337"}},
config: &ServerConfig{Addresses: []string{"127.0.0.1:1337", "[::1]:1337"}, PublicAddress: "testserver.de:1337"},
}
r.Publish("s1")
r.Subscribe("s1")
r.Subscribe("s1")
streams := s.GetStatistics()

expected := []*relay.StreamStatistics{
{Name: "s1", URL: "srt://127.0.0.1:1337?streamid=play/s1", Clients: 2, Created: streams[0].Created},
{Name: "s1", URL: "srt://testserver.de:1337?streamid=play/s1", Clients: 2, Created: streams[0].Created},
}
if err := compareStats(streams, expected); err != nil {
t.Error(err)
Expand Down

0 comments on commit 43eaaed

Please sign in to comment.