diff --git a/HiddenCLI/Commands.cpp b/HiddenCLI/Commands.cpp index ae2e2f3..221f693 100644 --- a/HiddenCLI/Commands.cpp +++ b/HiddenCLI/Commands.cpp @@ -16,30 +16,37 @@ Commands::Commands(Arguments& args) do { + bool found = false; + for (auto it = m_commandsStack.begin(); it != m_commandsStack.end(); it++) { if ((*it)->CompareCommand(arg)) { (*it)->LoadArgs(args); + m_current = *it; + found = true; break; } } + + if (!found) + throw WException(-2, L"Error, unknown command, please use 'hiddencli help'"); } while (args.GetNext(arg)); + } Commands::~Commands() { - } void Commands::LoadCommandsStack() { - m_commandsStack.push_back(new CommandHide()); - m_commandsStack.push_back(new CommandUnhide()); + m_commandsStack.push_back(CommandPtr(new CommandHide())); + m_commandsStack.push_back(CommandPtr(new CommandUnhide())); } void Commands::Perform(Connection& connection) { - + m_current->PerformCommand(connection); } diff --git a/HiddenCLI/Commands.h b/HiddenCLI/Commands.h index 2364736..a6f66cd 100644 --- a/HiddenCLI/Commands.h +++ b/HiddenCLI/Commands.h @@ -2,6 +2,7 @@ #include "Helper.h" #include "Connection.h" +#include class ICommand { @@ -16,7 +17,10 @@ public: class Commands { - std::vector m_commandsStack; + typedef std::shared_ptr CommandPtr; + + std::vector m_commandsStack; + CommandPtr m_current; void LoadCommandsStack(); diff --git a/HiddenCLI/Connection.cpp b/HiddenCLI/Connection.cpp index 393cbec..0d9337e 100644 --- a/HiddenCLI/Connection.cpp +++ b/HiddenCLI/Connection.cpp @@ -12,7 +12,7 @@ Connection::Connection(Arguments& args) : do { - if (arg == L"gate") + if (arg == L"/gate") { args.SwitchToNext(); if (!args.GetNext(m_deviceName)) diff --git a/HiddenCLI/Hide.cpp b/HiddenCLI/Hide.cpp index 36070fa..501fd42 100644 --- a/HiddenCLI/Hide.cpp +++ b/HiddenCLI/Hide.cpp @@ -1,10 +1,11 @@ #include "Hide.h" +#include using namespace std; // ================= -CommandHide::CommandHide() : m_command(L"hide") +CommandHide::CommandHide() : m_command(L"/hide") { } @@ -54,7 +55,32 @@ void CommandHide::LoadArgs(Arguments& args) void CommandHide::PerformCommand(Connection& connection) { + HidStatus status; + HidObjId objId; + switch (m_hideType) + { + case EHideTypes::TypeFile: + status = Hid_AddHiddenFile(connection.GetContext(), m_path.c_str(), &objId); + break; + case EHideTypes::TypeDir: + status = Hid_AddHiddenDir(connection.GetContext(), m_path.c_str(), &objId); + break; + case EHideTypes::TypeRegKey: + status = Hid_AddHiddenRegKey(connection.GetContext(), m_regRootType, m_path.c_str(), &objId); + break; + case EHideTypes::TypeRegVal: + status = Hid_AddHiddenRegValue(connection.GetContext(), m_regRootType, m_path.c_str(), &objId); + break; + default: + throw WException(-2, L"Internal error, invalid type for command 'hide'"); + } + + if (!HID_STATUS_SUCCESSFUL(status)) + throw WException(HID_STATUS_CODE(status), L"Error, command 'hide' rejected"); + + wcerr << L"Command 'hide' successful" << endl; + wcout << L"status:ok;id:" << objId << endl; } HidRegRootTypes CommandHide::GetRegType(wstring& path) @@ -75,8 +101,9 @@ HidRegRootTypes CommandHide::GetRegType(wstring& path) // ================= -CommandUnhide::CommandUnhide() : m_command(L"unhide") +CommandUnhide::CommandUnhide() : m_command(L"/unhide") { + m_targetId = 0; } CommandUnhide::~CommandUnhide() @@ -90,10 +117,92 @@ bool CommandUnhide::CompareCommand(std::wstring& command) void CommandUnhide::LoadArgs(Arguments& args) { + wstring object, target; + if (!args.GetNext(object)) + throw WException(-2, L"Error, mismatched argument #1 for command 'unhide'"); + + if (!args.GetNext(target)) + throw WException(-2, L"Error, mismatched argument #2 for command 'unhide'"); + + if (object == L"file") + { + m_hideType = EHideTypes::TypeFile; + } + else if (object == L"dir") + { + m_hideType = EHideTypes::TypeDir; + } + else if (object == L"regkey") + { + m_hideType = EHideTypes::TypeRegKey; + } + else if (object == L"regval") + { + m_hideType = EHideTypes::TypeRegVal; + } + else + { + throw WException(-2, L"Error, invalid argument for command 'unhide'"); + } + + m_targetAll = (target == L"all"); + if (!m_targetAll) + { + m_targetId = _wtoll(target.c_str()); + if (!m_targetId) + throw WException(-2, L"Error, invalid target id for command 'unhide'"); + } } void CommandUnhide::PerformCommand(Connection& connection) { + HidStatus status; + if (m_targetAll) + { + switch (m_hideType) + { + case EHideTypes::TypeFile: + status = Hid_RemoveAllHiddenFiles(connection.GetContext()); + break; + case EHideTypes::TypeDir: + status = Hid_RemoveAllHiddenDirs(connection.GetContext()); + break; + case EHideTypes::TypeRegKey: + status = Hid_RemoveAllHiddenRegKeys(connection.GetContext()); + break; + case EHideTypes::TypeRegVal: + status = Hid_RemoveAllHiddenRegValues(connection.GetContext()); + break; + default: + throw WException(-2, L"Internal error #1, invalid type for command 'unhide'"); + } + } + else + { + switch (m_hideType) + { + case EHideTypes::TypeFile: + status = Hid_RemoveHiddenFile(connection.GetContext(), m_targetId); + break; + case EHideTypes::TypeDir: + status = Hid_RemoveHiddenDir(connection.GetContext(), m_targetId); + break; + case EHideTypes::TypeRegKey: + status = Hid_RemoveHiddenRegKey(connection.GetContext(), m_targetId); + break; + case EHideTypes::TypeRegVal: + status = Hid_RemoveHiddenRegValue(connection.GetContext(), m_targetId); + break; + default: + throw WException(-2, L"Internal error #2, invalid type for command 'unhide'"); + } + } + + if (!HID_STATUS_SUCCESSFUL(status)) + throw WException(HID_STATUS_CODE(status), L"Error, command 'hide' rejected"); + + wcerr << L"Command 'unhide' successful" << endl; + wcout << L"status:ok" << endl; } diff --git a/HiddenCLI/Hide.h b/HiddenCLI/Hide.h index 48ad71f..ea08b51 100644 --- a/HiddenCLI/Hide.h +++ b/HiddenCLI/Hide.h @@ -34,6 +34,9 @@ class CommandUnhide : public ICommand { const wchar_t* m_command = nullptr; + EHideTypes m_hideType; + HidObjId m_targetId; + bool m_targetAll; public: CommandUnhide();