-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLinkLanControl.cs
392 lines (327 loc) · 14.3 KB
/
LinkLanControl.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
namespace EWeLink.Api
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EWeLink.Api.Models;
using EWeLink.Api.Models.Devices;
using EWeLink.Api.Models.EventParameters;
using EWeLink.Api.Models.Parameters;
using Makaretu.Dns;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
public interface ILinkLanControl
{
event Action<ILinkEvent<IEventParameters>>? ParametersUpdated;
event Action<IDevice>? DiscoveredDevice;
Task<bool?> SendSwitchRequest(IDevice device, Parameters data);
void Start();
void Stop();
}
public class LinkLanControl : ILinkLanControl, IDisposable
{
private static readonly Random Rand = new ();
private readonly IDeviceCache deviceCache;
private readonly ILogger<LinkLanControl> logger;
private readonly IHttpClientFactory httpClientFactory;
private readonly MulticastService multicastService = new ();
private readonly ConcurrentDictionary<string, long> deviceLastSeq = new ();
private readonly SemaphoreSlim answerLock = new (1, 1);
private readonly JsonSerializerSettings serializerSettings = new ()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter>()
{
new StringEnumConverter(),
},
NullValueHandling = NullValueHandling.Ignore,
};
private bool started;
public LinkLanControl(IDeviceCache deviceCache, ILogger<LinkLanControl> logger, IHttpClientFactory httpClientFactory)
{
this.deviceCache = deviceCache;
this.logger = logger;
this.httpClientFactory = httpClientFactory;
multicastService.NetworkInterfaceDiscovered += (_, _) => this.multicastService.SendQuery("_ewelink._tcp");
multicastService.AnswerReceived += OnAnswerReceived;
}
public event Action<ILinkEvent<IEventParameters>>? ParametersUpdated;
public event Action<IDevice>? DiscoveredDevice;
private string Sequence => DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
public void Start()
{
if (this.started)
{
return;
}
this.multicastService.Start();
this.started = true;
}
public void Stop()
{
if (this.started)
{
this.multicastService.Stop();
}
}
public void Dispose()
{
multicastService.Dispose();
answerLock.Dispose();
}
public async Task<bool?> SendSwitchRequest(IDevice device, Parameters data)
{
var lanInformation = device.LanControl;
if (lanInformation == null)
{
return null;
}
if (device.DeviceKey is null)
{
throw new ArgumentNullException(nameof(device.DeviceKey));
}
object jsonData = data;
var json = JsonConvert.SerializeObject(jsonData, serializerSettings);
var ePayload = lanInformation.EncryptionEnabled switch
{
true => Encrypt(device.DeviceKey, json),
_ => new PayLoad(false, null, json)
};
var payload =
new
{
sequence = Sequence,
deviceid = device.DeviceId,
selfApikey = device.ApiKey,
ePayload.iv,
ePayload.encrypt,
ePayload.data,
};
var uri = FormatSwitchUrl(lanInformation.IpAddress, lanInformation.Port, data is MultiSwitchParameters);
var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri)
{
Headers =
{
Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
CacheControl = new () { NoStore = true },
Connection = { "Keep-Alive" },
},
Content = new StringContent(JsonConvert.SerializeObject(payload, serializerSettings), Encoding.UTF8, "application/json"),
};
try
{
var httpClient = this.httpClientFactory.CreateClient("eWeLink-lan");
var response = await httpClient.SendAsync(requestMessage);
if (response.IsSuccessStatusCode)
{
var jsonRaw = await response.Content.ReadAsStringAsync();
var switchResponse = JsonConvert.DeserializeObject<SwitchResponse>(jsonRaw);
var errorCode = switchResponse?.error ?? 0;
if (errorCode == 0)
{
return true;
}
else
{
var message = $"Switch request failed with error code: {errorCode}";
this.logger.LogInformation(message);
throw new LanControlRequestException(message, errorCode);
}
}
}
catch (HttpRequestException e)
{
this.logger.LogWarning("Switch request failed", e);
}
return false;
}
private string FormatInfoUrl(IPAddress ipAddress, int port) => FormatInfoUrl(ipAddress.ToString(), port);
private string FormatInfoUrl(string ipAddress, int port) => FormatBaseUrl(ipAddress, port) + "info";
private string FormatSwitchUrl(IPAddress ipAddress, int port, bool multiple) => FormatSwitchUrl(ipAddress.ToString(), port, multiple);
private string FormatSwitchUrl(string ipAddress, int port, bool multiple) => FormatBaseUrl(ipAddress, port) + "switch" + (multiple ? "es" : string.Empty);
private string FormatBaseUrl(string ipAddress, int port) => $"http://{ipAddress}:{port}/zeroconf/";
private void OnAnswerReceived(object sender, MessageEventArgs e)
{
this.answerLock.Wait();
try
{
var serviceRecord = e.Message.Answers.FirstOrDefault(a => a.Type == DnsType.SRV) as SRVRecord;
var txtRecord = e.Message.Answers.FirstOrDefault(a => a.Type == DnsType.TXT) as TXTRecord;
var aRecord = e.Message.Answers.FirstOrDefault(a => a.Type == DnsType.A) as ARecord;
if (serviceRecord == null || txtRecord == null || aRecord == null)
{
return;
}
var port = serviceRecord.Port;
var ipAddress = aRecord.Address;
var information = txtRecord.Strings.Where(x => !string.IsNullOrWhiteSpace(x) && x.Contains('=')).Select(st => st.Split('=')).ToDictionary(st => st[0], st => st[1]);
if (information.Count == 0)
{
return;
}
if (!information.TryGetValue("id", out var id) || !deviceCache.TryGetDevice(id, out var device))
{
return;
}
bool encrypted = false;
if (information.TryGetValue("encrypt", out var encryptValue))
{
encrypted = encryptValue == "true";
}
if (!device!.HasLanControl)
{
device.LanControl = new LanControlInformation(ipAddress, port, encrypted);
DiscoveredDevice?.Invoke(device);
}
long seq = 0;
if (information.TryGetValue("seq", out var seqValue))
{
seq = long.Parse(seqValue);
}
var eventTime = new DateTimeOffset(DateTime.SpecifyKind(txtRecord.CreationTime, DateTimeKind.Local)); // this is in local time
var hasPreviousSeq = this.deviceLastSeq.TryGetValue(id, out var previousSeq);
if (previousSeq >= seq)
{
return;
}
this.deviceLastSeq.AddOrUpdate(id, (_) => seq, (_, _) => seq);
var json = GetData(information);
if (encrypted)
{
information.TryGetValue("iv", out var iv);
var deviceKey = device.DeviceKey;
if (deviceKey is null)
{
return;
}
json = Decrypt(deviceKey, iv, json);
}
// as this point we should have enough information to build an event parameter and raise it.
var parametersJsonObject = JObject.Parse(json);
var deviceUiid = device.Uiid;
Type deviceType = this.deviceCache.GetEventParameterTypeForUiid(deviceUiid) ?? typeof(EventParameters);
if (device is IDevice<Parameters> typedDevice)
{
var updateResult = typedDevice.Parameters.Update(json);
if (typeof(IMultiSwitchEventParameters).IsAssignableFrom(deviceType) && updateResult.HasValue)
{
parametersJsonObject.Add("triggeredOutlet", updateResult.Value);
}
}
if (hasPreviousSeq && this.ParametersUpdated != null)
{
if (typeof(EventParameters).IsAssignableFrom(deviceType))
{
if (!parametersJsonObject.ContainsKey("trigTime"))
{
parametersJsonObject.Add("trigTime", new JValue(eventTime.ToUnixTimeMilliseconds()));
}
}
var jsonObject = JObject.FromObject(new
{
action = "update",
deviceid = device.DeviceId,
uiid = device.Uiid,
eventSource = EventSource.Lan,
});
jsonObject.Add("params", parametersJsonObject);
var eventType = typeof(LinkEvent<>).MakeGenericType(deviceType);
if (jsonObject.ToObject(eventType) is ILinkEvent<IEventParameters> linkEvent)
{
this.ParametersUpdated(linkEvent);
}
}
}
finally
{
this.answerLock.Release();
}
}
private string GetData(Dictionary<string, string> information)
{
StringBuilder sb = new ();
for (var i = 1; i <= 4; i++)
{
var dataKey = $"data{i}";
if (!information.TryGetValue(dataKey, out var data))
{
break;
}
sb.Append(data);
}
return sb.ToString();
}
private string Decrypt(string deviceKey, string iv, string encoded)
{
var eDeviceKey = Encoding.UTF8.GetBytes(deviceKey);
using var md5 = MD5.Create();
var key = md5.ComputeHash(eDeviceKey);
using var aes = Aes.Create();
aes.Mode = CipherMode.CBC;
aes.BlockSize = key.Length * 8;
aes.KeySize = 128;
aes.Padding = PaddingMode.None;
byte[] Base64Decode(string input)
{
try
{
return Convert.FromBase64String(input);
}
catch (FormatException)
{
}
return Base64Decode(input + '=');
}
try
{
var ivBase64Decoded = Base64Decode(iv);
using var decryptor = aes.CreateDecryptor(key, ivBase64Decoded);
var ciphertext = Base64Decode(encoded);
var data = PerformCryptography(decryptor, ciphertext);
var paddingLength = data.Last();
return Encoding.UTF8.GetString(data, 0, data.Length - paddingLength);
}
catch (Exception e)
{
return string.Empty;
}
}
private PayLoad Encrypt(string deviceKey, string data)
{
var eDeviceKey = Encoding.UTF8.GetBytes(deviceKey);
using var md5 = MD5.Create();
var key = md5.ComputeHash(eDeviceKey);
byte[] iv = new byte[16];
Rand.NextBytes(iv);
var ivBase64Encoded = Convert.ToBase64String(iv);
var plaintext = Encoding.UTF8.GetBytes(data);
using var aes = Aes.Create();
using var encryptor = aes.CreateEncryptor(key, iv);
var encryptedData = PerformCryptography(encryptor, plaintext);
var dataEncoded = Convert.ToBase64String(encryptedData);
return new PayLoad(true, ivBase64Encoded, dataEncoded);
}
private byte[] PerformCryptography(ICryptoTransform cryptoTransform, byte[] data)
{
using var memoryStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();
}
private record PayLoad(bool encrypt, string? iv, string data);
private record SwitchResponse(long seq, int error, dynamic data);
}
}