ZLMediaKit/src/Shell/ShellSession.cpp

153 lines
5.0 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-04-01 16:35:56 +08:00
*/
#include "ShellSession.h"
2017-12-01 11:42:49 +08:00
#include "Util/CMD.h"
2017-04-01 16:35:56 +08:00
#include "Util/onceToken.h"
#include "Util/NoticeCenter.h"
2019-05-28 10:02:12 +08:00
#include "Common/config.h"
#include "ShellCMD.h"
2018-10-24 17:17:55 +08:00
using namespace toolkit;
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
2019-05-28 10:02:12 +08:00
static onceToken s_token([]() {
REGIST_CMD(media);
}, nullptr);
2019-05-27 18:39:43 +08:00
2018-12-20 10:42:51 +08:00
ShellSession::ShellSession(const Socket::Ptr &_sock) : TcpSession(_sock) {
2019-05-29 18:08:50 +08:00
DebugP(this);
2017-12-01 11:42:49 +08:00
pleaseInputUser();
2017-04-01 16:35:56 +08:00
}
ShellSession::~ShellSession() {
2019-05-29 18:08:50 +08:00
DebugP(this);
2017-04-01 16:35:56 +08:00
}
2018-02-23 15:36:51 +08:00
void ShellSession::onRecv(const Buffer::Ptr&buf) {
2020-03-20 11:51:24 +08:00
//DebugL << hexdump(buf->data(), buf->size());
2019-05-28 17:14:36 +08:00
GET_CONFIG(uint32_t,maxReqSize,Shell::kMaxReqSize);
2018-10-24 15:43:52 +08:00
if (_strRecvBuf.size() + buf->size() >= maxReqSize) {
2020-03-20 11:51:24 +08:00
shutdown(SockException(Err_other,"recv buffer overflow"));
return;
}
_beatTicker.resetTime();
_strRecvBuf.append(buf->data(), buf->size());
if (_strRecvBuf.find("\xff\xf4\xff\0xfd\x06") != std::string::npos) {
2020-04-23 17:50:12 +08:00
SockSender::send("\033[0m\r\n Bye bye!\r\n");
2020-03-20 11:51:24 +08:00
shutdown(SockException(Err_other,"received Ctrl+C"));
return;
}
size_t index;
string line;
while ((index = _strRecvBuf.find("\r\n")) != std::string::npos) {
line = _strRecvBuf.substr(0, index);
_strRecvBuf.erase(0, index + 2);
if (!onCommandLine(line)) {
shutdown(SockException(Err_other,"exit cmd"));
return;
}
}
2017-04-01 16:35:56 +08:00
}
2019-05-29 18:08:50 +08:00
void ShellSession::onError(const SockException &err){
WarnP(this) << err.what();
}
2017-04-01 16:35:56 +08:00
void ShellSession::onManager() {
2020-03-20 11:51:24 +08:00
if (_beatTicker.elapsedTime() > 1000 * 60 * 5) {
//5 miniutes for alive
2019-05-29 18:08:50 +08:00
shutdown(SockException(Err_timeout,"session timeout"));
2020-03-20 11:51:24 +08:00
return;
}
2017-04-01 16:35:56 +08:00
}
2017-12-01 11:42:49 +08:00
inline bool ShellSession::onCommandLine(const string& line) {
2018-10-24 15:43:52 +08:00
auto loginInterceptor = _loginInterceptor;
if (loginInterceptor) {
2020-03-20 11:51:24 +08:00
bool ret = loginInterceptor(line);
return ret;
}
2017-12-01 11:42:49 +08:00
try {
std::shared_ptr<stringstream> ss(new stringstream);
CMDRegister::Instance()(line,ss);
2020-04-23 17:50:12 +08:00
SockSender::send(ss->str());
2017-12-01 11:42:49 +08:00
}catch(ExitException &ex){
return false;
}catch(std::exception &ex){
2020-04-23 17:50:12 +08:00
SockSender::send(ex.what());
SockSender::send("\r\n");
2017-12-01 11:42:49 +08:00
}
printShellPrefix();
2020-03-20 11:51:24 +08:00
return true;
2017-04-01 16:35:56 +08:00
}
2017-12-01 11:42:49 +08:00
inline void ShellSession::pleaseInputUser() {
2020-04-23 17:50:12 +08:00
SockSender::send("\033[0m");
SockSender::send(StrPrinter << SERVER_NAME << " login: " << endl);
2020-03-20 11:51:24 +08:00
_loginInterceptor = [this](const string &user_name) {
_strUserName=user_name;
2017-12-01 11:42:49 +08:00
pleaseInputPasswd();
2020-03-20 11:51:24 +08:00
return true;
};
2017-04-01 16:35:56 +08:00
}
2017-12-01 11:42:49 +08:00
inline void ShellSession::pleaseInputPasswd() {
2020-04-23 17:50:12 +08:00
SockSender::send("Password: \033[8m");
2020-03-20 11:51:24 +08:00
_loginInterceptor = [this](const string &passwd) {
auto onAuth = [this](const string &errMessage){
if(!errMessage.empty()){
//鉴权失败
2020-04-23 17:50:12 +08:00
SockSender::send(StrPrinter
<< "\033[0mAuth failed("
<< errMessage
<< "), please try again.\r\n"
<< _strUserName << "@" << SERVER_NAME
<< "'s password: \033[8m"
<< endl);
return;
}
2020-04-23 17:50:12 +08:00
SockSender::send("\033[0m");
SockSender::send("-----------------------------------------\r\n");
SockSender::send(StrPrinter<<"欢迎来到"<<SERVER_NAME<<", 你可输入\"help\"查看帮助.\r\n"<<endl);
SockSender::send("-----------------------------------------\r\n");
printShellPrefix();
2018-10-24 15:43:52 +08:00
_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,_strUserName,passwd,invoker,static_cast<SockInfo &>(*this));
if(!flag){
//如果无人监听shell登录事件那么默认shell无法登录
onAuth("please listen kBroadcastShellLogin event");
}
2020-03-20 11:51:24 +08:00
return true;
};
2017-04-01 16:35:56 +08:00
}
2017-12-01 11:42:49 +08:00
inline void ShellSession::printShellPrefix() {
2020-04-23 17:50:12 +08:00
SockSender::send(StrPrinter << _strUserName << "@" << SERVER_NAME << "# " << endl);
2017-04-01 16:35:56 +08:00
}
2018-10-24 17:17:55 +08:00
}/* namespace mediakit */