Skip to content

Commit cc4c686

Browse files
committed
add support for key authentication
1 parent 36aaf27 commit cc4c686

File tree

4 files changed

+152
-20
lines changed

4 files changed

+152
-20
lines changed

Diff for: configure/channel.go

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package configure
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"log"
7+
"math/rand"
8+
"sync"
9+
"time"
10+
)
11+
12+
const roomKeySaveFile = "room_keys.json"
13+
14+
var RoomKeys = LoadRoomKey(roomKeySaveFile)
15+
16+
var roomUpdated = false
17+
18+
func init() {
19+
rand.Seed(time.Now().UnixNano())
20+
go func() {
21+
for {
22+
time.Sleep(15 * time.Second)
23+
if roomUpdated {
24+
RoomKeys.Save(roomKeySaveFile)
25+
roomUpdated = false
26+
}
27+
}
28+
}()
29+
}
30+
31+
32+
type RoomKeysType struct {
33+
mapChanKey sync.Map
34+
mapKeyChan sync.Map
35+
}
36+
37+
func LoadRoomKey(f string) *RoomKeysType {
38+
result := &RoomKeysType {
39+
mapChanKey: sync.Map{},
40+
mapKeyChan: sync.Map{},
41+
}
42+
raw := map[string]string{}
43+
content, err := ioutil.ReadFile(f)
44+
if err != nil {
45+
log.Printf("Failed to read file %s for room keys", f)
46+
return result
47+
}
48+
if json.Unmarshal(content, &raw) != nil {
49+
log.Printf("Failed to unmarshal file %s for room keys", f)
50+
return result
51+
}
52+
for room, key := range raw {
53+
result.mapChanKey.Store(room, key)
54+
result.mapKeyChan.Store(key, room)
55+
}
56+
return result
57+
}
58+
59+
func (r *RoomKeysType) Save(f string) {
60+
raw := map[string]string{}
61+
r.mapChanKey.Range(func(channel, key interface{}) bool {
62+
raw[channel.(string)] = key.(string)
63+
return true
64+
})
65+
content, err := json.Marshal(raw)
66+
if err != nil {
67+
log.Println("Failed to marshal room keys")
68+
return
69+
}
70+
if ioutil.WriteFile(f, content, 0644) != nil {
71+
log.Println("Failed to save room keys")
72+
return
73+
}
74+
}
75+
76+
// set/reset a random key for channel
77+
func (r *RoomKeysType) SetKey(channel string) string {
78+
var key string
79+
for {
80+
key = randStringRunes(48)
81+
if _, found := r.mapKeyChan.Load(key); !found {
82+
r.mapChanKey.Store(channel, key)
83+
r.mapKeyChan.Store(key, channel)
84+
break
85+
}
86+
}
87+
roomUpdated = true
88+
return key
89+
}
90+
91+
func (r *RoomKeysType) GetKey(channel string) string {
92+
var key interface{}
93+
var found bool
94+
if key, found = r.mapChanKey.Load(channel); found {
95+
return key.(string)
96+
} else {
97+
newkey := r.SetKey(channel)
98+
log.Printf("[KEY] new channel [%s]: %s", channel, newkey)
99+
return newkey
100+
}
101+
}
102+
103+
func (r *RoomKeysType) GetChannel(key string) string {
104+
channel, found := r.mapKeyChan.Load(key)
105+
if found {
106+
return channel.(string)
107+
} else {
108+
return ""
109+
}
110+
}
111+
112+
113+
// helpers
114+
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
115+
116+
func randStringRunes(n int) string {
117+
b := make([]rune, n)
118+
for i := range b {
119+
b[i] = letterRunes[rand.Intn(len(letterRunes))]
120+
}
121+
return string(b)
122+
}

Diff for: configure/liveconfig.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,39 @@ import (
88

99
/*
1010
{
11-
[
12-
{
13-
"application":"live",
14-
"live":"on",
15-
"hls":"on",
16-
"static_push":["rtmp://xx/live"]
17-
}
18-
]
11+
"server": [
12+
{
13+
"appname": "live",
14+
"liveon": "on",
15+
"hlson": "on",
16+
"static_push": []
17+
}
18+
]
1919
}
2020
*/
21+
2122
type Application struct {
22-
Appname string
23-
Liveon string
24-
Hlson string
25-
Static_push []string
23+
Appname string `json:"appname"`
24+
Liveon string `json:"liveon"`
25+
Hlson string `json:"hlson"`
26+
StaticPush []string `json:"static_push"`
2627
}
2728

2829
type ServerCfg struct {
29-
Server []Application
30+
Server []Application `json:"server"`
3031
}
3132

3233
var RtmpServercfg ServerCfg
3334

3435
func LoadConfig(configfilename string) error {
35-
log.Printf("starting load configure file(%s)......", configfilename)
36+
log.Printf("starting load configure file %s", configfilename)
3637
data, err := ioutil.ReadFile(configfilename)
3738
if err != nil {
3839
log.Printf("ReadFile %s error:%v", configfilename, err)
3940
return err
4041
}
4142

42-
log.Printf("loadconfig: \r\n%s", string(data))
43+
// log.Printf("loadconfig: \r\n%s", string(data))
4344

4445
err = json.Unmarshal(data, &RtmpServercfg)
4546
if err != nil {
@@ -62,8 +63,8 @@ func CheckAppName(appname string) bool {
6263
func GetStaticPushUrlList(appname string) ([]string, bool) {
6364
for _, app := range RtmpServercfg.Server {
6465
if (app.Appname == appname) && (app.Liveon == "on") {
65-
if len(app.Static_push) > 0 {
66-
return app.Static_push, true
66+
if len(app.StaticPush) > 0 {
67+
return app.StaticPush, true
6768
} else {
6869
return nil, false
6970
}

Diff for: protocol/rtmp/rtmp.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"flag"
66
"fmt"
7+
"github.com/gwuhaolin/livego/utils/uid"
78
"log"
89
"net"
910
"net/url"
@@ -15,7 +16,6 @@ import (
1516
"github.com/gwuhaolin/livego/configure"
1617
"github.com/gwuhaolin/livego/container/flv"
1718
"github.com/gwuhaolin/livego/protocol/rtmp/core"
18-
"github.com/gwuhaolin/livego/utils/uid"
1919
)
2020

2121
const (
@@ -111,7 +111,7 @@ func (s *Server) handleConn(conn *core.Conn) error {
111111
return err
112112
}
113113

114-
appname, _, _ := connServer.GetInfo()
114+
appname, name, _ := connServer.GetInfo()
115115

116116
if ret := configure.CheckAppName(appname); !ret {
117117
err := errors.New(fmt.Sprintf("application name=%s is not configured", appname))
@@ -122,6 +122,14 @@ func (s *Server) handleConn(conn *core.Conn) error {
122122

123123
log.Printf("handleConn: IsPublisher=%v", connServer.IsPublisher())
124124
if connServer.IsPublisher() {
125+
var channel = configure.RoomKeys.GetChannel(name)
126+
if channel == "" {
127+
err := errors.New(fmt.Sprintf("invalid key"))
128+
conn.Close()
129+
log.Println("CheckKey err:", err)
130+
return err
131+
}
132+
connServer.PublishInfo.Name = channel
125133
if pushlist, ret := configure.GetStaticPushUrlList(appname); ret && (pushlist != nil) {
126134
log.Printf("GetStaticPushUrlList: %v", pushlist)
127135
}
@@ -138,6 +146,7 @@ func (s *Server) handleConn(conn *core.Conn) error {
138146
flvWriter := new(flv.FlvDvr)
139147
s.handler.HandleWriter(flvWriter.GetWriter(reader.Info()))
140148
} else {
149+
configure.RoomKeys.GetKey(name) // set new key if this channel not exists
141150
writer := NewVirWriter(connServer)
142151
log.Printf("new player: %+v", writer.Info())
143152
s.handler.HandleWriter(writer)

Diff for: protocol/rtmp/rtmprelay/staticrelay.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func GetStaticPushObject(rtmpurl string) (*StaticPush, error) {
6363
}
6464
g_MapLock.RUnlock()
6565

66-
return nil, errors.New(fmt.Sprintf("G_StaticPushMap[%s] not exist...."))
66+
return nil, errors.New(fmt.Sprintf("G_StaticPushMap[%s] not exist....", rtmpurl))
6767
}
6868

6969
func ReleaseStaticPushObject(rtmpurl string) {

0 commit comments

Comments
 (0)