frida 16.7.14 → 16.7.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +1 -10
  2. package/build/BSDmakefile +6 -0
  3. package/build/Makefile +10 -0
  4. package/build/src/frida.d.ts +364 -0
  5. package/build/src/frida.js +962 -0
  6. package/build/src/frida_binding.d.ts +938 -0
  7. package/meson.build +34 -67
  8. package/package.json +30 -20
  9. package/scripts/fetch-abi-bits.py +15 -65
  10. package/scripts/install.js +5 -4
  11. package/src/addon.def +3 -0
  12. package/src/addon.symbols +2 -1
  13. package/src/addon.version +4 -0
  14. package/src/frida_bindgen/__init__.py +0 -0
  15. package/src/frida_bindgen/__main__.py +4 -0
  16. package/src/frida_bindgen/__pycache__/__init__.cpython-312.pyc +0 -0
  17. package/src/frida_bindgen/__pycache__/__main__.cpython-312.pyc +0 -0
  18. package/src/frida_bindgen/__pycache__/cli.cpython-312.pyc +0 -0
  19. package/src/frida_bindgen/__pycache__/codegen.cpython-312.pyc +0 -0
  20. package/src/frida_bindgen/__pycache__/customization.cpython-312.pyc +0 -0
  21. package/src/frida_bindgen/__pycache__/loader.cpython-312.pyc +0 -0
  22. package/src/frida_bindgen/__pycache__/model.cpython-312.pyc +0 -0
  23. package/src/frida_bindgen/assets/codegen_helpers.c +1970 -0
  24. package/src/frida_bindgen/assets/codegen_helpers.ts +100 -0
  25. package/src/frida_bindgen/assets/codegen_prototypes.h +78 -0
  26. package/src/frida_bindgen/assets/codegen_types.h +57 -0
  27. package/src/frida_bindgen/assets/customization_facade.exports +13 -0
  28. package/src/frida_bindgen/assets/customization_facade.ts +157 -0
  29. package/src/frida_bindgen/assets/customization_helpers.imports +2 -0
  30. package/src/frida_bindgen/assets/customization_helpers.ts +396 -0
  31. package/src/frida_bindgen/cli.py +96 -0
  32. package/src/frida_bindgen/codegen.py +2233 -0
  33. package/src/frida_bindgen/customization.py +924 -0
  34. package/src/frida_bindgen/loader.py +60 -0
  35. package/src/frida_bindgen/model.py +1357 -0
  36. package/src/meson.build +92 -27
  37. package/{lib/build.py → src/tsc.py} +12 -12
  38. package/src/win_delay_load_hook.c +56 -0
  39. package/subprojects/frida-core.wrap +1 -1
  40. package/test/data/index.ts +2 -2
  41. package/test/device.ts +1 -2
  42. package/test/device_manager.ts +1 -2
  43. package/test/labrat.ts +2 -2
  44. package/test/script.ts +12 -12
  45. package/test/session.ts +3 -3
  46. package/tsconfig.json +6 -11
  47. package/dist/application.d.ts +0 -81
  48. package/dist/application.js +0 -2
  49. package/dist/authentication.d.ts +0 -3
  50. package/dist/authentication.js +0 -2
  51. package/dist/bus.d.ts +0 -16
  52. package/dist/bus.js +0 -23
  53. package/dist/cancellable.d.ts +0 -15
  54. package/dist/cancellable.js +0 -41
  55. package/dist/child.d.ts +0 -16
  56. package/dist/child.js +0 -9
  57. package/dist/crash.d.ts +0 -10
  58. package/dist/crash.js +0 -2
  59. package/dist/device.d.ts +0 -156
  60. package/dist/device.js +0 -188
  61. package/dist/device_manager.d.ts +0 -25
  62. package/dist/device_manager.js +0 -42
  63. package/dist/endpoint_parameters.d.ts +0 -26
  64. package/dist/endpoint_parameters.js +0 -24
  65. package/dist/icon.d.ts +0 -14
  66. package/dist/icon.js +0 -2
  67. package/dist/index.d.ts +0 -161
  68. package/dist/index.js +0 -170
  69. package/dist/iostream.d.ts +0 -13
  70. package/dist/iostream.js +0 -73
  71. package/dist/native.d.ts +0 -1
  72. package/dist/native.js +0 -11
  73. package/dist/portal_membership.d.ts +0 -6
  74. package/dist/portal_membership.js +0 -12
  75. package/dist/portal_service.d.ts +0 -48
  76. package/dist/portal_service.js +0 -52
  77. package/dist/process.d.ts +0 -47
  78. package/dist/process.js +0 -2
  79. package/dist/relay.d.ts +0 -22
  80. package/dist/relay.js +0 -32
  81. package/dist/script.d.ts +0 -70
  82. package/dist/script.js +0 -266
  83. package/dist/service.d.ts +0 -16
  84. package/dist/service.js +0 -26
  85. package/dist/session.d.ts +0 -45
  86. package/dist/session.js +0 -73
  87. package/dist/signals.d.ts +0 -20
  88. package/dist/signals.js +0 -40
  89. package/dist/socket_address.d.ts +0 -25
  90. package/dist/socket_address.js +0 -2
  91. package/dist/spawn.d.ts +0 -4
  92. package/dist/spawn.js +0 -2
  93. package/dist/system_parameters.d.ts +0 -84
  94. package/dist/system_parameters.js +0 -2
  95. package/lib/application.ts +0 -98
  96. package/lib/authentication.ts +0 -3
  97. package/lib/bus.ts +0 -30
  98. package/lib/cancellable.ts +0 -48
  99. package/lib/child.ts +0 -15
  100. package/lib/crash.ts +0 -11
  101. package/lib/device.ts +0 -331
  102. package/lib/device_manager.ts +0 -69
  103. package/lib/endpoint_parameters.ts +0 -56
  104. package/lib/icon.ts +0 -15
  105. package/lib/index.ts +0 -316
  106. package/lib/iostream.ts +0 -78
  107. package/lib/meson.build +0 -53
  108. package/lib/native.ts +0 -9
  109. package/lib/portal_membership.ts +0 -10
  110. package/lib/portal_service.ts +0 -105
  111. package/lib/process.ts +0 -57
  112. package/lib/relay.ts +0 -44
  113. package/lib/script.ts +0 -361
  114. package/lib/service.ts +0 -34
  115. package/lib/session.ts +0 -113
  116. package/lib/signals.ts +0 -45
  117. package/lib/socket_address.ts +0 -35
  118. package/lib/spawn.ts +0 -4
  119. package/lib/system_parameters.ts +0 -103
  120. package/meson.options +0 -11
  121. package/src/addon.cc +0 -78
  122. package/src/application.cc +0 -148
  123. package/src/application.h +0 -31
  124. package/src/authentication.cc +0 -174
  125. package/src/authentication.h +0 -24
  126. package/src/bus.cc +0 -167
  127. package/src/bus.h +0 -33
  128. package/src/cancellable.cc +0 -117
  129. package/src/cancellable.h +0 -31
  130. package/src/child.cc +0 -150
  131. package/src/child.h +0 -32
  132. package/src/crash.cc +0 -122
  133. package/src/crash.h +0 -30
  134. package/src/device.cc +0 -1350
  135. package/src/device.h +0 -56
  136. package/src/device_manager.cc +0 -362
  137. package/src/device_manager.h +0 -35
  138. package/src/endpoint_parameters.cc +0 -171
  139. package/src/endpoint_parameters.h +0 -28
  140. package/src/glib_context.cc +0 -62
  141. package/src/glib_context.h +0 -29
  142. package/src/glib_object.cc +0 -25
  143. package/src/glib_object.h +0 -37
  144. package/src/iostream.cc +0 -243
  145. package/src/iostream.h +0 -30
  146. package/src/operation.h +0 -94
  147. package/src/portal_membership.cc +0 -100
  148. package/src/portal_membership.h +0 -26
  149. package/src/portal_service.cc +0 -401
  150. package/src/portal_service.h +0 -40
  151. package/src/process.cc +0 -135
  152. package/src/process.h +0 -30
  153. package/src/relay.cc +0 -139
  154. package/src/relay.h +0 -31
  155. package/src/runtime.cc +0 -568
  156. package/src/runtime.h +0 -69
  157. package/src/script.cc +0 -301
  158. package/src/script.h +0 -36
  159. package/src/service.cc +0 -224
  160. package/src/service.h +0 -36
  161. package/src/session.cc +0 -860
  162. package/src/session.h +0 -42
  163. package/src/signals.cc +0 -334
  164. package/src/signals.h +0 -47
  165. package/src/spawn.cc +0 -95
  166. package/src/spawn.h +0 -27
  167. package/src/usage_monitor.h +0 -117
  168. package/src/uv_context.cc +0 -118
  169. package/src/uv_context.h +0 -40
  170. package/src/win_delay_load_hook.cc +0 -63
  171. package/subprojects/nan.wrap +0 -9
  172. package/subprojects/packagefiles/nan.patch +0 -13
package/src/device.cc DELETED
@@ -1,1350 +0,0 @@
1
- #include "device.h"
2
-
3
- #include "application.h"
4
- #include "bus.h"
5
- #include "child.h"
6
- #include "crash.h"
7
- #include "iostream.h"
8
- #include "operation.h"
9
- #include "process.h"
10
- #include "service.h"
11
- #include "session.h"
12
- #include "signals.h"
13
- #include "spawn.h"
14
-
15
- #include <cstring>
16
-
17
- #define DEVICE_DATA_CONSTRUCTOR "device:ctor"
18
-
19
- using std::strcmp;
20
- using v8::Array;
21
- using v8::Boolean;
22
- using v8::DEFAULT;
23
- using v8::External;
24
- using v8::Function;
25
- using v8::Isolate;
26
- using v8::Local;
27
- using v8::Number;
28
- using v8::Object;
29
- using v8::Persistent;
30
- using v8::ReadOnly;
31
- using v8::String;
32
- using v8::Uint32;
33
- using v8::Value;
34
-
35
- namespace frida {
36
-
37
- Device::Device(FridaDevice* handle, Runtime* runtime)
38
- : GLibObject(handle, runtime) {
39
- g_object_ref(handle_);
40
- }
41
-
42
- Device::~Device() {
43
- frida_unref(handle_);
44
- }
45
-
46
- void Device::Init(Local<Object> exports, Runtime* runtime) {
47
- auto isolate = Isolate::GetCurrent();
48
-
49
- auto name = Nan::New("Device").ToLocalChecked();
50
- auto tpl = CreateTemplate(name, New, runtime);
51
-
52
- auto instance_tpl = tpl->InstanceTemplate();
53
- auto data = Local<Value>();
54
- Nan::SetAccessor(instance_tpl, Nan::New("isLost").ToLocalChecked(), IsLost, 0,
55
- data, DEFAULT, ReadOnly);
56
- Nan::SetAccessor(instance_tpl, Nan::New("bus").ToLocalChecked(), GetBus, 0,
57
- data, DEFAULT, ReadOnly);
58
- Nan::SetAccessor(instance_tpl, Nan::New("type").ToLocalChecked(), GetType, 0,
59
- data, DEFAULT, ReadOnly);
60
- Nan::SetAccessor(instance_tpl, Nan::New("icon").ToLocalChecked(), GetIcon, 0,
61
- data, DEFAULT, ReadOnly);
62
- Nan::SetAccessor(instance_tpl, Nan::New("name").ToLocalChecked(), GetName, 0,
63
- data, DEFAULT, ReadOnly);
64
- Nan::SetAccessor(instance_tpl, Nan::New("id").ToLocalChecked(), GetId, 0,
65
- data, DEFAULT, ReadOnly);
66
-
67
- Nan::SetPrototypeMethod(tpl, "querySystemParameters", QuerySystemParameters);
68
- Nan::SetPrototypeMethod(tpl, "getFrontmostApplication",
69
- GetFrontmostApplication);
70
- Nan::SetPrototypeMethod(tpl, "enumerateApplications", EnumerateApplications);
71
- Nan::SetPrototypeMethod(tpl, "enumerateProcesses", EnumerateProcesses);
72
- Nan::SetPrototypeMethod(tpl, "enableSpawnGating", EnableSpawnGating);
73
- Nan::SetPrototypeMethod(tpl, "disableSpawnGating", DisableSpawnGating);
74
- Nan::SetPrototypeMethod(tpl, "enumeratePendingSpawn", EnumeratePendingSpawn);
75
- Nan::SetPrototypeMethod(tpl, "enumeratePendingChildren",
76
- EnumeratePendingChildren);
77
- Nan::SetPrototypeMethod(tpl, "spawn", Spawn);
78
- Nan::SetPrototypeMethod(tpl, "input", Input);
79
- Nan::SetPrototypeMethod(tpl, "resume", Resume);
80
- Nan::SetPrototypeMethod(tpl, "kill", Kill);
81
- Nan::SetPrototypeMethod(tpl, "attach", Attach);
82
- Nan::SetPrototypeMethod(tpl, "injectLibraryFile", InjectLibraryFile);
83
- Nan::SetPrototypeMethod(tpl, "injectLibraryBlob", InjectLibraryBlob);
84
- Nan::SetPrototypeMethod(tpl, "openChannel", OpenChannel);
85
- Nan::SetPrototypeMethod(tpl, "openService", OpenService);
86
- Nan::SetPrototypeMethod(tpl, "unpair", Unpair);
87
-
88
- auto ctor = Nan::GetFunction(tpl).ToLocalChecked();
89
- Nan::Set(exports, name, ctor);
90
- runtime->SetDataPointer(DEVICE_DATA_CONSTRUCTOR,
91
- new Persistent<Function>(isolate, ctor));
92
- }
93
-
94
- Local<Object> Device::New(gpointer handle, Runtime* runtime) {
95
- auto ctor = Nan::New<Function>(
96
- *static_cast<Persistent<Function>*>(
97
- runtime->GetDataPointer(DEVICE_DATA_CONSTRUCTOR)));
98
- const int argc = 1;
99
- Local<Value> argv[argc] = { Nan::New<External>(handle) };
100
- return Nan::NewInstance(ctor, argc, argv).ToLocalChecked();
101
- }
102
-
103
- NAN_METHOD(Device::New) {
104
- if (!info.IsConstructCall()) {
105
- Nan::ThrowError("Use the `new` keyword to create a new instance");
106
- return;
107
- }
108
-
109
- if (info.Length() != 1 || !info[0]->IsExternal()) {
110
- Nan::ThrowTypeError("Bad argument, expected raw handle");
111
- return;
112
- }
113
-
114
- auto runtime = GetRuntimeFromConstructorArgs(info);
115
-
116
- auto handle = static_cast<FridaDevice*>(
117
- Local<External>::Cast(info[0])->Value());
118
- auto wrapper = new Device(handle, runtime);
119
- auto obj = info.This();
120
- wrapper->Wrap(obj);
121
- auto signals_obj = Signals::New(handle, runtime, TransformSignal, runtime);
122
-
123
- Nan::Set(obj, Nan::New("signals").ToLocalChecked(), signals_obj);
124
-
125
- auto signals_wrapper = ObjectWrap::Unwrap<Signals>(signals_obj);
126
- signals_wrapper->SetConnectCallback(OnConnect, runtime);
127
- signals_wrapper->SetDisconnectCallback(OnDisconnect, runtime);
128
-
129
- info.GetReturnValue().Set(obj);
130
- }
131
-
132
- NAN_PROPERTY_GETTER(Device::GetId) {
133
- auto handle = ObjectWrap::Unwrap<Device>(
134
- info.Holder())->GetHandle<FridaDevice>();
135
-
136
- info.GetReturnValue().Set(
137
- Nan::New(frida_device_get_id(handle)).ToLocalChecked());
138
- }
139
-
140
- NAN_PROPERTY_GETTER(Device::GetName) {
141
- auto handle = ObjectWrap::Unwrap<Device>(
142
- info.Holder())->GetHandle<FridaDevice>();
143
-
144
- info.GetReturnValue().Set(
145
- Nan::New(frida_device_get_name(handle)).ToLocalChecked());
146
- }
147
-
148
- NAN_PROPERTY_GETTER(Device::GetIcon) {
149
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
150
- auto handle = wrapper->GetHandle<FridaDevice>();
151
-
152
- GVariant* icon = frida_device_get_icon(handle);
153
- if (icon != NULL)
154
- info.GetReturnValue().Set(Runtime::ValueFromVariant(icon));
155
- else
156
- info.GetReturnValue().SetNull();
157
- }
158
-
159
- NAN_PROPERTY_GETTER(Device::GetType) {
160
- auto handle = ObjectWrap::Unwrap<Device>(
161
- info.Holder())->GetHandle<FridaDevice>();
162
-
163
- info.GetReturnValue().Set(Runtime::ValueFromEnum(
164
- frida_device_get_dtype(handle), FRIDA_TYPE_DEVICE_TYPE));
165
- }
166
-
167
- NAN_PROPERTY_GETTER(Device::GetBus) {
168
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
169
- auto handle = wrapper->GetHandle<FridaDevice>();
170
-
171
- info.GetReturnValue().Set(
172
- Bus::New(frida_device_get_bus(handle), wrapper->runtime_));
173
- }
174
-
175
- NAN_PROPERTY_GETTER(Device::IsLost) {
176
- auto handle = ObjectWrap::Unwrap<Device>(
177
- info.Holder())->GetHandle<FridaDevice>();
178
-
179
- info.GetReturnValue().Set(
180
- Nan::New(static_cast<bool>(frida_device_is_lost(handle))));
181
- }
182
-
183
- namespace {
184
-
185
- class QuerySystemParametersOperation : public Operation<FridaDevice> {
186
- protected:
187
- void Begin() {
188
- frida_device_query_system_parameters(handle_, cancellable_, OnReady, this);
189
- }
190
-
191
- void End(GAsyncResult* result, GError** error) {
192
- parameters_ = frida_device_query_system_parameters_finish(handle_, result,
193
- error);
194
- }
195
-
196
- Local<Value> Result(Isolate* isolate) {
197
- auto value = Runtime::ValueFromParametersDict(parameters_);
198
- g_hash_table_unref(parameters_);
199
- return value;
200
- }
201
-
202
- private:
203
- GHashTable* parameters_;
204
- };
205
-
206
- }
207
-
208
- NAN_METHOD(Device::QuerySystemParameters) {
209
- auto isolate = info.GetIsolate();
210
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
211
-
212
- auto operation = new QuerySystemParametersOperation();
213
- operation->Schedule(isolate, wrapper, info);
214
-
215
- info.GetReturnValue().Set(operation->GetPromise(isolate));
216
- }
217
-
218
- namespace {
219
-
220
- class GetFrontmostApplicationOperation : public Operation<FridaDevice> {
221
- public:
222
- GetFrontmostApplicationOperation(FridaFrontmostQueryOptions* options)
223
- : application_(NULL),
224
- options_(options) {
225
- }
226
-
227
- ~GetFrontmostApplicationOperation() {
228
- g_object_unref(options_);
229
- g_clear_object(&application_);
230
- }
231
-
232
- protected:
233
- void Begin() {
234
- frida_device_get_frontmost_application(handle_, options_, cancellable_,
235
- OnReady, this);
236
- }
237
-
238
- void End(GAsyncResult* result, GError** error) {
239
- application_ = frida_device_get_frontmost_application_finish(handle_,
240
- result, error);
241
- }
242
-
243
- Local<Value> Result(Isolate* isolate) {
244
- if (application_ == NULL)
245
- return Nan::Null();
246
- return Application::New(application_, runtime_);
247
- }
248
-
249
- private:
250
- FridaApplication* application_;
251
- FridaFrontmostQueryOptions* options_;
252
- };
253
-
254
- }
255
-
256
- NAN_METHOD(Device::GetFrontmostApplication) {
257
- auto isolate = info.GetIsolate();
258
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
259
-
260
- if (info.Length() < 1) {
261
- Nan::ThrowTypeError("Missing one or more arguments");
262
- return;
263
- }
264
-
265
- auto scope_value = info[0];
266
-
267
- auto options = frida_frontmost_query_options_new();
268
- bool valid = true;
269
-
270
- if (!scope_value->IsNull()) {
271
- FridaScope scope;
272
- if (Runtime::ValueToEnum(scope_value, FRIDA_TYPE_SCOPE, &scope))
273
- frida_frontmost_query_options_set_scope(options, scope);
274
- else
275
- valid = false;
276
- }
277
-
278
- if (!valid) {
279
- g_object_unref(options);
280
- return;
281
- }
282
-
283
- auto operation = new GetFrontmostApplicationOperation(options);
284
- operation->Schedule(isolate, wrapper, info);
285
-
286
- info.GetReturnValue().Set(operation->GetPromise(isolate));
287
- }
288
-
289
- namespace {
290
-
291
- class EnumerateApplicationsOperation : public Operation<FridaDevice> {
292
- public:
293
- EnumerateApplicationsOperation(FridaApplicationQueryOptions* options)
294
- : applications_(NULL),
295
- options_(options) {
296
- }
297
-
298
- ~EnumerateApplicationsOperation() {
299
- g_object_unref(options_);
300
- g_clear_object(&applications_);
301
- }
302
-
303
- protected:
304
- void Begin() {
305
- frida_device_enumerate_applications(handle_, options_, cancellable_,
306
- OnReady, this);
307
- }
308
-
309
- void End(GAsyncResult* result, GError** error) {
310
- applications_ = frida_device_enumerate_applications_finish(handle_, result,
311
- error);
312
- }
313
-
314
- Local<Value> Result(Isolate* isolate) {
315
- auto size = frida_application_list_size(applications_);
316
- auto applications = Nan::New<Array>(size);
317
- for (auto i = 0; i != size; i++) {
318
- auto handle = frida_application_list_get(applications_, i);
319
- auto application = Application::New(handle, runtime_);
320
- Nan::Set(applications, i, application);
321
- g_object_unref(handle);
322
- }
323
- return applications;
324
- }
325
-
326
- private:
327
- FridaApplicationList* applications_;
328
- FridaApplicationQueryOptions* options_;
329
- };
330
-
331
- }
332
-
333
- NAN_METHOD(Device::EnumerateApplications) {
334
- auto isolate = info.GetIsolate();
335
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
336
-
337
- if (info.Length() < 2) {
338
- Nan::ThrowTypeError("Missing one or more arguments");
339
- return;
340
- }
341
-
342
- auto identifiers_value = info[0];
343
- auto scope_value = info[1];
344
-
345
- auto options = frida_application_query_options_new();
346
- bool valid = true;
347
-
348
- if (identifiers_value->IsArray()) {
349
- auto array = Local<Array>::Cast(identifiers_value);
350
-
351
- uint32_t n = array->Length();
352
-
353
- for (uint32_t i = 0; i != n; i++) {
354
- auto element_value = Nan::Get(array, i).ToLocalChecked();
355
-
356
- if (!element_value->IsString()) {
357
- Nan::ThrowTypeError("Bad argument, not a valid application ID");
358
- valid = false;
359
- break;
360
- }
361
- Nan::Utf8String identifier(element_value);
362
-
363
- frida_application_query_options_select_identifier(options, *identifier);
364
- }
365
- } else {
366
- Nan::ThrowTypeError("Bad argument, 'identifiers' must be an array of "
367
- "application IDs");
368
- valid = false;
369
- }
370
-
371
- if (valid && !scope_value->IsNull()) {
372
- FridaScope scope;
373
- if (Runtime::ValueToEnum(scope_value, FRIDA_TYPE_SCOPE, &scope))
374
- frida_application_query_options_set_scope(options, scope);
375
- else
376
- valid = false;
377
- }
378
-
379
- if (!valid) {
380
- g_object_unref(options);
381
- return;
382
- }
383
-
384
- auto operation = new EnumerateApplicationsOperation(options);
385
- operation->Schedule(isolate, wrapper, info);
386
-
387
- info.GetReturnValue().Set(operation->GetPromise(isolate));
388
- }
389
-
390
- namespace {
391
-
392
- class EnumerateProcessesOperation : public Operation<FridaDevice> {
393
- public:
394
- EnumerateProcessesOperation(FridaProcessQueryOptions* options)
395
- : processes_(NULL),
396
- options_(options) {
397
- }
398
-
399
- ~EnumerateProcessesOperation() {
400
- g_object_unref(options_);
401
- g_clear_object(&processes_);
402
- }
403
-
404
- protected:
405
- void Begin() {
406
- frida_device_enumerate_processes(handle_, options_, cancellable_, OnReady,
407
- this);
408
- }
409
-
410
- void End(GAsyncResult* result, GError** error) {
411
- processes_ = frida_device_enumerate_processes_finish(handle_, result,
412
- error);
413
- }
414
-
415
- Local<Value> Result(Isolate* isolate) {
416
- auto size = frida_process_list_size(processes_);
417
- auto processes = Nan::New<Array>(size);
418
- for (auto i = 0; i != size; i++) {
419
- auto handle = frida_process_list_get(processes_, i);
420
- auto process = Process::New(handle, runtime_);
421
- Nan::Set(processes, i, process);
422
- g_object_unref(handle);
423
- }
424
- return processes;
425
- }
426
-
427
- private:
428
- FridaProcessList* processes_;
429
- FridaProcessQueryOptions* options_;
430
- };
431
-
432
- }
433
-
434
- NAN_METHOD(Device::EnumerateProcesses) {
435
- auto isolate = info.GetIsolate();
436
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
437
-
438
- if (info.Length() < 2) {
439
- Nan::ThrowTypeError("Missing one or more arguments");
440
- return;
441
- }
442
-
443
- auto pids_value = info[0];
444
- auto scope_value = info[1];
445
-
446
- auto options = frida_process_query_options_new();
447
- bool valid = true;
448
-
449
- if (pids_value->IsArray()) {
450
- auto array = Local<Array>::Cast(pids_value);
451
-
452
- uint32_t n = array->Length();
453
-
454
- for (uint32_t i = 0; i != n; i++) {
455
- auto element_value = Nan::Get(array, i).ToLocalChecked();
456
-
457
- int64_t pid = -1;
458
- if (element_value->IsNumber()) {
459
- pid = Nan::To<int64_t>(element_value).FromMaybe(-1);
460
- }
461
- if (pid < 0) {
462
- Nan::ThrowTypeError("Bad argument, not a valid process ID");
463
- valid = false;
464
- break;
465
- }
466
-
467
- frida_process_query_options_select_pid(options, pid);
468
- }
469
- } else {
470
- Nan::ThrowTypeError("Bad argument, 'pids' must be an array of process IDs");
471
- valid = false;
472
- }
473
-
474
- if (valid && !scope_value->IsNull()) {
475
- FridaScope scope;
476
- if (Runtime::ValueToEnum(scope_value, FRIDA_TYPE_SCOPE, &scope))
477
- frida_process_query_options_set_scope(options, scope);
478
- else
479
- valid = false;
480
- }
481
-
482
- if (!valid) {
483
- g_object_unref(options);
484
- return;
485
- }
486
-
487
- auto operation = new EnumerateProcessesOperation(options);
488
- operation->Schedule(isolate, wrapper, info);
489
-
490
- info.GetReturnValue().Set(operation->GetPromise(isolate));
491
- }
492
-
493
- namespace {
494
-
495
- class EnableSpawnGatingOperation : public Operation<FridaDevice> {
496
- protected:
497
- void Begin() {
498
- frida_device_enable_spawn_gating(handle_, cancellable_, OnReady, this);
499
- }
500
-
501
- void End(GAsyncResult* result, GError** error) {
502
- frida_device_enable_spawn_gating_finish(handle_, result, error);
503
- }
504
-
505
- Local<Value> Result(Isolate* isolate) {
506
- return Nan::Undefined();
507
- }
508
- };
509
-
510
- }
511
-
512
- NAN_METHOD(Device::EnableSpawnGating) {
513
- auto isolate = info.GetIsolate();
514
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
515
-
516
- auto operation = new EnableSpawnGatingOperation();
517
- operation->Schedule(isolate, wrapper, info);
518
-
519
- info.GetReturnValue().Set(operation->GetPromise(isolate));
520
- }
521
-
522
- namespace {
523
-
524
- class DisableSpawnGatingOperation : public Operation<FridaDevice> {
525
- protected:
526
- void Begin() {
527
- frida_device_disable_spawn_gating(handle_, cancellable_, OnReady, this);
528
- }
529
-
530
- void End(GAsyncResult* result, GError** error) {
531
- frida_device_disable_spawn_gating_finish(handle_, result, error);
532
- }
533
-
534
- Local<Value> Result(Isolate* isolate) {
535
- return Nan::Undefined();
536
- }
537
- };
538
-
539
- }
540
-
541
- NAN_METHOD(Device::DisableSpawnGating) {
542
- auto isolate = info.GetIsolate();
543
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
544
-
545
- auto operation = new DisableSpawnGatingOperation();
546
- operation->Schedule(isolate, wrapper, info);
547
-
548
- info.GetReturnValue().Set(operation->GetPromise(isolate));
549
- }
550
-
551
- namespace {
552
-
553
- class EnumeratePendingSpawnOperation : public Operation<FridaDevice> {
554
- protected:
555
- void Begin() {
556
- frida_device_enumerate_pending_spawn(handle_, cancellable_, OnReady, this);
557
- }
558
-
559
- void End(GAsyncResult* result, GError** error) {
560
- pending_spawn_ = frida_device_enumerate_pending_spawn_finish(handle_,
561
- result, error);
562
- }
563
-
564
- Local<Value> Result(Isolate* isolate) {
565
- auto size = frida_spawn_list_size(pending_spawn_);
566
- auto pending_spawn = Nan::New<Array>(size);
567
- for (auto i = 0; i != size; i++) {
568
- auto handle = frida_spawn_list_get(pending_spawn_, i);
569
- auto spawn = Spawn::New(handle, runtime_);
570
- Nan::Set(pending_spawn, i, spawn);
571
- g_object_unref(handle);
572
- }
573
-
574
- g_object_unref(pending_spawn_);
575
-
576
- return pending_spawn;
577
- }
578
-
579
- private:
580
- FridaSpawnList* pending_spawn_;
581
- };
582
-
583
- }
584
-
585
- NAN_METHOD(Device::EnumeratePendingSpawn) {
586
- auto isolate = info.GetIsolate();
587
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
588
-
589
- auto operation = new EnumeratePendingSpawnOperation();
590
- operation->Schedule(isolate, wrapper, info);
591
-
592
- info.GetReturnValue().Set(operation->GetPromise(isolate));
593
- }
594
-
595
- namespace {
596
-
597
- class EnumeratePendingChildrenOperation : public Operation<FridaDevice> {
598
- protected:
599
- void Begin() {
600
- frida_device_enumerate_pending_children(handle_, cancellable_, OnReady,
601
- this);
602
- }
603
-
604
- void End(GAsyncResult* result, GError** error) {
605
- pending_children_ = frida_device_enumerate_pending_children_finish(handle_,
606
- result, error);
607
- }
608
-
609
- Local<Value> Result(Isolate* isolate) {
610
- auto size = frida_child_list_size(pending_children_);
611
- auto pending_children = Nan::New<Array>(size);
612
- for (auto i = 0; i != size; i++) {
613
- auto handle = frida_child_list_get(pending_children_, i);
614
- auto child = Child::New(handle, runtime_);
615
- Nan::Set(pending_children, i, child);
616
- g_object_unref(handle);
617
- }
618
-
619
- g_object_unref(pending_children_);
620
-
621
- return pending_children;
622
- }
623
-
624
- private:
625
- FridaChildList* pending_children_;
626
- };
627
-
628
- }
629
-
630
- NAN_METHOD(Device::EnumeratePendingChildren) {
631
- auto isolate = info.GetIsolate();
632
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
633
-
634
- auto operation = new EnumeratePendingChildrenOperation();
635
- operation->Schedule(isolate, wrapper, info);
636
-
637
- info.GetReturnValue().Set(operation->GetPromise(isolate));
638
- }
639
-
640
- namespace {
641
-
642
- class SpawnOperation : public Operation<FridaDevice> {
643
- public:
644
- SpawnOperation(gchar* program, FridaSpawnOptions* options)
645
- : program_(program),
646
- options_(options) {
647
- }
648
-
649
- ~SpawnOperation() {
650
- g_object_unref(options_);
651
- g_free(program_);
652
- }
653
-
654
- protected:
655
- void Begin() {
656
- frida_device_spawn(handle_, program_, options_, cancellable_, OnReady,
657
- this);
658
- }
659
-
660
- void End(GAsyncResult* result, GError** error) {
661
- pid_ = frida_device_spawn_finish(handle_, result, error);
662
- }
663
-
664
- Local<Value> Result(Isolate* isolate) {
665
- return Nan::New<Uint32>(pid_);
666
- }
667
-
668
- private:
669
- gchar* program_;
670
- FridaSpawnOptions* options_;
671
- guint pid_;
672
- };
673
-
674
- }
675
-
676
- NAN_METHOD(Device::Spawn) {
677
- auto isolate = info.GetIsolate();
678
- auto context = isolate->GetCurrentContext();
679
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
680
-
681
- if (info.Length() < 7) {
682
- Nan::ThrowTypeError("Missing one or more arguments");
683
- return;
684
- }
685
-
686
- auto program_value = info[0];
687
- auto argv_value = info[1];
688
- auto envp_value = info[2];
689
- auto env_value = info[3];
690
- auto cwd_value = info[4];
691
- auto stdio_value = info[5];
692
- auto aux_value = info[6];
693
-
694
- if (!program_value->IsString()) {
695
- Nan::ThrowTypeError("Bad argument, 'program' must be a string");
696
- return;
697
- }
698
- Nan::Utf8String program(program_value);
699
-
700
- auto options = frida_spawn_options_new();
701
- bool valid = true;
702
-
703
- if (!argv_value->IsNull()) {
704
- gchar** argv;
705
- gint argv_length;
706
- valid = Runtime::ValueToStrv(argv_value, &argv, &argv_length);
707
- if (valid) {
708
- frida_spawn_options_set_argv(options, argv, argv_length);
709
- g_strfreev(argv);
710
- }
711
- }
712
-
713
- if (valid && !envp_value->IsNull()) {
714
- gchar** envp;
715
- gint envp_length;
716
- valid = Runtime::ValueToEnvp(envp_value, &envp, &envp_length);
717
- if (valid) {
718
- frida_spawn_options_set_envp(options, envp, envp_length);
719
- g_strfreev(envp);
720
- }
721
- }
722
-
723
- if (valid && !env_value->IsNull()) {
724
- gchar** env;
725
- gint env_length;
726
- valid = Runtime::ValueToEnvp(env_value, &env, &env_length);
727
- if (valid) {
728
- frida_spawn_options_set_env(options, env, env_length);
729
- g_strfreev(env);
730
- }
731
- }
732
-
733
- if (valid && !cwd_value->IsNull()) {
734
- if (cwd_value->IsString()) {
735
- Nan::Utf8String cwd(cwd_value);
736
- frida_spawn_options_set_cwd(options, *cwd);
737
- } else {
738
- Nan::ThrowTypeError("Bad argument, 'cwd' must be a string");
739
- valid = false;
740
- }
741
- }
742
-
743
- if (valid && !stdio_value->IsNull()) {
744
- FridaStdio stdio;
745
- valid = Runtime::ValueToEnum(stdio_value, FRIDA_TYPE_STDIO, &stdio);
746
- if (valid)
747
- frida_spawn_options_set_stdio(options, stdio);
748
- }
749
-
750
- if (valid) {
751
- if (aux_value->IsObject()) {
752
- auto object = Local<Object>::Cast(aux_value);
753
-
754
- Local<Array> keys(object->GetOwnPropertyNames(context).ToLocalChecked());
755
- uint32_t n = keys->Length();
756
-
757
- GHashTable* aux = frida_spawn_options_get_aux(options);
758
-
759
- for (uint32_t i = 0; i != n; i++) {
760
- auto key = Nan::Get(keys, i).ToLocalChecked();
761
- auto value = Nan::Get(object, key).ToLocalChecked();
762
- if (value->IsUndefined()) {
763
- continue;
764
- }
765
-
766
- Nan::Utf8String k(key);
767
-
768
- auto v = Runtime::ValueToVariant(value);
769
- if (v == NULL) {
770
- valid = false;
771
- break;
772
- }
773
-
774
- g_hash_table_insert(aux, g_strdup(*k), g_variant_ref_sink(v));
775
- }
776
- } else {
777
- Nan::ThrowTypeError("Bad argument, 'aux' must be an object");
778
- valid = false;
779
- }
780
- }
781
-
782
- if (!valid) {
783
- g_object_unref(options);
784
- return;
785
- }
786
-
787
- auto operation = new SpawnOperation(g_strdup(*program), options);
788
- operation->Schedule(isolate, wrapper, info);
789
-
790
- info.GetReturnValue().Set(operation->GetPromise(isolate));
791
- }
792
-
793
- namespace {
794
-
795
- class InputOperation : public Operation<FridaDevice> {
796
- public:
797
- InputOperation(guint pid, GBytes* data)
798
- : pid_(pid), data_(data) {
799
- }
800
-
801
- ~InputOperation() {
802
- g_bytes_unref(data_);
803
- }
804
-
805
- protected:
806
- void Begin() {
807
- frida_device_input(handle_, pid_, data_, cancellable_, OnReady, this);
808
- }
809
-
810
- void End(GAsyncResult* result, GError** error) {
811
- frida_device_input_finish(handle_, result, error);
812
- }
813
-
814
- Local<Value> Result(Isolate* isolate) {
815
- return Nan::Undefined();
816
- }
817
-
818
- private:
819
- const guint pid_;
820
- GBytes* data_;
821
- };
822
-
823
- }
824
-
825
- NAN_METHOD(Device::Input) {
826
- auto isolate = info.GetIsolate();
827
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
828
-
829
- if (info.Length() < 2 || !info[0]->IsNumber() ||
830
- !node::Buffer::HasInstance(info[1])) {
831
- Nan::ThrowTypeError("Bad argument, expected pid and data");
832
- return;
833
- }
834
-
835
- auto pid = Nan::To<int64_t>(info[0]).FromMaybe(0);
836
- if (pid <= 0) {
837
- Nan::ThrowTypeError("Bad pid");
838
- return;
839
- }
840
-
841
- auto buffer = info[1];
842
- auto data = g_bytes_new(node::Buffer::Data(buffer),
843
- node::Buffer::Length(buffer));
844
-
845
- auto operation = new InputOperation(static_cast<guint>(pid), data);
846
- operation->Schedule(isolate, wrapper, info);
847
-
848
- info.GetReturnValue().Set(operation->GetPromise(isolate));
849
- }
850
-
851
- namespace {
852
-
853
- class ResumeOperation : public Operation<FridaDevice> {
854
- public:
855
- ResumeOperation(guint pid) : pid_(pid) {
856
- }
857
-
858
- protected:
859
- void Begin() {
860
- frida_device_resume(handle_, pid_, cancellable_, OnReady, this);
861
- }
862
-
863
- void End(GAsyncResult* result, GError** error) {
864
- frida_device_resume_finish(handle_, result, error);
865
- }
866
-
867
- Local<Value> Result(Isolate* isolate) {
868
- return Nan::Undefined();
869
- }
870
-
871
- private:
872
- const guint pid_;
873
- };
874
-
875
- }
876
-
877
- NAN_METHOD(Device::Resume) {
878
- auto isolate = info.GetIsolate();
879
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
880
-
881
- if (info.Length() < 1 || !info[0]->IsNumber()) {
882
- Nan::ThrowTypeError("Bad argument, expected pid");
883
- return;
884
- }
885
- auto pid = Nan::To<int64_t>(info[0]).FromMaybe(0);
886
- if (pid <= 0) {
887
- Nan::ThrowTypeError("Bad argument, expected pid");
888
- return;
889
- }
890
-
891
- auto operation = new ResumeOperation(static_cast<guint>(pid));
892
- operation->Schedule(isolate, wrapper, info);
893
-
894
- info.GetReturnValue().Set(operation->GetPromise(isolate));
895
- }
896
-
897
- namespace {
898
-
899
- class KillOperation : public Operation<FridaDevice> {
900
- public:
901
- KillOperation(guint pid) : pid_(pid) {
902
- }
903
-
904
- protected:
905
- void Begin() {
906
- frida_device_kill(handle_, pid_, cancellable_, OnReady, this);
907
- }
908
-
909
- void End(GAsyncResult* result, GError** error) {
910
- frida_device_kill_finish(handle_, result, error);
911
- }
912
-
913
- Local<Value> Result(Isolate* isolate) {
914
- return Nan::Undefined();
915
- }
916
-
917
- private:
918
- const guint pid_;
919
- };
920
-
921
- }
922
-
923
- NAN_METHOD(Device::Kill) {
924
- auto isolate = info.GetIsolate();
925
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
926
-
927
- if (info.Length() < 1 || !info[0]->IsNumber()) {
928
- Nan::ThrowTypeError("Bad argument, expected pid");
929
- return;
930
- }
931
- auto pid = Nan::To<int64_t>(info[0]).FromMaybe(0);
932
- if (pid <= 0) {
933
- Nan::ThrowTypeError("Bad argument, expected pid");
934
- return;
935
- }
936
-
937
- auto operation = new KillOperation(static_cast<guint>(pid));
938
- operation->Schedule(isolate, wrapper, info);
939
-
940
- info.GetReturnValue().Set(operation->GetPromise(isolate));
941
- }
942
-
943
- namespace {
944
-
945
- class AttachOperation : public Operation<FridaDevice> {
946
- public:
947
- AttachOperation(guint pid, FridaSessionOptions* options)
948
- : pid_(pid),
949
- options_(options) {
950
- }
951
-
952
- ~AttachOperation() {
953
- g_object_unref(options_);
954
- }
955
-
956
- protected:
957
- void Begin() {
958
- frida_device_attach(handle_, pid_, options_, cancellable_, OnReady, this);
959
- }
960
-
961
- void End(GAsyncResult* result, GError** error) {
962
- session_ = frida_device_attach_finish(handle_, result, error);
963
- }
964
-
965
- Local<Value> Result(Isolate* isolate) {
966
- auto wrapper = Session::New(session_, runtime_);
967
- g_object_unref(session_);
968
- return wrapper;
969
- }
970
-
971
- private:
972
- const guint pid_;
973
- FridaSessionOptions* options_;
974
- FridaSession* session_;
975
- };
976
-
977
- }
978
-
979
- NAN_METHOD(Device::Attach) {
980
- auto isolate = info.GetIsolate();
981
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
982
-
983
- if (info.Length() < 3) {
984
- Nan::ThrowTypeError("Missing one or more arguments");
985
- return;
986
- }
987
-
988
- auto pid_value = info[0];
989
- auto realm_value = info[1];
990
- auto persist_timeout_value = info[2];
991
-
992
- int64_t pid = -1;
993
- if (pid_value->IsNumber()) {
994
- pid = Nan::To<int64_t>(pid_value).FromMaybe(-1);
995
- }
996
- if (pid < 0) {
997
- Nan::ThrowTypeError("Bad argument, expected pid");
998
- return;
999
- }
1000
-
1001
- auto options = frida_session_options_new();
1002
- bool valid = true;
1003
-
1004
- if (!realm_value->IsNull()) {
1005
- FridaRealm realm;
1006
- if (Runtime::ValueToEnum(realm_value, FRIDA_TYPE_REALM, &realm))
1007
- frida_session_options_set_realm(options, realm);
1008
- else
1009
- valid = false;
1010
- }
1011
-
1012
- if (valid && !persist_timeout_value->IsNull()) {
1013
- if (persist_timeout_value->IsNumber()) {
1014
- auto persist_timeout =
1015
- Nan::To<int32_t>(persist_timeout_value).FromMaybe(-1);
1016
- if (persist_timeout >= 0) {
1017
- frida_session_options_set_persist_timeout(options, persist_timeout);
1018
- } else {
1019
- Nan::ThrowTypeError("Bad argument, invalid 'persistTimeout'");
1020
- valid = false;
1021
- }
1022
- } else {
1023
- Nan::ThrowTypeError("Bad argument, 'persistTimeout' must be a number");
1024
- valid = false;
1025
- }
1026
- }
1027
-
1028
- if (!valid) {
1029
- g_object_unref(options);
1030
- return;
1031
- }
1032
-
1033
- auto operation = new AttachOperation(static_cast<guint>(pid), options);
1034
- operation->Schedule(isolate, wrapper, info);
1035
-
1036
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1037
- }
1038
-
1039
- namespace {
1040
-
1041
- class InjectLibraryFileOperation : public Operation<FridaDevice> {
1042
- public:
1043
- InjectLibraryFileOperation(guint pid, gchar* path, gchar* entrypoint,
1044
- gchar* data)
1045
- : pid_(pid),
1046
- path_(path),
1047
- entrypoint_(entrypoint),
1048
- data_(data) {
1049
- }
1050
-
1051
- ~InjectLibraryFileOperation() {
1052
- g_free(data_);
1053
- g_free(entrypoint_);
1054
- g_free(path_);
1055
- }
1056
-
1057
- protected:
1058
- void Begin() {
1059
- frida_device_inject_library_file(handle_, pid_, path_, entrypoint_, data_,
1060
- cancellable_, OnReady, this);
1061
- }
1062
-
1063
- void End(GAsyncResult* result, GError** error) {
1064
- id_ = frida_device_inject_library_file_finish(handle_, result, error);
1065
- }
1066
-
1067
- Local<Value> Result(Isolate* isolate) {
1068
- return Nan::New<Uint32>(id_);
1069
- }
1070
-
1071
- private:
1072
- const guint pid_;
1073
- gchar* path_;
1074
- gchar* entrypoint_;
1075
- gchar* data_;
1076
- guint id_;
1077
- };
1078
-
1079
- }
1080
-
1081
- NAN_METHOD(Device::InjectLibraryFile) {
1082
- auto isolate = info.GetIsolate();
1083
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
1084
-
1085
- if (info.Length() < 4 || !info[0]->IsNumber() || !info[1]->IsString() ||
1086
- !info[2]->IsString() || !info[3]->IsString()) {
1087
- Nan::ThrowTypeError("Bad argument");
1088
- return;
1089
- }
1090
-
1091
- auto pid = Nan::To<int64_t>(info[0]).FromMaybe(-1);
1092
- if (pid < 0) {
1093
- Nan::ThrowTypeError("Bad argument, expected pid");
1094
- return;
1095
- }
1096
- Nan::Utf8String path(info[1]);
1097
- Nan::Utf8String entrypoint(info[2]);
1098
- Nan::Utf8String data(info[3]);
1099
-
1100
- auto operation = new InjectLibraryFileOperation(static_cast<guint>(pid),
1101
- g_strdup(*path), g_strdup(*entrypoint), g_strdup(*data));
1102
- operation->Schedule(isolate, wrapper, info);
1103
-
1104
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1105
- }
1106
-
1107
- namespace {
1108
-
1109
- class InjectLibraryBlobOperation : public Operation<FridaDevice> {
1110
- public:
1111
- InjectLibraryBlobOperation(guint pid, GBytes* blob, gchar* entrypoint,
1112
- gchar* data)
1113
- : pid_(pid),
1114
- blob_(blob),
1115
- entrypoint_(entrypoint),
1116
- data_(data) {
1117
- }
1118
-
1119
- ~InjectLibraryBlobOperation() {
1120
- g_free(data_);
1121
- g_free(entrypoint_);
1122
- g_bytes_unref(blob_);
1123
- }
1124
-
1125
- protected:
1126
- void Begin() {
1127
- frida_device_inject_library_blob(handle_, pid_, blob_, entrypoint_, data_,
1128
- cancellable_, OnReady, this);
1129
- }
1130
-
1131
- void End(GAsyncResult* result, GError** error) {
1132
- id_ = frida_device_inject_library_blob_finish(handle_, result, error);
1133
- }
1134
-
1135
- Local<Value> Result(Isolate* isolate) {
1136
- return Nan::New<Uint32>(id_);
1137
- }
1138
-
1139
- private:
1140
- const guint pid_;
1141
- GBytes* blob_;
1142
- gchar* entrypoint_;
1143
- gchar* data_;
1144
- guint id_;
1145
- };
1146
-
1147
- }
1148
-
1149
- NAN_METHOD(Device::InjectLibraryBlob) {
1150
- auto isolate = info.GetIsolate();
1151
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
1152
-
1153
- if (info.Length() < 4 || !info[0]->IsNumber() ||
1154
- !node::Buffer::HasInstance(info[1]) || !info[2]->IsString() ||
1155
- !info[3]->IsString()) {
1156
- Nan::ThrowTypeError("Bad argument");
1157
- return;
1158
- }
1159
-
1160
- auto pid = Nan::To<int64_t>(info[0]).FromMaybe(-1);
1161
- if (pid < 0) {
1162
- Nan::ThrowTypeError("Bad argument, expected pid");
1163
- return;
1164
- }
1165
- auto buffer = info[1];
1166
- auto blob = g_bytes_new(node::Buffer::Data(buffer),
1167
- node::Buffer::Length(buffer));
1168
- Nan::Utf8String entrypoint(info[2]);
1169
- Nan::Utf8String data(info[3]);
1170
-
1171
- auto operation = new InjectLibraryBlobOperation(static_cast<guint>(pid),
1172
- blob, g_strdup(*entrypoint), g_strdup(*data));
1173
- operation->Schedule(isolate, wrapper, info);
1174
-
1175
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1176
- }
1177
-
1178
- namespace {
1179
-
1180
- class OpenChannelOperation : public Operation<FridaDevice> {
1181
- public:
1182
- OpenChannelOperation(gchar* address)
1183
- : address_(address),
1184
- stream_(NULL) {
1185
- }
1186
-
1187
- ~OpenChannelOperation() {
1188
- g_free(address_);
1189
- }
1190
-
1191
- protected:
1192
- void Begin() {
1193
- frida_device_open_channel(handle_, address_, cancellable_, OnReady, this);
1194
- }
1195
-
1196
- void End(GAsyncResult* result, GError** error) {
1197
- stream_ = frida_device_open_channel_finish(handle_, result, error);
1198
- }
1199
-
1200
- Local<Value> Result(Isolate* isolate) {
1201
- auto wrapper = IOStream::New(stream_, runtime_);
1202
- g_object_unref(stream_);
1203
- return wrapper;
1204
- }
1205
-
1206
- private:
1207
- gchar* address_;
1208
- GIOStream* stream_;
1209
- };
1210
-
1211
- }
1212
-
1213
- NAN_METHOD(Device::OpenChannel) {
1214
- auto isolate = info.GetIsolate();
1215
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
1216
-
1217
- if (info.Length() < 1 || !info[0]->IsString()) {
1218
- Nan::ThrowTypeError("Bad argument");
1219
- return;
1220
- }
1221
- Nan::Utf8String address(info[0]);
1222
-
1223
- auto operation = new OpenChannelOperation(g_strdup(*address));
1224
- operation->Schedule(isolate, wrapper, info);
1225
-
1226
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1227
- }
1228
-
1229
- namespace {
1230
-
1231
- class OpenServiceOperation : public Operation<FridaDevice> {
1232
- public:
1233
- OpenServiceOperation(gchar* address)
1234
- : address_(address) {
1235
- }
1236
-
1237
- ~OpenServiceOperation() {
1238
- g_free(address_);
1239
- }
1240
-
1241
- protected:
1242
- void Begin() {
1243
- frida_device_open_service(handle_, address_, cancellable_, OnReady, this);
1244
- }
1245
-
1246
- void End(GAsyncResult* result, GError** error) {
1247
- service_ = frida_device_open_service_finish(handle_, result, error);
1248
- }
1249
-
1250
- Local<Value> Result(Isolate* isolate) {
1251
- auto wrapper = Service::New(service_, runtime_);
1252
- g_object_unref(service_);
1253
- return wrapper;
1254
- }
1255
-
1256
- private:
1257
- gchar* address_;
1258
- FridaService* service_;
1259
- };
1260
-
1261
- }
1262
-
1263
- NAN_METHOD(Device::OpenService) {
1264
- auto isolate = info.GetIsolate();
1265
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
1266
-
1267
- if (info.Length() < 1 || !info[0]->IsString()) {
1268
- Nan::ThrowTypeError("Bad argument");
1269
- return;
1270
- }
1271
- Nan::Utf8String address(info[0]);
1272
-
1273
- auto operation = new OpenServiceOperation(g_strdup(*address));
1274
- operation->Schedule(isolate, wrapper, info);
1275
-
1276
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1277
- }
1278
-
1279
- namespace {
1280
-
1281
- class UnpairOperation : public Operation<FridaDevice> {
1282
- protected:
1283
- void Begin() {
1284
- frida_device_unpair(handle_, cancellable_, OnReady, this);
1285
- }
1286
-
1287
- void End(GAsyncResult* result, GError** error) {
1288
- frida_device_unpair_finish(handle_, result, error);
1289
- }
1290
-
1291
- Local<Value> Result(Isolate* isolate) {
1292
- return Nan::Undefined();
1293
- }
1294
- };
1295
-
1296
- }
1297
-
1298
- NAN_METHOD(Device::Unpair) {
1299
- auto isolate = info.GetIsolate();
1300
- auto wrapper = ObjectWrap::Unwrap<Device>(info.Holder());
1301
-
1302
- auto operation = new UnpairOperation();
1303
- operation->Schedule(isolate, wrapper, info);
1304
-
1305
- info.GetReturnValue().Set(operation->GetPromise(isolate));
1306
- }
1307
-
1308
- Local<Value> Device::TransformSignal(const gchar* name, guint index,
1309
- const GValue* value, gpointer user_data) {
1310
- auto runtime = static_cast<Runtime*>(user_data);
1311
-
1312
- if (index == 0 && (strcmp(name, "spawn-added") == 0 ||
1313
- strcmp(name, "spawn-removed") == 0))
1314
- return Spawn::New(g_value_get_object(value), runtime);
1315
-
1316
- if (index == 0 && (strcmp(name, "child-added") == 0 ||
1317
- strcmp(name, "child-removed") == 0))
1318
- return Child::New(g_value_get_object(value), runtime);
1319
-
1320
- if (index == 0 && strcmp(name, "process-crashed") == 0)
1321
- return Crash::New(g_value_get_object(value), runtime);
1322
-
1323
- return Local<Value>();
1324
- }
1325
-
1326
- void Device::OnConnect(const gchar* name, gpointer user_data) {
1327
- auto runtime = static_cast<Runtime*>(user_data);
1328
-
1329
- if (ShouldStayAliveToEmit(name))
1330
- runtime->GetUVContext()->IncreaseUsage();
1331
- }
1332
-
1333
- void Device::OnDisconnect(const gchar* name, gpointer user_data) {
1334
- auto runtime = static_cast<Runtime*>(user_data);
1335
-
1336
- if (ShouldStayAliveToEmit(name))
1337
- runtime->GetUVContext()->DecreaseUsage();
1338
- }
1339
-
1340
- bool Device::ShouldStayAliveToEmit(const gchar* name) {
1341
- return strcmp(name, "spawn-added") == 0 ||
1342
- strcmp(name, "spawn-removed") == 0 ||
1343
- strcmp(name, "child-added") == 0 ||
1344
- strcmp(name, "child-removed") == 0 ||
1345
- strcmp(name, "process-crashed") == 0 ||
1346
- strcmp(name, "output") == 0 ||
1347
- strcmp(name, "uninjected") == 0;
1348
- }
1349
-
1350
- }