You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.7 KiB

1 year ago
  1. using CircleSDK.Model;
  2. using Newtonsoft.Json;
  3. using System.Reflection;
  4. using System.Reflection.Metadata.Ecma335;
  5. using System.Reflection.PortableExecutable;
  6. namespace CDPShared
  7. {
  8. // All the code in this file is included in all platforms.
  9. public class CDPWorker
  10. {
  11. private CircleAPIHelper _apiHelper;
  12. private string _userName = "";
  13. private string _machine = "";
  14. Boolean _bReady = false;
  15. public Boolean Ready => _bReady;
  16. public string Status
  17. {
  18. get
  19. {
  20. return _apiHelper.Status;
  21. }
  22. }
  23. public CircleAPIHelper API => _apiHelper;
  24. public List<CircleInfo> Circles { get; set; }
  25. public string DefaultTopicId { get; set; } = "87654321-a314-4202-b959-c981a6bc3c24";
  26. public string DefaultCircleId { get; set; }
  27. public CDPWorker()
  28. {
  29. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  30. {
  31. Circles = new List<CircleInfo>();
  32. _apiHelper = new CircleAPIHelper(Constants.CustomerCode, Constants.AppKey, Constants.EndUserId, Constants.Secret);
  33. _userName = Environment.UserDomainName + "\\" + Environment.UserName;
  34. _machine = Environment.MachineName;
  35. Task.Run(async () => {await AsyncStartup(); });
  36. }
  37. }
  38. async Task AsyncStartup()
  39. {
  40. while (!_apiHelper.Ready)
  41. {
  42. Thread.Sleep(250);
  43. }
  44. Circles = await _apiHelper.EnumCircles();
  45. _bReady = true;
  46. }
  47. public async Task<DecryptReply> Decrypt(string file)
  48. {
  49. YellowFile yf = new YellowFile(file);
  50. DecryptRequest request = new DecryptRequest();
  51. request.CircleId = yf.CircleId;
  52. request.TopicId = yf.TopicId;
  53. request.ToDecrypt = Convert.ToBase64String(File.ReadAllBytes(file));
  54. DecryptReply result = null;
  55. result = await _apiHelper.Api.DecryptAsync(request, "Bearer " + _apiHelper.Token, "Dashboard");
  56. if (!result.Status.Result.GetValueOrDefault(false))
  57. return null;
  58. return result;
  59. //byte[] buf = Convert.FromBase64String(result.DecryptedData);
  60. //string ext = Path.GetExtension(result.FileName);
  61. //fileName = result.FileName;
  62. //return buf;
  63. }
  64. public async Task<CircleInfo> GetCircle(string circleId)
  65. {
  66. return await _apiHelper.GetCircleById(circleId);
  67. }
  68. public async Task<TopicInfo> GetTopic(string circleId, string topicId)
  69. {
  70. return await _apiHelper.GetTopicById(circleId, topicId);
  71. }
  72. public async Task TrackAuthorizedAccess(YellowFile yf, string fileName, Dictionary<string, string> metaData)
  73. {
  74. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  75. {
  76. try
  77. {
  78. DataGather g = new DataGather(_apiHelper);
  79. foreach (KeyValuePair<string, string> keyValuePair in metaData)
  80. {
  81. g.AddHead(keyValuePair.Key, keyValuePair.Value);
  82. }
  83. g.AddTail("File Id", yf.FileId);
  84. WhoAmIReply wr = await _apiHelper.WhoAmI(yf.CircleId);
  85. List<ProfileInfo> members = await _apiHelper.EnumCircleMembers(yf.CircleId);
  86. string senderName = yf.OwnerId;
  87. ProfileInfo sender = members.FirstOrDefault(dr => dr.ProfileId == yf.OwnerId);
  88. if (sender != null)
  89. senderName = sender.DisplayName;
  90. g.AddHead("Sender", senderName);
  91. g.AddTail("Sender Id", yf.OwnerId);
  92. string viewerName = "";
  93. ProfileInfo viewer = members.FirstOrDefault(dr => dr.ProfileId == wr.MemberId);
  94. if (viewer != null)
  95. viewerName = viewer.DisplayName;
  96. g.AddHead("Viewer", viewerName);
  97. g.AddTail("Viewer Id", wr.MemberId);
  98. yf.UpdateOwner(wr.MemberId);
  99. await AddMessage($"{fileName} decrypted by {_userName}", (Int32)CircleViewMessages.FileViewed, g);
  100. }
  101. catch (Exception e)
  102. {
  103. MinLogging.LogIt(e.Message);
  104. }
  105. }
  106. }
  107. public async Task TrackUnauthorizedAccess(YellowFile yf, string sourcePath)
  108. {
  109. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  110. {
  111. try
  112. {
  113. DataGather g = new DataGather(_apiHelper);
  114. g.AddHead($"Full path", sourcePath);
  115. g.AddTail("File Id", yf.FileId);
  116. string senderName = yf.OwnerId;
  117. g.AddTail("Sender Id", yf.OwnerId);
  118. string message = $"{_userName} was blocked attempting to view a protected file"; // we can't use the filename since it's in the encrypted header
  119. await PostCircleViewEvent(yf.CircleId, yf.FileId, (Int32)CircleViewMessages.BlockedViewAttempt, message, JsonConvert.SerializeObject(g.Gather()));
  120. }
  121. catch (Exception e)
  122. {
  123. MinLogging.LogIt(e.Message);
  124. }
  125. }
  126. }
  127. async Task PostCircleViewEvent(string circleId, string fileId, Int32 msgType, string message, string meta)
  128. {
  129. using (LogMethod.Log(MethodBase.GetCurrentMethod().ReflectedType.Name))
  130. {
  131. var url = Constants.CircleViewEventUrl + "?circleId=" + circleId;
  132. var data = new
  133. {
  134. FileId = fileId,
  135. MsgId = Guid.NewGuid().ToString(),
  136. Event = DateTime.UtcNow,
  137. MsgType = msgType,
  138. Message = message,
  139. Meta = meta
  140. };
  141. try
  142. {
  143. HttpClient client = new HttpClient();
  144. var json = JsonConvert.SerializeObject(data);
  145. var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
  146. MinLogging.LogIt(url);
  147. MinLogging.LogIt(json);
  148. var response = await client.PostAsync(url, content);
  149. response.EnsureSuccessStatusCode();
  150. string result = await response.Content.ReadAsStringAsync();
  151. MinLogging.LogIt(result);
  152. }
  153. catch (Exception e)
  154. {
  155. MinLogging.LogIt(" **** " + e.Message);
  156. }
  157. }
  158. }
  159. public async Task<List<TopicInfo>> EnumTopics(string circleId)
  160. {
  161. return await _apiHelper.EnumTopics(circleId);
  162. }
  163. public async Task<byte[]> EncryptFile(string circleId, string topicId, string fileName)
  164. {
  165. return await _apiHelper.EncryptFile(circleId, topicId, fileName);
  166. }
  167. /// <summary>
  168. /// this adds a message to the default circle and topic
  169. /// </summary>
  170. public async Task<AddMessageReply> AddMessage(string message, Int32 msgId, DataGather g = null)
  171. {
  172. return await API.AddMessage(DefaultCircleId, DefaultTopicId, message, msgId, g);
  173. }
  174. public async Task<GetMessagesReply> GetCircleViewwMessages()
  175. {
  176. return await API.GetCircleViewwMessages(DefaultCircleId, DefaultTopicId);
  177. }
  178. }
  179. }