2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
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"
|
2018-02-08 17:24:42 +08:00
|
|
|
|
#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) {
|
2017-12-01 11:42:49 +08:00
|
|
|
|
pleaseInputUser();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ShellSession::~ShellSession() {
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-23 15:36:51 +08:00
|
|
|
|
void ShellSession::onRecv(const Buffer::Ptr&buf) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//DebugL << hexdump(buf->data(), buf->size());
|
2018-10-24 17:17:55 +08:00
|
|
|
|
GET_CONFIG_AND_REGISTER(uint32_t,maxReqSize,Shell::kMaxReqSize);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRecvBuf.size() + buf->size() >= maxReqSize) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
WarnL << "接收缓冲区溢出!";
|
|
|
|
|
shutdown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_beatTicker.resetTime();
|
|
|
|
|
_strRecvBuf.append(buf->data(), buf->size());
|
|
|
|
|
if (_strRecvBuf.find("\xff\xf4\xff\0xfd\x06") != std::string::npos) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
WarnL << "收到Ctrl+C.";
|
|
|
|
|
send("\033[0m\r\n Bye bye!\r\n");
|
|
|
|
|
shutdown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
size_t index;
|
|
|
|
|
string line;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
while ((index = _strRecvBuf.find("\r\n")) != std::string::npos) {
|
|
|
|
|
line = _strRecvBuf.substr(0, index);
|
|
|
|
|
_strRecvBuf.erase(0, index + 2);
|
2017-12-01 11:42:49 +08:00
|
|
|
|
if (!onCommandLine(line)) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
shutdown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ShellSession::onManager() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_beatTicker.elapsedTime() > 1000 * 60 * 5) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//5 miniutes for alive
|
|
|
|
|
shutdown();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2018-02-08 17:24:42 +08:00
|
|
|
|
if (loginInterceptor) {
|
|
|
|
|
bool ret = loginInterceptor(line);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-12-01 11:42:49 +08:00
|
|
|
|
try {
|
|
|
|
|
std::shared_ptr<stringstream> ss(new stringstream);
|
|
|
|
|
CMDRegister::Instance()(line,ss);
|
|
|
|
|
send(ss->str());
|
|
|
|
|
}catch(ExitException &ex){
|
|
|
|
|
return false;
|
|
|
|
|
}catch(std::exception &ex){
|
|
|
|
|
send(ex.what());
|
|
|
|
|
send("\r\n");
|
|
|
|
|
}
|
|
|
|
|
printShellPrefix();
|
|
|
|
|
return true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 11:42:49 +08:00
|
|
|
|
inline void ShellSession::pleaseInputUser() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
send("\033[0m");
|
2018-02-02 18:06:08 +08:00
|
|
|
|
send(StrPrinter << SERVER_NAME << " login: " << endl);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_loginInterceptor = [this](const string &user_name) {
|
|
|
|
|
_strUserName=user_name;
|
2017-12-01 11:42:49 +08:00
|
|
|
|
pleaseInputPasswd();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-12-01 11:42:49 +08:00
|
|
|
|
inline void ShellSession::pleaseInputPasswd() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
send("Password: \033[8m");
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_loginInterceptor = [this](const string &passwd) {
|
2018-02-08 17:24:42 +08:00
|
|
|
|
auto onAuth = [this](const string &errMessage){
|
|
|
|
|
if(!errMessage.empty()){
|
|
|
|
|
//鉴权失败
|
|
|
|
|
send(StrPrinter
|
|
|
|
|
<<"\033[0mAuth failed("
|
|
|
|
|
<< errMessage
|
|
|
|
|
<<"), please try again.\r\n"
|
2018-10-24 15:43:52 +08:00
|
|
|
|
<<_strUserName<<"@"<<SERVER_NAME
|
2018-02-08 17:24:42 +08:00
|
|
|
|
<<"'s password: \033[8m"
|
|
|
|
|
<<endl);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
send("\033[0m");
|
|
|
|
|
send("-----------------------------------------\r\n");
|
|
|
|
|
send(StrPrinter<<"欢迎来到"<<SERVER_NAME<<", 你可输入\"help\"查看帮助.\r\n"<<endl);
|
|
|
|
|
send("-----------------------------------------\r\n");
|
|
|
|
|
printShellPrefix();
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_loginInterceptor=nullptr;
|
2018-02-08 17:24:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto flag = NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastShellLogin,_strUserName,passwd,invoker,*this);
|
2018-02-08 17:24:42 +08:00
|
|
|
|
if(!flag){
|
|
|
|
|
//如果无人监听shell登录事件,那么默认shell无法登录
|
|
|
|
|
onAuth("please listen kBroadcastShellLogin event");
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-01 11:42:49 +08:00
|
|
|
|
inline void ShellSession::printShellPrefix() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
send(StrPrinter << _strUserName << "@" << SERVER_NAME << "# " << endl);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
}/* namespace mediakit */
|