-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
274 lines (238 loc) · 6.04 KB
/
Copy pathMain.cpp
File metadata and controls
274 lines (238 loc) · 6.04 KB
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
#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <vector>
#include "imgui.h"
#include "imgui-SFML.h"
#include "Renderer.h"
#include "OSC.h"
#include "SHA256.h"
#include "Save.h"
#include "Load.h"
#if _WIN32
#include <windows.h>
#define MAIN APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdshow)
#elif
#define MAIN main()
#endif
std::atomic_bool stop = false;
std::atomic_bool start = false;
int key_idx = 0;
int key_length = 4;
char password[100];
int port = 9000;
bool show_log = false;
bool parameter_multiplexing = false;
bool save_password = true;
void DrawUI(OSC& osc)
{
static ImGuiWindowFlags flags =
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoSavedSettings;
ImGui::SetNextWindowSize(ImVec2(300, 200));
ImGui::SetNextWindowPos(ImVec2(0.f, 0.f));
ImGui::Begin("Window", 0, flags);
float textWidth = ImGui::CalcTextSize("Shell Protector").x;
float windowWidth = ImGui::GetWindowWidth();
float centerPosX = (windowWidth - textWidth) * 0.5f;
ImGui::SetCursorPosX(centerPosX);
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0, 255, 0, 255));
ImGui::Text("Shell Protector OSC");
ImGui::PopStyleColor();
ImGui::Separator();
const static char* items[] = { "4", "8", "12", "16" };
ImGui::Text("Password");
ImGui::SameLine();
ImGui::SetNextItemWidth(150);
ImGui::InputText("##Password", password, key_length + 1, ImGuiInputTextFlags_Password);
ImGui::SameLine();
ImGui::SetNextItemWidth(50);
ImGui::Combo(" ", &key_idx, items, 4);
key_length = 4 * key_idx + 4;
ImGui::Spacing();
ImGui::SetNextItemWidth(50);
ImGui::InputInt("OSC port", &port, 0);
ImGui::Spacing();
ImGui::Text("Parameter-multiplexing");
ImGui::SameLine();
ImGui::Checkbox("##parameter_multiplexing", ¶meter_multiplexing);
ImGui::Text("Save password");
ImGui::SameLine();
ImGui::Checkbox("##save_password", &save_password);
ImGui::SetNextItemWidth(100);
if (!start)
{
if (ImGui::Button("Start!"))
{
std::cout << password << '\n';
start = true;
}
}
else
{
if (ImGui::Button("Stop"))
start = false;
}
ImGui::SetCursorPosY(200 - 30);
if(ImGui::Button("Logs"))
{
show_log = true;
}
ImGui::End();
}
void DrawLog(OSC &osc) {
static ImGuiWindowFlags flags =
ImGuiWindowFlags_NoDecoration |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoSavedSettings;
ImGui::SetNextWindowSize(ImVec2(300, 200));
ImGui::SetNextWindowPos(ImVec2(0.f, 0.f));
ImGui::Begin("Log", 0, flags);
if (ImGui::Button("Back"))
{
ImGui::End();
show_log = false;
return;
}
ImGui::SameLine();
if (ImGui::Button("Clear"))
osc.ClearLogs();
ImGui::SameLine();
if (ImGui::Button("Lock"))
osc.log_lock = !osc.log_lock;
ImGui::SameLine();
ImGui::SetNextItemWidth(100);
ImGui::InputInt("Max", &osc.max_log, 0, 0);
ImGui::Separator();
ImGui::BeginChild("Scrolling", ImVec2(0.f, 0.f), true, ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_HorizontalScrollbar);
for (auto& log : osc.GetLogs())
{
ImGui::Text(log.c_str());
ImGui::Spacing();
}
ImGui::EndChild();
ImGui::End();
}
int MAIN
{
OSC osc;
osc.Init("127.0.0.1", port);
Load loader;
if (!loader.LoadFile())
osc.AddLog("Can't load save file");
else
{
for (int i = 0; i < loader.password.size(); ++i)
{
password[i] = loader.password[i];
}
key_idx = loader.key_idx;
}
std::thread thr([&] {
const float factor = pow(10.0f, 4);
std::string osc_addr = "";
auto switch0 = [&](int n) {
bool s = false;
osc_addr = "/avatar/parameters/encrypt_switch0";
s = (n & 0b0001) == 1 ? true : false;
osc.SendOSC(osc_addr, s);
};
auto switch1 = [&](int n) {
bool s = false;
osc_addr = "/avatar/parameters/encrypt_switch1";
s = (n & 0b0010) == 2 ? true : false;
osc.SendOSC(osc_addr, s);
};
auto switch2 = [&](int n) {
bool s = false;
osc_addr = "/avatar/parameters/encrypt_switch2";
s = (n & 0b0100) == 4 ? true : false;
osc.SendOSC(osc_addr, s);
};
auto switch3 = [&](int n) {
bool s = false;
osc_addr = "/avatar/parameters/encrypt_switch3";
s = (n & 0b1000) == 8 ? true : false;
osc.SendOSC(osc_addr, s);
};
while (!stop)
{
if (start)
{
osc.SetOSCPort(port);
SHA256 sha;
sha.update(password);
uint8_t* digest = sha.digest();
for (int i = 0; i < key_length; ++i)
{
if (parameter_multiplexing)
{
std::string osc_addr = "/avatar/parameters/encrypt_lock";
osc.SendOSC(osc_addr, true);
switch (key_length)
{
default: //fall down
switch3(i);
case 8:
switch2(i);
case 4:
switch1(i);
switch0(i);
break;
}
}
///////////////////Send password////////////////
if (password[i] == 0)
{
for(int j = i + 1; j < sizeof(password); ++j)
password[j] = 0;
}
float pwd;
unsigned char var = password[i] ^ digest[i];
osc.AddLog(std::to_string(i) + ":" + std::to_string(var));
pwd = 1 - var / 128.0f;
pwd = -(roundf(pwd * factor) / factor); //Rounding to 4 digits
if(parameter_multiplexing)
osc_addr = "/avatar/parameters/pkey";
else
osc_addr = "/avatar/parameters/pkey" + std::to_string(i);
osc.SendOSC(osc_addr, pwd);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
/////////////////////////////////////////////////
if (parameter_multiplexing)
{
osc_addr = "/avatar/parameters/encrypt_lock";
osc.SendOSC(osc_addr, false);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
Renderer* renderer = Renderer::GetInstance();
sf::Clock delta_clock;
while (renderer->GetWindow()->isOpen())
{
renderer->Update(delta_clock.restart());
if (show_log == false)
DrawUI(osc);
else
DrawLog(osc);
renderer->Render();
}
stop = true;
start = false;
thr.join();
if (save_password)
{
Save saver;
saver.password = password;
saver.key_idx = key_idx;
saver.SaveFile();
}
return 0;
}