V2Method.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. using CCWin.SkinControl;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using SufeiUtil;
  5. using SXLibrary;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.SQLite;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Web;
  15. using System.Windows.Forms;
  16. using HttpHelper = SufeiUtil.HttpHelper;
  17. namespace MOKA_Factory_Tools
  18. {
  19. internal class V2Method
  20. {
  21. public static MidList midList { get; set; }
  22. private static string url_getMessage = "https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do";
  23. public static void SetMexicanConfig(bool bMexican = false)
  24. {
  25. if (bMexican)
  26. url_getMessage = "http://10.138.96.32:81/IDManage/v1/order/getMessage";
  27. else
  28. url_getMessage = "https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do";
  29. if ( TestMode.testMode)
  30. url_getMessage = "http://test.scbc.qhmoka.com/scbc-server/clientType/getMessage.do";
  31. }
  32. public static void SetGetMessageUrl(string url = "https://cn.uc.qhmoka.com/scbc-server/clientType/getMessage.do")
  33. {
  34. url_getMessage = url;
  35. }
  36. public static RokuCustomer GetRokuInfo(string strRokuInfo)
  37. {
  38. try
  39. {
  40. JObject jObject = JObject.Parse(strRokuInfo);
  41. RokuCustomer rokuCustomer = new RokuCustomer();
  42. rokuCustomer.id = jObject["id"].ToString().Trim();
  43. rokuCustomer.ordernum = jObject["ordernum"].ToString().Trim();
  44. rokuCustomer.brand = jObject["brand"].ToString().Trim();
  45. rokuCustomer.oemmodel = jObject["oemmodel"].ToString().Trim();
  46. rokuCustomer.productiondate = jObject["productiondate"].ToString().Trim();
  47. rokuCustomer.remotetype = jObject["remotetype"].ToString().Trim();
  48. rokuCustomer.updatetime = jObject["updatetime"].ToString().Trim();
  49. rokuCustomer.createtime = jObject["createtime"].ToString().Trim();
  50. rokuCustomer.isdelete = jObject["isdelete"].ToString().Trim();
  51. if (jObject["data"] != null)
  52. {
  53. rokuCustomer.data = new List<RokuData>();
  54. foreach (var item in jObject["data"])
  55. {
  56. RokuData rokuData = new RokuData();
  57. rokuData.region = item["region"].Value<string>();
  58. rokuData.supporturl = item["supporturl"].Value<string>();
  59. rokuData.supportphone = item["supportphone"].Value<string>();
  60. rokuCustomer.data.Add(rokuData);
  61. }
  62. }
  63. return rokuCustomer;
  64. }
  65. catch (Exception ex)
  66. {
  67. Log.WriteErrorLog(string.Format("解析json失败:{0}", strRokuInfo));
  68. }
  69. return null;
  70. }
  71. public static RokuCustomer GetRokuFileInfo()
  72. {
  73. string fileName = LocalPath.localpath + "roku.info";
  74. if (File.Exists(fileName))
  75. {
  76. Log.WriteInfoLog(string.Format("using roku.info = {0}", fileName));
  77. StreamReader file = File.OpenText(fileName);
  78. RokuCustomer roku = GetRokuInfo(file.ReadToEnd());
  79. file.Close();
  80. return roku;
  81. }
  82. return null;
  83. }
  84. /// <summary>
  85. ///
  86. /// </summary>
  87. /// <param name="url"></param>
  88. /// <param name="order"></param>
  89. /// <param name="pcmac"></param>
  90. /// <param name="sqliteConn"></param>
  91. /// <param name="mid"></param>
  92. /// <returns></returns>
  93. public static bool GetMidInfo(string order, string pcmac, SQLiteConnection sqliteConn, out MidList mid)
  94. {
  95. mid = new MidList();
  96. string postData = string.Format("{{\"bid\":\"{0}\",\"mac\":\"{1}\"}}", order, pcmac);
  97. HttpHelper http = new HttpHelper();
  98. HttpItem item = new HttpItem()
  99. {
  100. URL = url_getMessage,//URL这里都是测试URl必需项
  101. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)可选项 默认类会自动识别//Encoding = Encoding.Default,
  102. Method = "post",
  103. Postdata = postData,
  104. PostEncoding = Encoding.UTF8,
  105. KeepAlive = false
  106. };
  107. item.ContentType = "application/json;charset=utf-8";
  108. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  109. stopwatch.Start();
  110. HttpResult result = http.GetHtml(item);
  111. stopwatch.Stop();
  112. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  113. stopwatch.Reset();
  114. string ParseText = result.Html;
  115. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  116. {
  117. Log.WriteGetKeyLog("\r\nGet MID info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url_getMessage + "\r\nPostdata:" + postData);
  118. }
  119. else
  120. {
  121. Log.WriteErrorLog("\r\nFail to GetMidInfo:" + result.StatusDescription + result.Html + "\r\nAddress:" + url_getMessage + "\r\nPostdata:" + postData);
  122. CommonMethod.ReportErrormsg("Fail to GetMidInfo", result.StatusDescription + result.Html + "\r\nAddress:" + url_getMessage + "\r\nPostdata:" + postData, sqliteConn);
  123. return false;
  124. }
  125. try
  126. {
  127. JObject jObject = JObject.Parse(ParseText);
  128. mid.message = jObject["message"].Value<string>();
  129. mid.code = jObject["code"].Value<string>();
  130. mid.factoryname = jObject["factoryName"].Value<string>();
  131. mid.factoryNum = jObject["factoryNum"].Value<string>();
  132. mid.version = jObject["version"].Value<string>();
  133. mid.quantity = jObject["quantity"].Value<string>();
  134. mid.projectid = jObject["projectId"].Value<string>();
  135. mid.clienttype = jObject["clientType"].Value<string>();
  136. mid.host = jObject["host"].Value<string>();
  137. mid.whiteType = jObject["whiteType"].Value<string>();
  138. string aesEncrypt = jObject["aesEncrypt"] == null ? null : jObject["aesEncrypt"].Value<string>();
  139. if (aesEncrypt != null)
  140. mid.aesEncrypt = aesEncrypt.Equals("yes", StringComparison.OrdinalIgnoreCase);
  141. object a = jObject["rokuCustomer"];
  142. if (jObject["rokuCustomer"].ToString().Length > 0)
  143. {
  144. mid.rokuCustomer = GetRokuInfo(jObject["rokuCustomer"].ToString());
  145. }
  146. mid.keytype = new Dictionary<string, string>();
  147. foreach (var ss in jObject["obj"]) //查找某个字段与值
  148. {
  149. mid.keytype.Add(ss["name"].Value<string>(), ss["type"].Value<string>());
  150. }
  151. if (mid.code == "1000")
  152. {
  153. midList = mid;
  154. return true;
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. Log.WriteErrorLog("\r\nFail to parse MID info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url_getMessage + "\r\nPostdata:" + postData);
  160. CommonMethod.ReportErrormsg("Fail to parse MID info", ex.Message + ":" + ParseText + "\r\nAddress:" + url_getMessage + "\r\nPostdata:" + postData, sqliteConn);
  161. MessageBox.Show("Fail to parse MID info\r\n " + ex.Message);
  162. }
  163. return false;
  164. }
  165. public static bool GetMidKey(string url, string sn, string order, string psn, string firetv_device_code, List<KeyType> key2Write, SQLiteConnection connection, ref KeyInfo keys, out string msg)
  166. {
  167. msg = "";
  168. string code;
  169. bool result = false;
  170. HttpHelper http = new HttpHelper();
  171. HttpItem item = new HttpItem()
  172. {
  173. Encoding = Encoding.UTF8,
  174. Method = "post",
  175. ContentType = "application/json",
  176. KeepAlive = false
  177. };
  178. // 计算函数耗时;
  179. Stopwatch watch = new Stopwatch();
  180. watch.Start();
  181. item.URL = url + "/bind/order";
  182. item.Postdata = string.Format("{{\"orderNum\":\"{0}\",\"sn\":\"{1}\",\"psn\":\"{2}\",\"orderCode\":\"{3}\",\"clientType\":\"{4}\",\"skipKey\":{5}}}", order, sn, psn, firetv_device_code, midList.clienttype, key2Write == null ? "[]" : JsonConvert.SerializeObject(key2Write).ToString());
  183. HttpResult httpResult = http.GetHtml(item);
  184. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
  185. {
  186. try
  187. {
  188. JObject jObject = JObject.Parse(httpResult.Html);
  189. msg = CommonMethod.JSON_SeleteNode(jObject, "msg");
  190. code = CommonMethod.JSON_SeleteNode(jObject, "code");
  191. if (code == "0")
  192. {
  193. //需要解密
  194. if (midList != null && midList.aesEncrypt)
  195. {
  196. AES_DES Aes = new AES_DES();
  197. //data解密
  198. string plainText = CommonMethod.JSON_SeleteNode(jObject, "data");
  199. string key = AES_DES.AESKey;
  200. byte[] IV = AES_DES.AESIV;
  201. string str = AES_DES.DecryptStringFromBytes_Aes(plainText, key, IV);
  202. Log.WriteInfoLog("解密后:" + str);
  203. jObject["data"] = str;
  204. }
  205. Dictionary<string, string> data = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObject["data"].ToString());
  206. if (data != null)
  207. {
  208. Func<string, string, bool> CheckMD5 = (data, md5) =>
  209. {
  210. if (data == null)
  211. return true;
  212. if (!GetMD5.GetStrMd5(data).Equals(md5, StringComparison.OrdinalIgnoreCase))
  213. return false;
  214. return true;
  215. };
  216. if (data.ContainsKey("mac") && data.ContainsKey("mac_md5"))
  217. {
  218. keys.Mac.data = data["mac"];
  219. keys.Mac.md5 = data["mac_md5"];
  220. if (!CheckMD5(keys.Mac.data, keys.Mac.md5))
  221. {
  222. msg = string.Format("Mac Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Mac.data, keys.Mac.md5);
  223. goto end;
  224. }
  225. }
  226. if (data.ContainsKey("btMac") && data.ContainsKey("btMac_md5"))
  227. {
  228. keys.BT_MAC.data = data["btMac"];
  229. keys.BT_MAC.md5 = data["btMac_md5"];
  230. if (!CheckMD5(keys.BT_MAC.data, keys.BT_MAC.md5))
  231. {
  232. msg = string.Format("BT_MAC Key Value md5 Is Incorrect: data={0}, md5={1}", keys.BT_MAC.data, keys.BT_MAC.md5);
  233. goto end;
  234. }
  235. }
  236. if (data.ContainsKey("wifiMac") && data.ContainsKey("wifiMac_md5"))
  237. {
  238. keys.WiFi_MAC.data = data["wifiMac"];
  239. keys.WiFi_MAC.md5 = data["wifiMac_md5"];
  240. if (!CheckMD5(keys.WiFi_MAC.data, keys.WiFi_MAC.md5))
  241. {
  242. msg = string.Format("WiFi_MAC Key Value md5 Is Incorrect: data={0}, md5={1}", keys.WiFi_MAC.data, keys.WiFi_MAC.md5);
  243. goto end;
  244. }
  245. }
  246. if (data.ContainsKey("deviceid") && data.ContainsKey("deviceid_md5"))
  247. {
  248. keys.DID.data = data["deviceid"];
  249. keys.DID.md5 = data["deviceid_md5"];
  250. if (!CheckMD5(keys.DID.data, keys.DID.md5))
  251. {
  252. msg = string.Format("DID Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DID.data, keys.DID.md5);
  253. goto end;
  254. }
  255. }
  256. if (data.ContainsKey("hdcp") && data.ContainsKey("hdcp_md5"))
  257. {
  258. keys.HDCP.data = data["hdcp"];
  259. keys.HDCP.md5 = data["hdcp_md5"];
  260. if (!CheckMD5(keys.HDCP.data, keys.HDCP.md5))
  261. {
  262. msg = string.Format("HDCP Key Value md5 Is Incorrect: data={0}, md5={1}", keys.HDCP.data, keys.HDCP.md5);
  263. goto end;
  264. }
  265. }
  266. if (data.ContainsKey("hdcp2") && data.ContainsKey("hdcp2_md5"))
  267. {
  268. keys.HDCP22.data = data["hdcp2"];
  269. keys.HDCP22.md5 = data["hdcp2_md5"];
  270. if (!CheckMD5(keys.HDCP22.data, keys.HDCP22.md5))
  271. {
  272. msg = string.Format("HDCP22 Key Value md5 Is Incorrect: data={0}, md5={1}", keys.HDCP22.data, keys.HDCP22.md5);
  273. goto end;
  274. }
  275. }
  276. if (data.ContainsKey("ci") && data.ContainsKey("ci_md5"))
  277. {
  278. keys.CI_plus.data = data["ci"];
  279. keys.CI_plus.md5 = data["ci_md5"];
  280. if (!CheckMD5(keys.CI_plus.data, keys.CI_plus.md5))
  281. {
  282. msg = string.Format("CI_plus Key Value md5 Is Incorrect: data={0}, md5={1}", keys.CI_plus.data, keys.CI_plus.md5);
  283. goto end;
  284. }
  285. }
  286. if (data.ContainsKey("ecp") && data.ContainsKey("ecp_md5"))
  287. {
  288. keys.ECP.data = data["ecp"];
  289. keys.ECP.md5 = data["ecp_md5"];
  290. if (!CheckMD5(keys.ECP.data, keys.ECP.md5))
  291. {
  292. msg = string.Format("ECP Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ECP.data, keys.ECP.md5);
  293. goto end;
  294. }
  295. }
  296. if (data.ContainsKey("acas") && data.ContainsKey("acas_md5"))
  297. {
  298. keys.ACASKey_Data.data = data["acas"];
  299. keys.ACASKey_Data.md5 = data["acas_md5"];
  300. if (!CheckMD5(keys.ACASKey_Data.data, keys.ACASKey_Data.md5))
  301. {
  302. msg = string.Format("ACASKey_Data Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ACASKey_Data.data, keys.ACASKey_Data.md5);
  303. goto end;
  304. }
  305. }
  306. if (data.ContainsKey("lek") && data.ContainsKey("lek_md5"))
  307. {
  308. keys.LEK.data = data["lek"];
  309. keys.LEK.md5 = data["lek_md5"];
  310. if (!CheckMD5(keys.LEK.data, keys.LEK.md5))
  311. {
  312. msg = string.Format("LEK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.LEK.data, keys.LEK.md5);
  313. goto end;
  314. }
  315. }
  316. if (data.ContainsKey("pek") && data.ContainsKey("pek_md5"))
  317. {
  318. keys.PEK.data = data["pek"];
  319. keys.PEK.md5 = data["pek_md5"];
  320. if (!CheckMD5(keys.PEK.data, keys.PEK.md5))
  321. {
  322. msg = string.Format("PEK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.PEK.data, keys.PEK.md5);
  323. goto end;
  324. }
  325. }
  326. if (data.ContainsKey("playready") && data.ContainsKey("playready_md5"))
  327. {
  328. keys.Playready.data = data["playready"];
  329. keys.Playready.md5 = data["playready_md5"];
  330. if (!CheckMD5(keys.Playready.data, keys.Playready.md5))
  331. {
  332. msg = string.Format("Playready Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Playready.data, keys.Playready.md5);
  333. goto end;
  334. }
  335. }
  336. if (data.ContainsKey("netfilx") && data.ContainsKey("netfilx_md5"))
  337. {
  338. keys.ESN.data = data["netfilx"];
  339. keys.ESN.md5 = data["netfilx_md5"];
  340. if (!CheckMD5(keys.ESN.data, keys.ESN.md5))
  341. {
  342. msg = string.Format("ESN Key Value md5 Is Incorrect: data={0}, md5={1}", keys.ESN.data, keys.ESN.md5);
  343. goto end;
  344. }
  345. }
  346. if (data.ContainsKey("hash") && data.ContainsKey("hash_md5"))
  347. {
  348. keys.Hashkey.data = data["hash"];
  349. keys.Hashkey.md5 = data["hash_md5"];
  350. if (!CheckMD5(keys.Hashkey.data, keys.Hashkey.md5))
  351. {
  352. msg = string.Format("Hashkey Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Hashkey.data, keys.Hashkey.md5);
  353. goto end;
  354. }
  355. }
  356. if (data.ContainsKey("mgk") && data.ContainsKey("mgk_md5"))
  357. {
  358. keys.MGK.data = data["mgk"];
  359. keys.MGK.md5 = data["mgk_md5"];
  360. if (!CheckMD5(keys.MGK.data, keys.MGK.md5))
  361. {
  362. msg = string.Format("MGK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.MGK.data, keys.MGK.md5);
  363. goto end;
  364. }
  365. }
  366. if (data.ContainsKey("youtube") && data.ContainsKey("youtube_md5"))
  367. {
  368. keys.YouTube_KEY.data = data["youtube"];
  369. keys.YouTube_KEY.md5 = data["youtube_md5"];
  370. if (!CheckMD5(keys.YouTube_KEY.data, keys.YouTube_KEY.md5))
  371. {
  372. msg = string.Format("YouTube_KEY Key Value md5 Is Incorrect: data={0}, md5={1}", keys.YouTube_KEY.data, keys.YouTube_KEY.md5);
  373. goto end;
  374. }
  375. }
  376. if (data.ContainsKey("fairplay") && data.ContainsKey("fairplay_md5"))
  377. {
  378. keys.Fairplay.data = data["fairplay"];
  379. keys.Fairplay.md5 = data["fairplay_md5"];
  380. if (!CheckMD5(keys.Fairplay.data, keys.Fairplay.md5))
  381. {
  382. msg = string.Format("Fairplay Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Fairplay.data, keys.Fairplay.md5);
  383. goto end;
  384. }
  385. }
  386. if (data.ContainsKey("widevine") && data.ContainsKey("widevine_md5"))
  387. {
  388. keys.Widevine.data = data["widevine"];
  389. keys.Widevine.md5 = data["widevine_md5"];
  390. if (!CheckMD5(keys.Widevine.data, keys.Widevine.md5))
  391. {
  392. msg = string.Format("Widevine Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Widevine.data, keys.Widevine.md5);
  393. goto end;
  394. }
  395. }
  396. if (data.ContainsKey("attention") && data.ContainsKey("attention_md5"))
  397. {
  398. keys.Attestation.data = data["attention"];
  399. keys.Attestation.md5 = data["attention_md5"];
  400. if (!CheckMD5(keys.Attestation.data, keys.Attestation.md5))
  401. {
  402. msg = string.Format("Attestation Key Value md5 Is Incorrect: data={0}, md5={1}", keys.Attestation.data, keys.Attestation.md5);
  403. goto end;
  404. }
  405. }
  406. if (data.ContainsKey("dsn") && data.ContainsKey("dsn_md5"))
  407. {
  408. keys.DSN.data = data["dsn"];
  409. keys.DSN.md5 = data["dsn_md5"];
  410. if (!CheckMD5(keys.DSN.data, keys.DSN.md5))
  411. {
  412. msg = string.Format("DSN Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DSN.data, keys.DSN.md5);
  413. goto end;
  414. }
  415. }
  416. if (data.ContainsKey("dak") && data.ContainsKey("dak_md5"))
  417. {
  418. keys.DAK.data = data["dak"];
  419. keys.DAK.md5 = data["dak_md5"];
  420. if (!CheckMD5(keys.DAK.data, keys.DAK.md5))
  421. {
  422. msg = string.Format("DAK Key Value md5 Is Incorrect: data={0}, md5={1}", keys.DAK.data, keys.DAK.md5);
  423. goto end;
  424. }
  425. }
  426. if (data.ContainsKey("kfp") && data.ContainsKey("kfp_md5"))
  427. {
  428. keys.KFP.data = data["kfp"];
  429. keys.KFP.md5 = data["kfp_md5"];
  430. if (!CheckMD5(keys.KFP.data, keys.KFP.md5))
  431. {
  432. msg = string.Format("KFP Key Value md5 Is Incorrect: data={0}, md5={1}", keys.KFP.data, keys.KFP.md5);
  433. goto end;
  434. }
  435. }
  436. result = true;
  437. }
  438. }
  439. }
  440. catch
  441. {
  442. msg = "解析Json失败";
  443. }
  444. }
  445. end:
  446. watch.Stop();
  447. Log.WriteInfoLog(string.Format("[GetIDMKeys] order={0},sn={1},Elapsed={2} GetKeys={3}", order, sn, watch.Elapsed.TotalMilliseconds, result ? "成功" : "失败"));
  448. if (!result)
  449. CommonMethod.ReportErrormsg("Fail to GetIDMKeys ", msg + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + httpResult.Html, connection);
  450. return result;
  451. }
  452. public static string GetReportJson(string order, string sn, bool isNodid, string clientType, string swVer, string pid, ref KeyInfo keyinfo, bool bMIEnable, ref MIKey mIKey)
  453. {
  454. Func<string, string, JObject, bool> AddJsonItem = (name, value, jobj) =>
  455. {
  456. if (name == null || name.Length == 0)
  457. return false;
  458. if (value == null || value.Length == 0)
  459. return false;
  460. if (jobj == null)
  461. return false;
  462. jobj.Add(name, (JToken)value);
  463. return true;
  464. };
  465. Func<KeyData, JObject, bool> AddKeyJson = (keydata, jobj) =>
  466. {
  467. if (keydata == null || jobj == null || keydata.data == null)
  468. return false;
  469. if (keydata.keyfrom == KEY_FROM.FROM_IDM && keydata.enable)
  470. {
  471. jobj.Add(keydata.name, (JToken)keydata.data);
  472. }
  473. return true;
  474. };
  475. Func<KeyData, JObject, string, bool> AddKeyJson2 = (keydata, jobj, name) =>
  476. {
  477. if (keydata == null || jobj == null || keydata.data == null)
  478. return false;
  479. if (keydata.keyfrom == KEY_FROM.FROM_IDM && keydata.enable)
  480. {
  481. jobj.Add(name, (JToken)keydata.data);
  482. }
  483. return true;
  484. };
  485. try
  486. {
  487. JObject postJson = new JObject();
  488. AddJsonItem("ordernum", order, postJson);
  489. AddJsonItem("sn", sn, postJson);
  490. AddJsonItem("nodid", isNodid.ToString().ToLower(), postJson);
  491. AddJsonItem("toolVersion", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), postJson);
  492. AddJsonItem("clienttype", clientType, postJson);
  493. AddJsonItem("sversionid", swVer, postJson);
  494. AddJsonItem("projectid", pid, postJson);
  495. if (keyinfo != null)
  496. {
  497. AddKeyJson2(keyinfo.DID, postJson, "deviceid");
  498. if ( !bMIEnable || (mIKey != null && mIKey.EthernetMac == null))
  499. AddKeyJson2(keyinfo.Mac, postJson, "mac");
  500. AddKeyJson2(keyinfo.HDCP, postJson, "hdcpkey");
  501. AddKeyJson2(keyinfo.HDCP22, postJson, "hdcpkey2");
  502. AddKeyJson2(keyinfo.ESN, postJson, "netfilxesn");
  503. AddKeyJson2(keyinfo.Widevine, postJson, "widevine");
  504. AddKeyJson2(keyinfo.CI_plus, postJson, "cikey");
  505. AddKeyJson2(keyinfo.Attestation, postJson, "attestation");
  506. AddKeyJson2(keyinfo.MGK, postJson, "mgk");
  507. AddKeyJson2(keyinfo.Fairplay, postJson, "fairplay");
  508. if (!bMIEnable || ( mIKey != null && mIKey.DSN == null))
  509. AddKeyJson2(keyinfo.DSN, postJson, "dsn");
  510. if (!bMIEnable || (mIKey != null && mIKey.WIFIMAC == null))
  511. AddKeyJson2(keyinfo.WiFi_MAC, postJson, "wifimac");
  512. if (!bMIEnable || (mIKey != null && mIKey.BTMac == null))
  513. AddKeyJson2(keyinfo.BT_MAC, postJson, "btmac");
  514. AddKeyJson2(keyinfo.LEK, postJson, "lek");
  515. AddKeyJson2(keyinfo.PEK, postJson, "pek");
  516. AddKeyJson2(keyinfo.Playready, postJson, "playready");
  517. AddKeyJson2(keyinfo.Hashkey, postJson, "hash");
  518. AddKeyJson2(keyinfo.ECP, postJson, "ecp");
  519. AddKeyJson2(keyinfo.YouTube_KEY, postJson, "youtubekey");
  520. AddKeyJson2(keyinfo.ACASKey_Data, postJson, "acas_data");
  521. AddKeyJson2(keyinfo.DAK, postJson, "dak");
  522. AddKeyJson2(keyinfo.KFP, postJson, "kfp");
  523. }
  524. string postData = postJson.ToString();
  525. //需要加密上报的话
  526. if (midList != null && midList.aesEncrypt)
  527. {
  528. AES_DES AES_DES = new AES_DES();
  529. string key = AES_DES.AESKey;
  530. byte[] IV = AES_DES.AESIV;
  531. postData = AES_DES.EncryptStringToBytes_Aes(postData, key, IV);
  532. //json
  533. string Data = string.Format("{{\"reportData\":\"{0}\"}}", postData);
  534. JObject jo = JObject.Parse(Data);
  535. postData = jo.ToString();
  536. }
  537. return postData;
  538. }
  539. catch (Exception ex)
  540. {
  541. Log.WriteErrorLog("\r\nFail to GetReportJson," + ex.Message + ":" + "\r\nOrder:" + order + "\r\nSN:" + sn);
  542. }
  543. return "";
  544. }
  545. /// <summary>
  546. /// 在线抄写完成上报 Key
  547. /// </summary>
  548. /// <param name="url"></param>
  549. /// <param name="order"></param>
  550. /// <param name="sn"></param>
  551. /// <param name="isNodid"></param>
  552. /// <param name="keyinfo"></param>
  553. /// <param name="sqliteConn"></param>
  554. /// <param name="msg"></param>
  555. /// <returns></returns>
  556. public static bool ReportOnlineData(string url, string order, string postData, SQLiteConnection sqliteConn, bool repeatupload, string id, int timeout)
  557. {
  558. if (timeout < 5000)
  559. timeout = 5000;
  560. HttpHelper http = new HttpHelper();
  561. HttpItem item = new HttpItem()
  562. {
  563. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)可选项 默认类会自动识别//Encoding = Encoding.Default,
  564. Method = "post",
  565. Postdata = postData,
  566. PostEncoding = Encoding.UTF8,
  567. Timeout = timeout,
  568. KeepAlive = false
  569. };
  570. item.URL = url + "/reportData/report";
  571. item.ContentType = "application/json;charset=utf-8";
  572. Stopwatch stopwatch = new Stopwatch();
  573. stopwatch.Start();
  574. HttpResult result = http.GetHtml(item);
  575. stopwatch.Stop();
  576. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  577. stopwatch.Reset();
  578. string ParseText = result.Html;
  579. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  580. {
  581. Log.WriteGetKeyLog("\r\nReportOnlineData info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  582. try
  583. {
  584. JObject jObject = JObject.Parse(ParseText);
  585. string msg = jObject["msg"].Value<string>();
  586. string code = jObject["code"].Value<string>();
  587. if (code == "0")
  588. return true;
  589. }
  590. catch (Exception ex)
  591. {
  592. Log.WriteErrorLog("\r\nFail to ReportOnlineData," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  593. CommonMethod.ReportErrormsg("Fail to ReportOnlineData", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata, sqliteConn);
  594. }
  595. if (repeatupload)
  596. {// 删除记录;
  597. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  598. }
  599. }
  600. else
  601. {
  602. Log.WriteErrorLog("\r\nFail to ReportOnlineData:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  603. if (!repeatupload)
  604. {
  605. if (CommonMethod.HTTPChecker(item.URL))
  606. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { order, url, item.Postdata });
  607. }
  608. CommonMethod.ReportErrormsg("Fail to ReportOnlineData", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata, sqliteConn);
  609. }
  610. return false;
  611. }
  612. //reportData/reportList
  613. /// <summary>
  614. /// 离线抄写上报
  615. /// </summary>
  616. /// <param name="url"></param>
  617. /// <param name="copydate"></param>
  618. /// <param name="SN"></param>
  619. /// <param name="localDBNow"></param>
  620. /// <param name="sqliteConn"></param>
  621. /// <param name="repeatupload"></param>
  622. /// <param name="id"></param>
  623. /// <param name="timeout"></param>
  624. /// <returns></returns>
  625. public static bool ReportOfflineData(string url, string copydate, string SN, SQLiteConnection localDBNow, SQLiteConnection sqliteConn, bool repeatupload, string id)
  626. {
  627. HttpHelper http = new HttpHelper();
  628. HttpItem item = new HttpItem()
  629. {
  630. Encoding = Encoding.UTF8,
  631. Method = "post",
  632. Postdata = string.Format("{{\"sn\":\"{0}\",\"date\":\"{1}\"}}", SN, copydate),
  633. PostEncoding = Encoding.UTF8,
  634. KeepAlive = false
  635. };
  636. item.URL = url + "/reportData/reportList";
  637. item.ContentType = "application/json;charset=utf-8";
  638. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  639. stopwatch.Start();
  640. HttpResult result = http.GetHtml(item);
  641. stopwatch.Stop();
  642. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  643. stopwatch.Reset();
  644. string ParseText = result.Html;
  645. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  646. {
  647. Log.WriteGetKeyLog("\r\nReportOfflineData info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  648. }
  649. else
  650. {
  651. Log.WriteErrorLog("\r\nFail to ReportOfflineData:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  652. CommonMethod.ReportErrormsg("Fail to ReportOfflineData", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata, sqliteConn);
  653. if (repeatupload)
  654. return false;
  655. if (CommonMethod.HTTPChecker(url))
  656. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { "preload_mode", url, SN + "@" + copydate });
  657. return false;
  658. }
  659. try
  660. {
  661. JObject jObject = JObject.Parse(ParseText);
  662. string msg = jObject["msg"].Value<string>();
  663. string code = jObject["code"].Value<string>();
  664. if (code == "0")
  665. {
  666. //更新上报内容
  667. if (SQLiteHelper.UpdateReportStatus(localDBNow, SN))
  668. {
  669. //更新成功
  670. Log.WriteGetKeyLog("\r\nUpdateReportStatus success:\r\n" + url + "/reportlist.do?\r\n" + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText);
  671. return true;
  672. }
  673. else
  674. {
  675. Log.WriteErrorLog("\r\nFail to UpdateReportStatus: " + SN);
  676. return false;
  677. }
  678. }
  679. else
  680. {
  681. CommonMethod.ReportErrormsg("Fail to UploadPreloadCopyResult", "Address: " + url + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText, sqliteConn);
  682. Log.WriteErrorLog("\r\nUpdateReportStatus code return error,should be 1000 " + "\r\n" + SN + ":" + ParseText + "\r\nAddress:" + url + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate);
  683. if (repeatupload)
  684. {
  685. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  686. }
  687. }
  688. }
  689. catch (Exception ex)
  690. {
  691. Log.WriteErrorLog("\r\nFail to ReportOfflineData," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata);
  692. CommonMethod.ReportErrormsg("Fail to ReportOfflineData", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + item.Postdata, sqliteConn);
  693. }
  694. return false;
  695. }
  696. public static bool GetOfflineData(string url, string order, SQLiteConnection sqliteConn, out List<MidAddress> midAddressList)
  697. {
  698. midAddressList = new List<MidAddress>();
  699. HttpHelper http = new HttpHelper();
  700. HttpItem item = new HttpItem()
  701. {
  702. Encoding = Encoding.UTF8,
  703. Method = "post",
  704. Postdata = string.Format("{{\"ordernum\":\"{0}\",\"mac\":\"{1}\"}}", order, TestMode.MAC),
  705. KeepAlive = false
  706. };
  707. item.URL = url + "/offline/getofflinelist";
  708. item.ContentType = "application/json;charset=utf-8";
  709. HttpResult result = http.GetHtml(item);
  710. string ParseText = result.Html;
  711. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  712. {
  713. }
  714. else
  715. {
  716. Log.WriteErrorLog("\r\nFail to GetOfflineData:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  717. CommonMethod.ReportErrormsg("Fail to GetOfflineData", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  718. return false;
  719. }
  720. try
  721. {
  722. JObject jObject = JObject.Parse(ParseText);
  723. if (jObject["data"] != null)
  724. {
  725. foreach (var data in jObject["data"])
  726. {
  727. midAddressList.Add(new MidAddress()
  728. {
  729. des = CommonMethod.JSON_SeleteNode(jObject, "des"),
  730. code = CommonMethod.JSON_SeleteNode(jObject, "code"),
  731. order = data["order_number"].ToObject<string>(),
  732. number = data["order_quantity"].ToObject<string>(),
  733. pid = data["project_id"].ToObject<string>(),
  734. ctype = data["client_type"].ToObject<string>(),
  735. purl = data["packet_url"].ToObject<string>(),
  736. psize = data["packet_size"].ToObject<string>(),
  737. pmd5 = data["packet_md5"].ToObject<string>(),
  738. version = data["soft_version"].ToObject<string>(),
  739. host = url
  740. });
  741. }
  742. Log.WriteGetKeyLog("\r\nGet GetOfflineData success:" + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  743. return true;
  744. }
  745. Log.WriteErrorLog("\r\nFail to GetOfflineData," + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  746. }
  747. catch (Exception ex)
  748. {
  749. Log.WriteErrorLog("\r\nFail to GetOfflineData," + ex.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  750. CommonMethod.ReportErrormsg("Fail to GetOfflineData", ex.Message + "\r\n" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  751. MessageBox.Show("Fail to GetOfflineData info\r\n " + ex.Message);
  752. return false;
  753. }
  754. return false;
  755. }
  756. public static bool ReportDownloadStatus(string url, string order, SQLiteConnection sqliteConn)
  757. {
  758. HttpHelper http = new HttpHelper();
  759. HttpItem item = new HttpItem()
  760. {
  761. Encoding = null,
  762. Method = "post",
  763. Postdata = string.Format("{{\"ordernum\":\"{0}\"}}", order),
  764. KeepAlive = false
  765. };
  766. item.URL = url += "/offline/getreportofflinesn";
  767. item.ContentType = "application/json;charset=utf-8";
  768. HttpResult result = http.GetHtml(item);
  769. string ParseText = result.Html;
  770. if (result.StatusCode != System.Net.HttpStatusCode.OK)
  771. {
  772. Log.WriteErrorLog("\r\nFail to report download status:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order));
  773. CommonMethod.ReportErrormsg("Fail to report download status", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order), sqliteConn);
  774. return false;
  775. }
  776. try
  777. {
  778. JObject jObject = JObject.Parse(ParseText);
  779. string msg = CommonMethod.JSON_SeleteNode(jObject, "msg");
  780. string code = CommonMethod.JSON_SeleteNode(jObject, "code");
  781. if (code != "0")
  782. {
  783. Log.WriteErrorLog("\r\nError report download status,should be 1000:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order));
  784. CommonMethod.ReportErrormsg("Error download status code return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order), sqliteConn);
  785. return false;
  786. }
  787. }
  788. catch (Exception ex)
  789. {
  790. MessageBox.Show(ex.Message);
  791. Log.WriteErrorLog("\r\n" + ex.Message + ":" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order));
  792. CommonMethod.ReportErrormsg("Error parse download status return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/offline/getreportofflinesn" + "\r\nPostdata:" + ("ordernum=" + order), sqliteConn);
  793. return false;
  794. }
  795. return true;
  796. }
  797. };
  798. };