按上回繼續,安全論證是絕大多數應用的基本要求,如果任何人都能無限制的發佈/播放視頻,顯然不適合。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端口,容易衝突
這樣,一個最基本的http call back server就ok了。(注:對spring boot cli不熟悉的同學,可參考spring-boot 速成(1) helloworld)
建議先用postman之類的http rest工具,做下測試:
將/etc/init.d/srs reload 讓配置生效,然後obs的推流地址,改成類似:
rtmp://localhost:1935/live?uid=jimmy
然後播放器(比如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是可以工作的,相當於把視頻發佈源頭控制住了。
參考文章: