zoukankan      html  css  js  c++  java
  • 开源流媒体服务器SRS学习笔记(3)

    按上回继续,安全论证是绝大多数应用的基本要求,如果任何人都能无限制的发布/播放视频,显然不适合。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是可以工作的,相当于把视频发布源头控制住了。

    参考文章:

    https://github.com/ossrs/srs/wiki/v2_CN_HTTPCallback

  • 相关阅读:
    OpenGL学习之路(四)
    OpenGL学习之路(三)
    ajax请求成功后新开窗口window.open()被拦截解决方法
    Mysql日志解析
    Kibana+Logstash+Elasticsearch 日志查询系统
    谈谈Redis的SETNX
    常用前端开发工具合集
    Media Queries 详解
    启动mysql时显示:/tmp/mysql.sock 不存在的解决方法
    找不到mysql.sock,mysql.sock丢失问题解决方法
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/srs_study_3_http-callback.html
Copyright © 2011-2022 走看看