token类似于refer,不过是放在RTMP url中,或者在connect的请求参数中:
rtmp://vhost/app?token=xxxx/stream
,这样服务器在on_connect回调接口中, 就会把url带过去验证。token可以指定超时时间,可以变更token。需要服务器端做定制,做验证,简单靠谱。
举个常用的token认证的例子:
on_connect http://127.0.0.1:8085/api/v1/clients;
srs配置http_callback.conf 如下:
vhost your_vhost { http_hooks { # whether the http hooks enalbe. # default off. enabled on; # when client connect to vhost/app, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_connect", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "tcUrl": "rtmp://video.test.com/live?key=d2fa801d08e3f90ed1e1670e6e52651a", # "pageUrl": "http://www.test.com/live.html" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_connect http://xxx/api0 http://xxx/api1 http://xxx/apiN on_connect http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients; # when client close/disconnect to vhost/app/stream, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_close", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_close http://xxx/api0 http://xxx/api1 http://xxx/apiN on_close http://127.0.0.1:8085/api/v1/clients http://localhost:8085/api/v1/clients; # when client(encoder) publish to vhost/app/stream, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_publish", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "stream": "livestream" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_publish http://xxx/api0 http://xxx/api1 http://xxx/apiN on_publish http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams; # when client(encoder) stop publish to vhost/app/stream, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_unpublish", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "stream": "livestream" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_unpublish http://xxx/api0 http://xxx/api1 http://xxx/apiN on_unpublish http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams; # when client start to play vhost/app/stream, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_play", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "stream": "livestream", # "pageUrl": "http://www.test.com/live.html" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_play http://xxx/api0 http://xxx/api1 http://xxx/apiN on_play http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions; # when client stop to play vhost/app/stream, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_stop", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "stream": "livestream" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 # support multiple api hooks, format: # on_stop http://xxx/api0 http://xxx/api1 http://xxx/apiN on_stop http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions; # when srs reap a dvr file, call the hook, # the request in the POST data string is a object encode by json: # { # "action": "on_dvr", # "client_id": 1985, # "ip": "192.168.1.10", "vhost": "video.test.com", "app": "live", # "stream": "livestream", # "cwd": "/usr/local/srs", # "file": "./objs/nginx/html/live/livestream.1420254068776.flv" # } # if valid, the hook must return HTTP code 200(Stauts OK) and response # an int value specifies the error code(0 corresponding to success): # 0 on_dvr http://127.0.0.1:8085/api/v1/dvrs http://localhost:8085/api/v1/dvrs; } }
运行srs ./objs/srs -c http_callback.conf
srs提供有一个简单的python脚本可以用于简单验证 文件在 research/api-server/server.py
指定端口 启动脚本 python research/api-server/server.py 8085
修改脚本内的返回值 可以简单验证防盗链功能