From 43eaaedd52af3a90ab28f5bd6a6c91a9542e2b48 Mon Sep 17 00:00:00 2001 From: Anton Schubert Date: Wed, 15 Dec 2021 20:25:01 +0100 Subject: [PATCH] add publicAddress for configuring url in API responses --- config.toml.example | 4 ++++ config/config.go | 41 +++++++++++++++++++++++++++++++++++------ go.mod | 1 + go.sum | 2 ++ main.go | 11 ++++++----- srt/server.go | 13 +++++++------ srt/server_test.go | 4 ++-- 7 files changed, 57 insertions(+), 19 deletions(-) diff --git a/config.toml.example b/config.toml.example index c2a0503..8143111 100644 --- a/config.toml.example +++ b/config.toml.example @@ -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. diff --git a/config/config.go b/config/config.go index 04cfb14..9064117 100644 --- a/config/config.go +++ b/config/config.go @@ -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" ) @@ -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 { @@ -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 @@ -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 } diff --git a/go.mod b/go.mod index 9267e3d..d4aa8e2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e87db32..133cad7 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 46093f5..d581750 100644 --- a/main.go +++ b/main.go @@ -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/ diff --git a/srt/server.go b/srt/server.go index 76cd24c..caabb6d 100644 --- a/srt/server.go +++ b/srt/server.go @@ -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 @@ -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 } diff --git a/srt/server_test.go b/srt/server_test.go index 12a87c5..4245041 100644 --- a/srt/server_test.go +++ b/srt/server_test.go @@ -24,7 +24,7 @@ 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") @@ -32,7 +32,7 @@ func TestServerImpl_GetStatistics(t *testing.T) { 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)