-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredis_cmds.go
142 lines (117 loc) · 2.75 KB
/
redis_cmds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Redis commands
package main
import (
"context"
"crypto/tls"
"errors"
"os"
"strings"
"time"
"github.com/redis/go-redis/v9"
)
func setupRedisCommandReceiver(server *RTMPServer) {
useRedis := os.Getenv("REDIS_USE")
if useRedis != "YES" {
return // Not using redis
}
defer func() {
if err := recover(); err != nil {
switch x := err.(type) {
case string:
LogError(errors.New(x))
case error:
LogError(x)
default:
LogError(errors.New("could not connect to redis"))
}
}
LogWarning("Connection to Redis lost!")
}()
redisHost := os.Getenv("REDIS_HOST")
if redisHost == "" {
redisHost = "localhost"
}
redisPort := os.Getenv("REDIS_PORT")
if redisPort == "" {
redisPort = "6379"
}
redisPassword := os.Getenv("REDIS_PASSWORD")
redisChannel := os.Getenv("REDIS_CHANNEL")
if redisChannel == "" {
redisChannel = "rtmp_commands"
}
redisTLS := os.Getenv("REDIS_TLS")
ctx := context.Background()
var redisClient *redis.Client
if redisTLS == "YES" {
redisClient = redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort,
Password: redisPassword,
TLSConfig: &tls.Config{},
})
} else {
redisClient = redis.NewClient(&redis.Options{
Addr: redisHost + ":" + redisPort,
Password: redisPassword,
})
}
subscriber := redisClient.Subscribe(ctx, redisChannel)
LogInfo("[REDIS] Listening for commands on channel '" + redisChannel + "'")
for {
msg, err := subscriber.ReceiveMessage(ctx)
if err != nil {
LogWarning("Could not connect to Redis: " + err.Error())
time.Sleep(10 * time.Second)
} else {
// Parse message
parseRedisCommand(server, msg.Payload)
}
}
}
func parseRedisCommand(server *RTMPServer, cmd string) {
defer func() {
if err := recover(); err != nil {
switch x := err.(type) {
case string:
LogError(errors.New(x))
case error:
LogError(x)
default:
LogError(errors.New("parsing error"))
}
}
LogWarning("Could not parse message: " + cmd)
}()
parts := strings.Split(cmd, ">")
if len(parts) != 2 {
LogWarning("Invalid message from Redis: " + cmd)
return // Invalid message
}
cmdName := parts[0]
cmdArgs := strings.Split(parts[1], "|")
switch cmdName {
case "kill-session":
if len(cmdArgs) < 1 {
LogWarning("Invalid message from Redis: " + cmd)
return
}
channel := cmdArgs[0]
publisher := server.GetPublisher(channel)
if publisher != nil {
publisher.Kill()
}
case "close-stream":
if len(cmdArgs) < 2 {
LogWarning("Invalid message from Redis: " + cmd)
return
}
channel := cmdArgs[0]
streamId := cmdArgs[1]
publisher := server.GetPublisher(channel)
if publisher != nil && publisher.stream_id == streamId {
publisher.Kill()
}
default:
LogWarning("Unknown Redis command: " + cmd)
}
}