From cc4c68620a33a88ff8d89e55dbf9076d03d12944 Mon Sep 17 00:00:00 2001 From: DKing Date: Wed, 29 Jan 2020 07:52:51 +0800 Subject: [PATCH 01/17] add support for key authentication --- configure/channel.go | 122 +++++++++++++++++++++++++ configure/liveconfig.go | 35 +++---- protocol/rtmp/rtmp.go | 13 ++- protocol/rtmp/rtmprelay/staticrelay.go | 2 +- 4 files changed, 152 insertions(+), 20 deletions(-) create mode 100755 configure/channel.go diff --git a/configure/channel.go b/configure/channel.go new file mode 100755 index 00000000..25bb061d --- /dev/null +++ b/configure/channel.go @@ -0,0 +1,122 @@ +package configure + +import ( + "encoding/json" + "io/ioutil" + "log" + "math/rand" + "sync" + "time" +) + +const roomKeySaveFile = "room_keys.json" + +var RoomKeys = LoadRoomKey(roomKeySaveFile) + +var roomUpdated = false + +func init() { + rand.Seed(time.Now().UnixNano()) + go func() { + for { + time.Sleep(15 * time.Second) + if roomUpdated { + RoomKeys.Save(roomKeySaveFile) + roomUpdated = false + } + } + }() +} + + +type RoomKeysType struct { + mapChanKey sync.Map + mapKeyChan sync.Map +} + +func LoadRoomKey(f string) *RoomKeysType { + result := &RoomKeysType { + mapChanKey: sync.Map{}, + mapKeyChan: sync.Map{}, + } + raw := map[string]string{} + content, err := ioutil.ReadFile(f) + if err != nil { + log.Printf("Failed to read file %s for room keys", f) + return result + } + if json.Unmarshal(content, &raw) != nil { + log.Printf("Failed to unmarshal file %s for room keys", f) + return result + } + for room, key := range raw { + result.mapChanKey.Store(room, key) + result.mapKeyChan.Store(key, room) + } + return result +} + +func (r *RoomKeysType) Save(f string) { + raw := map[string]string{} + r.mapChanKey.Range(func(channel, key interface{}) bool { + raw[channel.(string)] = key.(string) + return true + }) + content, err := json.Marshal(raw) + if err != nil { + log.Println("Failed to marshal room keys") + return + } + if ioutil.WriteFile(f, content, 0644) != nil { + log.Println("Failed to save room keys") + return + } +} + +// set/reset a random key for channel +func (r *RoomKeysType) SetKey(channel string) string { + var key string + for { + key = randStringRunes(48) + if _, found := r.mapKeyChan.Load(key); !found { + r.mapChanKey.Store(channel, key) + r.mapKeyChan.Store(key, channel) + break + } + } + roomUpdated = true + return key +} + +func (r *RoomKeysType) GetKey(channel string) string { + var key interface{} + var found bool + if key, found = r.mapChanKey.Load(channel); found { + return key.(string) + } else { + newkey := r.SetKey(channel) + log.Printf("[KEY] new channel [%s]: %s", channel, newkey) + return newkey + } +} + +func (r *RoomKeysType) GetChannel(key string) string { + channel, found := r.mapKeyChan.Load(key) + if found { + return channel.(string) + } else { + return "" + } +} + + +// helpers +var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + +func randStringRunes(n int) string { + b := make([]rune, n) + for i := range b { + b[i] = letterRunes[rand.Intn(len(letterRunes))] + } + return string(b) +} diff --git a/configure/liveconfig.go b/configure/liveconfig.go index 68305735..302bf52f 100644 --- a/configure/liveconfig.go +++ b/configure/liveconfig.go @@ -8,38 +8,39 @@ import ( /* { - [ - { - "application":"live", - "live":"on", - "hls":"on", - "static_push":["rtmp://xx/live"] - } - ] + "server": [ + { + "appname": "live", + "liveon": "on", + "hlson": "on", + "static_push": [] + } + ] } */ + type Application struct { - Appname string - Liveon string - Hlson string - Static_push []string + Appname string `json:"appname"` + Liveon string `json:"liveon"` + Hlson string `json:"hlson"` + StaticPush []string `json:"static_push"` } type ServerCfg struct { - Server []Application + Server []Application `json:"server"` } var RtmpServercfg ServerCfg func LoadConfig(configfilename string) error { - log.Printf("starting load configure file(%s)......", configfilename) + log.Printf("starting load configure file %s", configfilename) data, err := ioutil.ReadFile(configfilename) if err != nil { log.Printf("ReadFile %s error:%v", configfilename, err) return err } - log.Printf("loadconfig: \r\n%s", string(data)) + // log.Printf("loadconfig: \r\n%s", string(data)) err = json.Unmarshal(data, &RtmpServercfg) if err != nil { @@ -62,8 +63,8 @@ func CheckAppName(appname string) bool { func GetStaticPushUrlList(appname string) ([]string, bool) { for _, app := range RtmpServercfg.Server { if (app.Appname == appname) && (app.Liveon == "on") { - if len(app.Static_push) > 0 { - return app.Static_push, true + if len(app.StaticPush) > 0 { + return app.StaticPush, true } else { return nil, false } diff --git a/protocol/rtmp/rtmp.go b/protocol/rtmp/rtmp.go index b8b475a4..69f0bea3 100755 --- a/protocol/rtmp/rtmp.go +++ b/protocol/rtmp/rtmp.go @@ -4,6 +4,7 @@ import ( "errors" "flag" "fmt" + "github.com/gwuhaolin/livego/utils/uid" "log" "net" "net/url" @@ -15,7 +16,6 @@ import ( "github.com/gwuhaolin/livego/configure" "github.com/gwuhaolin/livego/container/flv" "github.com/gwuhaolin/livego/protocol/rtmp/core" - "github.com/gwuhaolin/livego/utils/uid" ) const ( @@ -111,7 +111,7 @@ func (s *Server) handleConn(conn *core.Conn) error { return err } - appname, _, _ := connServer.GetInfo() + appname, name, _ := connServer.GetInfo() if ret := configure.CheckAppName(appname); !ret { err := errors.New(fmt.Sprintf("application name=%s is not configured", appname)) @@ -122,6 +122,14 @@ func (s *Server) handleConn(conn *core.Conn) error { log.Printf("handleConn: IsPublisher=%v", connServer.IsPublisher()) if connServer.IsPublisher() { + var channel = configure.RoomKeys.GetChannel(name) + if channel == "" { + err := errors.New(fmt.Sprintf("invalid key")) + conn.Close() + log.Println("CheckKey err:", err) + return err + } + connServer.PublishInfo.Name = channel if pushlist, ret := configure.GetStaticPushUrlList(appname); ret && (pushlist != nil) { log.Printf("GetStaticPushUrlList: %v", pushlist) } @@ -138,6 +146,7 @@ func (s *Server) handleConn(conn *core.Conn) error { flvWriter := new(flv.FlvDvr) s.handler.HandleWriter(flvWriter.GetWriter(reader.Info())) } else { + configure.RoomKeys.GetKey(name) // set new key if this channel not exists writer := NewVirWriter(connServer) log.Printf("new player: %+v", writer.Info()) s.handler.HandleWriter(writer) diff --git a/protocol/rtmp/rtmprelay/staticrelay.go b/protocol/rtmp/rtmprelay/staticrelay.go index 6a376b89..4fb17ff4 100644 --- a/protocol/rtmp/rtmprelay/staticrelay.go +++ b/protocol/rtmp/rtmprelay/staticrelay.go @@ -63,7 +63,7 @@ func GetStaticPushObject(rtmpurl string) (*StaticPush, error) { } g_MapLock.RUnlock() - return nil, errors.New(fmt.Sprintf("G_StaticPushMap[%s] not exist....")) + return nil, errors.New(fmt.Sprintf("G_StaticPushMap[%s] not exist....", rtmpurl)) } func ReleaseStaticPushObject(rtmpurl string) { From 8f69e4d760b35912584111c3bf2a749b3845390d Mon Sep 17 00:00:00 2001 From: DKing Date: Thu, 30 Jan 2020 07:00:08 +0800 Subject: [PATCH 02/17] fix manage page --- configure/channel.go | 20 +++++++++ container/flv/muxer.go | 10 +++-- protocol/httpopera/http_opera.go | 70 +++++++++++++++++++++++++++----- 3 files changed, 86 insertions(+), 14 deletions(-) diff --git a/configure/channel.go b/configure/channel.go index 25bb061d..44ec1747 100755 --- a/configure/channel.go +++ b/configure/channel.go @@ -109,6 +109,26 @@ func (r *RoomKeysType) GetChannel(key string) string { } } +func (r *RoomKeysType) DeleteChannel(channel string) bool { + key, ok := r.mapChanKey.Load(channel) + if ok { + r.mapChanKey.Delete(channel) + r.mapKeyChan.Delete(key) + return true + } + return false +} + +func (r *RoomKeysType) DeleteKey(key string) bool { + channel, ok := r.mapKeyChan.Load(key) + if ok { + r.mapChanKey.Delete(channel) + r.mapKeyChan.Delete(key) + return true + } + return false +} + // helpers var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") diff --git a/container/flv/muxer.go b/container/flv/muxer.go index 465ba3d3..08cc266c 100755 --- a/container/flv/muxer.go +++ b/container/flv/muxer.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "os" + "path" "strings" "time" @@ -16,9 +17,10 @@ import ( var ( flvHeader = []byte{0x46, 0x4c, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0x09} - flvFile = flag.String("filFile", "./out.flv", "output flv file name") + flvDir = flag.String("flvDir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv") ) +/* func NewFlv(handler av.Handler, info av.Info) { patths := strings.SplitN(info.Key, "/", 2) @@ -41,6 +43,7 @@ func NewFlv(handler av.Handler, info av.Info) { log.Println("close flv file") writer.ctx.Close() } +*/ const ( headerLen = 11 @@ -144,18 +147,17 @@ type FlvDvr struct{} func (f *FlvDvr) GetWriter(info av.Info) av.WriteCloser { paths := strings.SplitN(info.Key, "/", 2) if len(paths) != 2 { - log.Println("invalid info") return nil } - err := os.MkdirAll(paths[0], 0755) + err := os.MkdirAll(path.Join(*flvDir, paths[0]), 0755) if err != nil { log.Println("mkdir error:", err) return nil } - fileName := fmt.Sprintf("%s_%d.%s", info.Key, time.Now().Unix(), "flv") + fileName := fmt.Sprintf("%s_%d.%s", path.Join(*flvDir, info.Key), time.Now().Unix(), "flv") log.Println("flv dvr save stream to: ", fileName) w, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0755) if err != nil { diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index 22071b44..6bf5948e 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -3,6 +3,7 @@ package httpopera import ( "encoding/json" "fmt" + "github.com/gwuhaolin/livego/configure" "io" "log" "net" @@ -61,7 +62,7 @@ func NewServer(h av.Handler, rtmpAddr string) *Server { func (s *Server) Serve(l net.Listener) error { mux := http.NewServeMux() - mux.Handle("/statics", http.FileServer(http.Dir("statics"))) + mux.Handle("/statics/", http.StripPrefix("/statics/", http.FileServer(http.Dir("statics")))) mux.HandleFunc("/control/push", func(w http.ResponseWriter, r *http.Request) { s.handlePush(w, r) @@ -69,6 +70,15 @@ func (s *Server) Serve(l net.Listener) error { mux.HandleFunc("/control/pull", func(w http.ResponseWriter, r *http.Request) { s.handlePull(w, r) }) + mux.HandleFunc("/control/get", func(w http.ResponseWriter, r *http.Request) { + s.handleGet(w, r) + }) + mux.HandleFunc("/control/reset", func(w http.ResponseWriter, r *http.Request) { + s.handleReset(w, r) + }) + mux.HandleFunc("/control/delete", func(w http.ResponseWriter, r *http.Request) { + s.handleDelete(w, r) + }) mux.HandleFunc("/stat/livestat", func(w http.ResponseWriter, r *http.Request) { s.GetLiveStatics(w, r) }) @@ -78,12 +88,12 @@ func (s *Server) Serve(l net.Listener) error { type stream struct { Key string `json:"key"` - Url string `json:"Url"` - StreamId uint32 `json:"StreamId"` - VideoTotalBytes uint64 `json:123456` - VideoSpeed uint64 `json:123456` - AudioTotalBytes uint64 `json:123456` - AudioSpeed uint64 `json:123456` + Url string `json:"url"` + StreamId uint32 `json:"stream_id"` + VideoTotalBytes uint64 `json:"video_total_bytes"` + VideoSpeed uint64 `json:"video_speed"` + AudioTotalBytes uint64 `json:"audio_total_bytes"` + AudioSpeed uint64 `json:"audio_speed"` } type streams struct { @@ -135,12 +145,15 @@ func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) { w.Write(resp) } -//http://127.0.0.1:8090/control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456 +//http://127.0.0.1:8090/control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456 func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { var retString string var err error - req.ParseForm() + if req.ParseForm() != nil { + fmt.Fprintf(w, "url: /control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456") + return + } oper := req.Form["oper"] app := req.Form["app"] @@ -192,7 +205,10 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { var retString string var err error - req.ParseForm() + if req.ParseForm() != nil { + fmt.Fprintf(w, "url: /control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456") + return + } oper := req.Form["oper"] app := req.Form["app"] @@ -238,3 +254,37 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { log.Printf("push start return %s", retString) } } + +//http://127.0.0.1:8090/control/reset?room=ROOM_NAME +func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) { + if r.ParseForm() != nil { + fmt.Fprintf(w, "url: /control/reset?room=ROOM_NAME") + return + } + room := r.Form["room"][0] + fmt.Fprintf(w, configure.RoomKeys.SetKey(room)) +} + +//http://127.0.0.1:8090/control/get?room=ROOM_NAME +func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) { + if r.ParseForm() != nil { + fmt.Fprintf(w, "url: /control/get?room=ROOM_NAME") + return + } + room := r.Form["room"][0] + fmt.Fprintf(w, configure.RoomKeys.GetKey(room)) +} + +//http://127.0.0.1:8090/control/delete?room=ROOM_NAME +func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) { + if r.ParseForm() != nil { + fmt.Fprintf(w, "url: /control/delete?room=ROOM_NAME") + return + } + room := r.Form["room"][0] + if configure.RoomKeys.DeleteChannel(room) { + fmt.Fprintf(w, "OK") + } else { + fmt.Fprintf(w, "Room Not Found") + } +} From 7dca40f9e65fd27fffa0566fff965ed3b277f90a Mon Sep 17 00:00:00 2001 From: DKing Date: Fri, 31 Jan 2020 00:00:08 +0800 Subject: [PATCH 03/17] remove workaround for creating keys --- configure/channel.go | 10 ++++++---- protocol/httpopera/http_opera.go | 4 ++-- protocol/rtmp/rtmp.go | 1 - 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/configure/channel.go b/configure/channel.go index 44ec1747..307a371a 100755 --- a/configure/channel.go +++ b/configure/channel.go @@ -2,16 +2,18 @@ package configure import ( "encoding/json" + "flag" "io/ioutil" "log" "math/rand" "sync" "time" ) +var ( + roomKeySaveFile = flag.String("keyFile", "room_keys.json", "path to save room keys") +) -const roomKeySaveFile = "room_keys.json" - -var RoomKeys = LoadRoomKey(roomKeySaveFile) +var RoomKeys = LoadRoomKey(*roomKeySaveFile) var roomUpdated = false @@ -21,7 +23,7 @@ func init() { for { time.Sleep(15 * time.Second) if roomUpdated { - RoomKeys.Save(roomKeySaveFile) + RoomKeys.Save(*roomKeySaveFile) roomUpdated = false } } diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index 6bf5948e..71ea94e9 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -89,7 +89,7 @@ func (s *Server) Serve(l net.Listener) error { type stream struct { Key string `json:"key"` Url string `json:"url"` - StreamId uint32 `json:"stream_id"` + StreamId uint32 `json:"-"` // hide VideoTotalBytes uint64 `json:"video_total_bytes"` VideoSpeed uint64 `json:"video_speed"` AudioTotalBytes uint64 `json:"audio_total_bytes"` @@ -98,7 +98,7 @@ type stream struct { type streams struct { Publishers []stream `json:"publishers"` - Players []stream `json:"players"` + Players []stream `json:"-"` // hide } //http://127.0.0.1:8090/stat/livestat diff --git a/protocol/rtmp/rtmp.go b/protocol/rtmp/rtmp.go index 69f0bea3..c5aea81c 100755 --- a/protocol/rtmp/rtmp.go +++ b/protocol/rtmp/rtmp.go @@ -146,7 +146,6 @@ func (s *Server) handleConn(conn *core.Conn) error { flvWriter := new(flv.FlvDvr) s.handler.HandleWriter(flvWriter.GetWriter(reader.Info())) } else { - configure.RoomKeys.GetKey(name) // set new key if this channel not exists writer := NewVirWriter(connServer) log.Printf("new player: %+v", writer.Info()) s.handler.HandleWriter(writer) From 3eb4ba34f0ca93583fd039b347484e215a41fdab Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:18:43 +0200 Subject: [PATCH 04/17] JWT --- .livego.json | 4 ++++ configure/liveconfig.go | 12 +++++++--- go.mod | 5 ++++ go.sum | 20 ++++++++++++++++ protocol/httpopera/http_opera.go | 40 ++++++++++++++++++++++++++++---- room_keys.json | 1 + 6 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 room_keys.json diff --git a/.livego.json b/.livego.json index 47bab74c..80defe37 100644 --- a/.livego.json +++ b/.livego.json @@ -1,4 +1,8 @@ { + "jwt": { + "secret": "testing", + "algorithm": "HS256s" + }, "server": [ { "appname": "live", diff --git a/configure/liveconfig.go b/configure/liveconfig.go index 302bf52f..e701c3a6 100644 --- a/configure/liveconfig.go +++ b/configure/liveconfig.go @@ -20,13 +20,19 @@ import ( */ type Application struct { - Appname string `json:"appname"` - Liveon string `json:"liveon"` - Hlson string `json:"hlson"` + Appname string `json:"appname"` + Liveon string `json:"liveon"` + Hlson string `json:"hlson"` StaticPush []string `json:"static_push"` } +type JWTCfg struct { + Secret string `json:"secret"` + Algorithm string `json:"algorithm"` +} + type ServerCfg struct { + JWTCfg `json:"jwt"` Server []Application `json:"server"` } diff --git a/go.mod b/go.mod index eb767277..79058cdd 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,14 @@ module github.com/gwuhaolin/livego go 1.13 require ( + github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/gorilla/mux v1.7.4 // indirect github.com/kr/pretty v0.1.0 // indirect github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 github.com/satori/go.uuid v1.2.0 + github.com/smartystreets/goconvey v1.6.4 // indirect github.com/stretchr/testify v1.4.0 + github.com/urfave/negroni v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index a066b49a..738f62c3 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,14 @@ +github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b h1:CvoEHGmxWl5kONC5icxwqV899dkf4VjOScbxLpllEnw= +github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b/go.mod h1:LWMyo4iOLWXHGdBki7NIht1kHru/0wM179h+d3g8ATM= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -11,9 +20,20 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= +github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index 71ea94e9..34c3cf7c 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -3,13 +3,15 @@ package httpopera import ( "encoding/json" "fmt" - "github.com/gwuhaolin/livego/configure" "io" "log" "net" "net/http" + jwtmiddleware "github.com/auth0/go-jwt-middleware" + "github.com/dgrijalva/jwt-go" "github.com/gwuhaolin/livego/av" + "github.com/gwuhaolin/livego/configure" "github.com/gwuhaolin/livego/protocol/rtmp" "github.com/gwuhaolin/livego/protocol/rtmp/rtmprelay" ) @@ -59,10 +61,38 @@ func NewServer(h av.Handler, rtmpAddr string) *Server { } } +func JWTMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if len(configure.RtmpServercfg.JWTCfg.Secret) > 0 { + var algorithm jwt.SigningMethod + if len(configure.RtmpServercfg.JWTCfg.Algorithm) > 0 { + algorithm = jwt.GetSigningMethod(configure.RtmpServercfg.JWTCfg.Algorithm) + } + + if algorithm == nil { + algorithm = jwt.SigningMethodHS256 + } + + jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{ + Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader, jwtmiddleware.FromParameter("jwt")), + ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) { + return []byte(configure.RtmpServercfg.Secret), nil + }, + SigningMethod: algorithm, + }) + + jwtMiddleware.HandlerWithNext(w, r, next.ServeHTTP) + return + } + next.ServeHTTP(w, r) + + }) +} + func (s *Server) Serve(l net.Listener) error { mux := http.NewServeMux() - mux.Handle("/statics/", http.StripPrefix("/statics/", http.FileServer(http.Dir("statics")))) + mux.Handle("/statics/", http.StripPrefix("/statics/", http.FileServer(http.Dir("statics")))) mux.HandleFunc("/control/push", func(w http.ResponseWriter, r *http.Request) { s.handlePush(w, r) @@ -82,14 +112,14 @@ func (s *Server) Serve(l net.Listener) error { mux.HandleFunc("/stat/livestat", func(w http.ResponseWriter, r *http.Request) { s.GetLiveStatics(w, r) }) - http.Serve(l, mux) + http.Serve(l, JWTMiddleware(mux)) return nil } type stream struct { Key string `json:"key"` Url string `json:"url"` - StreamId uint32 `json:"-"` // hide + StreamId uint32 `json:"-"` // hide VideoTotalBytes uint64 `json:"video_total_bytes"` VideoSpeed uint64 `json:"video_speed"` AudioTotalBytes uint64 `json:"audio_total_bytes"` @@ -98,7 +128,7 @@ type stream struct { type streams struct { Publishers []stream `json:"publishers"` - Players []stream `json:"-"` // hide + Players []stream `json:"-"` // hide } //http://127.0.0.1:8090/stat/livestat diff --git a/room_keys.json b/room_keys.json new file mode 100644 index 00000000..cd0bb926 --- /dev/null +++ b/room_keys.json @@ -0,0 +1 @@ +{"ROOM_NAME":"VdagOakQauW5JhEeZ3qFM30fO6bzXd7skvkyboJH9xoISPe0"} \ No newline at end of file From 8b2b7d3e85dad13023e68b687a83e6da7f481b78 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:30:06 +0200 Subject: [PATCH 05/17] JWT middelware --- .livego.json | 4 ---- CHANGELOG.md | 27 ++++++++++++++++++++++ README.md | 64 ++++++++++++++++++++++++++-------------------------- go.sum | 1 + 4 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 CHANGELOG.md diff --git a/.livego.json b/.livego.json index 80defe37..47bab74c 100644 --- a/.livego.json +++ b/.livego.json @@ -1,8 +1,4 @@ { - "jwt": { - "secret": "testing", - "algorithm": "HS256s" - }, "server": [ { "appname": "live", diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..e058199d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,27 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.0.0] - 2020-04-08 +### Added +- JSON Web Token support. +``` json + // .livego.json + { + "jwt": { + "secret": "testing", + "algorithm": "HS256s" + }, + "server": [ + { + "appname": "live", + "liveon": "on", + "hlson": "on" + } + ] + } +``` diff --git a/README.md b/README.md index c07f4967..80ada9ae 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,43 @@ # livego -简单高效的直播服务器: -- 安装和使用非常简单; -- 纯 Golang 编写,性能高,跨平台; -- 支持常用的传输协议、文件格式、编码格式; +Simple and efficient live broadcast server: +-Very simple to install and use; +-Pure Golang, high performance, cross-platform; +-Support commonly used transmission protocols, file formats, encoding formats; -#### 支持的传输协议 -- RTMP -- AMF -- HLS -- HTTP-FLV +#### Supported transport protocols +-RTMP +-AMF +-HLS +-HTTP-FLV -#### 支持的容器格式 -- FLV -- TS +#### Supported container formats +-FLV +-TS -#### 支持的编码格式 -- H264 -- AAC -- MP3 +#### Supported encoding formats +-H264 +-AAC +-MP3 -## 安装 -直接下载编译好的[二进制文件](https://github.com/gwuhaolin/livego/releases)后,在命令行中执行。 +## Installation +After directly downloading the compiled [binary file] (https://github.com/gwuhaolin/livego/releases), execute it on the command line. -#### 从 Docker 启动 -执行`docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gwuhaolin/livego`启动 +#### Boot from Docker +Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuhaolin / livego` to start -#### 从源码编译 -1. 下载源码 `git clone https://github.com/gwuhaolin/livego.git` -2. 去 livego 目录中 执行 `go build` +#### Compile from source +1. Download the source code `git clone https: // github.com / gwuhaolin / livego.git` +2. Go to the livego directory and execute `go build` -## 使用 -2. 启动服务:执行 `livego` 二进制文件启动 livego 服务; -3. 上行推流:通过 `RTMP` 协议把视频流推送到 `rtmp://localhost:1935/live/movie`,例如使用 `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost:1935/live/movie` 推送; -4. 下行播放:支持以下三种播放协议,播放地址如下: - - `RTMP`:`rtmp://localhost:1935/live/movie` - - `FLV`:`http://127.0.0.1:7001/live/movie.flv` - - `HLS`:`http://127.0.0.1:7002/live/movie.m3u8` +## Use +2. Start the service: execute the livego binary file to start the livego service; +3. Upstream push: Push the video stream to `rtmp: // localhost: 1935 / live / movie` through the` RTMP` protocol, for example, use `ffmpeg -re -i demo.flv -c copy -f flv rtmp: / / localhost: 1935 / live / movie` push; +4. Downstream playback: The following three playback protocols are supported, and the playback address is as follows: + -`RTMP`:` rtmp: // localhost: 1935 / live / movie` + -`FLV`:` http: //127.0.0.1: 7001 / live / movie.flv` + -`HLS`:` http: //127.0.0.1: 7002 / live / movie.m3u8` -### [和 flv.js 搭配使用](https://github.com/gwuhaolin/blog/issues/3) +### [Use with flv.js] (https://github.com/gwuhaolin/blog/issues/3) -对Golang感兴趣?请看[Golang 中文学习资料汇总](http://go.wuhaolin.cn/) +Interested in Golang? Please see [Golang Chinese Learning Materials Summary] (http://go.wuhaolin.cn/) diff --git a/go.sum b/go.sum index 738f62c3..5bab2014 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= From e2435f3a7ed0c00059c04a6e646e7caab9cf820e Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:30:38 +0200 Subject: [PATCH 06/17] JWT middelware --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e058199d..983e7834 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [1.0.0] - 2020-04-08 ### Added - JSON Web Token support. ``` json From 4cc56dd5a7cfedf125bbd93f4a11c903cb2a1ef1 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:33:06 +0200 Subject: [PATCH 07/17] Show players, stream_id --- CHANGELOG.md | 4 ++++ protocol/httpopera/http_opera.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 983e7834..f0736702 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,3 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ] } ``` + +### Changed +- Show `players`. +- Show `stream_id`. \ No newline at end of file diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index 34c3cf7c..e207a347 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -119,7 +119,7 @@ func (s *Server) Serve(l net.Listener) error { type stream struct { Key string `json:"key"` Url string `json:"url"` - StreamId uint32 `json:"-"` // hide + StreamId uint32 `json:"stream_id"` VideoTotalBytes uint64 `json:"video_total_bytes"` VideoSpeed uint64 `json:"video_speed"` AudioTotalBytes uint64 `json:"audio_total_bytes"` @@ -128,7 +128,7 @@ type stream struct { type streams struct { Publishers []stream `json:"publishers"` - Players []stream `json:"-"` // hide + Players []stream `json:"players"` } //http://127.0.0.1:8090/stat/livestat From 834a8e43df1a11cbd6211fe74dbc094e4eb77c80 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:38:17 +0200 Subject: [PATCH 08/17] Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 80ada9ae..1f8f8051 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,6 @@ Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuha -`HLS`:` http: //127.0.0.1: 7002 / live / movie.m3u8` -### [Use with flv.js] (https://github.com/gwuhaolin/blog/issues/3) +### [Use with flv.js](https://github.com/gwuhaolin/blog/issues/3) Interested in Golang? Please see [Golang Chinese Learning Materials Summary] (http://go.wuhaolin.cn/) From 03f2909a3f8fc7ce1be82da9d0fc403d41ff6c8f Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:41:12 +0200 Subject: [PATCH 09/17] Readme --- README.md | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1f8f8051..3c4da4fe 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,26 @@ # livego Simple and efficient live broadcast server: --Very simple to install and use; --Pure Golang, high performance, cross-platform; --Support commonly used transmission protocols, file formats, encoding formats; +- Very simple to install and use; +- Pure Golang, high performance, cross-platform; +- Support commonly used transmission protocols, file formats, encoding formats; #### Supported transport protocols --RTMP --AMF --HLS --HTTP-FLV +- RTMP +- AMF +- HLS +- HTTP-FLV #### Supported container formats --FLV --TS +- FLV +- TS #### Supported encoding formats --H264 --AAC --MP3 +- H264 +- AAC +- sMP3 ## Installation -After directly downloading the compiled [binary file] (https://github.com/gwuhaolin/livego/releases), execute it on the command line. +After directly downloading the compiled [binary file](https://github.com/gwuhaolin/livego/releases), execute it on the command line. #### Boot from Docker Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuhaolin / livego` to start @@ -31,13 +31,13 @@ Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuha ## Use 2. Start the service: execute the livego binary file to start the livego service; -3. Upstream push: Push the video stream to `rtmp: // localhost: 1935 / live / movie` through the` RTMP` protocol, for example, use `ffmpeg -re -i demo.flv -c copy -f flv rtmp: / / localhost: 1935 / live / movie` push; +3. Upstream push: Push the video stream to `rtmp://localhost:1935/live/movie` through the` RTMP` protocol, for example, use `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost:1935/live/movie` push; 4. Downstream playback: The following three playback protocols are supported, and the playback address is as follows: - -`RTMP`:` rtmp: // localhost: 1935 / live / movie` - -`FLV`:` http: //127.0.0.1: 7001 / live / movie.flv` - -`HLS`:` http: //127.0.0.1: 7002 / live / movie.m3u8` + -`RTMP`:`rtmp://localhost:1935/live/movie` + -`FLV`:`http://127.0.0.1:7001/live/movie.flv` + -`HLS`:`http://127.0.0.1:7002/live/movie.m3u8` ### [Use with flv.js](https://github.com/gwuhaolin/blog/issues/3) -Interested in Golang? Please see [Golang Chinese Learning Materials Summary] (http://go.wuhaolin.cn/) +Interested in Golang? Please see [Golang Chinese Learning Materials Summary](http://go.wuhaolin.cn/) From dfa54ffc9c540a5d7fd69fc7064531e1234ac1c8 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 13:49:18 +0200 Subject: [PATCH 10/17] room_keys.json --- .gitignore | 1 + Dockerfile | 2 +- room_keys.json | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 room_keys.json diff --git a/.gitignore b/.gitignore index 4e89af21..84af34e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Created by .ignore support plugin (hsz.mobi) .idea dist +room_keys.json \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 2bf89c10..76656715 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o livego ./ FROM alpine:latest -LABEL maintainer="gwuhaolin " +LABEL maintainer="Ruben Cid Lara " ENV RTMP_PORT 1935 ENV HTTP_FLV_PORT 7001 ENV HLS_PORT 7002 diff --git a/room_keys.json b/room_keys.json deleted file mode 100644 index cd0bb926..00000000 --- a/room_keys.json +++ /dev/null @@ -1 +0,0 @@ -{"ROOM_NAME":"VdagOakQauW5JhEeZ3qFM30fO6bzXd7skvkyboJH9xoISPe0"} \ No newline at end of file From ebac4bff82998b67c441920215ce12541e03fd40 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 16:04:04 +0200 Subject: [PATCH 11/17] using redis for store roomkeys --- .gitignore | 4 +- .livego.json | 1 + CHANGELOG.md | 14 ++++ configure/channel.go | 116 +++++++++++++++++++++++-------- configure/liveconfig.go | 44 +++++++++++- go.mod | 4 +- go.sum | 58 ++++++++++++++++ protocol/httpopera/http_opera.go | 33 ++++++++- protocol/rtmp/rtmp.go | 7 +- 9 files changed, 241 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index 84af34e8..be692409 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ # Created by .ignore support plugin (hsz.mobi) .idea dist -room_keys.json \ No newline at end of file +room_keys.json +.vscode +.tmp \ No newline at end of file diff --git a/.livego.json b/.livego.json index 47bab74c..9cb50ba8 100644 --- a/.livego.json +++ b/.livego.json @@ -1,4 +1,5 @@ { + "redis_addr": "localhost:6379", "server": [ { "appname": "live", diff --git a/CHANGELOG.md b/CHANGELOG.md index f0736702..b89bd581 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ] } ``` +- Use redis for store room keys +``` json + // .livego.json + { + "redis_addr": "localhost:6379", + "server": [ + { + "appname": "live", + "liveon": "on", + "hlson": "on" + } + ] + } +``` ### Changed - Show `players`. diff --git a/configure/channel.go b/configure/channel.go index 307a371a..38eb30a5 100755 --- a/configure/channel.go +++ b/configure/channel.go @@ -2,34 +2,54 @@ package configure import ( "encoding/json" - "flag" + "fmt" "io/ioutil" "log" "math/rand" "sync" "time" -) -var ( - roomKeySaveFile = flag.String("keyFile", "room_keys.json", "path to save room keys") + + "github.com/go-redis/redis/v7" ) -var RoomKeys = LoadRoomKey(*roomKeySaveFile) +var RoomKeys = LoadRoomKey(*GetKeyFile()) var roomUpdated = false -func init() { +var saveInFile = true +var redisCli *redis.Client + +func Init() { + saveInFile = GetRedisAddr() == nil + rand.Seed(time.Now().UnixNano()) - go func() { - for { - time.Sleep(15 * time.Second) - if roomUpdated { - RoomKeys.Save(*roomKeySaveFile) - roomUpdated = false + if saveInFile { + go func() { + for { + time.Sleep(15 * time.Second) + if roomUpdated { + RoomKeys.Save(*roomKeySaveFile) + roomUpdated = false + } } - } - }() -} + }() + return + } + + redisCli = redis.NewClient(&redis.Options{ + Addr: *GetRedisAddr(), + Password: *GetRedisPwd(), + DB: 0, + }) + + _, err := redisCli.Ping().Result() + if err != nil { + panic(err) + } + + log.Printf("Redis connected") +} type RoomKeysType struct { mapChanKey sync.Map @@ -37,7 +57,7 @@ type RoomKeysType struct { } func LoadRoomKey(f string) *RoomKeysType { - result := &RoomKeysType { + result := &RoomKeysType{ mapChanKey: sync.Map{}, mapKeyChan: sync.Map{}, } @@ -76,8 +96,24 @@ func (r *RoomKeysType) Save(f string) { } // set/reset a random key for channel -func (r *RoomKeysType) SetKey(channel string) string { - var key string +func (r *RoomKeysType) SetKey(channel string) (key string, err error) { + if !saveInFile { + for { + key = randStringRunes(48) + if _, err = redisCli.Get(key).Result(); err == redis.Nil { + err = redisCli.Set(channel, key, 0).Err() + if err != nil { + return + } + + err = redisCli.Set(key, channel, 0).Err() + return + } else if err != nil { + return + } + } + } + for { key = randStringRunes(48) if _, found := r.mapKeyChan.Load(key); !found { @@ -87,31 +123,48 @@ func (r *RoomKeysType) SetKey(channel string) string { } } roomUpdated = true - return key + return } -func (r *RoomKeysType) GetKey(channel string) string { +func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) { + if !saveInFile { + if newKey, err = redisCli.Get(channel).Result(); err == redis.Nil { + newKey, err = r.SetKey(channel) + log.Printf("[KEY] new channel [%s]: %s", channel, newKey) + return + } + + return + } + var key interface{} var found bool if key, found = r.mapChanKey.Load(channel); found { - return key.(string) - } else { - newkey := r.SetKey(channel) - log.Printf("[KEY] new channel [%s]: %s", channel, newkey) - return newkey + return key.(string), nil } + newKey, err = r.SetKey(channel) + log.Printf("[KEY] new channel [%s]: %s", channel, newKey) + return } -func (r *RoomKeysType) GetChannel(key string) string { - channel, found := r.mapKeyChan.Load(key) +func (r *RoomKeysType) GetChannel(key string) (channel string, err error) { + if !saveInFile { + return redisCli.Get(key).Result() + } + + chann, found := r.mapKeyChan.Load(key) if found { - return channel.(string) + return chann.(string), nil } else { - return "" + return "", fmt.Errorf("%s does not exists", key) } } func (r *RoomKeysType) DeleteChannel(channel string) bool { + if !saveInFile { + return redisCli.Del(channel).Err() != nil + } + key, ok := r.mapChanKey.Load(channel) if ok { r.mapChanKey.Delete(channel) @@ -122,6 +175,10 @@ func (r *RoomKeysType) DeleteChannel(channel string) bool { } func (r *RoomKeysType) DeleteKey(key string) bool { + if !saveInFile { + return redisCli.Del(key).Err() != nil + } + channel, ok := r.mapKeyChan.Load(key) if ok { r.mapChanKey.Delete(channel) @@ -131,7 +188,6 @@ func (r *RoomKeysType) DeleteKey(key string) bool { return false } - // helpers var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") diff --git a/configure/liveconfig.go b/configure/liveconfig.go index e701c3a6..af5350cb 100644 --- a/configure/liveconfig.go +++ b/configure/liveconfig.go @@ -2,6 +2,7 @@ package configure import ( "encoding/json" + "flag" "io/ioutil" "log" ) @@ -18,6 +19,11 @@ import ( ] } */ +var ( + roomKeySaveFile = flag.String("KeyFile", "room_keys.json", "path to save room keys") + RedisAddr = flag.String("redis_addr", "", "redis addr to save room keys ex. localhost:6379") + RedisPwd = flag.String("redis_pwd", "", "redis password") +) type Application struct { Appname string `json:"appname"` @@ -32,8 +38,11 @@ type JWTCfg struct { } type ServerCfg struct { - JWTCfg `json:"jwt"` - Server []Application `json:"server"` + KeyFile string `json:"key_file"` + RedisAddr string `json:"redis_addr"` + RedisPwd string `json:"redis_pwd"` + JWTCfg `json:"jwt"` + Server []Application `json:"server"` } var RtmpServercfg ServerCfg @@ -54,9 +63,40 @@ func LoadConfig(configfilename string) error { return err } log.Printf("get config json data:%v", RtmpServercfg) + + Init() + return nil } +func GetKeyFile() *string { + if len(RtmpServercfg.KeyFile) > 0 { + *roomKeySaveFile = RtmpServercfg.KeyFile + } + + return roomKeySaveFile +} + +func GetRedisAddr() *string { + if len(RtmpServercfg.RedisAddr) > 0 { + *RedisAddr = RtmpServercfg.RedisAddr + } + + if len(*RedisAddr) == 0 { + return nil + } + + return RedisAddr +} + +func GetRedisPwd() *string { + if len(RtmpServercfg.RedisPwd) > 0 { + *RedisPwd = RtmpServercfg.RedisPwd + } + + return RedisPwd +} + func CheckAppName(appname string) bool { for _, app := range RtmpServercfg.Server { if (app.Appname == appname) && (app.Liveon == "on") { diff --git a/go.mod b/go.mod index 79058cdd..dbd43d6c 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,12 @@ go 1.13 require ( github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/go-redis/redis/v7 v7.2.0 github.com/gorilla/mux v1.7.4 // indirect - github.com/kr/pretty v0.1.0 // indirect + github.com/labstack/echo/v4 v4.1.16 // indirect github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 github.com/satori/go.uuid v1.2.0 github.com/smartystreets/goconvey v1.6.4 // indirect github.com/stretchr/testify v1.4.0 github.com/urfave/negroni v1.0.0 // indirect - gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect ) diff --git a/go.sum b/go.sum index 5bab2014..f4118db3 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,18 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U= +github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs= +github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -15,6 +23,23 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/labstack/echo/v4 v4.1.16 h1:8swiwjE5Jkai3RPfZoahp8kjVCRNq+y7Q0hPji2Kz0o= +github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI= +github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= +github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= +github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= +github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 h1:lNCW6THrCKBiJBpz8kbVGjC7MgdCGKwuvBgc7LoD6sw= github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -30,13 +55,46 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4= +github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw= +golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= +golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index e207a347..c10b473f 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -24,6 +24,7 @@ type Response struct { func (r *Response) SendJson() (int, error) { resp, _ := json.Marshal(r) + r.w.WriteHeader(r.Status) r.w.Header().Set("Content-Type", "application/json") return r.w.Write(resp) } @@ -292,7 +293,21 @@ func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) { return } room := r.Form["room"][0] - fmt.Fprintf(w, configure.RoomKeys.SetKey(room)) + + status := 200 + msg, err := configure.RoomKeys.SetKey(room) + + if err != nil { + msg = err.Error() + status = 400 + } + + res := &Response{ + w: w, + Message: msg, + Status: status, + } + res.SendJson() } //http://127.0.0.1:8090/control/get?room=ROOM_NAME @@ -302,7 +317,21 @@ func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) { return } room := r.Form["room"][0] - fmt.Fprintf(w, configure.RoomKeys.GetKey(room)) + + status := 200 + msg, err := configure.RoomKeys.GetKey(room) + + if err != nil { + msg = err.Error() + status = 400 + } + + res := &Response{ + w: w, + Message: msg, + Status: status, + } + res.SendJson() } //http://127.0.0.1:8090/control/delete?room=ROOM_NAME diff --git a/protocol/rtmp/rtmp.go b/protocol/rtmp/rtmp.go index c5aea81c..f81ecf36 100755 --- a/protocol/rtmp/rtmp.go +++ b/protocol/rtmp/rtmp.go @@ -4,7 +4,6 @@ import ( "errors" "flag" "fmt" - "github.com/gwuhaolin/livego/utils/uid" "log" "net" "net/url" @@ -12,6 +11,8 @@ import ( "strings" "time" + "github.com/gwuhaolin/livego/utils/uid" + "github.com/gwuhaolin/livego/av" "github.com/gwuhaolin/livego/configure" "github.com/gwuhaolin/livego/container/flv" @@ -122,8 +123,8 @@ func (s *Server) handleConn(conn *core.Conn) error { log.Printf("handleConn: IsPublisher=%v", connServer.IsPublisher()) if connServer.IsPublisher() { - var channel = configure.RoomKeys.GetChannel(name) - if channel == "" { + channel, err := configure.RoomKeys.GetChannel(name) + if err != nil { err := errors.New(fmt.Sprintf("invalid key")) conn.Close() log.Println("CheckKey err:", err) From 9f0f3b1c030d4d723f3ab556bcddc2c5c76c8249 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 16:39:08 +0200 Subject: [PATCH 12/17] Dockerfile --- Dockerfile | 7 +++++-- README.md | 5 ++--- .livego.json => config/livego.json | 0 main.go | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) rename .livego.json => config/livego.json (100%) diff --git a/Dockerfile b/Dockerfile index 76656715..37aa7d11 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,17 +3,20 @@ WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o livego ./ +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o livego . FROM alpine:latest LABEL maintainer="Ruben Cid Lara " +RUN mkdir -p /app/config +WORKDIR /app ENV RTMP_PORT 1935 ENV HTTP_FLV_PORT 7001 ENV HLS_PORT 7002 ENV HTTP_OPERATION_PORT 8090 +COPY --from=builder /app/config ./config COPY --from=builder /app/livego . EXPOSE ${RTMP_PORT} EXPOSE ${HTTP_FLV_PORT} EXPOSE ${HLS_PORT} EXPOSE ${HTTP_OPERATION_PORT} -CMD ./livego \ No newline at end of file +ENTRYPOINT ["./livego"] diff --git a/README.md b/README.md index 3c4da4fe..71a0b2d4 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ Simple and efficient live broadcast server: After directly downloading the compiled [binary file](https://github.com/gwuhaolin/livego/releases), execute it on the command line. #### Boot from Docker -Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuhaolin / livego` to start +Run `docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gwuhaolin/livego` to start #### Compile from source -1. Download the source code `git clone https: // github.com / gwuhaolin / livego.git` +1. Download the source code `git clone https://github.com/gwuhaolin/livego.git` 2. Go to the livego directory and execute `go build` ## Use @@ -37,7 +37,6 @@ Run `docker run -p 1935: 1935 -p 7001: 7001 -p 7002: 7002 -d --name livego gwuha -`FLV`:`http://127.0.0.1:7001/live/movie.flv` -`HLS`:`http://127.0.0.1:7002/live/movie.m3u8` - ### [Use with flv.js](https://github.com/gwuhaolin/blog/issues/3) Interested in Golang? Please see [Golang Chinese Learning Materials Summary](http://go.wuhaolin.cn/) diff --git a/.livego.json b/config/livego.json similarity index 100% rename from .livego.json rename to config/livego.json diff --git a/main.go b/main.go index bdd45ffc..3c2d83f5 100755 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ var ( httpFlvAddr = flag.String("httpflv-addr", ":7001", "HTTP-FLV server listen address") hlsAddr = flag.String("hls-addr", ":7002", "HLS server listen address") operaAddr = flag.String("manage-addr", ":8090", "HTTP manage interface server listen address") - configfilename = flag.String("cfgfile", ".livego.json", "configure filename") + configfilename = flag.String("config-file", "config/livego.json", "configure filename") ) func init() { From 3e85a56cfb7ac114240a92aab3cfdb0cc7359d8f Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 16:59:30 +0200 Subject: [PATCH 13/17] Gomod recreated --- .gitignore | 3 ++- go.mod | 4 ++-- go.sum | 34 ++++++---------------------------- 3 files changed, 10 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index be692409..9836eec0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ dist room_keys.json .vscode -.tmp \ No newline at end of file +.tmp +vendor \ No newline at end of file diff --git a/go.mod b/go.mod index dbd43d6c..6b4faef3 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/gwuhaolin/livego +module livego go 1.13 @@ -7,7 +7,7 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-redis/redis/v7 v7.2.0 github.com/gorilla/mux v1.7.4 // indirect - github.com/labstack/echo/v4 v4.1.16 // indirect + github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7 github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 github.com/satori/go.uuid v1.2.0 github.com/smartystreets/goconvey v1.6.4 // indirect diff --git a/go.sum b/go.sum index f4118db3..afae03f0 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,20 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U= github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs= github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7 h1:Hwr5HBWp2+ftb74B9dgSHgN2Z7ndoYEkX9TqkPeegJ0= +github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7/go.mod h1:XPGYyS5lXw2ForUaQjb/hJbzhuKPjnLxcGgSWCXZUGQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= @@ -23,18 +27,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= -github.com/labstack/echo/v4 v4.1.16 h1:8swiwjE5Jkai3RPfZoahp8kjVCRNq+y7Q0hPji2Kz0o= -github.com/labstack/echo/v4 v4.1.16/go.mod h1:awO+5TzAjvL8XpibdsfXxPgHr+orhtXZJZIQCVjogKI= -github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= -github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= -github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= -github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= -github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= -github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= @@ -53,34 +45,20 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= -github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= -github.com/valyala/fasttemplate v1.1.0 h1:RZqt0yGBsps8NGvLSGW804QQqCUYYLsaOjTVHy1Ocw4= -github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw= -golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= From dd55887620c2fe0e831b7ff28ebeffe3321c3be0 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 17:00:49 +0200 Subject: [PATCH 14/17] docker image --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 71a0b2d4..c8e772ea 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Simple and efficient live broadcast server: After directly downloading the compiled [binary file](https://github.com/gwuhaolin/livego/releases), execute it on the command line. #### Boot from Docker -Run `docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gwuhaolin/livego` to start +Run `docker run -p 1935:1935 -p 7001:7001 -p 7002:7002 -d --name livego gnurub/livego` to start #### Compile from source 1. Download the source code `git clone https://github.com/gwuhaolin/livego.git` From bc882c1e7cde87db32b925b8ca6540376f6f2339 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 17:27:27 +0200 Subject: [PATCH 15/17] current remopo --- container/flv/demuxer.go | 2 +- container/flv/muxer.go | 10 +++++----- container/flv/tag.go | 2 +- container/ts/muxer.go | 2 +- container/ts/muxer_test.go | 3 ++- go.mod | 1 - go.sum | 6 ------ main.go | 10 +++++----- parser/aac/parser.go | 2 +- parser/parser.go | 8 ++++---- protocol/hls/hls.go | 5 +++-- protocol/hls/source.go | 8 ++++---- protocol/httpflv/server.go | 4 ++-- protocol/httpflv/writer.go | 8 ++++---- protocol/httpopera/http_opera.go | 9 +++++---- protocol/rtmp/cache/cache.go | 2 +- protocol/rtmp/cache/gop.go | 2 +- protocol/rtmp/cache/special.go | 4 ++-- protocol/rtmp/core/chunk_stream.go | 4 ++-- protocol/rtmp/core/chunk_stream_test.go | 3 ++- protocol/rtmp/core/conn.go | 4 ++-- protocol/rtmp/core/conn_client.go | 4 ++-- protocol/rtmp/core/conn_server.go | 4 ++-- protocol/rtmp/core/conn_test.go | 3 ++- protocol/rtmp/core/handshake.go | 2 +- protocol/rtmp/rtmp.go | 10 +++++----- protocol/rtmp/rtmprelay/rtmprelay.go | 4 ++-- protocol/rtmp/rtmprelay/staticrelay.go | 6 +++--- protocol/rtmp/stream.go | 9 +++++---- utils/queue/queue.go | 2 +- 30 files changed, 71 insertions(+), 72 deletions(-) diff --git a/container/flv/demuxer.go b/container/flv/demuxer.go index 2c205ba3..bfcc7842 100755 --- a/container/flv/demuxer.go +++ b/container/flv/demuxer.go @@ -3,7 +3,7 @@ package flv import ( "errors" - "github.com/gwuhaolin/livego/av" + "livego/av" ) var ( diff --git a/container/flv/muxer.go b/container/flv/muxer.go index 08cc266c..a7eaa884 100755 --- a/container/flv/muxer.go +++ b/container/flv/muxer.go @@ -9,15 +9,15 @@ import ( "strings" "time" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/amf" - "github.com/gwuhaolin/livego/utils/pio" - "github.com/gwuhaolin/livego/utils/uid" + "livego/av" + "livego/protocol/amf" + "livego/utils/pio" + "livego/utils/uid" ) var ( flvHeader = []byte{0x46, 0x4c, 0x56, 0x01, 0x05, 0x00, 0x00, 0x00, 0x09} - flvDir = flag.String("flvDir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv") + flvDir = flag.String("flvDir", "tmp", "output flv file at flvDir/APP/KEY_TIME.flv") ) /* diff --git a/container/flv/tag.go b/container/flv/tag.go index 5c85466e..d2aec4f5 100755 --- a/container/flv/tag.go +++ b/container/flv/tag.go @@ -3,7 +3,7 @@ package flv import ( "fmt" - "github.com/gwuhaolin/livego/av" + "livego/av" ) type flvTag struct { diff --git a/container/ts/muxer.go b/container/ts/muxer.go index cbb9981d..8f7ad3d2 100755 --- a/container/ts/muxer.go +++ b/container/ts/muxer.go @@ -3,7 +3,7 @@ package ts import ( "io" - "github.com/gwuhaolin/livego/av" + "livego/av" ) const ( diff --git a/container/ts/muxer_test.go b/container/ts/muxer_test.go index 52b36685..5ee3cf38 100755 --- a/container/ts/muxer_test.go +++ b/container/ts/muxer_test.go @@ -3,7 +3,8 @@ package ts import ( "testing" - "github.com/gwuhaolin/livego/av" + "livego/av" + "github.com/stretchr/testify/assert" ) diff --git a/go.mod b/go.mod index 6b4faef3..5294b52a 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,6 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-redis/redis/v7 v7.2.0 github.com/gorilla/mux v1.7.4 // indirect - github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7 github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6 github.com/satori/go.uuid v1.2.0 github.com/smartystreets/goconvey v1.6.4 // indirect diff --git a/go.sum b/go.sum index afae03f0..000ad9eb 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/go-redis/redis v6.15.7+incompatible h1:3skhDh95XQMpnqeqNftPkQD9jL9e5e36z/1SUm6dy1U= github.com/go-redis/redis/v7 v7.2.0 h1:CrCexy/jYWZjW0AyVoHlcJUeZN19VWlbepTh1Vq6dJs= github.com/go-redis/redis/v7 v7.2.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -16,8 +15,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7 h1:Hwr5HBWp2+ftb74B9dgSHgN2Z7ndoYEkX9TqkPeegJ0= -github.com/gwuhaolin/livego v0.0.0-20191025074832-36aaf27ee5c7/go.mod h1:XPGYyS5lXw2ForUaQjb/hJbzhuKPjnLxcGgSWCXZUGQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= @@ -45,8 +42,6 @@ github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc= github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -72,7 +67,6 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 3c2d83f5..09575bc8 100755 --- a/main.go +++ b/main.go @@ -6,11 +6,11 @@ import ( "net" "time" - "github.com/gwuhaolin/livego/configure" - "github.com/gwuhaolin/livego/protocol/hls" - "github.com/gwuhaolin/livego/protocol/httpflv" - "github.com/gwuhaolin/livego/protocol/httpopera" - "github.com/gwuhaolin/livego/protocol/rtmp" + "livego/configure" + "livego/protocol/hls" + "livego/protocol/httpflv" + "livego/protocol/httpopera" + "livego/protocol/rtmp" ) var ( diff --git a/parser/aac/parser.go b/parser/aac/parser.go index e9caf57a..7ad9f05f 100755 --- a/parser/aac/parser.go +++ b/parser/aac/parser.go @@ -4,7 +4,7 @@ import ( "errors" "io" - "github.com/gwuhaolin/livego/av" + "livego/av" ) type mpegExtension struct { diff --git a/parser/parser.go b/parser/parser.go index 05b4d730..31bdd4fa 100755 --- a/parser/parser.go +++ b/parser/parser.go @@ -4,10 +4,10 @@ import ( "errors" "io" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/parser/aac" - "github.com/gwuhaolin/livego/parser/h264" - "github.com/gwuhaolin/livego/parser/mp3" + "livego/av" + "livego/parser/aac" + "livego/parser/h264" + "livego/parser/mp3" ) var ( diff --git a/protocol/hls/hls.go b/protocol/hls/hls.go index 749b81ae..0648f033 100755 --- a/protocol/hls/hls.go +++ b/protocol/hls/hls.go @@ -11,8 +11,9 @@ import ( "strings" "time" - "github.com/gwuhaolin/livego/av" - "github.com/orcaman/concurrent-map" + "livego/av" + + cmap "github.com/orcaman/concurrent-map" ) const ( diff --git a/protocol/hls/source.go b/protocol/hls/source.go index c2fc7a0f..60f4f832 100644 --- a/protocol/hls/source.go +++ b/protocol/hls/source.go @@ -7,10 +7,10 @@ import ( "log" "time" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/container/flv" - "github.com/gwuhaolin/livego/container/ts" - "github.com/gwuhaolin/livego/parser" + "livego/av" + "livego/container/flv" + "livego/container/ts" + "livego/parser" ) const ( diff --git a/protocol/httpflv/server.go b/protocol/httpflv/server.go index 8c46c238..1974dbbf 100644 --- a/protocol/httpflv/server.go +++ b/protocol/httpflv/server.go @@ -7,8 +7,8 @@ import ( "net/http" "strings" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/rtmp" + "livego/av" + "livego/protocol/rtmp" ) type Server struct { diff --git a/protocol/httpflv/writer.go b/protocol/httpflv/writer.go index 70ed67b1..e97e6006 100755 --- a/protocol/httpflv/writer.go +++ b/protocol/httpflv/writer.go @@ -7,10 +7,10 @@ import ( "net/http" "time" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/amf" - "github.com/gwuhaolin/livego/utils/pio" - "github.com/gwuhaolin/livego/utils/uid" + "livego/av" + "livego/protocol/amf" + "livego/utils/pio" + "livego/utils/uid" ) const ( diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index c10b473f..7816cb71 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -8,12 +8,13 @@ import ( "net" "net/http" + "livego/av" + "livego/configure" + "livego/protocol/rtmp" + "livego/protocol/rtmp/rtmprelay" + jwtmiddleware "github.com/auth0/go-jwt-middleware" "github.com/dgrijalva/jwt-go" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/configure" - "github.com/gwuhaolin/livego/protocol/rtmp" - "github.com/gwuhaolin/livego/protocol/rtmp/rtmprelay" ) type Response struct { diff --git a/protocol/rtmp/cache/cache.go b/protocol/rtmp/cache/cache.go index a5c604b7..b644e94d 100755 --- a/protocol/rtmp/cache/cache.go +++ b/protocol/rtmp/cache/cache.go @@ -3,7 +3,7 @@ package cache import ( "flag" - "github.com/gwuhaolin/livego/av" + "livego/av" ) var ( diff --git a/protocol/rtmp/cache/gop.go b/protocol/rtmp/cache/gop.go index ececab4b..401bab08 100755 --- a/protocol/rtmp/cache/gop.go +++ b/protocol/rtmp/cache/gop.go @@ -3,7 +3,7 @@ package cache import ( "errors" - "github.com/gwuhaolin/livego/av" + "livego/av" ) var ( diff --git a/protocol/rtmp/cache/special.go b/protocol/rtmp/cache/special.go index 89c9693b..dce82a3f 100755 --- a/protocol/rtmp/cache/special.go +++ b/protocol/rtmp/cache/special.go @@ -4,8 +4,8 @@ import ( "bytes" "log" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/amf" + "livego/av" + "livego/protocol/amf" ) const ( diff --git a/protocol/rtmp/core/chunk_stream.go b/protocol/rtmp/core/chunk_stream.go index 33970c48..379646b2 100755 --- a/protocol/rtmp/core/chunk_stream.go +++ b/protocol/rtmp/core/chunk_stream.go @@ -4,8 +4,8 @@ import ( "encoding/binary" "fmt" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/utils/pool" + "livego/av" + "livego/utils/pool" ) type ChunkStream struct { diff --git a/protocol/rtmp/core/chunk_stream_test.go b/protocol/rtmp/core/chunk_stream_test.go index 749c7abb..94cd05a6 100755 --- a/protocol/rtmp/core/chunk_stream_test.go +++ b/protocol/rtmp/core/chunk_stream_test.go @@ -4,7 +4,8 @@ import ( "bytes" "testing" - "github.com/gwuhaolin/livego/utils/pool" + "livego/utils/pool" + "github.com/stretchr/testify/assert" ) diff --git a/protocol/rtmp/core/conn.go b/protocol/rtmp/core/conn.go index 610a58c8..2e563a9f 100755 --- a/protocol/rtmp/core/conn.go +++ b/protocol/rtmp/core/conn.go @@ -5,8 +5,8 @@ import ( "net" "time" - "github.com/gwuhaolin/livego/utils/pio" - "github.com/gwuhaolin/livego/utils/pool" + "livego/utils/pio" + "livego/utils/pool" ) const ( diff --git a/protocol/rtmp/core/conn_client.go b/protocol/rtmp/core/conn_client.go index 566e67fc..42db529d 100755 --- a/protocol/rtmp/core/conn_client.go +++ b/protocol/rtmp/core/conn_client.go @@ -12,8 +12,8 @@ import ( "log" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/amf" + "livego/av" + "livego/protocol/amf" ) var ( diff --git a/protocol/rtmp/core/conn_server.go b/protocol/rtmp/core/conn_server.go index 006b5204..f1bb88d9 100755 --- a/protocol/rtmp/core/conn_server.go +++ b/protocol/rtmp/core/conn_server.go @@ -7,8 +7,8 @@ import ( "log" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/amf" + "livego/av" + "livego/protocol/amf" ) var ( diff --git a/protocol/rtmp/core/conn_test.go b/protocol/rtmp/core/conn_test.go index 170543de..1f4cb9b4 100755 --- a/protocol/rtmp/core/conn_test.go +++ b/protocol/rtmp/core/conn_test.go @@ -5,7 +5,8 @@ import ( "io" "testing" - "github.com/gwuhaolin/livego/utils/pool" + "livego/utils/pool" + "github.com/stretchr/testify/assert" ) diff --git a/protocol/rtmp/core/handshake.go b/protocol/rtmp/core/handshake.go index e5583768..61a79b20 100755 --- a/protocol/rtmp/core/handshake.go +++ b/protocol/rtmp/core/handshake.go @@ -10,7 +10,7 @@ import ( "time" - "github.com/gwuhaolin/livego/utils/pio" + "livego/utils/pio" ) var ( diff --git a/protocol/rtmp/rtmp.go b/protocol/rtmp/rtmp.go index f81ecf36..d999c9bb 100755 --- a/protocol/rtmp/rtmp.go +++ b/protocol/rtmp/rtmp.go @@ -11,12 +11,12 @@ import ( "strings" "time" - "github.com/gwuhaolin/livego/utils/uid" + "livego/utils/uid" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/configure" - "github.com/gwuhaolin/livego/container/flv" - "github.com/gwuhaolin/livego/protocol/rtmp/core" + "livego/av" + "livego/configure" + "livego/container/flv" + "livego/protocol/rtmp/core" ) const ( diff --git a/protocol/rtmp/rtmprelay/rtmprelay.go b/protocol/rtmp/rtmprelay/rtmprelay.go index 1980b9b5..5434b0e9 100644 --- a/protocol/rtmp/rtmprelay/rtmprelay.go +++ b/protocol/rtmp/rtmprelay/rtmprelay.go @@ -7,8 +7,8 @@ import ( "io" "log" - "github.com/gwuhaolin/livego/protocol/amf" - "github.com/gwuhaolin/livego/protocol/rtmp/core" + "livego/protocol/amf" + "livego/protocol/rtmp/core" ) var ( diff --git a/protocol/rtmp/rtmprelay/staticrelay.go b/protocol/rtmp/rtmprelay/staticrelay.go index 4fb17ff4..00e6cf9f 100644 --- a/protocol/rtmp/rtmprelay/staticrelay.go +++ b/protocol/rtmp/rtmprelay/staticrelay.go @@ -6,9 +6,9 @@ import ( "log" "sync" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/configure" - "github.com/gwuhaolin/livego/protocol/rtmp/core" + "livego/av" + "livego/configure" + "livego/protocol/rtmp/core" ) type StaticPush struct { diff --git a/protocol/rtmp/stream.go b/protocol/rtmp/stream.go index a5dbaed6..381cef0b 100755 --- a/protocol/rtmp/stream.go +++ b/protocol/rtmp/stream.go @@ -6,10 +6,11 @@ import ( "strings" "time" - "github.com/gwuhaolin/livego/av" - "github.com/gwuhaolin/livego/protocol/rtmp/cache" - "github.com/gwuhaolin/livego/protocol/rtmp/rtmprelay" - "github.com/orcaman/concurrent-map" + "livego/av" + "livego/protocol/rtmp/cache" + "livego/protocol/rtmp/rtmprelay" + + cmap "github.com/orcaman/concurrent-map" ) var ( diff --git a/utils/queue/queue.go b/utils/queue/queue.go index fc82ff9b..03e4b6f7 100755 --- a/utils/queue/queue.go +++ b/utils/queue/queue.go @@ -3,7 +3,7 @@ package queue import ( "sync" - "github.com/gwuhaolin/livego/av" + "livego/av" ) // Queue is a basic FIFO queue for Messages. From 170a1fa5fd52df6eeb7e82ed817e898c3f9b1ed6 Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 17:31:15 +0200 Subject: [PATCH 16/17] removed redis add from config file --- config/livego.json | 1 - 1 file changed, 1 deletion(-) diff --git a/config/livego.json b/config/livego.json index 9cb50ba8..47bab74c 100644 --- a/config/livego.json +++ b/config/livego.json @@ -1,5 +1,4 @@ { - "redis_addr": "localhost:6379", "server": [ { "appname": "live", From 24928da0e1b5f576e04c47b939bf13c66b6cabbe Mon Sep 17 00:00:00 2001 From: Ruben Cid Date: Wed, 8 Apr 2020 17:57:35 +0200 Subject: [PATCH 17/17] Json api responses --- protocol/httpopera/http_opera.go | 157 +++++++++++++++++++++---------- 1 file changed, 109 insertions(+), 48 deletions(-) diff --git a/protocol/httpopera/http_opera.go b/protocol/httpopera/http_opera.go index 7816cb71..584f0e14 100755 --- a/protocol/httpopera/http_opera.go +++ b/protocol/httpopera/http_opera.go @@ -3,7 +3,6 @@ package httpopera import ( "encoding/json" "fmt" - "io" "log" "net" "net/http" @@ -18,15 +17,15 @@ import ( ) type Response struct { - w http.ResponseWriter - Status int `json:"status"` - Message string `json:"message"` + w http.ResponseWriter + Status int `json:"status"` + Data interface{} `json:"data"` } func (r *Response) SendJson() (int, error) { resp, _ := json.Marshal(r) - r.w.WriteHeader(r.Status) r.w.Header().Set("Content-Type", "application/json") + r.w.WriteHeader(r.Status) return r.w.Write(resp) } @@ -135,9 +134,18 @@ type streams struct { //http://127.0.0.1:8090/stat/livestat func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) { + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + + defer res.SendJson() + rtmpStream := server.handler.(*rtmp.RtmpStream) if rtmpStream == nil { - io.WriteString(w, "

Get rtmp stream information error

") + res.Status = 500 + res.Data = "Get rtmp stream information error" return } @@ -172,9 +180,9 @@ func (server *Server) GetLiveStatics(w http.ResponseWriter, req *http.Request) { } } } + resp, _ := json.Marshal(msgs) - w.Header().Set("Content-Type", "application/json") - w.Write(resp) + res.Data = resp } //http://127.0.0.1:8090/control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456 @@ -182,8 +190,17 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { var retString string var err error + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + + defer res.SendJson() + if req.ParseForm() != nil { - fmt.Fprintf(w, "url: /control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456") + res.Status = 400 + res.Data = "url: /control/pull?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456" return } @@ -194,7 +211,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { log.Printf("control pull: oper=%v, app=%v, name=%v, url=%v", oper, app, name, url) if (len(app) <= 0) || (len(name) <= 0) || (len(url) <= 0) { - io.WriteString(w, "control push parameter error, please check them.
") + res.Status = 400 + res.Data = "control push parameter error, please check them." return } @@ -207,7 +225,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { if !found { retString = fmt.Sprintf("session key[%s] not exist, please check it again.", keyString) - io.WriteString(w, retString) + res.Status = 400 + res.Data = retString return } log.Printf("rtmprelay stop push %s from %s", remoteurl, localurl) @@ -215,7 +234,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { delete(s.session, keyString) retString = fmt.Sprintf("

push url stop %s ok


", url[0]) - io.WriteString(w, retString) + res.Status = 400 + res.Data = retString log.Printf("pull stop return %s", retString) } else { pullRtmprelay := rtmprelay.NewRtmpRelay(&localurl, &remoteurl) @@ -227,7 +247,8 @@ func (s *Server) handlePull(w http.ResponseWriter, req *http.Request) { s.session[keyString] = pullRtmprelay retString = fmt.Sprintf("

push url start %s ok


", url[0]) } - io.WriteString(w, retString) + res.Status = 400 + res.Data = retString log.Printf("pull start return %s", retString) } } @@ -237,8 +258,16 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { var retString string var err error + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + + defer res.SendJson() + if req.ParseForm() != nil { - fmt.Fprintf(w, "url: /control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456") + res.Data = "url: /control/push?&oper=start&app=live&name=123456&url=rtmp://192.168.16.136/live/123456" return } @@ -249,7 +278,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { log.Printf("control push: oper=%v, app=%v, name=%v, url=%v", oper, app, name, url) if (len(app) <= 0) || (len(name) <= 0) || (len(url) <= 0) { - io.WriteString(w, "control push parameter error, please check them.
") + res.Data = "control push parameter error, please check them." return } @@ -261,7 +290,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { pushRtmprelay, found := s.session[keyString] if !found { retString = fmt.Sprintf("

session key[%s] not exist, please check it again.

", keyString) - io.WriteString(w, retString) + res.Data = retString return } log.Printf("rtmprelay stop push %s from %s", remoteurl, localurl) @@ -269,7 +298,7 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { delete(s.session, keyString) retString = fmt.Sprintf("

push url stop %s ok


", url[0]) - io.WriteString(w, retString) + res.Data = retString log.Printf("push stop return %s", retString) } else { pushRtmprelay := rtmprelay.NewRtmpRelay(&localurl, &remoteurl) @@ -282,69 +311,101 @@ func (s *Server) handlePush(w http.ResponseWriter, req *http.Request) { s.session[keyString] = pushRtmprelay } - io.WriteString(w, retString) + res.Data = retString log.Printf("push start return %s", retString) } } //http://127.0.0.1:8090/control/reset?room=ROOM_NAME func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) { - if r.ParseForm() != nil { - fmt.Fprintf(w, "url: /control/reset?room=ROOM_NAME") + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + defer res.SendJson() + + if err := r.ParseForm(); err != nil { + res.Status = 400 + res.Data = "url: /control/reset?room=ROOM_NAME" + return + } + room := r.Form.Get("room") + + if len(room) == 0 { + res.Status = 400 + res.Data = "url: /control/get?room=ROOM_NAME" return } - room := r.Form["room"][0] - status := 200 msg, err := configure.RoomKeys.SetKey(room) if err != nil { msg = err.Error() - status = 400 + res.Status = 400 } - res := &Response{ - w: w, - Message: msg, - Status: status, - } - res.SendJson() + res.Data = msg } //http://127.0.0.1:8090/control/get?room=ROOM_NAME func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) { - if r.ParseForm() != nil { - fmt.Fprintf(w, "url: /control/get?room=ROOM_NAME") + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + defer res.SendJson() + + if err := r.ParseForm(); err != nil { + res.Status = 400 + res.Data = "url: /control/get?room=ROOM_NAME" return } - room := r.Form["room"][0] - status := 200 - msg, err := configure.RoomKeys.GetKey(room) + room := r.Form.Get("room") - if err != nil { - msg = err.Error() - status = 400 + if len(room) == 0 { + res.Status = 400 + res.Data = "url: /control/get?room=ROOM_NAME" + return } - res := &Response{ - w: w, - Message: msg, - Status: status, + msg, err := configure.RoomKeys.GetKey(room) + if err != nil { + msg = err.Error() + res.Status = 400 } - res.SendJson() + res.Data = msg } //http://127.0.0.1:8090/control/delete?room=ROOM_NAME func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) { - if r.ParseForm() != nil { - fmt.Fprintf(w, "url: /control/delete?room=ROOM_NAME") + res := &Response{ + w: w, + Data: nil, + Status: 200, + } + defer res.SendJson() + + if err := r.ParseForm(); err != nil { + res.Status = 400 + res.Data = "url: /control/delete?room=ROOM_NAME" return } - room := r.Form["room"][0] + + room := r.Form.Get("room") + + if len(room) == 0 { + res.Status = 400 + res.Data = "url: /control/get?room=ROOM_NAME" + return + } + if configure.RoomKeys.DeleteChannel(room) { - fmt.Fprintf(w, "OK") - } else { - fmt.Fprintf(w, "Room Not Found") + res.Data = "Ok" + return } + res.Status = 404 + res.Data = "Room not found" }