|
5 | 5 | using System;
|
6 | 6 | using System.Text;
|
7 | 7 | using System.Net;
|
8 |
| -#if !LOOTLOCKER_USE_ZERODEPJSON && !LOOTLOCKER_USE_NEWTONSOFTJSON |
9 |
| -using LLlibs.Newtonsoft.Json; |
10 |
| -using LLlibs.Newtonsoft.Json.Linq; |
| 8 | +#if LOOTLOCKER_USE_ZERODEPJSON |
| 9 | +using LootLocker.ZeroDepJson; |
11 | 10 | #elif LOOTLOCKER_USE_NEWTONSOFTJSON
|
12 | 11 | using Newtonsoft.Json;
|
13 | 12 | using Newtonsoft.Json.Linq;
|
| 13 | +#else |
| 14 | +using LLlibs.Newtonsoft.Json; |
| 15 | +using LLlibs.Newtonsoft.Json.Linq; |
14 | 16 | #endif
|
15 | 17 | using LootLocker.Requests;
|
16 | 18 |
|
@@ -172,12 +174,12 @@ IEnumerator coroutine()
|
172 | 174 | else
|
173 | 175 | {
|
174 | 176 | tries = 0;
|
175 |
| - response.Error += " " + webRequest.downloadHandler.text; |
| 177 | + response.Error += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text); |
176 | 178 | response.statusCode = (int)webRequest.responseCode;
|
177 | 179 | response.success = false;
|
178 | 180 | response.hasError = true;
|
179 | 181 | response.text = webRequest.downloadHandler.text;
|
180 |
| - LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(ObfuscateJsonStringForLogging(response.Error)); |
| 182 | + LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(response.Error); |
181 | 183 | OnServerResponse?.Invoke(response);
|
182 | 184 | }
|
183 | 185 |
|
@@ -372,8 +374,72 @@ public ObfuscationDetails(string key, char replacementChar = '*', int visibleCha
|
372 | 374 | private static string ObfuscateJsonStringForLogging(string json)
|
373 | 375 | {
|
374 | 376 | #if LOOTLOCKER_USE_ZERODEPJSON
|
375 |
| - //TODO: Fix this json usage |
376 |
| - return json; |
| 377 | + if (string.IsNullOrEmpty(json) || json.Equals("{}")) |
| 378 | + { |
| 379 | + return json; |
| 380 | + } |
| 381 | + |
| 382 | + Dictionary<string, object> jsonObject = null; |
| 383 | + try |
| 384 | + { |
| 385 | + jsonObject = Json.Deserialize(json) as Dictionary<string, object>; |
| 386 | + } |
| 387 | + catch (JsonException) |
| 388 | + { |
| 389 | + return json; |
| 390 | + } |
| 391 | + |
| 392 | + if (jsonObject != null && jsonObject.Count > 0) |
| 393 | + { |
| 394 | + foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate) |
| 395 | + { |
| 396 | + string valueToObfuscate; |
| 397 | + try |
| 398 | + { |
| 399 | + if (!jsonObject.ContainsKey(obfuscationInfo.key)) |
| 400 | + { |
| 401 | + continue; |
| 402 | + } |
| 403 | + |
| 404 | + valueToObfuscate = Json.Serialize(jsonObject[obfuscationInfo.key]); |
| 405 | + } |
| 406 | + catch (KeyNotFoundException) |
| 407 | + { |
| 408 | + continue; |
| 409 | + } |
| 410 | + |
| 411 | + if (string.IsNullOrEmpty(valueToObfuscate)) |
| 412 | + continue; |
| 413 | + |
| 414 | + if (valueToObfuscate.Equals("null", StringComparison.Ordinal)) |
| 415 | + continue; |
| 416 | + |
| 417 | + int replaceFrom = 0; |
| 418 | + int replaceTo = valueToObfuscate.Length; |
| 419 | + |
| 420 | + // Deal with short strings |
| 421 | + if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd) |
| 422 | + { |
| 423 | + if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything |
| 424 | + continue; |
| 425 | + } |
| 426 | + // Replace in |
| 427 | + else |
| 428 | + { |
| 429 | + replaceFrom += obfuscationInfo.visibleCharsFromBeginning; |
| 430 | + replaceTo -= obfuscationInfo.visibleCharsFromEnd; |
| 431 | + } |
| 432 | + |
| 433 | + StringBuilder replacement = new StringBuilder(); |
| 434 | + replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom); |
| 435 | + StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate); |
| 436 | + obfuscatedValue.Remove(replaceFrom, replacement.Length); |
| 437 | + obfuscatedValue.Insert(replaceFrom, replacement.ToString()); |
| 438 | + jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString(); |
| 439 | + } |
| 440 | + } |
| 441 | + |
| 442 | + return LootLockerJson.SerializeObject(jsonObject); |
377 | 443 | #else
|
378 | 444 | if (string.IsNullOrEmpty(json))
|
379 | 445 | {
|
|
0 commit comments