Net6实现ZLMediakit播放鉴权

复制代码
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using ZLMediaKitHook.Utility;
using ZLMediaKitHook.Utilty;
using ZLMediaKitHook.Utilty.dto;
using ZLMediaKitHook.Utilty.dto.rtsp_auth;

namespace ZLMediaKitHook.Controllers
{
    /// <summary>
    /// ZLM HTTP HOOK API [hook] enable=1
    /// </summary>
    [Route("api/hook/[action]")]
    [ApiController]
    public class HookController : ControllerBase
    {
        /// <summary>
        /// 播放鉴权事件 
        /// on_play=http://127.0.0.1:8999/api/hook/on_play
        /// rtsp://127.0.0.1:554/live/test?token=123456 该方式需要在接口on_rtsp_realm返回response["realm"] = "";
        /// ws://127.0.0.1:80/live/test.live.flv?token=123456 在线播放器地址:https://jessibuca.com/player.html
        /// </summary> 
        [HttpPost]
        public string on_play([FromBody] BaseRequest data)
        { 
            // Params:token=123456 
            BaseResponse response = new BaseResponse();

            try
            {
                Logger.Info($"api/hook/on_play,data:{System.Text.Json.JsonSerializer.Serialize(data)}");

                DataMgr.CheckToken(data.Params, response); 
            }
            catch (Exception ex)
            {
                Logger.Error($"api/hook/on_play,errmsg:{ex.Message}");
            }

            return JsonConvert.SerializeObject(response);
        }

        /// <summary>
        /// 推流鉴权事件
        /// on_publish=http://127.0.0.1:8999/api/hook/on_publish 
        /// </summary> 
        [HttpPost]
        public string on_publish([FromBody] BaseRequest data)
        { 
            BaseResponse response = new BaseResponse();

            try
            {
                Logger.Info($"api/hook/on_publish,data:{System.Text.Json.JsonSerializer.Serialize(data)}");
            }
            catch (Exception ex)
            {
                Logger.Error($"api/hook/on_publish,errmsg:{ex.Message}");
            }

            return JsonConvert.SerializeObject(response);
        } 

        /// <summary>
        /// rtsp专用方鉴权,步骤1 
        /// on_rtsp_realm=http://127.0.0.1:8999/api/hook/on_rtsp_realm  
        /// rtsp://admin:123456@127.0.0.1:554/live/test
        /// </summary> 
        [HttpPost]
        public string on_rtsp_realm([FromBody] BaseRequest data)
        {
            Dictionary<string, object> response = new Dictionary<string, object>();

            try
            {
                Logger.Info($"api/hook/on_rtsp_realm,data:{System.Text.Json.JsonSerializer.Serialize(data)}");

                response["code"] = 0;
                response["realm"] = "";// rtsp账户(admin,空代表不鉴权) 
            }
            catch (Exception ex)
            { 
                Logger.Error($"api/hook/on_rtsp_realm,errmsg:{ex.Message}");
            }

            return JsonConvert.SerializeObject(response);
        }

        /// <summary>
        /// rtsp专用方鉴权,步骤2
        /// on_rtsp_auth=http://127.0.0.1:8999/api/hook/on_rtsp_auth
        /// </summary> 
        [HttpPost]
        public string on_rtsp_auth([FromBody] Rtsp_AuthRequest data)
        {
            Dictionary<string, object> response = new Dictionary<string, object>();

            try
            {
                Logger.Info($"api/hook/on_rtsp_auth,data:{System.Text.Json.JsonSerializer.Serialize(data)}");

                if (data.Realm != data.User_Name)
                {
                    response["code"] = 1000;// 账号错误
                }
                else
                {
                    response["code"] = 0;
                    response["encrypted"] = false;
                    response["passwd"] = "123456";// rtsp密码
                } 
            }
            catch (Exception ex)
            { 
                Logger.Error($"api/hook/on_rtsp_auth,errmsg:{ex.Message}");
            }

            return JsonConvert.SerializeObject(response);
        } 
    }
}
复制代码

 

posted @   CHHC  阅读(60)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-08-22 GB28181压力测试工具
点击右上角即可分享
微信分享提示