forked from molenzwiebel/Mimic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobileConnectionHandler.cs
191 lines (161 loc) · 6.99 KB
/
MobileConnectionHandler.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Threading;
using WebSocketSharp;
namespace Conduit
{
/**
* This is the class that handles a direct talk with a mobile client. All messages are tunneled through
* Rift, but that is transparent to this class. For all purposes, this class acts as if there is a direct
* encrypted connection with the client (that somehow already knows our public key).
*/
class MobileConnectionHandler
{
public delegate void SendMessageDelegate(string s);
private LeagueConnection league;
private byte[] key;
private Dictionary<string, Regex> observedPaths = new Dictionary<string, Regex>();
private SendMessageDelegate Send;
private SendMessageDelegate SendRaw;
public MobileConnectionHandler(LeagueConnection league, SendMessageDelegate send)
{
this.league = league;
this.league.OnWebsocketEvent += HandleLeagueEvent;
this.SendRaw = send;
this.Send = msg => SendRaw("\"" + CryptoHelpers.EncryptAES(key, msg) + "\"");
}
/**
* Called by the HubConnectionHandler when the connection with this mobile peer closes.
*/
public void OnClose()
{
this.league.OnWebsocketEvent -= HandleLeagueEvent;
}
/**
* Handles a message incoming through rift. First checks its opcode, then possibly decrypts the contents.
*/
public void HandleMessage(dynamic msg)
{
if (key != null)
{
try
{
var contents = CryptoHelpers.DecryptAES(key, (string) msg);
this.HandleMimicMessage(SimpleJson.DeserializeObject(contents));
}
catch
{
// Ignore message.
return;
}
}
if (!(msg is JsonArray)) return;
if (msg[0] == (long) MobileOpcode.Secret)
{
var info = CryptoHelpers.DecryptRSA((string) msg[1]);
if (info == null)
{
this.SendRaw("[" + MobileOpcode.SecretResponse + ",false]");
return;
}
dynamic contents = SimpleJson.DeserializeObject(info);
if (contents["secret"] == null || contents["identity"] == null || contents["device"] == null || contents["browser"] == null) return;
// If this device is already approved, immediately respond.
if (Persistence.IsDeviceApproved(contents["identity"]))
{
this.key = Convert.FromBase64String((string)contents["secret"]);
this.SendRaw("[" + (long) MobileOpcode.SecretResponse + ",true]");
}
else
{
// Note: UI prompt needs to happen on an STA thread.
var promptThread = new Thread(() =>
{
// Else, prompt the user.
var window = new DeviceConnectionPrompt((string)contents["device"], (string)contents["browser"], result =>
{
// If approved, save the device identity and set the key.
if (result)
{
this.key = Convert.FromBase64String((string)contents["secret"]);
Persistence.ApproveDevice(contents["identity"]);
}
// Send the result of the prompt to the device.
this.SendRaw("[" + (long)MobileOpcode.SecretResponse + "," + result.ToString().ToLower() + "]");
});
window.Show();
window.Focus();
Dispatcher.Run();
});
promptThread.SetApartmentState(ApartmentState.STA);
promptThread.Start();
}
}
}
/**
* Handles a message post-decryption, which is the raw message sent by the mobile client.
*/
private async void HandleMimicMessage(dynamic msg)
{
if (!(msg is JsonArray)) return;
if (msg[0] == (long) MobileOpcode.Subscribe)
{
var path = (string) msg[1];
if (!observedPaths.ContainsKey(path)) observedPaths.Add(path, new Regex(path));
}
else if (msg[0] == (long) MobileOpcode.Unsubscribe)
{
var path = (string) msg[1];
if (observedPaths.ContainsKey(path)) observedPaths.Remove(path);
}
else if (msg[0] == (long) MobileOpcode.Request)
{
var id = (long) msg[1];
var path = (string) msg[2];
var method = (string) msg[3];
var body = (string) msg[4];
var result = await league.Request(method, path, body);
var contents = await result.Content.ReadAsStringAsync();
if (contents.IsNullOrEmpty()) contents = "null";
Send("[" + (long) MobileOpcode.Response + "," + id + "," + (long) result.StatusCode + "," + contents + "]");
}
else if (msg[0] == (long) MobileOpcode.Version)
{
Send("[" + (long) MobileOpcode.VersionResponse + ", \"" + Program.VERSION + "\", \"" + Environment.MachineName + "\"]");
}
}
/**
* Handles an update coming from the League socket.
*/
private void HandleLeagueEvent(OnWebsocketEventArgs ev)
{
if (!observedPaths.Values.Any(x => x.IsMatch(ev.Path))) return;
var status = ev.Type.Equals("Create") || ev.Type.Equals("Update") ? 200 : 404;
Send("[" + (long) MobileOpcode.Update + ",\"" + ev.Path + "\"," + status + "," + (ev.Data != null ? ev.Data.ToString() : "null") + "]");
}
}
enum MobileOpcode : long
{
// Mobile -> Conduit, sends encrypted shared secret.
Secret = 1,
// Conduit <- Mobile, sends result of secret negotiation. If true, rest of communications is encrypted.
SecretResponse = 2,
// Mobile -> Conduit, request version
Version = 3,
// Conduit <- Mobile, send version
VersionResponse = 4,
// Mobile -> Conduit, subscribe to LCU updates that match regex
Subscribe = 5,
// Mobile -> Conduit, unsibscribe to LCU updates that match regex
Unsubscribe = 6,
// Mobile -> Conduit, make LCU request
Request = 7,
// Conduit -> Mobile, response of a previous request message.
Response = 8,
// Conduit -> Mobile, when any subscribed endpoint gets an update
Update = 9
}
}