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);
        } 
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值