ZLMediaKit/src/Shell/ShellSession.cpp

149 lines
4.2 KiB
C++
Raw Normal View History

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
*
2017-09-27 16:20:30 +08:00
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* 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 "Common/config.h"
#include "Util/CMD.h"
2017-04-01 16:35:56 +08:00
#include "Util/onceToken.h"
using namespace ZL::Util;
namespace ZL {
namespace Shell {
unordered_map<string, string> ShellSession::g_mapUser;
ShellSession::ShellSession(const std::shared_ptr<ThreadPool> &_th,
2017-12-01 11:42:49 +08:00
const Socket::Ptr &_sock) :
TcpLimitedSession(_th, _sock) {
pleaseInputUser();
2017-04-01 16:35:56 +08:00
}
ShellSession::~ShellSession() {
}
void ShellSession::onRecv(const Socket::Buffer::Ptr&buf) {
//DebugL << hexdump(buf->data(), buf->size());
static uint32_t maxReqSize = mINI::Instance()[Config::Shell::kMaxReqSize].as<uint32_t>();
if (m_strRecvBuf.size() + buf->size() >= maxReqSize) {
WarnL << "接收缓冲区溢出!";
shutdown();
return;
}
m_beatTicker.resetTime();
m_strRecvBuf.append(buf->data(), buf->size());
if (m_strRecvBuf.find("\xff\xf4\xff\0xfd\x06") != std::string::npos) {
WarnL << "收到Ctrl+C.";
send("\033[0m\r\n Bye bye!\r\n");
shutdown();
return;
}
size_t index;
string line;
while ((index = m_strRecvBuf.find("\r\n")) != std::string::npos) {
line = m_strRecvBuf.substr(0, index);
m_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() {
if (m_beatTicker.elapsedTime() > 1000 * 60 * 5) {
//5 miniutes for alive
shutdown();
return;
}
}
2017-12-01 11:42:49 +08:00
inline bool ShellSession::onCommandLine(const string& line) {
2017-04-01 16:35:56 +08:00
if (m_requestCB) {
bool ret = m_requestCB(line);
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");
send(StrPrinter << SERVER_NAME << " login: " << endl);
2017-04-01 16:35:56 +08:00
m_requestCB = [this](const string &line) {
m_strUserName=line;
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");
m_requestCB = [this](const string &passwd) {
2017-12-01 11:42:49 +08:00
if(!onAuth(m_strUserName, passwd)) {
2017-04-01 16:35:56 +08:00
send(StrPrinter
<<"\033[0mPermission denied,"
<<" please try again.\r\n"
<<m_strUserName<<"@"<<SERVER_NAME
2017-04-01 16:35:56 +08:00
<<"'s password: \033[8m"
<<endl);
return true;
}
send("\033[0m");
send("-----------------------------------------\r\n");
send(StrPrinter<<"欢迎来到"<<SERVER_NAME<<", 你可输入\"help\"查看帮助.\r\n"<<endl);
2017-04-01 16:35:56 +08:00
send("-----------------------------------------\r\n");
2017-12-01 11:42:49 +08:00
printShellPrefix();
2017-04-01 16:35:56 +08:00
m_requestCB=nullptr;
return true;
};
}
2017-12-01 11:42:49 +08:00
inline void ShellSession::printShellPrefix() {
send(StrPrinter << m_strUserName << "@" << SERVER_NAME << "# " << endl);
2017-04-01 16:35:56 +08:00
}
2017-12-01 11:42:49 +08:00
inline bool ShellSession::onAuth(const string &user, const string &pwd) {
2017-04-01 16:35:56 +08:00
auto it = g_mapUser.find(user);
if (it == g_mapUser.end()) {
2017-05-05 21:29:12 +08:00
//WarnL << user << " " << pwd;
2017-04-01 16:35:56 +08:00
return false;
}
return it->second == pwd;
}
2017-12-01 11:42:49 +08:00
}/* namespace Shell */
2017-04-01 16:35:56 +08:00
} /* namespace ZL */