-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.cpp
277 lines (234 loc) · 8.62 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
#include <cstdlib>
#include <cstring>
#include <vector>
#include <unordered_map>
#include "napi.h"
#include "includes.h"
#include "encoding/base64.h"
#include "aes/aes.hpp"
#define KEY_LENGTH 32
#define FN_MODULE_PROTOTYPE__COMPILE 0
namespace {
struct AddonData {
// std::unordered_map<int, Napi::ObjectReference> modules;
std::unordered_map<int, Napi::FunctionReference> functions;
};
const char errmsg[] = "This program has been changed by others.";
/* void ConsoleLog(const Napi::Env& env, Napi::Value value) {
Napi::Object console = env.Global().As<Napi::Object>()
.Get("console").As<Napi::Object>();
Napi::Function log = console.Get("log").As<Napi::Function>();
log.Call(console, { value });
} */
void ConsoleError(const Napi::Env& env, Napi::Value value) {
Napi::Object console = env.Global().As<Napi::Object>()
.Get("console").As<Napi::Object>();
Napi::Function error = console.Get("error").As<Napi::Function>();
error.Call(console, { value });
}
const uint8_t* GetKey() {
static const uint8_t key[KEY_LENGTH] = {
#include "key.txt"
};
return key;
}
Napi::Array GetKeyArray(const Napi::Env& env) {
const uint8_t* key = GetKey();
Napi::Array arrkey = Napi::Array::New(env, KEY_LENGTH);
for (uint32_t i = 0; i < KEY_LENGTH; i++) {
arrkey.Set(i, key[i]);
}
return arrkey;
}
int Pkcs7cut(uint8_t *p, int plen) {
uint8_t last = p[plen - 1];
if (last > 0 && last <= 16) {
for (int x = 2; x <= last; x++) {
if (p[plen - x] != last) {
return plen;
}
}
return plen - last;
}
return plen;
}
std::string Aesdec(const std::vector<uint8_t>& data,
const uint8_t* key,
const uint8_t* iv) {
size_t l = data.size();
uint8_t* encrypt = new uint8_t[l];
memcpy(encrypt, data.data(), l);
struct AES_ctx ctx;
AES_init_ctx_iv(&ctx, key, iv);
AES_CBC_decrypt_buffer(&ctx, encrypt, l);
uint8_t* out = new uint8_t[l + 1];
memcpy(out, encrypt, l);
out[l] = 0;
int realLength = Pkcs7cut(out, l);
out[realLength] = 0;
delete[] encrypt;
std::string res = reinterpret_cast<char*>(out);
delete[] out;
return res;
}
std::string Decrypt(const std::string& base64) {
size_t buflen = base64_decode(base64.c_str(), base64.length(), nullptr);
if (buflen == 0) return "";
std::vector<uint8_t> buf(buflen);
base64_decode(base64.c_str(), base64.length(), &buf[0]);
std::vector<uint8_t> iv(buf.begin(), buf.begin() + 16);
std::vector<uint8_t> data(buf.begin() + 16, buf.end());
return Aesdec(data, GetKey(), iv.data());
}
Napi::Value ModulePrototypeCompile(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
AddonData* addon_data = static_cast<AddonData*>(info.Data());
Napi::String content = info[0].As<Napi::String>();
Napi::String filename = info[1].As<Napi::String>();
std::string filename_str = filename.Utf8Value();
Napi::Function old_compile =
addon_data->functions[FN_MODULE_PROTOTYPE__COMPILE].Value();
if (filename_str.find("app.asar") != std::string::npos) {
return old_compile.Call(info.This(),
{ Napi::String::New(env, Decrypt(content.Utf8Value())), filename });
}
return old_compile.Call(info.This(), { content, filename });
}
Napi::Function MakeRequireFunction(Napi::Env* env,
const Napi::Object& mod) {
Napi::Function make_require = env->RunScript(scriptRequire)
.As<Napi::Function>();
return make_require({ mod }).As<Napi::Function>();
}
Napi::Value GetModuleObject(Napi::Env* env,
const Napi::Object& main_module,
const Napi::Object& this_exports) {
Napi::Function find_function = env->RunScript(scriptFind)
.As<Napi::Function>();
Napi::Value res = find_function({ main_module, this_exports });
if (res.IsNull()) {
Napi::Error::New(*env, "Cannot find module object.")
.ThrowAsJavaScriptException();
}
return res;
}
void ShowErrorAndQuit(const Napi::Env& env,
const Napi::Object& electron,
const Napi::String& message) {
Napi::Value ELECTRON_RUN_AS_NODE = env.Global().As<Napi::Object>()
.Get("process").As<Napi::Object>()
.Get("env").As<Napi::Object>()
.Get("ELECTRON_RUN_AS_NODE");
if (!ELECTRON_RUN_AS_NODE.IsUndefined() &&
ELECTRON_RUN_AS_NODE != Napi::Number::New(env, 0) &&
ELECTRON_RUN_AS_NODE != Napi::String::New(env, "")) {
ConsoleError(env, message);
exit(1);
} else {
Napi::Object dialog = electron.Get("dialog").As<Napi::Object>();
dialog.Get("showErrorBox").As<Napi::Function>()
.Call(dialog, { Napi::String::New(env, "Error"), message });
Napi::Object app = electron.Get("app").As<Napi::Object>();
Napi::Function quit = app.Get("quit").As<Napi::Function>();
quit.Call(app, {});
}
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
#ifdef _TARGET_ELECTRON_RENDERER_
Napi::Object main_module = env.Global().Get("module").As<Napi::Object>();
#else
Napi::Object process = env.Global().Get("process").As<Napi::Object>();
Napi::Array argv = process.Get("argv").As<Napi::Array>();
for (uint32_t i = 0; i < argv.Length(); ++i) {
std::string arg = argv.Get(i).As<Napi::String>().Utf8Value();
if (arg.find("--inspect") == 0 ||
arg.find("--remote-debugging-port") == 0) {
Napi::Error::New(env, "Not allow debugging this program.")
.ThrowAsJavaScriptException();
return exports;
}
}
Napi::Object main_module = process.Get("mainModule").As<Napi::Object>();
#endif
Napi::Object this_module = GetModuleObject(&env, main_module, exports)
.As<Napi::Object>();
Napi::Function require = MakeRequireFunction(&env, this_module);
Napi::Object electron = require({ Napi::String::New(env, "electron") })
.As<Napi::Object>();
Napi::Object module_constructor = require({
Napi::String::New(env, "module") }).As<Napi::Object>();
Napi::Value module_parent = this_module.Get("parent");
#ifdef _TARGET_ELECTRON_RENDERER_
if (module_parent != main_module) {
Napi::Object ipcRenderer = electron.Get("ipcRenderer").As<Napi::Object>();
Napi::Function sendSync = ipcRenderer.Get("sendSync").As<Napi::Function>();
sendSync.Call(ipcRenderer,
{ Napi::String::New(env, "__SHOW_ERROR_AND_QUIT__") });
return exports;
}
#else
if (this_module != main_module || (
module_parent != module_constructor &&
module_parent != env.Undefined() &&
module_parent != env.Null())) {
ShowErrorAndQuit(env, electron, Napi::String::New(env, errmsg));
return exports;
}
#endif
AddonData* addon_data = env.GetInstanceData<AddonData>();
if (addon_data == nullptr) {
addon_data = new AddonData();
env.SetInstanceData(addon_data);
}
Napi::Object module_prototype = module_constructor.Get("prototype")
.As<Napi::Object>();
addon_data->functions[FN_MODULE_PROTOTYPE__COMPILE] =
Napi::Persistent(module_prototype.Get("_compile").As<Napi::Function>());
module_prototype.DefineProperty(
Napi::PropertyDescriptor::Function(env,
Napi::Object::New(env),
"_compile",
ModulePrototypeCompile,
napi_enumerable,
addon_data));
#ifdef _TARGET_ELECTRON_RENDERER_
return exports;
#else
Napi::Value ELECTRON_RUN_AS_NODE = env.Global().As<Napi::Object>()
.Get("process").As<Napi::Object>()
.Get("env").As<Napi::Object>()
.Get("ELECTRON_RUN_AS_NODE");
if (ELECTRON_RUN_AS_NODE.IsUndefined() ||
ELECTRON_RUN_AS_NODE == Napi::Number::New(env, 0) ||
ELECTRON_RUN_AS_NODE == Napi::String::New(env, "")) {
Napi::Object ipcMain = electron.Get("ipcMain").As<Napi::Object>();
Napi::Function once = ipcMain.Get("once").As<Napi::Function>();
once.Call(ipcMain, {
Napi::String::New(env, "__SHOW_ERROR_AND_QUIT__"),
Napi::Function::New(env,
[](const Napi::CallbackInfo& info) -> Napi::Value {
Napi::Env env = info.Env();
Napi::Object event = info[0].As<Napi::Object>();
Napi::Object mm = env.Global().Get("process").As<Napi::Object>()
.Get("mainModule").As<Napi::Object>();
Napi::Function req = mm.Get("require").As<Napi::Function>();
ShowErrorAndQuit(env,
req.Call(mm, { Napi::String::New(env, "electron") })
.As<Napi::Object>(),
Napi::String::New(env, errmsg));
event.Set("returnValue", env.Null());
return env.Undefined();
})
});
}
try {
require({ Napi::String::New(env, "./main.js") })
.As<Napi::Function>().Call({ GetKeyArray(env) });
} catch (const Napi::Error& e) {
ShowErrorAndQuit(env, electron, e.Get("stack").As<Napi::String>());
}
return exports;
#endif
}
} // namespace
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)