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);
}
}
}
Net6实现ZLMediakit播放鉴权
最新推荐文章于 2024-09-06 16:56:41 发布