-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.cpp
199 lines (170 loc) · 4.46 KB
/
update.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
#include "update.hpp"
#include <iostream>
char getRandomChar()
{
// a-zA-Z0-9
constexpr int n = 26 + 26 + 10;
int pick = rand() % n;
if (pick < 26)
return 'a' + pick;
else if (pick < 26 + 26)
return 'A' + pick - 26;
else
return '0' + pick - 26 - 26;
}
std::string Update::GenerateTempFileName(std::string dir)
{
std::string out = "";
do {
for (int i = 0; i < 5; ++i)
out += getRandomChar();
} while (PathFileExistsA((dir + "\\.__injectortempupdate_" + out + ".exe").c_str()));
return ".__injectortempupdate_" + out + ".exe";
}
namespace Update
{
Error::Context ErrorContext = Error::Context::none;
int ErrorCode = 0;
std::string Directory = "";
std::string FileName = "";
std::string TempFileName = "";
std::string LastTempFileFullPath = "";
}
void Update::deletePreviousVersion()
{
if (LastTempFileFullPath.length() <= 3) return;
size_t attempts = 0;
while (!DeleteFileA(LastTempFileFullPath.c_str()) && ++attempts < 5)
{
Sleep(1000);
}
}
Update::VersionCheckResult Update::versionCheck()
{
size_t bytesRead = 0;
char* result = HTTP::GET("https://www.a4g4.com/API/injector/version.php", &bytesRead);
if (!result) return VersionCheckResult::Error;
// make sure that the server responded with a real version instead of, say, an HTTP 404 page
try {
float x = std::stof(std::string(result, bytesRead));
if (x <= 0.f)
return VersionCheckResult::Error;
}
catch (std::exception&)
{
return VersionCheckResult::Error;
}
if (INJECTOR_CURRENT_VERSION_STRLEN != bytesRead || strncmp(INJECTOR_CURRENT_VERSION, result, INJECTOR_CURRENT_VERSION_STRLEN))
return VersionCheckResult::NeedsUpdate;
else
return VersionCheckResult::UpToDate;
}
char* Update::downloadLatestVersion(size_t* bytesRead)
{
return HTTP::GET("https://www.a4g4.com/API/injector/download.php", bytesRead);
/*
auto file = std::ifstream(
Directory + "\\" + FileName,
std::ios::in | std::ios::binary | std::ifstream::ate
);
size_t fileSize = (size_t)file.tellg();
*bytesRead = fileSize;
char* out = (char*)malloc(fileSize);
file.seekg(0);
file.read(out, fileSize);
return out;
*/
}
bool Update::renameMyself(std::string newName)
{
if (0 != rename((Directory + "\\" + FileName).c_str(), (Directory + "\\" + newName).c_str()))
{
OutputDebugStringA((Directory + "\\" + FileName + "\n").c_str());
OutputDebugStringA((Directory + "\\" + newName + "\n").c_str());
ErrorContext = Error::Context::rename_win;
ErrorCode = GetLastError();
return false;
}
return true;
}
bool Update::writeNewVersion(char* file, size_t nBytes)
{
auto outputFile = std::fstream(Directory + "\\" + FileName, std::ios::out | std::ios::binary);
if (!outputFile.is_open())
{
ErrorContext = Error::Context::writeoutput_win;
ErrorCode = GetLastError();
return false;
}
outputFile.write(file, nBytes);
outputFile.flush();
outputFile.close();
return true;
}
bool Update::executeNewVersion()
{
STARTUPINFOA si = { sizeof(si) };
PROCESS_INFORMATION pi;
std::string exe = Directory + "\\" + FileName;
std::string arg = Directory + "\\" + TempFileName;
if (CreateProcessA((LPSTR)exe.c_str(), (LPSTR)("\"" + exe + "\" \"" + arg + "\"").c_str(), 0, 0, true, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
else
{
ErrorContext = Error::Context::executenew_win;
ErrorCode = GetLastError();
return false;
}
}
bool Update::init(std::string argv)
{
srand((unsigned int)time(0));
char exePath[MAX_PATH];
GetModuleFileNameA(NULL, exePath, MAX_PATH);
std::string fullPath(exePath);
size_t slash = fullPath.rfind("\\");
if (slash >= fullPath.length())
{
ErrorContext = Error::Context::init;
ErrorCode = 1;
return false;
}
Directory = fullPath.substr(0, slash);
FileName = fullPath.substr(slash + 1);
LastTempFileFullPath = argv;
deletePreviousVersion();
return true;
}
bool Update::run()
{
// check if we need to update
switch (versionCheck())
{
case VersionCheckResult::Error:
return false;
case VersionCheckResult::UpToDate:
return true;
default:
// we need to update
break;
}
// download new version
size_t newVersionSize = 0;
char* newVersionFile = downloadLatestVersion(&newVersionSize);
if (!newVersionFile || newVersionSize == 0)
return false;
// rename myself to a temp filename
TempFileName = GenerateTempFileName(Directory);
if (!renameMyself(TempFileName))
return false;
// write new version to file
writeNewVersion(newVersionFile, newVersionSize);
if (!executeNewVersion())
return false;
std::exit(1);
return true;
}