-
-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathportal-client.vala
More file actions
364 lines (290 loc) · 9.91 KB
/
portal-client.vala
File metadata and controls
364 lines (290 loc) · 9.91 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
namespace Frida {
public class PortalClient : Object, AgentSessionProvider {
public signal void resume ();
public signal void kill ();
public weak ProcessInvader invader {
get;
construct;
}
public SocketConnectable connectable {
get;
construct;
}
public TlsCertificate? certificate {
get;
construct;
}
public string? token {
get;
construct;
}
public string[]? acl {
get;
construct;
}
public HostApplicationInfo app_info {
get;
construct;
}
private DBusConnection? connection;
private SourceFunc? on_connection_event;
private TimeoutSource? reconnect_timer;
private Promise<bool> stopped = new Promise<bool> ();
private Gee.Collection<uint> registrations = new Gee.ArrayList<uint> ();
private PortalSession? portal_session;
private Gee.Map<AgentSessionId?, LiveAgentSession> agent_sessions =
new Gee.HashMap<AgentSessionId?, LiveAgentSession> (AgentSessionId.hash, AgentSessionId.equal);
private Gee.Collection<Gum.Script> eternalized_scripts = new Gee.ArrayList<Gum.Script> ();
private Cancellable io_cancellable = new Cancellable ();
public PortalClient (ProcessInvader invader, SocketConnectable connectable, TlsCertificate? certificate, string? token,
string[]? acl, HostApplicationInfo app_info) {
Object (
invader: invader,
connectable: connectable,
certificate: certificate,
token: token,
acl: acl,
app_info: app_info
);
}
public async void start (Cancellable? cancellable = null) throws Error, IOError {
var promise = new Promise<bool> ();
maintain_connection.begin (promise);
yield promise.future.wait_async (cancellable);
}
public async void stop (Cancellable? cancellable = null) throws IOError {
if (reconnect_timer != null) {
reconnect_timer.destroy ();
reconnect_timer = null;
}
io_cancellable.cancel ();
if (on_connection_event != null)
on_connection_event ();
try {
yield stopped.future.wait_async (cancellable);
yield teardown_connection (cancellable);
} catch (GLib.Error e) {
assert_not_reached ();
}
}
private async void maintain_connection (Promise<bool> start_request) {
bool waiting = false;
on_connection_event = () => {
if (waiting)
maintain_connection.callback ();
return false;
};
uint reconnect_delay = 0;
do {
try {
yield establish_connection ();
if (start_request != null) {
start_request.resolve (true);
start_request = null;
}
reconnect_delay = 0;
waiting = true;
yield;
waiting = false;
} catch (GLib.Error e) {
if (start_request != null) {
DBusError.strip_remote_error (e);
GLib.Error start_error = (e is Error || e is IOError.CANCELLED)
? e
: new Error.TRANSPORT ("%s", e.message);
start_request.reject (start_error);
start_request = null;
break;
}
}
if (io_cancellable.is_cancelled ())
break;
var source = new TimeoutSource (reconnect_delay + Random.int_range (0, 3000));
source.set_callback (() => {
maintain_connection.callback ();
return false;
});
source.attach (MainContext.get_thread_default ());
reconnect_timer = source;
waiting = true;
yield;
waiting = false;
reconnect_delay = (reconnect_delay != 0)
? uint.min (reconnect_delay * 2, 17000)
: 2000;
} while (!io_cancellable.is_cancelled ());
on_connection_event = null;
stopped.resolve (true);
}
private async void establish_connection () throws GLib.Error {
var client = new SocketClient ();
SocketConnection socket_connection = yield client.connect_async (connectable, io_cancellable);
var socket = socket_connection.socket;
if (socket.get_family () != UNIX)
Tcp.enable_nodelay (socket);
IOStream stream = socket_connection;
if (certificate != null) {
var tc = TlsClientConnection.new (stream, null);
tc.set_database (null);
var accept_handler = tc.accept_certificate.connect ((peer_cert, errors) => {
return peer_cert.verify (null, certificate) == 0;
});
try {
yield tc.handshake_async (Priority.DEFAULT, io_cancellable);
} finally {
tc.disconnect (accept_handler);
}
stream = tc;
}
var transport = (certificate != null) ? WebServiceTransport.TLS : WebServiceTransport.PLAIN;
string? origin = null;
stream = yield negotiate_connection (stream, transport, origin, io_cancellable);
connection = yield new DBusConnection (stream, null, DELAY_MESSAGE_PROCESSING, null, io_cancellable);
connection.on_closed.connect (on_connection_closed);
AgentSessionProvider provider = this;
registrations.add (connection.register_object (ObjectPath.AGENT_SESSION_PROVIDER, provider));
connection.start_message_processing ();
if (token != null) {
AuthenticationService auth_service = yield connection.get_proxy (null, ObjectPath.AUTHENTICATION_SERVICE,
DO_NOT_LOAD_PROPERTIES, io_cancellable);
yield auth_service.authenticate (token, io_cancellable);
}
portal_session = yield connection.get_proxy (null, ObjectPath.PORTAL_SESSION, DO_NOT_LOAD_PROPERTIES,
io_cancellable);
portal_session.resume.connect (on_resume);
portal_session.kill.connect (on_kill);
SpawnStartState current_state = invader.query_current_spawn_state ();
SpawnStartState next_state;
var interrupted_sessions = new AgentSessionId[0];
foreach (LiveAgentSession session in agent_sessions.values.to_array ()) {
AgentSessionId id = session.id;
assert (session.persist_timeout != 0);
interrupted_sessions += id;
try {
session.message_sink = yield connection.get_proxy (null, ObjectPath.for_agent_message_sink (id),
DO_NOT_LOAD_PROPERTIES, io_cancellable);
} catch (IOError e) {
throw_dbus_error (e);
}
assert (session.registration_id == 0);
try {
session.registration_id = connection.register_object (ObjectPath.for_agent_session (id),
(AgentSession) session);
} catch (IOError io_error) {
assert_not_reached ();
}
}
HashTable<string, Variant> options = make_parameters_dict ();
if (acl != null)
options["acl"] = new Variant.strv (acl);
yield portal_session.join (app_info, current_state, interrupted_sessions, options, io_cancellable, out next_state);
if (next_state == RUNNING)
resume ();
}
private void on_connection_closed (DBusConnection connection, bool remote_peer_vanished, GLib.Error? error) {
teardown_connection.begin (null);
}
private async void teardown_connection (Cancellable? cancellable) throws IOError {
if (connection == null)
return;
bool stopping = io_cancellable.is_cancelled ();
foreach (var session in agent_sessions.values.to_array ()) {
if (!stopping && session.persist_timeout != 0) {
unregister_session (session);
session.interrupt.begin (io_cancellable);
continue;
}
try {
yield session.close (cancellable);
} catch (GLib.Error e) {
assert (e is IOError.CANCELLED);
throw (IOError) e;
}
}
foreach (var id in registrations)
connection.unregister_object (id);
registrations.clear ();
connection = null;
if (on_connection_event != null)
on_connection_event ();
}
private async void open (AgentSessionId id, HashTable<string, Variant> options,
Cancellable? cancellable) throws Error, IOError {
var opts = SessionOptions._deserialize (options);
if (opts.realm == EMULATED)
throw new Error.NOT_SUPPORTED ("Emulated realm is not supported by frida-gadget");
AgentMessageSink sink;
try {
sink = yield connection.get_proxy (null, ObjectPath.for_agent_message_sink (id), DO_NOT_LOAD_PROPERTIES,
cancellable);
} catch (IOError e) {
throw_dbus_error (e);
}
MainContext dbus_context = yield get_dbus_context ();
LiveAgentSession? session = agent_sessions[id];
if (session != null)
throw new Error.INVALID_ARGUMENT ("Session already exists");
session = new LiveAgentSession (invader, id, opts.persist_timeout, sink, dbus_context);
agent_sessions[id] = session;
session.closed.connect (on_session_closed);
session.script_eternalized.connect (on_script_eternalized);
try {
session.registration_id = connection.register_object (ObjectPath.for_agent_session (id),
(AgentSession) session);
} catch (IOError io_error) {
assert_not_reached ();
}
opened (id);
}
#if !WINDOWS
private async void migrate (AgentSessionId id, Socket to_socket, Cancellable? cancellable) throws Error, IOError {
throw new Error.NOT_SUPPORTED ("Session migration is not supported with frida-portal");
}
#endif
private async void unload (Cancellable? cancellable) throws Error, IOError {
throw new Error.INVALID_OPERATION ("Unload is not allowed with frida-portal");
}
private void on_resume () {
resume ();
}
private void on_kill () {
kill ();
}
private void on_session_closed (BaseAgentSession base_session) {
LiveAgentSession session = (LiveAgentSession) base_session;
closed (session.id);
unregister_session (session);
session.script_eternalized.disconnect (on_script_eternalized);
session.closed.disconnect (on_session_closed);
agent_sessions.unset (session.id);
}
private void unregister_session (LiveAgentSession session) {
var id = session.registration_id;
if (id != 0) {
connection.unregister_object (id);
session.registration_id = 0;
}
}
private void on_script_eternalized (Gum.Script script) {
eternalized_scripts.add (script);
eternalized ();
}
private class LiveAgentSession : BaseAgentSession {
public uint registration_id {
get;
set;
}
public LiveAgentSession (ProcessInvader invader, AgentSessionId id, uint persist_timeout, AgentMessageSink sink,
MainContext dbus_context) {
Object (
invader: invader,
id: id,
persist_timeout: persist_timeout,
message_sink: sink,
frida_context: MainContext.ref_thread_default (),
dbus_context: dbus_context
);
}
}
}
}