WXUserReader.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using SQLite;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Interop;
  11. using System.Xml.Linq;
  12. using WechatPCMsgBakTool.Helpers;
  13. using WechatPCMsgBakTool.Model;
  14. namespace WechatPCMsgBakTool
  15. {
  16. public class WXUserReader
  17. {
  18. private Dictionary<string, SQLiteConnection> DBInfo = new Dictionary<string, SQLiteConnection>();
  19. private UserBakConfig? UserBakConfig = null;
  20. public WXUserReader(UserBakConfig userBakConfig) {
  21. string path = Path.Combine(userBakConfig.UserWorkspacePath, "DecDB");
  22. UserBakConfig = userBakConfig;
  23. LoadDB(path);
  24. }
  25. public void LoadDB(string path)
  26. {
  27. string[] dbFileList = Directory.GetFiles(path);
  28. foreach (var item in dbFileList)
  29. {
  30. FileInfo fileInfo = new FileInfo(item);
  31. if (fileInfo.Extension != ".db")
  32. continue;
  33. SQLiteConnection con = new SQLiteConnection(item);
  34. string dbName = fileInfo.Name.Split('.')[0];
  35. DBInfo.Add(dbName, con);
  36. }
  37. }
  38. public List<WXContact>? GetWXContacts(string? name = null)
  39. {
  40. SQLiteConnection con = DBInfo["MicroMsg"];
  41. if (con == null)
  42. return null;
  43. string query = "select * from contact";
  44. if (name != null)
  45. {
  46. query = "select * from contact where username = ? or alias = ?";
  47. return con.Query<WXContact>(query, name, name);
  48. }
  49. return con.Query<WXContact>(query);
  50. }
  51. public List<WXMsg>? GetWXMsgs(string uid)
  52. {
  53. List<WXMsg> tmp = new List<WXMsg>();
  54. for (int i = 0; i <= 99; i++)
  55. {
  56. if(DBInfo.ContainsKey("MSG" + i.ToString()))
  57. {
  58. SQLiteConnection con = DBInfo["MSG" + i.ToString()];
  59. if (con == null)
  60. return tmp;
  61. string query = "select * from MSG where StrTalker=?";
  62. List<WXMsg> wXMsgs = con.Query<WXMsg>(query, uid);
  63. foreach (WXMsg w in wXMsgs)
  64. {
  65. tmp.Add(w);
  66. }
  67. }
  68. }
  69. return tmp;
  70. }
  71. public WXSessionAttachInfo? GetWXMsgAtc(WXMsg msg)
  72. {
  73. SQLiteConnection con = DBInfo["MultiSearchChatMsg"];
  74. if (con == null)
  75. return null;
  76. string query = "select * from SessionAttachInfo where msgId = ? order by attachsize desc";
  77. List<WXSessionAttachInfo> list = con.Query<WXSessionAttachInfo>(query, msg.MsgSvrID);
  78. if (list.Count != 0)
  79. {
  80. //部分附件可能有md5校验,这里移除校验,给的是正确路径
  81. WXSessionAttachInfo acc = list[0];
  82. int index = acc.attachPath.IndexOf(".dat");
  83. int index2 = acc.attachPath.IndexOf(".dat");
  84. if (acc.attachPath.Length - index > 10 && index != -1)
  85. {
  86. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  87. }
  88. if (acc.attachPath.Length - index2 > 10 && index2 != -1)
  89. {
  90. acc.attachPath = acc.attachPath.Substring(0, acc.attachPath.Length - 32);
  91. }
  92. return acc;
  93. }
  94. else
  95. return null;
  96. }
  97. public WXMediaMsg? GetVoiceMsg(WXMsg msg)
  98. {
  99. for (int i = 0; i <= 99; i++)
  100. {
  101. if(DBInfo.ContainsKey("MediaMSG" + i.ToString()))
  102. {
  103. SQLiteConnection con = DBInfo["MediaMSG" + i.ToString()];
  104. if (con == null)
  105. continue;
  106. string query = "select * from Media where Reserved0=?";
  107. List<WXMediaMsg> wXMsgs = con.Query<WXMediaMsg>(query, msg.MsgSvrID);
  108. if (wXMsgs.Count != 0)
  109. return wXMsgs[0];
  110. }
  111. }
  112. return null;
  113. }
  114. public string? GetAttachment(WXMsgType type, WXMsg msg)
  115. {
  116. if (UserBakConfig == null)
  117. return null;
  118. string? tmpPath = Path.Combine(UserBakConfig.UserWorkspacePath, "Temp");
  119. if (!Directory.Exists(tmpPath))
  120. Directory.CreateDirectory(tmpPath);
  121. // 如果是图片和视频,从附件库中搜索
  122. string? path = null;
  123. if (type == WXMsgType.Image || type == WXMsgType.Video)
  124. {
  125. WXSessionAttachInfo? atcInfo = GetWXMsgAtc(msg);
  126. if (atcInfo == null)
  127. return null;
  128. path = atcInfo.attachPath;
  129. }
  130. // 如果是从语音,从媒体库查找
  131. else if (type == WXMsgType.Audio)
  132. {
  133. WXMediaMsg? voiceMsg = GetVoiceMsg(msg);
  134. if (voiceMsg == null)
  135. return null;
  136. if (voiceMsg.Buf == null)
  137. return null;
  138. // 从DB取音频文件到临时目录
  139. string tmp_file_path = Path.Combine(tmpPath, voiceMsg.Key + ".arm");
  140. using (FileStream stream = new FileStream(tmp_file_path, FileMode.OpenOrCreate))
  141. {
  142. stream.Write(voiceMsg.Buf, 0, voiceMsg.Buf.Length);
  143. }
  144. path = tmp_file_path;
  145. }
  146. if (path == null)
  147. return null;
  148. // 获取到原路径后,开始进行解密转移,只有图片和语音需要解密,解密后是直接归档目录
  149. if(type == WXMsgType.Image || type== WXMsgType.Audio)
  150. {
  151. path = DecryptAttachment(type, path);
  152. }
  153. else if (type == WXMsgType.Video)
  154. {
  155. string video_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Video");
  156. if(!Directory.Exists(video_dir))
  157. Directory.CreateDirectory(video_dir);
  158. FileInfo fileInfo = new FileInfo(path);
  159. string video_file_path = Path.Combine(video_dir, fileInfo.Name);
  160. // 视频的路径是相对路径,需要加上资源目录
  161. path = Path.Combine(UserBakConfig.UserResPath, path);
  162. if(!File.Exists(video_file_path))
  163. File.Move(path, video_file_path);
  164. path = video_file_path;
  165. }
  166. if (path == null)
  167. return null;
  168. // 该相对路径
  169. path = path.Replace(UserBakConfig.UserWorkspacePath + "\\", "");
  170. return path;
  171. }
  172. public string? DecryptAttachment(WXMsgType type, string path)
  173. {
  174. if (UserBakConfig == null)
  175. return null;
  176. string? file_path = null;
  177. switch (type)
  178. {
  179. case WXMsgType.Image:
  180. string img_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Image");
  181. if (!Directory.Exists(img_dir))
  182. Directory.CreateDirectory(img_dir);
  183. // 图片的路径是相对路径,需要加上资源目录
  184. path = Path.Combine(UserBakConfig.UserResPath, path);
  185. byte[] decFileByte = DecryptionHelper.DecImage(path);
  186. string decFiletype = DecryptionHelper.CheckFileType(decFileByte);
  187. file_path = DecryptionHelper.SaveDecImage(decFileByte, path, img_dir, decFiletype);
  188. break;
  189. case WXMsgType.Audio:
  190. string audio_dir = Path.Combine(UserBakConfig.UserWorkspacePath, "Audio");
  191. if (!Directory.Exists(audio_dir))
  192. Directory.CreateDirectory(audio_dir);
  193. FileInfo fileInfo = new FileInfo(path);
  194. string audio_file_dir = Path.Combine(audio_dir, fileInfo.Name + ".mp3");
  195. ToolsHelper.DecodeVoice(path, path + ".pcm", audio_file_dir);
  196. file_path = audio_file_dir;
  197. break;
  198. }
  199. return file_path;
  200. }
  201. }
  202. public enum WXMsgType
  203. {
  204. Image = 0,
  205. Video = 1,
  206. Audio = 2,
  207. File = 3,
  208. }
  209. }