-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.cc
297 lines (251 loc) · 9.55 KB
/
main.cc
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
#define _WINSOCKAPI_
#define WIN32_LEAN_AND_MEAN
#define UNICODE
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <filesystem>
#include <fstream>
#include <fmt/core.h>
// #include <boxer/boxer.h>
#include <sys/log.h>
#include <core/loader.h>
#include <core/py_controller/co_spawn.h>
#include <core/ftp/serv.h>
#include <signal.h>
#include <cxxabi.h>
#include <pipes/terminal_pipe.h>
#include <api/executor.h>
const static void VerifyEnvironment()
{
const auto filePath = SystemIO::GetSteamPath() / ".cef-enable-remote-debugging";
// Steam's CEF Remote Debugger isn't exposed to port 8080
if (!std::filesystem::exists(filePath))
{
std::ofstream(filePath).close();
Logger.Log("Successfully enabled CEF remote debugging, you can now restart Steam...");
std::exit(1);
}
}
void OnTerminate()
{
auto const exceptionPtr = std::current_exception();
std::string errorMessage = "Millennium has a fatal error that it can't recover from, check the logs for more details!";
if (exceptionPtr)
{
try
{
int status;
auto const exceptionType = abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), 0, 0, &status);
errorMessage.append("\nTerminating with uncaught exception of type `");
errorMessage.append(exceptionType);
errorMessage.append("`");
std::rethrow_exception(exceptionPtr); // rethrow the exception to catch its exception message
}
catch (const std::exception& e)
{
errorMessage.append(" with `what()` = \"");
errorMessage.append(e.what());
errorMessage.append("\"");
}
catch (...) { }
}
#ifdef _WIN32
MessageBoxA(NULL, errorMessage.c_str(), "Oops!", MB_ICONERROR | MB_OK);
#elif __linux__
std::cerr << errorMessage << std::endl;
#endif
}
/* Wrapped cross platform entrypoint */
const static void EntryMain()
{
std::set_terminate(OnTerminate); // Set custom terminate handler for easier debugging
/** Handle signal interrupts (^C) */
signal(SIGINT, [](int signalCode) { std::exit(128 + SIGINT); });
#ifdef _WIN32
// try {
// if (std::filesystem::exists(SystemIO::GetInstallPath() / "user32.queue.dll"))
// {
// Logger.Log("Updating shim module from cache...");
// while (true) {
// try {
// std::filesystem::remove(SystemIO::GetInstallPath() / "user32.dll");
// break;
// }
// catch (std::filesystem::filesystem_error& e) {
// continue;
// }
// }
// Logger.Log("Removed old inject shim...");
// std::filesystem::rename(SystemIO::GetInstallPath() / "user32.queue.dll", SystemIO::GetInstallPath() / "user32.dll");
// Logger.Log("Successfully updated user32.dll!");
// }
// }
// catch (std::exception& e) {
// LOG_ERROR("Failed to update user32.dll: {}", e.what());
// }
std::unique_ptr<StartupParameters> startupParams = std::make_unique<StartupParameters>();
if (startupParams->HasArgument("-verbose"))
{
CreateTerminalPipe();
}
#endif
uint16_t ftpPort = Crow::CreateAsyncServer();
const auto startTime = std::chrono::system_clock::now();
VerifyEnvironment();
std::shared_ptr<PluginLoader> loader = std::make_shared<PluginLoader>(startTime, ftpPort);
SetPluginLoader(loader);
PythonManager& manager = PythonManager::GetInstance();
auto backendThread = std::thread([&loader, &manager] { loader->StartBackEnds(manager); });
auto frontendThreads = std::thread([&loader] { loader->StartFrontEnds(); });
backendThread.join();
frontendThreads.join();
Logger.Log("Millennium has gracefully shut down.");
}
#ifdef _WIN32
std::unique_ptr<std::thread> g_millenniumThread;
int __stdcall DllMain(void*, unsigned long fdwReason, void*)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
{
g_millenniumThread = std::make_unique<std::thread>(EntryMain);
break;
}
case DLL_PROCESS_DETACH:
{
Logger.Log("Shutting down Millennium...");
std::exit(EXIT_SUCCESS);
g_threadTerminateFlag->flag.store(true);
Sockets::Shutdown();
g_millenniumThread->join();
Logger.PrintMessage(" MAIN ", "Millennium has been shut down.", COL_MAGENTA);
std::this_thread::sleep_for(std::chrono::seconds(1000));
break;
}
}
return true;
}
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, PSTR cmdline, int cmdShow)
{
//boxer::show("Millennium successfully loaded!", "Yay!");
//EntryMain();
return 1;
}
#elif __linux__
#include <stdio.h>
#include <stdlib.h>
#include <posix/helpers.h>
#include <dlfcn.h>
#include <fstream>
#include <chrono>
#include <ctime>
#include <stdexcept>
extern "C"
{
/* Trampoline for the real main() */
static int (*fnMainOriginal)(int, char **, char **);
std::unique_ptr<std::thread> g_millenniumThread;
/* Our fake main() that gets called by __libc_start_main() */
int MainHooked(int argc, char **argv, char **envp)
{
Logger.Log("Hooked main() with PID: {}", getpid());
/**
* Since this isn't an executable, and is "preloaded", the kernel doesn't implicitly load dependencies, so we need to manually.
* libpython3.11.so.1.0 should already be in $PATH, so we can just load it from there.
*/
std::string pythonPath = fmt::format("{}/.millennium/libpython-3.11.8.so", std::getenv("HOME"));
if (!dlopen(pythonPath.c_str(), RTLD_LAZY | RTLD_GLOBAL))
{
LOG_ERROR("Failed to load python libraries: {},\n\nThis is likely because it was not found on disk, try reinstalling Millennium.", dlerror());
}
#ifdef MILLENNIUM_SHARED
{
/** Start Millennium on a new thread to prevent I/O blocking */
g_millenniumThread = std::make_unique<std::thread>(EntryMain);
int steam_main = fnMainOriginal(argc, argv, envp);
Logger.Log("Hooked Steam entry returned {}", steam_main);
g_threadTerminateFlag->flag.store(true);
Sockets::Shutdown();
g_millenniumThread->join();
Logger.Log("Shutting down Millennium...");
return steam_main;
}
#else
{
g_threadTerminateFlag->flag.store(true);
g_millenniumThread = std::make_unique<std::thread>(EntryMain);
g_millenniumThread->join();
return 0;
}
#endif
}
void RemoveFromLdPreload()
{
const std::string fileToRemove = std::filesystem::path(std::getenv("HOME")) / ".millennium" / "libMillennium.so";
const char* ldPreload = std::getenv("LD_PRELOAD");
if (!ldPreload)
{
std::cerr << "LD_PRELOAD is not set." << std::endl;
return;
}
std::string token;
std::string ldPreloadStr = ldPreload;
std::istringstream iss(ldPreloadStr);
std::vector<std::string> updatedPreload;
// Split LD_PRELOAD by spaces and filter out fileToRemove
while (iss >> token)
{
if (token != fileToRemove)
{
updatedPreload.push_back(token);
}
}
// Join the remaining paths back into a single string
std::ostringstream oss;
for (size_t i = 0; i < updatedPreload.size(); ++i)
{
if (i > 0) oss << ' ';
oss << updatedPreload[i];
}
// Set the updated LD_PRELOAD
if (setenv("LD_PRELOAD", oss.str().c_str(), 1) != 0)
{
perror("setenv");
}
}
#ifdef MILLENNIUM_SHARED
/*
* Trampoline for __libc_start_main() that replaces the real main
* function with our hooked version.
*/
int __libc_start_main(
int (*main)(int, char **, char **), int argc, char **argv,
int (*init)(int, char **, char **), void (*fini)(void), void (*rtld_fini)(void), void *stack_end)
{
/* Save the real main function address */
fnMainOriginal = main;
/* Get the address of the real __libc_start_main() */
decltype(&__libc_start_main) orig = (decltype(&__libc_start_main))dlsym(RTLD_NEXT, "__libc_start_main");
/* Get the path to the Steam executable */
std::filesystem::path steamPath = std::filesystem::path(std::getenv("HOME")) / ".steam/steam/ubuntu12_32/steam";
/** not loaded in a invalid child process */
if (argv[0] != steamPath.string())
{
return orig(main, argc, argv, init, fini, rtld_fini, stack_end);
}
/* Remove the Millennium library from LD_PRELOAD */
RemoveFromLdPreload();
/* Log that we've loaded Millennium */
Logger.Log("Loaded Millennium on {}, system architecture {}", GetLinuxDistro(), GetSystemArchitecture());
/* ... and call it with our custom main function */
return orig(MainHooked, argc, argv, init, fini, rtld_fini, stack_end);
}
#endif
}
int main(int argc, char **argv, char **envp)
{
return MainHooked(argc, argv, envp);
}
#endif