Skip to content

Commit 3eb4ba3

Browse files
committed
JWT
1 parent 7dca40f commit 3eb4ba3

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

Diff for: .livego.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"jwt": {
3+
"secret": "testing",
4+
"algorithm": "HS256s"
5+
},
26
"server": [
37
{
48
"appname": "live",

Diff for: configure/liveconfig.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ import (
2020
*/
2121

2222
type Application struct {
23-
Appname string `json:"appname"`
24-
Liveon string `json:"liveon"`
25-
Hlson string `json:"hlson"`
23+
Appname string `json:"appname"`
24+
Liveon string `json:"liveon"`
25+
Hlson string `json:"hlson"`
2626
StaticPush []string `json:"static_push"`
2727
}
2828

29+
type JWTCfg struct {
30+
Secret string `json:"secret"`
31+
Algorithm string `json:"algorithm"`
32+
}
33+
2934
type ServerCfg struct {
35+
JWTCfg `json:"jwt"`
3036
Server []Application `json:"server"`
3137
}
3238

Diff for: go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ module github.com/gwuhaolin/livego
33
go 1.13
44

55
require (
6+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b
7+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
8+
github.com/gorilla/mux v1.7.4 // indirect
69
github.com/kr/pretty v0.1.0 // indirect
710
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
811
github.com/satori/go.uuid v1.2.0
12+
github.com/smartystreets/goconvey v1.6.4 // indirect
913
github.com/stretchr/testify v1.4.0
14+
github.com/urfave/negroni v1.0.0 // indirect
1015
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1116
)

Diff for: go.sum

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b h1:CvoEHGmxWl5kONC5icxwqV899dkf4VjOScbxLpllEnw=
2+
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b/go.mod h1:LWMyo4iOLWXHGdBki7NIht1kHru/0wM179h+d3g8ATM=
13
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
24
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
6+
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
7+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
8+
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
9+
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
10+
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
11+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
312
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
413
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
514
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
1120
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1221
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
1322
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
23+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
24+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
25+
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
26+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
1427
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1528
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1629
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
30+
github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
31+
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
32+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
33+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
34+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
35+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
36+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
1737
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1838
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
1939
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

Diff for: protocol/httpopera/http_opera.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package httpopera
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/gwuhaolin/livego/configure"
76
"io"
87
"log"
98
"net"
109
"net/http"
1110

11+
jwtmiddleware "github.com/auth0/go-jwt-middleware"
12+
"github.com/dgrijalva/jwt-go"
1213
"github.com/gwuhaolin/livego/av"
14+
"github.com/gwuhaolin/livego/configure"
1315
"github.com/gwuhaolin/livego/protocol/rtmp"
1416
"github.com/gwuhaolin/livego/protocol/rtmp/rtmprelay"
1517
)
@@ -59,10 +61,38 @@ func NewServer(h av.Handler, rtmpAddr string) *Server {
5961
}
6062
}
6163

64+
func JWTMiddleware(next http.Handler) http.Handler {
65+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
if len(configure.RtmpServercfg.JWTCfg.Secret) > 0 {
67+
var algorithm jwt.SigningMethod
68+
if len(configure.RtmpServercfg.JWTCfg.Algorithm) > 0 {
69+
algorithm = jwt.GetSigningMethod(configure.RtmpServercfg.JWTCfg.Algorithm)
70+
}
71+
72+
if algorithm == nil {
73+
algorithm = jwt.SigningMethodHS256
74+
}
75+
76+
jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
77+
Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader, jwtmiddleware.FromParameter("jwt")),
78+
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
79+
return []byte(configure.RtmpServercfg.Secret), nil
80+
},
81+
SigningMethod: algorithm,
82+
})
83+
84+
jwtMiddleware.HandlerWithNext(w, r, next.ServeHTTP)
85+
return
86+
}
87+
next.ServeHTTP(w, r)
88+
89+
})
90+
}
91+
6292
func (s *Server) Serve(l net.Listener) error {
6393
mux := http.NewServeMux()
6494

65-
mux.Handle("/statics/", http.StripPrefix("/statics/", http.FileServer(http.Dir("statics"))))
95+
mux.Handle("/statics/", http.StripPrefix("/statics/", http.FileServer(http.Dir("statics"))))
6696

6797
mux.HandleFunc("/control/push", func(w http.ResponseWriter, r *http.Request) {
6898
s.handlePush(w, r)
@@ -82,14 +112,14 @@ func (s *Server) Serve(l net.Listener) error {
82112
mux.HandleFunc("/stat/livestat", func(w http.ResponseWriter, r *http.Request) {
83113
s.GetLiveStatics(w, r)
84114
})
85-
http.Serve(l, mux)
115+
http.Serve(l, JWTMiddleware(mux))
86116
return nil
87117
}
88118

89119
type stream struct {
90120
Key string `json:"key"`
91121
Url string `json:"url"`
92-
StreamId uint32 `json:"-"` // hide
122+
StreamId uint32 `json:"-"` // hide
93123
VideoTotalBytes uint64 `json:"video_total_bytes"`
94124
VideoSpeed uint64 `json:"video_speed"`
95125
AudioTotalBytes uint64 `json:"audio_total_bytes"`
@@ -98,7 +128,7 @@ type stream struct {
98128

99129
type streams struct {
100130
Publishers []stream `json:"publishers"`
101-
Players []stream `json:"-"` // hide
131+
Players []stream `json:"-"` // hide
102132
}
103133

104134
//http://127.0.0.1:8090/stat/livestat

Diff for: room_keys.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"ROOM_NAME":"VdagOakQauW5JhEeZ3qFM30fO6bzXd7skvkyboJH9xoISPe0"}

0 commit comments

Comments
 (0)