安全论证是绝大多数应用的基本要求,如果任何人都能无限制的发布/播放视频,显然不适合。SRS中可以通过HTTPCallback机制来实现,参考下面的配置:
...
vhost __defaultVhost__ {
...
# http回调
http_hooks{
enabled on;
on_connect http://192.168.7.100:9000/srs_http_call_back;
on_close http://192.168.7.100:9000/srs_http_call_back;
on_publish http://192.168.7.100:9000/srs_http_call_back;
on_unpublish http://192.168.7.100:9000/srs_http_call_back;
on_play http://192.168.7.100:9000/srs_http_call_back;
on_stop http://192.168.7.100:9000/srs_http_call_back;
on_dvr http://192.168.7.100:9000/srs_http_call_back;
on_hls http://192.168.7.100:9000/srs_http_call_back;
on_hls_notify http://192.168.7.100:9000/srs_http_call_back;
}
}
只要打开http_hooks,然后在各个事件中,配置回调的url即可。(大家可以把上面的192.168.7.100换成实际地址)
回调的http url有二个基本要求:
1、 srs服务器能正常访问该url
2、该url接受post参数,如果校验成功,http status必须返回200,同时输出0,否则视为校验失败
post参数的json格式示例如下:
{
"action": "on_connect",
"client_id": 1985,
"ip": "192.168.1.10",
"vhost": "video.test.com",
"app": "live",
"tcUrl": "rtmp://x/x?key=xxx&uid=jimmy",
"pageUrl": "http://x/x.html"
}
这里,我们用spring boot(groovy语言)实现一个最基本的on_connect安全校验(注:仅出于演示目的,只要tcUrl中包括jimmy这个字符串就算通过,实际应用中,可以从db中校验,并结合一定的加解密算法,校验有效性)
@RestController
class SrsHttpCallBack{
@RequestMapping("/srs_http_call_back")
String auth(@RequestBody CallBackRequestData data){
if (data){
println data.dump()
if (data.action=="on_connect"){
//简单示例:仅校验on_connect(校验只有jimmy这个用户,允许连接)
if (data.tcUrl!=null && data.tcUrl.indexOf("jimmy")!=-1){
//pass
return "0"
}
else{
// fail
return "-1"
}
}
}
//其它情况,返回成功
"0"
}
@RequestMapping("/")
String home(){
"hello world"
}
}
@groovy.transform.ToString
class CallBackRequestData{
def action
def client_id
def ip
def vhost
def app
def tcUrl
def pageUrl
def send_bytes
def recv_bytes
def stream
def file
def cmd
}
随便找个编辑器(比如:vscode),把上面的代码复制进去,保存为http_call_back.groovy,然后利用springboot cli,启动:
`spring run http_call_back.groovy -- --server.port=9000`
注:如果srs与spring boot cli都在本机,注意要把端口错开,否则都是8080端口,容易冲突
![](https://img.haomeiwen.com/i14384594/2494a749dbbe31cb.png)
这样,一个最基本的http call back server就ok了。(注:对spring boot cli不熟悉的同学,可参考spring-boot 速成(1) helloworld)
建议先用postman之类的http rest工具,做下测试:
![](https://img.haomeiwen.com/i14384594/c496325d63d03eab.png)
将/etc/init.d/srs reload 让配置生效,然后obs的推流地址,改成类似:
rtmp://localhost:1935/live?uid=jimmy
![](https://img.haomeiwen.com/i14384594/451e43e0d343e1ae.png)
然后播放器(比如VLC Player)中,播放的地址要改成:
rtmp://localhost:1935/live?uid=jimmy/livestream
再观察spring boot clil中的输出,应该可以看到类似下面的日志:
...
<CallBackRequestData@637ab6fc action=on_connect client_id=215 ip=172.17.0.1 vhost=defaultVhost app=live tcUrl=rtmp://localhost:1935/live?uid=jimmy pageUrl= send_bytes=null recv_bytes=null stream=null file=null cmd=null>
<CallBackRequestData@3fa5cd6d action=on_publish client_id=215 ip=172.17.0.1 vhost=defaultVhost app=live tcUrl=rtmp://localhost:1935/live?uid=jimmy pageUrl=null send_bytes=null recv_bytes=null stream=livestream file=null cmd=null>
...
<CallBackRequestData@7942880d action=on_connect client_id=239 ip=172.17.0.1 vhost=defaultVhost app=live tcUrl=rtmp://localhost:1935/live?uid=jimmy pageUrl= send_bytes=null recv_bytes=null stream=null file=null cmd=null>
<CallBackRequestData@4f6cc49d action=on_play client_id=239 ip=172.17.0.1 vhost=defaultVhost app=live tcUrl=null pageUrl= send_bytes=null recv_bytes=null stream=livestream file=null cmd=null>
<CallBackRequestData@10cc39b5 action=on_hls client_id=230 ip=172.17.0.1 vhost=defaultVhost app=live tcUrl=null pageUrl=null send_bytes=null recv_bytes=null stream=livestream file=./objs/nginx/html/live/livestream-186.ts cmd=null>
...
有兴趣的同学,可以把rtmp url中的jimmy改成其它值,比如guest试试,应该就不能播放了。
注:对于播放器端,只有rtmp协议,上述安全校验才起作用,对于hls/http-flv这种方式的播放,http callback无效;但是考虑到推流(即:直播的源头)基本上都是rtmp协议,http callback是可以工作的,相当于把视频发布源头控制住了。
参考文章:
网友评论