|
6 | 6 | using System.Text;
|
7 | 7 | using LootLocker.LootLockerEnums;
|
8 | 8 | using UnityEditor;
|
9 |
| -#if LOOTLOCKER_USE_NEWTONSOFTJSON |
10 |
| -using Newtonsoft.Json; |
11 |
| -using Newtonsoft.Json.Linq; |
12 |
| -#else |
13 |
| -using LLlibs.ZeroDepJson; |
14 |
| -#endif |
15 | 9 | using LootLocker.Requests;
|
16 | 10 |
|
17 | 11 | namespace LootLocker.LootLockerEnums
|
@@ -110,13 +104,13 @@ IEnumerator coroutine()
|
110 | 104 |
|
111 | 105 | try
|
112 | 106 | {
|
113 |
| - LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("Server Response: " + webRequest.responseCode + " " + request.endpoint + " completed in " + (Time.time - startTime).ToString("n4") + " secs.\nResponse: " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text)); |
| 107 | + LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("Server Response: " + webRequest.responseCode + " " + request.endpoint + " completed in " + (Time.time - startTime).ToString("n4") + " secs.\nResponse: " + LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text)); |
114 | 108 | }
|
115 | 109 | catch
|
116 | 110 | {
|
117 | 111 | LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(request.httpMethod.ToString());
|
118 | 112 | LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(request.endpoint);
|
119 |
| - LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(ObfuscateJsonStringForLogging(webRequest.downloadHandler.text)); |
| 113 | + LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text)); |
120 | 114 | }
|
121 | 115 |
|
122 | 116 | if (WebRequestSucceeded(webRequest))
|
@@ -198,7 +192,7 @@ IEnumerator coroutine()
|
198 | 192 | errorData.message = "Unknown error.";
|
199 | 193 | break;
|
200 | 194 | }
|
201 |
| - errorData.message += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text); |
| 195 | + errorData.message += " " + LootLockerObfuscator.ObfuscateJsonStringForLogging(webRequest.downloadHandler.text); |
202 | 196 | }
|
203 | 197 | response.errorData = errorData;
|
204 | 198 | LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(response.errorData.message + (!string.IsNullOrEmpty(response.errorData.doc_url) ? " -- " + response.errorData.doc_url : ""));
|
@@ -410,7 +404,7 @@ private UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest req
|
410 | 404 | {
|
411 | 405 | string json = (request.payload != null && request.payload.Count > 0) ? LootLockerJson.SerializeObject(request.payload) : request.jsonPayload;
|
412 | 406 | #if UNITY_EDITOR
|
413 |
| - LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("REQUEST BODY = " + ObfuscateJsonStringForLogging(json)); |
| 407 | + LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Verbose)("REQUEST BODY = " + LootLockerObfuscator.ObfuscateJsonStringForLogging(json)); |
414 | 408 | #endif
|
415 | 409 | byte[] bytes = System.Text.Encoding.UTF8.GetBytes(string.IsNullOrEmpty(json) ? "{}" : json);
|
416 | 410 | webRequest = UnityWebRequest.Put(url, bytes);
|
@@ -461,173 +455,6 @@ private UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest req
|
461 | 455 | return webRequest;
|
462 | 456 | }
|
463 | 457 |
|
464 |
| - private struct ObfuscationDetails |
465 |
| - { |
466 |
| - public string key { get; set; } |
467 |
| - public char replacementChar { get; set; } |
468 |
| - public int visibleCharsFromBeginning { get; set; } |
469 |
| - public int visibleCharsFromEnd { get; set; } |
470 |
| - public bool hideCharactersForShortStrings { get; set; } |
471 |
| - |
472 |
| - public ObfuscationDetails(string key, char replacementChar = '*', int visibleCharsFromBeginning = 3, int visibleCharsFromEnd = 3, bool hideCharactersForShortStrings = true) |
473 |
| - { |
474 |
| - this.key = key; |
475 |
| - this.replacementChar = replacementChar; |
476 |
| - this.visibleCharsFromBeginning = visibleCharsFromBeginning; |
477 |
| - this.visibleCharsFromEnd = visibleCharsFromEnd; |
478 |
| - this.hideCharactersForShortStrings = hideCharactersForShortStrings; |
479 |
| - } |
480 |
| - } |
481 |
| - |
482 |
| - static readonly List<ObfuscationDetails> FieldsToObfuscate = new List<ObfuscationDetails> |
483 |
| - { |
484 |
| - new ObfuscationDetails("game_key", '*', 4, 3, false), |
485 |
| - new ObfuscationDetails("email"), |
486 |
| - new ObfuscationDetails("password", '*', 0, 0), |
487 |
| - new ObfuscationDetails("domain_key"), |
488 |
| - new ObfuscationDetails("session_token"), |
489 |
| - new ObfuscationDetails("token") |
490 |
| - }; |
491 |
| - |
492 |
| - private static string ObfuscateJsonStringForLogging(string json) |
493 |
| - { |
494 |
| -#if LOOTLOCKER_USE_NEWTONSOFTJSON |
495 |
| - if (string.IsNullOrEmpty(json)) |
496 |
| - { |
497 |
| - return json; |
498 |
| - } |
499 |
| - |
500 |
| - JObject jsonObject; |
501 |
| - try |
502 |
| - { |
503 |
| - jsonObject = JObject.Parse(json); |
504 |
| - } |
505 |
| - catch (JsonReaderException) |
506 |
| - { |
507 |
| - return json; |
508 |
| - } |
509 |
| - ; |
510 |
| - if (jsonObject.HasValues) |
511 |
| - { |
512 |
| - foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate) |
513 |
| - { |
514 |
| - string valueToObfuscate; |
515 |
| - try |
516 |
| - { |
517 |
| - JToken jsonValue; |
518 |
| - jsonObject.TryGetValue(obfuscationInfo.key, StringComparison.Ordinal, out jsonValue); |
519 |
| - if (jsonValue == null || (jsonValue.Type != JTokenType.String && jsonValue.Type != JTokenType.Integer)) |
520 |
| - continue; |
521 |
| - valueToObfuscate = jsonValue.ToString(); |
522 |
| - } |
523 |
| - catch (KeyNotFoundException) |
524 |
| - { |
525 |
| - continue; |
526 |
| - } |
527 |
| - |
528 |
| - if (string.IsNullOrEmpty(valueToObfuscate)) |
529 |
| - continue; |
530 |
| - |
531 |
| - if (valueToObfuscate.Equals("null", StringComparison.Ordinal)) |
532 |
| - continue; |
533 |
| - |
534 |
| - int replaceFrom = 0; |
535 |
| - int replaceTo = valueToObfuscate.Length; |
536 |
| - |
537 |
| - // Deal with short strings |
538 |
| - if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd) |
539 |
| - { |
540 |
| - if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything |
541 |
| - continue; |
542 |
| - } |
543 |
| - // Replace in |
544 |
| - else |
545 |
| - { |
546 |
| - replaceFrom += obfuscationInfo.visibleCharsFromBeginning; |
547 |
| - replaceTo -= obfuscationInfo.visibleCharsFromEnd; |
548 |
| - } |
549 |
| - |
550 |
| - StringBuilder replacement = new StringBuilder(); |
551 |
| - replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom); |
552 |
| - StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate); |
553 |
| - obfuscatedValue.Remove(replaceFrom, replacement.Length); |
554 |
| - obfuscatedValue.Insert(replaceFrom, replacement.ToString()); |
555 |
| - jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString(); |
556 |
| - } |
557 |
| - } |
558 |
| - |
559 |
| - return LootLockerJson.SerializeObject(jsonObject); |
560 |
| - |
561 |
| -#else //LOOTLOCKER_USE_NEWTONSOFTJSON |
562 |
| - if (string.IsNullOrEmpty(json) || json.Equals("{}")) |
563 |
| - { |
564 |
| - return json; |
565 |
| - } |
566 |
| - |
567 |
| - Dictionary<string, object> jsonObject = null; |
568 |
| - try |
569 |
| - { |
570 |
| - jsonObject = Json.Deserialize(json) as Dictionary<string, object>; |
571 |
| - } |
572 |
| - catch (JsonException) |
573 |
| - { |
574 |
| - return json; |
575 |
| - } |
576 |
| - |
577 |
| - if (jsonObject != null && jsonObject.Count > 0) |
578 |
| - { |
579 |
| - foreach (ObfuscationDetails obfuscationInfo in FieldsToObfuscate) |
580 |
| - { |
581 |
| - string valueToObfuscate; |
582 |
| - try |
583 |
| - { |
584 |
| - if (!jsonObject.ContainsKey(obfuscationInfo.key)) |
585 |
| - { |
586 |
| - continue; |
587 |
| - } |
588 |
| - |
589 |
| - valueToObfuscate = Json.Serialize(jsonObject[obfuscationInfo.key]); |
590 |
| - } |
591 |
| - catch (KeyNotFoundException) |
592 |
| - { |
593 |
| - continue; |
594 |
| - } |
595 |
| - |
596 |
| - if (string.IsNullOrEmpty(valueToObfuscate)) |
597 |
| - continue; |
598 |
| - |
599 |
| - if (valueToObfuscate.Equals("null", StringComparison.Ordinal)) |
600 |
| - continue; |
601 |
| - |
602 |
| - int replaceFrom = 0; |
603 |
| - int replaceTo = valueToObfuscate.Length; |
604 |
| - |
605 |
| - // Deal with short strings |
606 |
| - if (valueToObfuscate.Length <= obfuscationInfo.visibleCharsFromBeginning + obfuscationInfo.visibleCharsFromEnd) |
607 |
| - { |
608 |
| - if (!obfuscationInfo.hideCharactersForShortStrings) // Hide nothing, else hide everything |
609 |
| - continue; |
610 |
| - } |
611 |
| - // Replace in |
612 |
| - else |
613 |
| - { |
614 |
| - replaceFrom += obfuscationInfo.visibleCharsFromBeginning; |
615 |
| - replaceTo -= obfuscationInfo.visibleCharsFromEnd; |
616 |
| - } |
617 |
| - |
618 |
| - StringBuilder replacement = new StringBuilder(); |
619 |
| - replacement.Append(obfuscationInfo.replacementChar, replaceTo - replaceFrom); |
620 |
| - StringBuilder obfuscatedValue = new StringBuilder(valueToObfuscate); |
621 |
| - obfuscatedValue.Remove(replaceFrom, replacement.Length); |
622 |
| - obfuscatedValue.Insert(replaceFrom, replacement.ToString()); |
623 |
| - jsonObject[obfuscationInfo.key] = obfuscatedValue.ToString(); |
624 |
| - } |
625 |
| - } |
626 |
| - |
627 |
| - return LootLockerJson.SerializeObject(jsonObject); |
628 |
| -#endif //LOOTLOCKER_USE_NEWTONSOFTJSON |
629 |
| - } |
630 |
| - |
631 | 458 | private string BuildUrl(string endpoint, Dictionary<string, string> queryParams = null, LootLockerCallerRole callerRole = LootLockerCallerRole.User)
|
632 | 459 | {
|
633 | 460 | string ep = endpoint.StartsWith("/") ? endpoint.Trim() : "/" + endpoint.Trim();
|
|
0 commit comments