From 3f211d1653a3280f02f7e193e0637b805492b727 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Mon, 27 May 2019 13:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Eshell=E7=99=BB=E5=BD=95hook?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=20=E9=BB=98=E8=AE=A4hook=20url=E6=94=B9?= =?UTF-8?q?=E6=88=90https=20=E9=BB=98=E8=AE=A4=E5=BC=80=E5=A7=8Bhttp=20hoo?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 40 ++++++++++++++++++++++------------------ server/WebHook.cpp | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 38c5dd8e..72c6d678 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -47,7 +47,7 @@ static map s_map_api; namespace API { typedef enum { - InvalidArgsFailed = -300, + InvalidArgs = -300, SqlFailed = -200, AuthFailed = -100, OtherFailed = -1, @@ -81,10 +81,16 @@ public: ~AuthException() = default; }; -class InvalidArgs: public ApiRetException { +class InvalidArgsException: public ApiRetException { public: - InvalidArgs(const char *str):ApiRetException(str,API::InvalidArgsFailed){} - ~InvalidArgs() = default; + InvalidArgsException(const char *str):ApiRetException(str,API::InvalidArgs){} + ~InvalidArgsException() = default; +}; + +class SuccessException: public ApiRetException { +public: + SuccessException():ApiRetException("success",API::Success){} + ~SuccessException() = default; }; @@ -192,7 +198,7 @@ bool checkArgs(Args &&args,First &&first,KeyTypes && ...keys){ #define CHECK_ARGS(...) \ if(!checkArgs(allArgs,##__VA_ARGS__)){ \ - throw InvalidArgs("缺少必要参数:" #__VA_ARGS__); \ + throw InvalidArgsException("缺少必要参数:" #__VA_ARGS__); \ } #define CHECK_SECRET() \ @@ -413,24 +419,21 @@ void installWebApi() { ////////////以下是注册的Hook API//////////// API_REGIST(hook,on_publish,{ //开始推流事件 - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); API_REGIST(hook,on_play,{ //开始播放事件 - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); API_REGIST(hook,on_flow_report,{ //流量统计hook api - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); API_REGIST(hook,on_rtsp_realm,{ - //rtsp是否需要鉴权 + //rtsp是否需要鉴权,默认需要鉴权 val["code"] = 0; val["realm"] = "zlmediakit_reaml"; }); @@ -446,23 +449,24 @@ void installWebApi() { API_REGIST(hook,on_stream_changed,{ //媒体注册或反注册事件 - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); API_REGIST(hook,on_stream_not_found,{ //媒体未找到事件 - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); API_REGIST(hook,on_record_mp4,{ //录制mp4分片完毕事件 - val["code"] = 0; - val["msg"] = "success"; + throw SuccessException(); }); + API_REGIST(hook,on_shell_login,{ + //shell登录调试事件 + throw SuccessException(); + }); } void unInstallWebApi(){ diff --git a/server/WebHook.cpp b/server/WebHook.cpp index 6a132556..63565b14 100644 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -41,19 +41,21 @@ const char kOnRtspAuth[] = HOOK_FIELD"on_rtsp_auth"; const char kOnStreamChanged[] = HOOK_FIELD"on_stream_changed"; const char kOnStreamNotFound[] = HOOK_FIELD"on_stream_not_found"; const char kOnRecordMp4[] = HOOK_FIELD"on_record_mp4"; +const char kOnShellLogin[] = HOOK_FIELD"on_shell_login"; const char kAdminParams[] = HOOK_FIELD"admin_params"; onceToken token([](){ - mINI::Instance()[kEnable] = false; + mINI::Instance()[kEnable] = true; mINI::Instance()[kTimeoutSec] = 10; - mINI::Instance()[kOnPublish] = "http://127.0.0.1/index/hook/on_publish"; - mINI::Instance()[kOnPlay] = "http://127.0.0.1/index/hook/on_play"; - mINI::Instance()[kOnFlowReport] = "http://127.0.0.1/index/hook/on_flow_report"; - mINI::Instance()[kOnRtspRealm] = "http://127.0.0.1/index/hook/on_rtsp_realm"; - mINI::Instance()[kOnRtspAuth] = "http://127.0.0.1/index/hook/on_rtsp_auth"; - mINI::Instance()[kOnStreamChanged] = "http://127.0.0.1/index/hook/on_stream_changed"; - mINI::Instance()[kOnStreamNotFound] = "http://127.0.0.1/index/hook/on_stream_not_found"; - mINI::Instance()[kOnRecordMp4] = "http://127.0.0.1/index/hook/on_record_mp4"; + mINI::Instance()[kOnPublish] = "https://127.0.0.1/index/hook/on_publish"; + mINI::Instance()[kOnPlay] = "https://127.0.0.1/index/hook/on_play"; + mINI::Instance()[kOnFlowReport] = "https://127.0.0.1/index/hook/on_flow_report"; + mINI::Instance()[kOnRtspRealm] = "https://127.0.0.1/index/hook/on_rtsp_realm"; + mINI::Instance()[kOnRtspAuth] = "https://127.0.0.1/index/hook/on_rtsp_auth"; + mINI::Instance()[kOnStreamChanged] = "https://127.0.0.1/index/hook/on_stream_changed"; + mINI::Instance()[kOnStreamNotFound] = "https://127.0.0.1/index/hook/on_stream_not_found"; + mINI::Instance()[kOnRecordMp4] = "https://127.0.0.1/index/hook/on_record_mp4"; + mINI::Instance()[kOnShellLogin] = "https://127.0.0.1/index/hook/on_shell_login"; mINI::Instance()[kAdminParams] = "secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc"; },nullptr); }//namespace Hook @@ -156,6 +158,7 @@ void installWebHook(){ GET_CONFIG_AND_REGISTER(string,hook_stream_chaned,Hook::kOnStreamChanged); GET_CONFIG_AND_REGISTER(string,hook_stream_not_found,Hook::kOnStreamNotFound); GET_CONFIG_AND_REGISTER(string,hook_record_mp4,Hook::kOnRecordMp4); + GET_CONFIG_AND_REGISTER(string,hook_shell_login,Hook::kOnShellLogin); NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastMediaPublish,[](BroadcastMediaPublishArgs){ @@ -305,6 +308,23 @@ void installWebHook(){ }); #endif //ENABLE_MP4V2 + NoticeCenter::Instance().addListener(nullptr,Broadcast::kBroadcastShellLogin,[](BroadcastShellLoginArgs){ + if(!hook_enable || hook_shell_login.empty()){ + return; + } + ArgsType body; + body["ip"] = sender.get_peer_ip(); + body["port"] = sender.get_peer_port(); + body["id"] = sender.getIdentifier(); + body["user_name"] = user_name; + body["passwd"] = passwd; + + //执行hook + do_http_hook(hook_shell_login,body, [invoker](const Value &,const string &err){ + invoker(err); + }); + }); + } void unInstallWebHook(){