mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-19 14:52:20 +08:00
shell登录鉴权改成广播方式
删除rtsp/rtmp shell命令,添加media命令 加载配置文件后发送广播
This commit is contained in:
@@ -3,56 +3,94 @@
|
||||
//
|
||||
|
||||
#include "Util/CMD.h"
|
||||
#include "Rtsp/RtspMediaSource.h"
|
||||
#include "Rtmp/RtmpMediaSource.h"
|
||||
#include "Common/MediaSource.h"
|
||||
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Rtsp;
|
||||
using namespace ZL::Rtmp;
|
||||
using namespace ZL::Media;
|
||||
|
||||
namespace ZL {
|
||||
namespace Shell {
|
||||
|
||||
class CMD_rtsp: public CMD {
|
||||
|
||||
class CMD_media: public CMD {
|
||||
public:
|
||||
CMD_rtsp(){
|
||||
_parser.reset(new OptionParser(nullptr));
|
||||
(*_parser) << Option('l', "list", Option::ArgNone, nullptr,false, "list all media source of rtsp",
|
||||
[](const std::shared_ptr<ostream> &stream, const string &arg) {
|
||||
// auto mediaSet = RtspMediaSource::getMediaSet();
|
||||
// for (auto &src : mediaSet) {
|
||||
// (*stream) << "\t" << src << "\r\n";
|
||||
// }
|
||||
return false;
|
||||
});
|
||||
CMD_media(){
|
||||
_parser.reset(new OptionParser([](const std::shared_ptr<ostream> &stream,mINI &ini){
|
||||
MediaSource::for_each_media([&](const string &schema,
|
||||
const string &vhost,
|
||||
const string &app,
|
||||
const string &streamid,
|
||||
const MediaSource::Ptr &media){
|
||||
if(!ini["schema"].empty() && ini["schema"] != schema){
|
||||
//筛选协议不匹配
|
||||
return;
|
||||
}
|
||||
if(!ini["vhost"].empty() && ini["vhost"] != vhost){
|
||||
//筛选虚拟主机不匹配
|
||||
return;
|
||||
}
|
||||
if(!ini["app"].empty() && ini["app"] != app){
|
||||
//筛选应用名不匹配
|
||||
return;
|
||||
}
|
||||
if(!ini["stream"].empty() && ini["stream"] != streamid){
|
||||
//流id不匹配
|
||||
return;
|
||||
}
|
||||
if(ini.find("list") != ini.end()){
|
||||
//列出源
|
||||
(*stream) << "\t"
|
||||
<< schema << "/"
|
||||
<< vhost << "/"
|
||||
<< app << "/"
|
||||
<< streamid
|
||||
<< "\r\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if(ini.find("kick") != ini.end()){
|
||||
//踢出源
|
||||
do{
|
||||
if(!media) {
|
||||
break;
|
||||
}
|
||||
if(!media->shutDown()) {
|
||||
break;
|
||||
}
|
||||
(*stream) << "\t踢出成功:"
|
||||
<< schema << "/"
|
||||
<< vhost << "/"
|
||||
<< app << "/"
|
||||
<< streamid
|
||||
<< "\r\n";
|
||||
return;
|
||||
}while(0);
|
||||
(*stream) << "\t踢出失败:"
|
||||
<< schema << "/"
|
||||
<< vhost << "/"
|
||||
<< app << "/"
|
||||
<< streamid
|
||||
<< "\r\n";
|
||||
return;
|
||||
}
|
||||
|
||||
});
|
||||
}));
|
||||
(*_parser) << Option('k', "kick", Option::ArgNone,nullptr,false, "踢出媒体源", nullptr);
|
||||
(*_parser) << Option('l', "list", Option::ArgNone,nullptr,false, "列出媒体源", nullptr);
|
||||
(*_parser) << Option('S', "schema", Option::ArgRequired,nullptr,false, "协议筛选", nullptr);
|
||||
(*_parser) << Option('v', "vhost", Option::ArgRequired,nullptr,false, "虚拟主机筛选", nullptr);
|
||||
(*_parser) << Option('a', "app", Option::ArgRequired,nullptr,false, "应用名筛选", nullptr);
|
||||
(*_parser) << Option('s', "stream", Option::ArgRequired,nullptr,false, "流id筛选", nullptr);
|
||||
}
|
||||
virtual ~CMD_rtsp() {}
|
||||
virtual ~CMD_media() {}
|
||||
const char *description() const override {
|
||||
return "查看rtsp服务器相关信息.";
|
||||
}
|
||||
};
|
||||
class CMD_rtmp: public CMD {
|
||||
public:
|
||||
CMD_rtmp(){
|
||||
_parser.reset(new OptionParser(nullptr));
|
||||
(*_parser) << Option('l', "list", Option::ArgNone,nullptr,false, "list all media source of rtmp",
|
||||
[](const std::shared_ptr<ostream> &stream, const string &arg) {
|
||||
// auto mediaSet = RtmpMediaSource::getMediaSet();
|
||||
// for (auto &src : mediaSet) {
|
||||
// (*stream) << "\t" << src << "\r\n";
|
||||
// }
|
||||
return false;
|
||||
});
|
||||
}
|
||||
virtual ~CMD_rtmp() {}
|
||||
const char *description() const override {
|
||||
return "查看rtmp服务器相关信息.";
|
||||
return "媒体源相关操作.";
|
||||
}
|
||||
};
|
||||
|
||||
static onceToken s_token([]() {
|
||||
REGIST_CMD(rtmp);
|
||||
REGIST_CMD(rtsp);
|
||||
REGIST_CMD(media);
|
||||
}, nullptr);
|
||||
|
||||
|
||||
|
||||
@@ -28,14 +28,14 @@
|
||||
#include "Common/config.h"
|
||||
#include "Util/CMD.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Util/NoticeCenter.h"
|
||||
|
||||
using namespace Config;
|
||||
using namespace ZL::Util;
|
||||
|
||||
namespace ZL {
|
||||
namespace Shell {
|
||||
|
||||
unordered_map<string, string> ShellSession::g_mapUser;
|
||||
|
||||
ShellSession::ShellSession(const std::shared_ptr<ThreadPool> &_th,
|
||||
const Socket::Ptr &_sock) :
|
||||
TcpLimitedSession(_th, _sock) {
|
||||
@@ -82,8 +82,9 @@ void ShellSession::onManager() {
|
||||
}
|
||||
|
||||
inline bool ShellSession::onCommandLine(const string& line) {
|
||||
if (m_requestCB) {
|
||||
bool ret = m_requestCB(line);
|
||||
auto loginInterceptor = m_loginInterceptor;
|
||||
if (loginInterceptor) {
|
||||
bool ret = loginInterceptor(line);
|
||||
return ret;
|
||||
}
|
||||
try {
|
||||
@@ -103,30 +104,55 @@ inline bool ShellSession::onCommandLine(const string& line) {
|
||||
inline void ShellSession::pleaseInputUser() {
|
||||
send("\033[0m");
|
||||
send(StrPrinter << SERVER_NAME << " login: " << endl);
|
||||
m_requestCB = [this](const string &line) {
|
||||
m_strUserName=line;
|
||||
m_loginInterceptor = [this](const string &user_name) {
|
||||
m_strUserName=user_name;
|
||||
pleaseInputPasswd();
|
||||
return true;
|
||||
};
|
||||
}
|
||||
inline void ShellSession::pleaseInputPasswd() {
|
||||
send("Password: \033[8m");
|
||||
m_requestCB = [this](const string &passwd) {
|
||||
if(!onAuth(m_strUserName, passwd)) {
|
||||
send(StrPrinter
|
||||
<<"\033[0mPermission denied,"
|
||||
<<" please try again.\r\n"
|
||||
<<m_strUserName<<"@"<<SERVER_NAME
|
||||
<<"'s password: \033[8m"
|
||||
<<endl);
|
||||
return true;
|
||||
}
|
||||
send("\033[0m");
|
||||
send("-----------------------------------------\r\n");
|
||||
send(StrPrinter<<"欢迎来到"<<SERVER_NAME<<", 你可输入\"help\"查看帮助.\r\n"<<endl);
|
||||
send("-----------------------------------------\r\n");
|
||||
printShellPrefix();
|
||||
m_requestCB=nullptr;
|
||||
m_loginInterceptor = [this](const string &passwd) {
|
||||
auto onAuth = [this](const string &errMessage){
|
||||
if(!errMessage.empty()){
|
||||
//鉴权失败
|
||||
send(StrPrinter
|
||||
<<"\033[0mAuth failed("
|
||||
<< errMessage
|
||||
<<"), please try again.\r\n"
|
||||
<<m_strUserName<<"@"<<SERVER_NAME
|
||||
<<"'s password: \033[8m"
|
||||
<<endl);
|
||||
return;
|
||||
}
|
||||
send("\033[0m");
|
||||
send("-----------------------------------------\r\n");
|
||||
send(StrPrinter<<"欢迎来到"<<SERVER_NAME<<", 你可输入\"help\"查看帮助.\r\n"<<endl);
|
||||
send("-----------------------------------------\r\n");
|
||||
printShellPrefix();
|
||||
m_loginInterceptor=nullptr;
|
||||
};
|
||||
|
||||
weak_ptr<ShellSession> weakSelf = dynamic_pointer_cast<ShellSession>(shared_from_this());
|
||||
Broadcast::AuthInvoker invoker = [weakSelf,onAuth](const string &errMessage){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
}
|
||||
strongSelf->async([errMessage,weakSelf,onAuth](){
|
||||
auto strongSelf = weakSelf.lock();
|
||||
if(!strongSelf){
|
||||
return;
|
||||
}
|
||||
onAuth(errMessage);
|
||||
});
|
||||
};
|
||||
|
||||
auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,m_strUserName,passwd,invoker);
|
||||
if(!flag){
|
||||
//如果无人监听shell登录事件,那么默认shell无法登录
|
||||
onAuth("please listen kBroadcastShellLogin event");
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -135,14 +161,5 @@ inline void ShellSession::printShellPrefix() {
|
||||
send(StrPrinter << m_strUserName << "@" << SERVER_NAME << "# " << endl);
|
||||
}
|
||||
|
||||
inline bool ShellSession::onAuth(const string &user, const string &pwd) {
|
||||
auto it = g_mapUser.find(user);
|
||||
if (it == g_mapUser.end()) {
|
||||
//WarnL << user << " " << pwd;
|
||||
return false;
|
||||
}
|
||||
return it->second == pwd;
|
||||
}
|
||||
|
||||
}/* namespace Shell */
|
||||
} /* namespace ZL */
|
||||
|
||||
@@ -48,22 +48,16 @@ public:
|
||||
void onError(const SockException &err) override {};
|
||||
void onManager() override;
|
||||
|
||||
static void addUser(const string &userName,const string &userPwd){
|
||||
g_mapUser[userName] = userPwd;
|
||||
}
|
||||
private:
|
||||
inline bool onCommandLine(const string &);
|
||||
inline bool onAuth(const string &user, const string &pwd);
|
||||
inline void pleaseInputUser();
|
||||
inline void pleaseInputPasswd();
|
||||
inline void printShellPrefix();
|
||||
|
||||
function<bool(const string &)> m_requestCB;
|
||||
function<bool(const string &)> m_loginInterceptor;
|
||||
string m_strRecvBuf;
|
||||
Ticker m_beatTicker;
|
||||
string m_strUserName;
|
||||
|
||||
static unordered_map<string, string> g_mapUser;
|
||||
};
|
||||
|
||||
} /* namespace Shell */
|
||||
|
||||
Reference in New Issue
Block a user