Reject invalid streaming key

This commit is contained in:
Gabe Kangas 2020-06-01 17:15:11 -07:00
parent ca622c85c7
commit 161aaeec8d
5 changed files with 60 additions and 45 deletions

View File

@ -20,8 +20,9 @@ type Config struct {
}
type VideoSettings struct {
ResolutionWidth int `yaml:"resolutionWidth"`
ChunkLengthInSeconds int `yaml:"chunkLengthInSeconds"`
ResolutionWidth int `yaml:"resolutionWidth"`
ChunkLengthInSeconds int `yaml:"chunkLengthInSeconds"`
StreamingKey string `yaml:"streamingKey"`
}
// MaxNumberOnDisk must be at least as large as MaxNumberInPlaylist
@ -39,7 +40,7 @@ func getConfig() Config {
filePath := "config/config.yaml"
if !fileExists(filePath) {
log.Fatal("ERROR: valid config/config.yaml is required")
log.Fatal("ERROR: valid config/config.yaml is required. Copy config/config-example.yaml to config/config.yaml and edit.")
}
yamlFile, err := ioutil.ReadFile(filePath)

View File

@ -6,11 +6,12 @@ webServerPort: 8080
videoSettings:
resolutionWidth: 900
chunkLengthInSeconds: 4
streamingKey: abc123
files:
maxNumberInPlaylist: 30
maxNumberOnDisk: 60
ipfs:
enabled: true
enabled: false
gateway: https://ipfs.io

View File

@ -41,6 +41,12 @@ func (h *Handler) OnCreateStream(timestamp uint32, cmd *rtmpmsg.NetConnectionCre
func (h *Handler) OnPublish(timestamp uint32, cmd *rtmpmsg.NetStreamPublish) error {
// log.Printf("OnPublish: %#v", cmd)
log.Println("Incoming stream connected.")
if cmd.PublishingName != configuration.VideoSettings.StreamingKey {
return errors.New("Invalid streaming key! Rejecting incoming stream.")
}
// (example) Reject a connection when PublishingName is empty
if cmd.PublishingName == "" {
return errors.New("PublishingName is empty")

41
main.go
View File

@ -1,15 +1,11 @@
package main
import (
"io"
"net"
"net/http"
"strconv"
icore "github.com/ipfs/interface-go-ipfs-core"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/yutopp/go-rtmp"
)
var ipfs icore.CoreAPI
@ -56,40 +52,3 @@ func startChatServer() {
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(configuration.WebServerPort), nil))
}
func startRTMPService() {
port := 1935
log.Printf("RTMP server is listening for incoming stream on port %d.\n", port)
tcpAddr, err := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(port))
if err != nil {
log.Panicf("Failed: %+v", err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Panicf("Failed: %+v", err)
}
srv := rtmp.NewServer(&rtmp.ServerConfig{
OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *rtmp.ConnConfig) {
l := log.StandardLogger()
l.SetLevel(logrus.WarnLevel)
h := &Handler{}
return conn, &rtmp.ConnConfig{
Handler: h,
ControlState: rtmp.StreamControlStateConfig{
DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8,
},
Logger: l,
}
},
})
if err := srv.Serve(listener); err != nil {
log.Panicf("Failed: %+v", err)
}
}

48
rtmp.go Normal file
View File

@ -0,0 +1,48 @@
package main
import (
"io"
"net"
"strconv"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/yutopp/go-rtmp"
)
func startRTMPService() {
port := 1935
log.Printf("RTMP server is listening for incoming stream on port %d.\n", port)
tcpAddr, err := net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(port))
if err != nil {
log.Panicf("Failed: %+v", err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Panicf("Failed: %+v", err)
}
srv := rtmp.NewServer(&rtmp.ServerConfig{
OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *rtmp.ConnConfig) {
l := log.StandardLogger()
l.SetLevel(logrus.WarnLevel)
h := &Handler{}
return conn, &rtmp.ConnConfig{
Handler: h,
ControlState: rtmp.StreamControlStateConfig{
DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8,
},
Logger: l,
}
},
})
if err := srv.Serve(listener); err != nil {
log.Panicf("Failed: %+v", err)
}
}