summaryrefslogtreecommitdiffhomepage
path: root/wfpctl/src/extras/cli/modules
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-04-11 10:36:43 +0200
committerOdd Stranne <odd@mullvad.net>2018-04-11 10:36:43 +0200
commit1b97cdcd94e549decfe1d6081155c8efefe9b110 (patch)
tree66bc668bfe1868d925e32044373af83a3175bd2d /wfpctl/src/extras/cli/modules
parenta5f7cf5592003093caaa658516758fb3c45206d1 (diff)
parentf53d90c5d69e81b09eec67a406825c96ea19d072 (diff)
downloadmullvadvpn-1b97cdcd94e549decfe1d6081155c8efefe9b110.tar.xz
mullvadvpn-1b97cdcd94e549decfe1d6081155c8efefe9b110.zip
Merge branch 'migrate-wfpctl'
Diffstat (limited to 'wfpctl/src/extras/cli/modules')
-rw-r--r--wfpctl/src/extras/cli/modules/imodule.h19
-rw-r--r--wfpctl/src/extras/cli/modules/list.h33
-rw-r--r--wfpctl/src/extras/cli/modules/module.cpp70
-rw-r--r--wfpctl/src/extras/cli/modules/module.h50
-rw-r--r--wfpctl/src/extras/cli/modules/monitor.h21
-rw-r--r--wfpctl/src/extras/cli/modules/wfpctl.h25
6 files changed, 218 insertions, 0 deletions
diff --git a/wfpctl/src/extras/cli/modules/imodule.h b/wfpctl/src/extras/cli/modules/imodule.h
new file mode 100644
index 0000000000..82221f284f
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/imodule.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "cli/propertylist.h"
+#include <string>
+#include <vector>
+
+namespace modules
+{
+
+struct IModule
+{
+ virtual std::wstring name() = 0;
+ virtual std::wstring description() = 0;
+ virtual PropertyList commands() = 0;
+
+ virtual void handleRequest(const std::vector<std::wstring> &request) = 0;
+};
+
+}
diff --git a/wfpctl/src/extras/cli/modules/list.h b/wfpctl/src/extras/cli/modules/list.h
new file mode 100644
index 0000000000..23751c2a26
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/list.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "module.h"
+#include "cli/util.h"
+#include "cli/commands/list/sessions.h"
+#include "cli/commands/list/providers.h"
+#include "cli/commands/list/events.h"
+#include "cli/commands/list/filters.h"
+#include "cli/commands/list/layers.h"
+#include "cli/commands/list/providercontexts.h"
+#include "cli/commands/list/sublayers.h"
+
+namespace modules
+{
+
+class List : public Module
+{
+public:
+
+ List(MessageSink messageSink)
+ : Module(L"list", L"List various objects in the WFP universe.")
+ {
+ addCommand(std::make_unique<commands::list::Sessions>(messageSink));
+ addCommand(std::make_unique<commands::list::Providers>(messageSink));
+ addCommand(std::make_unique<commands::list::Events>(messageSink));
+ addCommand(std::make_unique<commands::list::Filters>(messageSink));
+ addCommand(std::make_unique<commands::list::Layers>(messageSink));
+ addCommand(std::make_unique<commands::list::ProviderContexts>(messageSink));
+ addCommand(std::make_unique<commands::list::Sublayers>(messageSink));
+ }
+};
+
+}
diff --git a/wfpctl/src/extras/cli/modules/module.cpp b/wfpctl/src/extras/cli/modules/module.cpp
new file mode 100644
index 0000000000..f66ae5a084
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/module.cpp
@@ -0,0 +1,70 @@
+#include "stdafx.h"
+#include "module.h"
+#include "cli/util.h"
+#include "libcommon/string.h"
+#include <sstream>
+#include <utility>
+
+namespace modules
+{
+
+PropertyList Module::commands()
+{
+ PropertyList c;
+
+ for (auto &command : m_commands)
+ {
+ c.add(common::string::Lower(command.second->name()), command.second->description());
+ }
+
+ return c;
+}
+
+void Module::handleRequest(const std::vector<std::wstring> &request)
+{
+ //
+ // The request has the form of:
+ //
+ // [0] command
+ // [1] arg1
+ // [2] arg2
+ // ...
+ //
+
+ if (request.empty())
+ {
+ std::wstringstream ss;
+
+ ss << L"Command missing. Try 'help " << m_name << "'.";
+
+ throw std::runtime_error(common::string::ToAnsi(ss.str()));
+ }
+
+ auto wanted = common::string::Lower(request[0]);
+ auto found = m_commands.find(wanted);
+
+ if (found == m_commands.end())
+ {
+ std::wstringstream ss;
+
+ ss << L"Module '" << m_name << "' doesn't support the command '" << request[0] << "'.";
+
+ throw std::runtime_error(common::string::ToAnsi(ss.str()));
+ }
+
+ auto args = request;
+
+ args.erase(args.begin());
+
+ found->second->handleRequest(args);
+}
+
+void Module::addCommand(std::unique_ptr<commands::ICommand> command)
+{
+ m_commands.insert(std::make_pair(
+ common::string::Lower(command->name()),
+ std::move(command)
+ ));
+}
+
+}
diff --git a/wfpctl/src/extras/cli/modules/module.h b/wfpctl/src/extras/cli/modules/module.h
new file mode 100644
index 0000000000..45587ab7e5
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/module.h
@@ -0,0 +1,50 @@
+#pragma once
+
+#include "imodule.h"
+#include "cli/commands/icommand.h"
+#include <map>
+#include <memory>
+
+namespace modules
+{
+
+class Module : public IModule
+{
+public:
+
+ Module(const std::wstring &name, const std::wstring &description)
+ : m_name(name)
+ , m_description(description)
+ {
+ }
+
+ std::wstring name() override
+ {
+ return m_name;
+ }
+
+ std::wstring description() override
+ {
+ return m_description;
+ }
+
+ // Collect name and description from commands.
+ PropertyList commands() override;
+
+ // Identify requested command and dispatch to it.
+ void handleRequest(const std::vector<std::wstring> &request) override;
+
+ void addCommand(std::unique_ptr<commands::ICommand> command);
+
+private:
+
+ Module(const Module &);
+ Module &operator=(const Module &);
+
+ std::wstring m_name;
+ std::wstring m_description;
+
+ std::map<std::wstring, std::unique_ptr<commands::ICommand> > m_commands;
+};
+
+}
diff --git a/wfpctl/src/extras/cli/modules/monitor.h b/wfpctl/src/extras/cli/modules/monitor.h
new file mode 100644
index 0000000000..e227192e82
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/monitor.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "module.h"
+#include "cli/util.h"
+#include "cli/commands/monitor/m_events.h"
+
+namespace modules
+{
+
+class Monitor : public Module
+{
+public:
+
+ Monitor(MessageSink messageSink)
+ : Module(L"monitor", L"Real-time monitoring of events and object creation/deletion in WFP.")
+ {
+ addCommand(std::make_unique<commands::monitor::Events>(messageSink));
+ }
+};
+
+}
diff --git a/wfpctl/src/extras/cli/modules/wfpctl.h b/wfpctl/src/extras/cli/modules/wfpctl.h
new file mode 100644
index 0000000000..b690454e19
--- /dev/null
+++ b/wfpctl/src/extras/cli/modules/wfpctl.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "module.h"
+#include "cli/util.h"
+#include "cli/commands/wfpctl/init.h"
+#include "cli/commands/wfpctl/deinit.h"
+#include "cli/commands/wfpctl/policy.h"
+
+namespace modules
+{
+
+class Wfpctl : public Module
+{
+public:
+
+ Wfpctl(MessageSink messageSink)
+ : Module(L"wfpctl", L"Exercise functionality provided by \"wfpctl.dll\".")
+ {
+ addCommand(std::make_unique<commands::wfpctl::Init>(messageSink));
+ addCommand(std::make_unique<commands::wfpctl::Deinit>(messageSink));
+ addCommand(std::make_unique<commands::wfpctl::Policy>(messageSink));
+ }
+};
+
+}