-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
357 lines (309 loc) · 16.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <boost/asio.hpp>
#include <boost/program_options.hpp>
#define PDC_WIDE
#define CHTYPE_32
#include <curses.h>
#include "Includes/IPCapDevice.h"
#undef timeout
#include "Includes/Logger.h"
#include "Includes/MonitorDevice.h"
#include "Includes/NetConversionFunctions.h"
#include "Includes/Timer.h"
#include "Includes/UserInterface/KeyboardController.h"
#include "Includes/UserInterface/MainWindowController.h"
#include "Includes/WirelessPSPPluginDevice.h"
#include "Includes/WirelessPromiscuousDevice.h"
#include "Includes/XLinkKaiConnection.h"
namespace
{
constexpr std::string_view cLogFileName{"log.txt"};
constexpr bool cLogToDisk{true};
constexpr std::string_view cConfigFileName{"config.txt"};
// Indicates if the program should be running or not, used to gracefully exit the program.
bool gRunning{true};
} // namespace
namespace po = boost::program_options;
// Add npcap to the dll path for windows
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
bool InitNPcapDLLPath()
{
bool lReturn{false};
std::string lNPcapDirectory{};
lNPcapDirectory.resize(MAX_PATH);
unsigned int lLength = GetSystemDirectory(lNPcapDirectory.data(), MAX_PATH);
lNPcapDirectory.resize(lLength);
if (lLength > 0) {
lNPcapDirectory.append("\\Npcap");
if (SetDllDirectory(lNPcapDirectory.data()) != 0) {
lReturn = true;
}
}
return lReturn;
}
#endif
static void SignalHandler(const boost::system::error_code& aError, int aSignalNumber)
{
if (!aError) {
if (aSignalNumber == SIGINT || aSignalNumber == SIGTERM) {
// Quit gracefully.
gRunning = false;
}
}
}
int main(int argc, char* argv[])
{
std::string lProgramPath{"./"};
#if not defined(_WIN32) && not defined(_WIN64)
setlocale(LC_ALL, "");
// Make robust against sudo path change.
std::array<char, PATH_MAX> lResolvedPath{};
if (realpath(argv[0], lResolvedPath.data()) != nullptr) {
lProgramPath = std::string(lResolvedPath.begin(), lResolvedPath.end());
// Remove excecutable name from path
size_t lExcecutableNameIndex{lProgramPath.rfind('/')};
if (lExcecutableNameIndex != std::string::npos) {
lProgramPath.erase(lExcecutableNameIndex + 1, lProgramPath.length() - lExcecutableNameIndex - 1);
}
}
#else
// Npcap needs this
if (!InitNPcapDLLPath()) {
// Quit the application almost immediately
gRunning = false;
}
#endif
po::options_description lDescription("Options");
// clang-format off
lDescription.add_options()
("help,h", "Shows this help message.")
("verbose,v", "Disables HUD and shows log directly on screen.");
// clang-format on
po::variables_map lVariableMap;
po::store(po::command_line_parser(argc, argv).options(lDescription).run(), lVariableMap);
if ((lVariableMap.count("help") != 0U) || (lVariableMap.count("h") != 0U)) {
std::cout << lDescription << std::endl;
} else {
po::notify(lVariableMap);
bool lContinue{true};
std::vector<std::string> lSSIDFilters{};
std::shared_ptr<MainWindowController> lWindowController{nullptr};
std::shared_ptr<KeyboardController> lKeyboardController{nullptr};
// Handle quit signals gracefully.
boost::asio::io_service lSignalIoService{};
boost::asio::signal_set lSignals(lSignalIoService, SIGINT, SIGTERM);
lSignals.async_wait(&SignalHandler);
std::thread lThread{[lIoService = &lSignalIoService] { lIoService->run(); }};
WindowModel mWindowModel{};
// Check if wizard can be skipped
bool lSkipWizard{false};
if (mWindowModel.LoadFromFile(lProgramPath + cConfigFileName.data())) {
lSkipWizard = true;
}
mWindowModel.mProgramPath = lProgramPath;
Logger::GetInstance().Init(mWindowModel.mLogLevel, cLogToDisk, lProgramPath + cLogFileName.data());
if ((lVariableMap.count("verbose") != 0U) || (lVariableMap.count("v") != 0U)) {
Logger::GetInstance().SetLogToScreen(true);
if (lSkipWizard) {
// Start the engine immediately
mWindowModel.mCommand = WindowModel_Constants::Command::StartEngine;
} else {
Logger::GetInstance().Log("No config file found! First run the wizard before running in verbose mode",
Logger::Level::ERROR);
lContinue = false;
}
} else {
lWindowController = std::make_shared<MainWindowController>(mWindowModel, lSkipWizard);
lKeyboardController = std::make_shared<KeyboardController>(
[&](unsigned int aAction) { lWindowController->KeyAction(aAction); });
if (lWindowController->SetUp()) {
lKeyboardController->StartThread();
} else {
Logger::GetInstance().Log("Error initializing the TUI", Logger::Level::ERROR);
// Stop the app, we have no TUI
lContinue = false;
}
}
if (lContinue) {
Timer lTimer{};
std::shared_ptr<IPCapDevice> lDevice{nullptr};
std::shared_ptr<XLinkKaiConnection> lXLinkKaiConnection{std::make_shared<XLinkKaiConnection>()};
bool lSuccess{false};
// If we need more entry methods, make an actual state machine
bool lWaitEntry{true};
std::chrono::time_point<std::chrono::system_clock> lWaitStart{std::chrono::seconds{0}};
WindowModel_Constants::ConnectionMethod lOldMethod = mWindowModel.mConnectionMethod;
while (gRunning) {
if (lWindowController == nullptr || lWindowController->Process()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
switch (mWindowModel.mCommand) {
case WindowModel_Constants::Command::StartEngine:
if (mWindowModel.mLogLevel != Logger::GetInstance().GetLogLevel()) {
Logger::GetInstance().SetLogLevel(mWindowModel.mLogLevel);
}
switch (mWindowModel.mConnectionMethod) {
case WindowModel_Constants::ConnectionMethod::Plugin:
if (std::dynamic_pointer_cast<WirelessPSPPluginDevice>(lDevice) == nullptr) {
std::chrono::seconds lTimeOut =
std::chrono::seconds(std::stoi(mWindowModel.mReConnectionTimeOutS));
lDevice = std::make_shared<WirelessPSPPluginDevice>(
mWindowModel.mAutoDiscoverPSPVitaNetworks,
lTimeOut,
&mWindowModel.mCurrentlyConnectedNetwork);
Logger::GetInstance().Log("Plugin Device created!", Logger::Level::INFO);
}
break;
case WindowModel_Constants::ConnectionMethod::Promiscuous:
if (std::dynamic_pointer_cast<WirelessPromiscuousDevice>(lDevice) == nullptr) {
std::chrono::seconds lTimeOut =
std::chrono::seconds(std::stoi(mWindowModel.mReConnectionTimeOutS));
lDevice = std::make_shared<WirelessPromiscuousDevice>(
mWindowModel.mAutoDiscoverPSPVitaNetworks,
lTimeOut,
&mWindowModel.mCurrentlyConnectedNetwork);
Logger::GetInstance().Log("Promiscuous Device created!", Logger::Level::INFO);
}
break;
#if not defined(_WIN32) && not defined(_WIN64)
case WindowModel_Constants::ConnectionMethod::Monitor:
if (std::dynamic_pointer_cast<MonitorDevice>(lDevice) == nullptr) {
lDevice =
std::make_shared<MonitorDevice>(MacToInt(mWindowModel.mOnlyAcceptFromMac),
mWindowModel.mAcknowledgeDataFrames,
&mWindowModel.mCurrentlyConnectedNetwork);
Logger::GetInstance().Log("Monitor Device created!", Logger::Level::INFO);
break;
}
break;
#endif
default:
Logger::GetInstance().Log("Unknown method!", Logger::Level::ERROR);
gRunning = false;
break;
}
// Completely reset the xlink kai connection on a mode switch
if (lOldMethod != mWindowModel.mConnectionMethod) {
lXLinkKaiConnection->Close();
lXLinkKaiConnection = nullptr;
lXLinkKaiConnection = std::make_shared<XLinkKaiConnection>();
}
lOldMethod = mWindowModel.mConnectionMethod;
lXLinkKaiConnection->SetIncomingConnection(lDevice);
lXLinkKaiConnection->SetUseHostSSID(mWindowModel.mUseSSIDFromHost);
lDevice->SetConnector(lXLinkKaiConnection);
// If we are auto discovering PSP/VITA networks add those to the filter list
if (mWindowModel.mAutoDiscoverPSPVitaNetworks) {
lSSIDFilters.emplace_back(Net_Constants::cPSPSSIDFilterName.data());
lSSIDFilters.emplace_back(Net_Constants::cVitaSSIDFilterName.data());
}
// Set the XLink Kai connection up, if we are autodiscovering we don't need to provide an IP
if (!mWindowModel.mAutoDiscoverXLinkKaiInstance) {
lSuccess = lXLinkKaiConnection->Open(mWindowModel.mXLinkIp,
std::stoi(mWindowModel.mXLinkPort));
} else {
lSuccess = lXLinkKaiConnection->Open("");
}
// Now set up the wifi interface
if (lSuccess) {
if (lDevice->Open(mWindowModel.mWifiAdapter, lSSIDFilters)) {
if (lDevice->StartReceiverThread() && lXLinkKaiConnection->StartReceiverThread()) {
mWindowModel.mEngineStatus = WindowModel_Constants::EngineStatus::Running;
mWindowModel.mCommand = WindowModel_Constants::Command::NoCommand;
} else {
Logger::GetInstance().Log("Failed to start receiver threads",
Logger::Level::ERROR);
mWindowModel.mEngineStatus = WindowModel_Constants::EngineStatus::Error;
mWindowModel.mCommand = WindowModel_Constants::Command::WaitForTime;
mWindowModel.mTimeToWait = std::chrono::seconds(5);
mWindowModel.mCommandAfterWait = WindowModel_Constants::Command::StopEngine;
}
} else {
Logger::GetInstance().Log("Failed to activate monitor interface",
Logger::Level::ERROR);
mWindowModel.mEngineStatus = WindowModel_Constants::EngineStatus::Error;
mWindowModel.mCommand = WindowModel_Constants::Command::WaitForTime;
mWindowModel.mTimeToWait = std::chrono::seconds(5);
mWindowModel.mCommandAfterWait = WindowModel_Constants::Command::StopEngine;
}
} else {
Logger::GetInstance().Log(
"Failed to open connection to XLink Kai, retrying in 10 seconds!",
Logger::Level::ERROR);
// Have it take some time between tries
mWindowModel.mCommand = WindowModel_Constants::Command::WaitForTime;
mWindowModel.mTimeToWait = std::chrono::seconds(10);
mWindowModel.mCommandAfterWait = WindowModel_Constants::Command::NoCommand;
}
break;
case WindowModel_Constants::Command::WaitForTime:
// Wait state, use this to add a delay without making the UI unresponsive.
if (lWaitEntry) {
lTimer.Start(mWindowModel.mTimeToWait);
lWaitEntry = false;
}
if (lTimer.IsTimedOut()) {
mWindowModel.mCommand = mWindowModel.mCommandAfterWait;
lWaitEntry = true;
}
break;
case WindowModel_Constants::Command::StopEngine:
lXLinkKaiConnection->Close();
lDevice->Close();
lSSIDFilters.clear();
// Let's actually just remove the device, easier this way
lDevice = nullptr;
// Remove the Connected To portion to make it easier for people to understand that the
// network was disconnected.
mWindowModel.mCurrentlyConnectedNetwork.clear();
mWindowModel.mEngineStatus = WindowModel_Constants::EngineStatus::Idle;
mWindowModel.mCommand = WindowModel_Constants::Command::NoCommand;
break;
case WindowModel_Constants::Command::StartSearchNetworks:
case WindowModel_Constants::Command::StopSearchNetworks:
// TODO: implement.
break;
case WindowModel_Constants::Command::ReConnect:
if (lDevice != nullptr) {
lDevice->Connect("");
}
mWindowModel.mCommand = WindowModel_Constants::Command::NoCommand;
break;
case WindowModel_Constants::Command::SetHosting:
if (lXLinkKaiConnection != nullptr) {
lXLinkKaiConnection->SetHosting(mWindowModel.mHosting);
}
break;
case WindowModel_Constants::Command::NoCommand:
break;
}
} else {
gRunning = false;
}
}
mWindowModel.mEngineStatus = WindowModel_Constants::EngineStatus::Idle;
mWindowModel.mCommand = WindowModel_Constants::Command::NoCommand;
if (lDevice != nullptr) {
lDevice->Close();
}
lXLinkKaiConnection->Close();
lSSIDFilters.clear();
lDevice = nullptr;
lXLinkKaiConnection = nullptr;
} else {
gRunning = false;
}
if (lKeyboardController != nullptr) {
lKeyboardController->StopThread();
lKeyboardController = nullptr;
}
lWindowController = nullptr;
lSignalIoService.stop();
if (lThread.joinable()) {
lThread.join();
}
}
}