-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtmp_callback.go
136 lines (98 loc) · 2.38 KB
/
rtmp_callback.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
// RTMP callback
package main
import (
"os"
"time"
"net/http"
"github.com/golang-jwt/jwt"
)
const JWT_EXPIRATION_TIME_SECONDS = 120
func (s *RTMPSession) SendStartCallback() bool {
JWT_SECRET := os.Getenv("JWT_SECRET")
CALLBACK_URL := os.Getenv("CALLBACK_URL")
if CALLBACK_URL == "" {
return true // No callback
}
LogDebugSession(s.id, s.ip, "POST "+CALLBACK_URL+" | Event: START | Channel: "+s.channel)
var subject = os.Getenv("CUSTOM_JWT_SUBJECT")
if subject == "" {
subject = "rtmp_event"
}
exp := time.Now().Unix() + JWT_EXPIRATION_TIME_SECONDS
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": subject,
"event": "start",
"channel": s.channel,
"key": s.key,
"client_ip": s.ip,
"rtmp_host": s.server.host,
"rtmp_port": s.server.port,
"exp": exp,
})
tokenb64, e := token.SignedString([]byte(JWT_SECRET))
if e != nil {
LogError(e)
return false
}
client := &http.Client{}
req, e := http.NewRequest("POST", CALLBACK_URL, nil)
if e != nil {
LogError(e)
return false
}
req.Header.Set("rtmp-event", tokenb64)
res, e := client.Do(req)
if e != nil {
LogError(e)
return false
}
if res.StatusCode != 200 {
return false
}
s.stream_id = res.Header.Get("stream-id")
LogDebugSession(s.id, s.ip, "Stream ID: "+s.stream_id)
return true
}
func (s *RTMPSession) SendStopCallback() bool {
JWT_SECRET := os.Getenv("JWT_SECRET")
CALLBACK_URL := os.Getenv("CALLBACK_URL")
if CALLBACK_URL == "" {
return true // No callback
}
LogDebugSession(s.id, s.ip, "POST "+CALLBACK_URL+" | Event: STOP | Channel: "+s.channel)
var subject = os.Getenv("CUSTOM_JWT_SUBJECT")
if subject == "" {
subject = "rtmp_event"
}
exp := time.Now().Unix() + JWT_EXPIRATION_TIME_SECONDS
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": subject,
"event": "stop",
"channel": s.channel,
"key": s.key,
"stream_id": s.stream_id,
"client_ip": s.ip,
"exp": exp,
})
tokenb64, e := token.SignedString([]byte(JWT_SECRET))
if e != nil {
LogError(e)
return false
}
client := &http.Client{}
req, e := http.NewRequest("POST", CALLBACK_URL, nil)
if e != nil {
LogError(e)
return false
}
req.Header.Set("rtmp-event", tokenb64)
res, e := client.Do(req)
if e != nil {
LogError(e)
return false
}
if res.StatusCode != 200 {
return false
}
return true
}