CommonMethod.cs 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using SufeiUtil;
  4. using SXLibrary;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Data.SQLite;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Management;
  13. using System.Net;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. using System.Windows.Forms;
  17. using System.Xml;
  18. using WHC.OrderWater.Commons;
  19. using HttpHelper = SufeiUtil.HttpHelper;
  20. using System.Web;
  21. using System.Security.Cryptography.X509Certificates;
  22. using System.Net.Security;
  23. using System.Runtime.InteropServices;
  24. using System.Diagnostics;
  25. namespace MOKA_Factory_Tools
  26. {
  27. class CommonMethod
  28. {
  29. [DllImport("kernel32")]
  30. public static extern bool GetProfileString(string lpApplicationName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize);
  31. [DllImport("kernel32")]
  32. public static extern bool WriteProfileString(string lpApplicationName, string lpKeyName, string lpString);
  33. public static string FactoryName = "Unknown";
  34. private static string lastOrder = "";
  35. private static List<string> ListACASSN = new List<string>();
  36. public static string ReadProfileString(string lpApplicationName, string lpKeyName, string lpDefault)
  37. {
  38. StringBuilder MyString = new StringBuilder(256);
  39. GetProfileString(lpApplicationName, lpKeyName, lpDefault, MyString, 256);
  40. return MyString.ToString();
  41. }
  42. //public static string LocalMacAddress = GetMacAddress();
  43. /// <summary>
  44. /// 获取活动网卡硬件地址
  45. /// </summary>
  46. /// <returns></returns>
  47. public static string GetMacAddress()
  48. {
  49. string mac = "";
  50. using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
  51. {
  52. ManagementObjectCollection moc = mc.GetInstances();
  53. int i = 0;
  54. int j = 0;
  55. foreach (ManagementObject mo in moc)
  56. {
  57. if (mo["MacAddress"] != null)
  58. {
  59. if (Convert.ToBoolean(mo["IPEnabled"]))
  60. {
  61. if (j < 2)
  62. {
  63. mac = mo["MacAddress"].ToString() + "&" + mac;
  64. j++;
  65. }
  66. }
  67. else
  68. {
  69. if (i < 1)
  70. {
  71. mac += mo["MacAddress"].ToString() + "&";
  72. i++;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return mac.Replace(":", "-");
  79. //return "C0-B6-F9-44-D7-BB";
  80. }
  81. /// <summary>
  82. /// 获取订单信息
  83. /// </summary>
  84. /// <param name="url"></param>
  85. /// <param name="order"></param>
  86. /// <param name="pcmac"></param>
  87. /// <param name="iptvip"></param>
  88. /// <param name="version"></param>
  89. /// <param name="sqliteConn"></param>
  90. /// <param name="mid"></param>
  91. /// <returns></returns>
  92. public static bool GetMidInfo(string url, string order, string pcmac, string iptvip, string version, SQLiteConnection sqliteConn, out MidList mid)
  93. {
  94. mid = new MidList();
  95. OrderInput orderInput1 = new OrderInput { bid = order, mac = pcmac, version = "", clientType = "" };//"74-27-EA-6E-DA-9F"
  96. string jsonorder = JsonConvert.SerializeObject(orderInput1, Newtonsoft.Json.Formatting.Indented);
  97. HttpHelper http = new HttpHelper();
  98. HttpItem item = new HttpItem()
  99. {
  100. URL = url,//URL这里都是测试URl   必需项
  101. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  102. //Encoding = Encoding.Default,
  103. Method = "post",//URL     可选项 默认为Get
  104. Postdata = jsonorder,
  105. PostEncoding = Encoding.UTF8,
  106. KeepAlive = false
  107. };
  108. item.ContentType = "application/json;charset=utf-8";
  109. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  110. stopwatch.Start();
  111. HttpResult result = http.GetHtml(item);
  112. stopwatch.Stop();
  113. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  114. stopwatch.Reset();
  115. string ParseText = result.Html;
  116. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  117. {
  118. Log.WriteGetKeyLog("\r\nGet MID info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  119. }
  120. else
  121. {
  122. Log.WriteErrorLog("\r\nFail to GetMidInfo:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  123. ReportErrormsg("Fail to GetMidInfo", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  124. return false;
  125. }
  126. try
  127. {
  128. JObject jObject = JObject.Parse(ParseText);
  129. mid.message = jObject["message"].Value<string>();
  130. mid.code = jObject["code"].Value<string>();
  131. mid.factoryname = jObject["factoryName"].Value<string>();
  132. mid.factoryNum = jObject["factoryNum"].Value<string>();
  133. mid.version = jObject["version"].Value<string>();
  134. mid.quantity = jObject["quantity"].Value<string>();
  135. mid.projectid = jObject["projectId"].Value<string>();
  136. mid.clienttype = jObject["clientType"].Value<string>();
  137. mid.host = jObject["host"].Value<string>();
  138. mid.whiteType = jObject["whiteType"].Value<string>();
  139. object a = jObject["rokuCustomer"];
  140. if (jObject["rokuCustomer"].ToString().Length > 0)
  141. mid.rokuCustomer = new RokuCustomer()
  142. {
  143. id = JSON_SeleteNode(jObject["rokuCustomer"], "id").Trim(),
  144. ordernum = JSON_SeleteNode(jObject["rokuCustomer"], "ordernum").Trim(),
  145. brand = JSON_SeleteNode(jObject["rokuCustomer"], "brand").Trim(),
  146. region = JSON_SeleteNode(jObject["rokuCustomer"], "region").Trim(),
  147. oemmodel = JSON_SeleteNode(jObject["rokuCustomer"], "oemmodel").Trim(),
  148. supporturl = JSON_SeleteNode(jObject["rokuCustomer"], "supporturl").Trim(),
  149. supportphone = JSON_SeleteNode(jObject["rokuCustomer"], "supportphone").Trim(),
  150. productiondate = JSON_SeleteNode(jObject["rokuCustomer"], "productiondate").Trim(),
  151. remotetype = JSON_SeleteNode(jObject["rokuCustomer"], "remotetype").Trim(),
  152. updatetime = JSON_SeleteNode(jObject["rokuCustomer"], "updatetime").Trim(),
  153. createtime = JSON_SeleteNode(jObject["rokuCustomer"], "createtime").Trim(),
  154. isdelete = JSON_SeleteNode(jObject["rokuCustomer"], "isdelete").Trim()
  155. };
  156. mid.keytype = new Dictionary<string, string>();
  157. foreach (var ss in jObject["obj"]) //查找某个字段与值
  158. {
  159. mid.keytype.Add(ss["name"].Value<string>(), ss["type"].Value<string>());
  160. }
  161. if (url == Serverurl.Testurl)
  162. mid.host = "http://test.idmanage.qhmoka.com/IDManage";
  163. if (mid.code == "1000")
  164. return true;
  165. else
  166. return false;
  167. }
  168. catch (Exception ex)
  169. {
  170. Log.WriteErrorLog("\r\nFail to parse MID info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  171. ReportErrormsg("Fail to parse MID info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  172. MessageBox.Show("Fail to parse MID info\r\n " + ex.Message);
  173. return false;
  174. }
  175. }
  176. /// <summary>
  177. /// 获取预绑定模式下的key包地址
  178. /// </summary>
  179. /// <param name="midInfo"></param>
  180. /// <param name="order"></param>
  181. /// <param name="sqliteConn"></param>
  182. /// <param name="midAddress"></param>
  183. /// <returns></returns>
  184. public static bool GetMidAddress(MidList midInfo, string order, SQLiteConnection sqliteConn, out MidAddress midAddress)
  185. {
  186. midAddress = new MidAddress();
  187. HttpHelper http = new HttpHelper();
  188. HttpItem item = new HttpItem()
  189. {
  190. URL = midInfo.host + "/getofflinelist.do?",//URL这里都是测试URl   必需项
  191. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  192. //Encoding = Encoding.Default,
  193. Method = "post",//URL     可选项 默认为Get
  194. Postdata = ("orderNumber=" + order + "&mac=" + HttpUtility.UrlEncode(TestMode.MAC)),
  195. KeepAlive = false
  196. };
  197. item.ContentType = "application/x-www-form-urlencoded";
  198. HttpResult result = http.GetHtml(item);
  199. string ParseText = result.Html;
  200. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  201. {
  202. }
  203. else
  204. {
  205. Log.WriteErrorLog("\r\nFail to get Mid address:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  206. CommonMethod.ReportErrormsg("Fail to get Mid address", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  207. return false;
  208. }
  209. try
  210. {
  211. JObject jObject = JObject.Parse(ParseText);
  212. midAddress.des = JSON_SeleteNode(jObject, "des");
  213. midAddress.code = JSON_SeleteNode(jObject, "code");
  214. midAddress.order = JSON_SeleteNode(jObject, "order_number");
  215. midAddress.number = JSON_SeleteNode(jObject, "order_quantity");
  216. midAddress.pid = JSON_SeleteNode(jObject, "project_id");
  217. midAddress.ctype = JSON_SeleteNode(jObject, "client_type");
  218. midAddress.purl = JSON_SeleteNode(jObject, "packet_url");
  219. midAddress.psize = JSON_SeleteNode(jObject, "packet_size");
  220. midAddress.pmd5 = JSON_SeleteNode(jObject, "packet_md5");
  221. midAddress.version = JSON_SeleteNode(jObject, "soft_version");
  222. midAddress.host = midInfo.host;
  223. Log.WriteGetKeyLog("\r\nGet Mid address success:" + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  224. return true;
  225. }
  226. catch (Exception ex)
  227. {
  228. Log.WriteErrorLog("\r\nFail to parse MID address," + ex.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  229. CommonMethod.ReportErrormsg("Fail to parse MID address", ex.Message + "\r\n" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  230. MessageBox.Show("Fail to parse MIDAddress info\r\n " + ex.Message);
  231. return false;
  232. }
  233. }
  234. /// <summary>
  235. /// 获取预绑定模式下的key包地址
  236. /// </summary>
  237. /// <param name="midInfo"></param>
  238. /// <param name="order"></param>
  239. /// <param name="sqliteConn"></param>
  240. /// <param name="midAddress"></param>
  241. /// <returns></returns>
  242. public static bool GetMidAddress2(MidList midInfo, string order, SQLiteConnection sqliteConn, out List<MidAddress> midAddressList)
  243. {
  244. midAddressList = new List<MidAddress>();
  245. HttpHelper http = new HttpHelper();
  246. HttpItem item = new HttpItem()
  247. {
  248. URL = midInfo.host + "/getofflinelist.do?",//URL这里都是测试URl   必需项
  249. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  250. //Encoding = Encoding.Default,
  251. Method = "post",//URL     可选项 默认为Get
  252. Postdata = ("orderNumber=" + order + "&mac=" + HttpUtility.UrlEncode(TestMode.MAC)),
  253. KeepAlive = false
  254. };
  255. item.ContentType = "application/x-www-form-urlencoded";
  256. HttpResult result = http.GetHtml(item);
  257. string ParseText = result.Html;
  258. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  259. {
  260. }
  261. else
  262. {
  263. Log.WriteErrorLog("\r\nFail to get Mid address:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  264. CommonMethod.ReportErrormsg("Fail to get Mid address", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  265. return false;
  266. }
  267. try
  268. {
  269. JObject jObject = JObject.Parse(ParseText);
  270. if (jObject["data"] != null)
  271. {
  272. foreach (var data in jObject["data"])
  273. {
  274. midAddressList.Add(new MidAddress()
  275. {
  276. des = JSON_SeleteNode(jObject, "des"),
  277. code = JSON_SeleteNode(jObject, "code"),
  278. order = data["order_number"].ToObject<string>(),
  279. number = data["order_quantity"].ToObject<string>(),
  280. pid = data["project_id"].ToObject<string>(),
  281. ctype = data["client_type"].ToObject<string>(),
  282. purl = data["packet_url"].ToObject<string>(),
  283. psize = data["packet_size"].ToObject<string>(),
  284. pmd5 = data["packet_md5"].ToObject<string>(),
  285. version = data["soft_version"].ToObject<string>(),
  286. host = midInfo.host
  287. });
  288. }
  289. }
  290. Log.WriteGetKeyLog("\r\nGet Mid address success:" + "\r\n" + result.Html + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  291. return true;
  292. }
  293. catch (Exception ex)
  294. {
  295. Log.WriteErrorLog("\r\nFail to parse MID address," + ex.Message + ":" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata);
  296. CommonMethod.ReportErrormsg("Fail to parse MID address", ex.Message + "\r\n" + ParseText + "\r\nAddress:" + item.URL + "\r\nPostdata:" + item.Postdata, sqliteConn);
  297. MessageBox.Show("Fail to parse MIDAddress info\r\n " + ex.Message);
  298. return false;
  299. }
  300. }
  301. /// <summary>
  302. /// 解析XML格式的DID
  303. /// </summary>
  304. /// <param name="str"></param>
  305. /// <param name="node"></param>
  306. /// <param name="node2"></param>
  307. /// <param name="code"></param>
  308. /// <param name="desc"></param>
  309. /// <param name="value"></param>
  310. /// <returns></returns>
  311. public static bool GetDIDKeyXml(string str, string node, string node2, out string code, out string desc, out string value)
  312. {
  313. code = "";
  314. value = "";
  315. try
  316. {
  317. XmlDocument xml = new XmlDocument();
  318. //xml.Load(path);
  319. xml.LoadXml(str);
  320. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  321. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  322. if (code == "200")
  323. {
  324. value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("deviceid").InnerText;
  325. string CRC32 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText;
  326. if (CrcUtils.CRC32(value) == Convert.ToUInt32(CRC32))
  327. {
  328. return true;
  329. }
  330. else
  331. {
  332. desc = "CRC32 error";
  333. return false;
  334. }
  335. }
  336. return false;
  337. }
  338. catch (Exception ex)
  339. {
  340. desc = ex.Message;
  341. return false;
  342. }
  343. }
  344. /// <summary>
  345. /// 解析其他keys
  346. /// </summary>
  347. /// <param name="url"></param>
  348. /// <param name="keymessage"></param>
  349. /// <param name="keytype"></param>
  350. /// <param name="sqliteConn"></param>
  351. /// <param name="key"></param>
  352. /// <param name="error"></param>
  353. /// <returns></returns>
  354. public static bool GetKeys(string url, string keymessage, string sn, string keytype, SQLiteConnection sqliteConn, out string key, out string error, string order, out string md5)
  355. {
  356. string code;
  357. string desc;
  358. error = "";
  359. key = "";
  360. md5 = "";
  361. HttpHelper http = new HttpHelper();
  362. HttpItem item = new HttpItem()
  363. {
  364. Encoding = Encoding.Default,
  365. Method = "post",
  366. ContentType = "application/x-www-form-urlencoded",
  367. KeepAlive = false
  368. };
  369. // 计算函数耗时;
  370. Stopwatch watch = new Stopwatch();
  371. watch.Start();
  372. switch (keymessage)
  373. {
  374. case "DeviceID":
  375. {
  376. item.URL = url + "/getid.do?";
  377. item.Postdata = "devicetype=tcl_unknown_model" + "&sn=" + sn;
  378. if (sn.Trim().Length > 0)
  379. item.Postdata += "&order=" + order;
  380. HttpResult result = http.GetHtml(item);
  381. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  382. {
  383. Log.WriteGetKeyLog("\r\nGetDeviceID:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  384. if (Xmlconfig.GetDIDKeyXml(result.Html, "response", "id", out code, out desc, out key))
  385. {
  386. //
  387. }
  388. else
  389. {
  390. error = desc;
  391. ReportErrormsg("Fail to parse DID", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  392. return false;
  393. }
  394. }
  395. else
  396. {
  397. error = result.StatusDescription;
  398. ReportErrormsg("Fail to get DID", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  399. return false;
  400. }
  401. break;
  402. }
  403. case "MAC":
  404. {
  405. item.URL = url + "/getmac.do?";
  406. item.Postdata = "typeString=" + keytype + "&sn=" + sn;
  407. if (sn.Trim().Length > 0)
  408. item.Postdata += "&order=" + order;
  409. HttpResult result = http.GetHtml(item);
  410. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  411. {
  412. Log.WriteGetKeyLog("\r\nGetMac:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  413. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "mac", out code, out desc, out key, out md5))
  414. {
  415. }
  416. else
  417. {
  418. error = desc;
  419. ReportErrormsg("Fail to parse Mac", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  420. return false;
  421. }
  422. }
  423. else
  424. {
  425. error = result.StatusDescription;
  426. ReportErrormsg("Fail to get Mac", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  427. return false;
  428. }
  429. break;
  430. }
  431. case "HDCP_KEY":
  432. {
  433. item.URL = url + "/gethdcpkey.do?";
  434. item.Postdata = "type=" + keytype + "&sn=" + sn;
  435. if (sn.Trim().Length > 0)
  436. item.Postdata += "&order=" + order;
  437. HttpResult result = http.GetHtml(item);
  438. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  439. {
  440. Log.WriteGetKeyLog("\r\nGetHDCP_KEY:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  441. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "key", out code, out desc, out key, out md5))
  442. {
  443. }
  444. else
  445. {
  446. error = desc;
  447. ReportErrormsg("Fail to parse hdcp", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  448. return false;
  449. }
  450. }
  451. else
  452. {
  453. error = result.StatusDescription;
  454. ReportErrormsg("Fail to get hdcp", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  455. return false;
  456. }
  457. break;
  458. }
  459. case "CI_PLUS_KEY":
  460. {
  461. item.URL = url + "/getcikey.do?";
  462. item.Postdata = "type=" + keytype + "&sn=" + sn;
  463. if (sn.Trim().Length > 0)
  464. item.Postdata += "&order=" + order;
  465. HttpResult result = http.GetHtml(item);
  466. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  467. {
  468. Log.WriteGetKeyLog("\r\nGetCI_PLUS_KEY:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  469. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "key", out code, out desc, out key, out md5))
  470. {
  471. }
  472. else
  473. {
  474. error = desc;
  475. ReportErrormsg("Fail to parse ciplus", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  476. return false;
  477. }
  478. }
  479. else
  480. {
  481. error = result.StatusDescription;
  482. ReportErrormsg("Fail to get ciplus", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  483. return false;
  484. }
  485. break;
  486. }
  487. case "WiDi":
  488. {
  489. item.URL = url + "/getwidi.do?";
  490. item.Postdata = "type=" + keytype + "&sn=" + sn;
  491. if (sn.Trim().Length > 0)
  492. item.Postdata += "&order=" + order;
  493. HttpResult result = http.GetHtml(item);
  494. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  495. {
  496. Log.WriteGetKeyLog("\r\nGetWiDi:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  497. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "key", out code, out desc, out key, out md5))
  498. {
  499. }
  500. else
  501. {
  502. error = desc;
  503. ReportErrormsg("Fail to parse widi", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  504. return false;
  505. }
  506. }
  507. else
  508. {
  509. error = result.StatusDescription;
  510. ReportErrormsg("Fail to get widi", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  511. return false;
  512. }
  513. break;
  514. }
  515. case "HDCP2.2_KEY":
  516. {
  517. item.URL = url + "/gethdcpkey2.do?";
  518. item.Postdata = "type=" + keytype + "&sn=" + sn;
  519. if (sn.Trim().Length > 0)
  520. item.Postdata += "&order=" + order;
  521. HttpResult result = http.GetHtml(item);
  522. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  523. {
  524. Log.WriteGetKeyLog("\r\nGetHDCP2.2_KEY:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  525. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "key", out code, out desc, out key, out md5))
  526. {
  527. }
  528. else
  529. {
  530. error = desc;
  531. ReportErrormsg("Fail to parse hdcp22", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  532. return false;
  533. }
  534. }
  535. else
  536. {
  537. error = result.StatusDescription;
  538. ReportErrormsg("Fail to get hdcp22", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  539. return false;
  540. }
  541. break;
  542. }
  543. case "NETFILX_ESN":
  544. {
  545. item.URL = url + "/getnetfilxesn.do?";
  546. item.Postdata = "type=" + keytype + "&sn=" + sn;
  547. if (sn.Trim().Length > 0)
  548. item.Postdata += "&order=" + order;
  549. HttpResult result = http.GetHtml(item);
  550. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  551. {
  552. Log.WriteGetKeyLog("\r\nGetNETFILX_ESN:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  553. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "esn", out code, out desc, out key, out md5))
  554. {
  555. }
  556. else
  557. {
  558. error = desc;
  559. ReportErrormsg("Fail to parse esn", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  560. return false;
  561. }
  562. }
  563. else
  564. {
  565. error = result.StatusDescription;
  566. ReportErrormsg("Fail to get esn", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  567. return false;
  568. }
  569. break;
  570. }
  571. case "MGK_KEY":
  572. {
  573. item.URL = url + "/getmgk.do?";
  574. item.Postdata = "type=" + keytype + "&sn=" + sn;
  575. if (sn.Trim().Length > 0)
  576. item.Postdata += "&order=" + order;
  577. HttpResult result = http.GetHtml(item);
  578. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  579. {
  580. Log.WriteGetKeyLog("\r\nGetMGK:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  581. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "key", out code, out desc, out key, out md5))
  582. {
  583. }
  584. else
  585. {
  586. error = desc;
  587. ReportErrormsg("Fail to parse MGK", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  588. return false;
  589. }
  590. }
  591. else
  592. {
  593. error = result.StatusDescription;
  594. ReportErrormsg("Fail to get MGK", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  595. return false;
  596. }
  597. break;
  598. }
  599. case "Fairplay_KEY":
  600. {
  601. item.URL = url + "/getfairplay.do?";
  602. item.Postdata = "type=" + keytype + "&sn=" + sn;
  603. if (sn.Trim().Length > 0)
  604. item.Postdata += "&order=" + order;
  605. HttpResult result = http.GetHtml(item);
  606. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  607. {
  608. Log.WriteGetKeyLog("\r\nGetFairplay:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  609. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "fairplay", out code, out desc, out key, out md5))
  610. {
  611. }
  612. else
  613. {
  614. error = desc;
  615. ReportErrormsg("Fail to parse Fairplay", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  616. return false;
  617. }
  618. }
  619. else
  620. {
  621. error = result.StatusDescription;
  622. ReportErrormsg("Fail to get Fairplay", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  623. return false;
  624. }
  625. break;
  626. }
  627. case "ECP_KEY":
  628. {
  629. item.URL = url + "/getecp.do?";
  630. item.Postdata = "type=" + keytype + "&sn=" + sn;
  631. if (sn.Trim().Length > 0)
  632. item.Postdata += "&order=" + order;
  633. HttpResult result = http.GetHtml(item);
  634. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  635. {
  636. Log.WriteGetKeyLog("\r\nGetecp:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  637. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "ecp", out code, out desc, out key, out md5))
  638. {
  639. }
  640. else
  641. {
  642. error = desc;
  643. ReportErrormsg("Fail to parse ecp", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  644. return false;
  645. }
  646. }
  647. else
  648. {
  649. error = result.StatusDescription;
  650. ReportErrormsg("Fail to get ecp", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  651. return false;
  652. }
  653. break;
  654. }
  655. case "LEK":
  656. {
  657. item.URL = url + "/getlek.do?";
  658. item.Postdata = "type=" + keytype + "&sn=" + sn;
  659. if (sn.Trim().Length > 0)
  660. item.Postdata += "&order=" + order;
  661. HttpResult result = http.GetHtml(item);
  662. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  663. {
  664. Log.WriteGetKeyLog("\r\nGetlek:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  665. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "lek", out code, out desc, out key, out md5))
  666. {
  667. }
  668. else
  669. {
  670. error = desc;
  671. ReportErrormsg("Fail to parse lek", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  672. return false;
  673. }
  674. }
  675. else
  676. {
  677. error = result.StatusDescription;
  678. ReportErrormsg("Fail to get lek", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  679. return false;
  680. }
  681. break;
  682. }
  683. case "PEK":
  684. {
  685. item.URL = url + "/getpek.do?";
  686. item.Postdata = "type=" + keytype + "&sn=" + sn;
  687. if (sn.Trim().Length > 0)
  688. item.Postdata += "&order=" + order;
  689. HttpResult result = http.GetHtml(item);
  690. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  691. {
  692. Log.WriteGetKeyLog("\r\nGetpek:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  693. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "pek", out code, out desc, out key, out md5))
  694. {
  695. }
  696. else
  697. {
  698. error = desc;
  699. ReportErrormsg("Fail to parse pek", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  700. return false;
  701. }
  702. }
  703. else
  704. {
  705. error = result.StatusDescription;
  706. ReportErrormsg("Fail to get pek", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  707. return false;
  708. }
  709. break;
  710. }
  711. case "PlayReady_key":
  712. {
  713. item.URL = url + "/getplayready.do?";
  714. item.Postdata = "type=" + keytype + "&sn=" + sn;
  715. if (sn.Trim().Length > 0)
  716. item.Postdata += "&order=" + order;
  717. HttpResult result = http.GetHtml(item);
  718. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  719. {
  720. Log.WriteGetKeyLog("\r\nGetplayready:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  721. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "playReady", out code, out desc, out key, out md5))
  722. {
  723. }
  724. else
  725. {
  726. error = desc;
  727. ReportErrormsg("Fail to parse playready", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  728. return false;
  729. }
  730. }
  731. else
  732. {
  733. error = result.StatusDescription;
  734. ReportErrormsg("Fail to get playready", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  735. return false;
  736. }
  737. break;
  738. }
  739. case "Hashkey":
  740. {
  741. item.URL = url + "/gethash.do?";
  742. item.Postdata = "type=" + keytype + "&sn=" + sn;
  743. if (sn.Trim().Length > 0)
  744. item.Postdata += "&order=" + order;
  745. HttpResult result = http.GetHtml(item);
  746. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  747. {
  748. Log.WriteGetKeyLog("\r\nGethash:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  749. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "hash", out code, out desc, out key, out md5))
  750. {
  751. }
  752. else
  753. {
  754. error = desc;
  755. ReportErrormsg("Fail to parse hash", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  756. return false;
  757. }
  758. }
  759. else
  760. {
  761. error = result.StatusDescription;
  762. ReportErrormsg("Fail to get hash", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  763. return false;
  764. }
  765. break;
  766. }
  767. case "YouTube_KEY":
  768. {
  769. item.URL = url + "/getYoutubeKey.do?";
  770. item.Postdata = "type=" + keytype + "&sn=" + sn;
  771. if (sn.Trim().Length > 0)
  772. item.Postdata += "&order=" + order;
  773. HttpResult result = http.GetHtml(item);
  774. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  775. {
  776. Log.WriteGetKeyLog("\r\nGet youtube key:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  777. if (Xmlconfig.GetOthersKeyXml(result.Html, "response", "youtubeKey", out code, out desc, out key, out md5))
  778. {
  779. }
  780. else
  781. {
  782. error = desc;
  783. ReportErrormsg("Fail to parse youtube key", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  784. return false;
  785. }
  786. }
  787. else
  788. {
  789. error = result.StatusDescription;
  790. ReportErrormsg("Fail to get youtube key", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  791. return false;
  792. }
  793. break;
  794. }
  795. }
  796. watch.Stop();
  797. Log.WriteInfoLog(string.Format("[GetKeys] order={0},sn={1},keytype={2},keymessage={3}, Elapsed={4}", order,sn,keytype,keymessage,watch.Elapsed.TotalMilliseconds));
  798. return true;
  799. }
  800. public static bool GetWidevineAnAttestationKey(
  801. string url,
  802. string sn,
  803. string keytype,
  804. SQLiteConnection sqliteConn,
  805. out bool isAndroidTV,
  806. out string Widevinekey,
  807. out string Attestationkey,
  808. out string error,
  809. string order,
  810. out string AttestationMd5)
  811. {
  812. string code;
  813. string desc;
  814. isAndroidTV = false;
  815. error = "";
  816. Widevinekey = "";
  817. Attestationkey = "";
  818. AttestationMd5 = "";
  819. HttpHelper http = new HttpHelper();
  820. HttpItem item = new HttpItem()
  821. {
  822. Encoding = Encoding.Default,
  823. Method = "post",
  824. ContentType = "application/x-www-form-urlencoded",
  825. KeepAlive = false
  826. };
  827. // 计算函数耗时;
  828. Stopwatch watch = new Stopwatch();
  829. watch.Start();
  830. item.URL = url + "/getWidevineAndAttestation.do?";
  831. item.Postdata = "type=" + keytype + "&order=" + order + "&sn=" + sn;
  832. HttpResult result = http.GetHtml(item);
  833. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  834. {
  835. Log.WriteGetKeyLog("\r\nGetWidevine_KEY:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  836. if (!Xmlconfig.GetWidevineAndAttestationKeyXml(result.Html, "response", out code, out desc, out isAndroidTV, out Widevinekey, out Attestationkey, out AttestationMd5))
  837. {
  838. error = desc;
  839. ReportErrormsg("Fail to parse widevineAndAttestation", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  840. return false;
  841. }
  842. }
  843. else
  844. {
  845. error = result.StatusDescription;
  846. ReportErrormsg("Fail to get widevineAndAttestation", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  847. return false;
  848. }
  849. watch.Stop();
  850. Log.WriteInfoLog(string.Format("[getWidevineAndAttestation] order={0},sn={1},keytype={2},Elapsed={3}", order, sn, keytype, watch.Elapsed.TotalMilliseconds));
  851. return true;
  852. }
  853. public static bool GetAcasKey(string url, string keymessage, string sn, string keytype, SQLiteConnection sqliteConn, string order, out string acaskey_data, out string acaskey_datamd5, out string acaskey_tool, out string acaskey_toolmd5, out string error)
  854. {
  855. string code;
  856. string desc;
  857. error = "";
  858. acaskey_data = "";
  859. acaskey_datamd5 = "";
  860. acaskey_tool = "";
  861. acaskey_toolmd5 = "";
  862. HttpHelper http = new HttpHelper();
  863. HttpItem item = new HttpItem()
  864. {
  865. Encoding = Encoding.Default,
  866. Method = "post",
  867. ContentType = "application/x-www-form-urlencoded",
  868. KeepAlive = false
  869. };
  870. // 计算函数耗时;
  871. Stopwatch watch = new Stopwatch();
  872. watch.Start();
  873. item.URL = url + "/getAcasKey.do?";
  874. item.Postdata = "type=" + keytype + "&sn=" + sn;
  875. if (sn.Trim().Length > 0)
  876. item.Postdata += "&order=" + order;
  877. HttpResult result = http.GetHtml(item);
  878. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  879. {
  880. Log.WriteGetKeyLog("\r\nGet ACAS key:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  881. if (Xmlconfig.Get2KeyInfoFromXml(result.Html, "acasKey_data", "acasKey_tool", out code, out desc, out acaskey_data, out acaskey_datamd5, out acaskey_tool, out acaskey_toolmd5))
  882. {
  883. }
  884. else
  885. {
  886. error = desc;
  887. ReportErrormsg("Fail to parse ACAS key", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  888. return false;
  889. }
  890. }
  891. else
  892. {
  893. error = result.StatusDescription;
  894. ReportErrormsg("Fail to get ACAS key", error + "\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html, sqliteConn);
  895. return false;
  896. }
  897. watch.Stop();
  898. Log.WriteInfoLog(string.Format("[getAcasKey] order={0},sn={1},keytype={2},Elapsed={3}", order, sn, keytype, watch.Elapsed.TotalMilliseconds));
  899. return true;
  900. }
  901. /// <summary>
  902. /// 异常上报
  903. /// </summary>
  904. /// <param name="reportType"></param>
  905. /// <param name="reportData"></param>
  906. /// <param name="sqliteConn"></param>
  907. /// <returns></returns>
  908. public static bool ReportErrormsg(string reportType, string reportData, SQLiteConnection sqliteConn)
  909. {
  910. // 计算函数耗时;
  911. Stopwatch watch = new Stopwatch();
  912. watch.Start();
  913. ErrorInfo ErrorInfo1 = new ErrorInfo { reportType = reportType, reportData = reportData.Replace("'", ""), version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), factory = FactoryName, mac = TestMode.MAC, reportTime = System.DateTime.Now.ToString(("yyyy-MM-dd HH:mm:ss")) };
  914. string jsonError = Newtonsoft.Json.JsonConvert.SerializeObject(ErrorInfo1, Newtonsoft.Json.Formatting.Indented);
  915. HttpHelper http = new HttpHelper();
  916. HttpItem item = new HttpItem()
  917. {
  918. URL = TestMode.errorreportServerurl,//URL这里都是测试URl   必需项
  919. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  920. //Encoding = Encoding.Default,
  921. Method = "post",//URL     可选项 默认为Get
  922. Postdata = jsonError,
  923. PostEncoding = Encoding.UTF8,
  924. KeepAlive = false
  925. };
  926. item.ContentType = "application/json;charset=utf-8";
  927. HttpResult result = http.GetHtml(item);
  928. string ParseText = result.Html;
  929. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  930. {
  931. Log.WriteGetKeyLog("\r\nReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  932. }
  933. else
  934. {
  935. if (HTTPChecker(item.URL))
  936. SQLiteHelper.InsertDelayErrorReport(sqliteConn, new object[] { item.URL, item.Postdata });
  937. Log.WriteErrorLog("\r\nFail to ReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  938. return false;
  939. }
  940. try
  941. {
  942. JObject jObject = JObject.Parse(ParseText);
  943. string message = jObject["message"].Value<string>();
  944. string code = jObject["code"].Value<string>();
  945. if (code != "1000")
  946. {
  947. Log.WriteErrorLog("\r\nError report return error:\r\n" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  948. return false;
  949. }
  950. watch.Stop();
  951. Log.WriteInfoLog(string.Format("[ReportErrormsg] Elapsed={0}", watch.Elapsed.TotalMilliseconds));
  952. return true;
  953. }
  954. catch (Exception ex)
  955. {
  956. Log.WriteErrorLog("\r\nFail to parse Error report return info," + ex.Message + ":" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + jsonError);
  957. return false;
  958. }
  959. }
  960. /// <summary>
  961. /// 上报预绑定包下载结果
  962. /// </summary>
  963. /// <param name="url"></param>
  964. /// <param name="order"></param>
  965. /// <param name="sqliteConn"></param>
  966. /// <returns></returns>
  967. public static bool ReportDownloadStatus(string url, string order, SQLiteConnection sqliteConn)
  968. {
  969. HttpHelper http = new HttpHelper();
  970. HttpItem item = new HttpItem()
  971. {
  972. URL = url + "/getreportofflinesn.do?",//URL这里都是测试URl   必需项
  973. Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  974. //Encoding = Encoding.Default,
  975. Method = "post",//URL     可选项 默认为Get
  976. Postdata = ("orderNumber =" + order),
  977. KeepAlive = false
  978. };
  979. item.ContentType = "application/x-www-form-urlencoded";
  980. HttpResult result = http.GetHtml(item);
  981. string ParseText = result.Html;
  982. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  983. {
  984. //
  985. }
  986. else
  987. {
  988. Log.WriteErrorLog("\r\nFail to report download status:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  989. CommonMethod.ReportErrormsg("Fail to report download status", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  990. return false;
  991. }
  992. try
  993. {
  994. JObject jObject = JObject.Parse(ParseText);
  995. string des = JSON_SeleteNode(jObject, "des");
  996. string code = JSON_SeleteNode(jObject, "code");
  997. string data = JSON_SeleteNode(jObject, "data");
  998. if (code == "1000")
  999. return true;
  1000. else
  1001. {
  1002. Log.WriteErrorLog("\r\nError report download status,should be 1000:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  1003. CommonMethod.ReportErrormsg("Error download status code return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  1004. return false;
  1005. }
  1006. }
  1007. catch (Exception ex)
  1008. {
  1009. MessageBox.Show(ex.Message);
  1010. Log.WriteErrorLog("\r\n" + ex.Message + ":" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order));
  1011. CommonMethod.ReportErrormsg("Error parse download status return", result.StatusDescription + result.Html + "\r\nAddress:" + url + "/getreportofflinesn.do?" + "\r\nPostdata:" + ("orderNumber =" + order), sqliteConn);
  1012. return false;
  1013. }
  1014. }
  1015. /// <summary>
  1016. /// 生成DB格式key json字符串
  1017. /// </summary>
  1018. /// <param name="keyInfo"></param>
  1019. /// <param name="keydata"></param>
  1020. /// <param name="error"></param>
  1021. /// <returns></returns>
  1022. public static bool CreateDBkeyString(KeyInfo keyInfo, out string keydata, out string error)
  1023. {
  1024. keydata = "";
  1025. error = "";
  1026. try
  1027. {
  1028. DBKeysStringFormat DBKeysStringFormat1 = new DBKeysStringFormat { cikey = keyInfo.CI_plus == null ? "" : keyInfo.CI_plus, esn = keyInfo.ESN == null ? "" : keyInfo.ESN, hdcp = keyInfo.HDCP == null ? "" : keyInfo.HDCP, widevine = keyInfo.Widevine == null ? "" : keyInfo.Widevine, hdcp22 = keyInfo.HDCP22 == null ? "" : keyInfo.HDCP22, did = keyInfo.DID == null ? "" : keyInfo.DID, mac = keyInfo.Mac == null ? "" : keyInfo.Mac, widi = keyInfo.WiDi == null ? "" : keyInfo.WiDi, attestation = keyInfo.Attestation == null ? "" : keyInfo.Attestation, mgk = keyInfo.MGK == null ? "" : keyInfo.MGK };
  1029. keydata = Newtonsoft.Json.JsonConvert.SerializeObject(DBKeysStringFormat1, Newtonsoft.Json.Formatting.Indented);
  1030. return true;
  1031. }
  1032. catch (Exception ex)
  1033. {
  1034. error = ex.Message;
  1035. return false;
  1036. }
  1037. }
  1038. /// <summary>
  1039. /// 获取相应json子节点的值
  1040. /// </summary>
  1041. /// <param name="childnodelist"></param>
  1042. public static string JSON_SeleteNode(JToken json, string ReName)
  1043. {
  1044. try
  1045. {
  1046. string result = "";
  1047. //这里6.0版块可以用正则匹配
  1048. var node = json.SelectToken("$.." + ReName);
  1049. if (node != null)
  1050. {
  1051. //判断节点类型
  1052. if (node.Type == JTokenType.String || node.Type == JTokenType.Integer || node.Type == JTokenType.Float)
  1053. {
  1054. //返回string值
  1055. result = node.Value<object>().ToString();
  1056. }
  1057. }
  1058. return result;
  1059. }
  1060. catch (Exception ex)
  1061. {
  1062. return "";
  1063. }
  1064. }
  1065. /// <summary>
  1066. /// 确认是否为有效的url(检测开头是否为http://或者https://)
  1067. /// </summary>
  1068. /// <param name="Value"></param>
  1069. /// <returns></returns>
  1070. public static bool HTTPChecker(string Value)
  1071. {
  1072. try
  1073. {
  1074. if ((Value.Substring(0, 7) == "http://") || (Value.Substring(0, 8) == "https://"))
  1075. {
  1076. return true;
  1077. }
  1078. else
  1079. return false;
  1080. }
  1081. catch (Exception ex)
  1082. {
  1083. return false;
  1084. }
  1085. }
  1086. /// <summary>
  1087. /// 检查是否能正常访问网络
  1088. /// </summary>
  1089. /// <param name="url"></param>
  1090. /// <returns></returns>
  1091. public static bool CheckUrlConnection(string url)
  1092. {
  1093. try
  1094. {
  1095. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  1096. myRequest.Method = "HEAD"; //设置提交方式可以为"get","head"等
  1097. myRequest.Timeout = 10000; //设置网页响应时间长度
  1098. myRequest.AllowAutoRedirect = false;//是否允许自动重定向
  1099. myRequest.UseDefaultCredentials = true;
  1100. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  1101. myRequest.UseDefaultCredentials = true;//权限设置
  1102. return (myResponse.StatusCode == HttpStatusCode.OK);//返回响应的状态
  1103. }
  1104. catch (Exception ex)
  1105. {
  1106. return false;
  1107. }
  1108. }
  1109. /// <summary>
  1110. /// 指定目录创建文件夹
  1111. /// </summary>
  1112. /// <param name="path"></param>
  1113. public static void CreateDirectory(string path)
  1114. {
  1115. if (!Directory.Exists(path))//如果不存在就创建file文件夹
  1116. {
  1117. Directory.CreateDirectory(path);
  1118. }
  1119. }
  1120. /// <summary>
  1121. /// 在线下载抄写模式抄写的上报
  1122. /// </summary>
  1123. /// <param name="content"></param>
  1124. /// <returns></returns>
  1125. public static bool UploadCopyResult(string content, string url, SQLiteConnection sqliteConn, string order, bool repeatupload, string id, int timeout)
  1126. {
  1127. if (timeout < 5000)
  1128. timeout = 5000;
  1129. HttpHelper http = new HttpHelper();
  1130. HttpItem item = new HttpItem()
  1131. {
  1132. Encoding = Encoding.UTF8,
  1133. Method = "post",
  1134. ContentType = "application/x-www-form-urlencoded",
  1135. Timeout = timeout,
  1136. KeepAlive = false
  1137. };
  1138. item.PostEncoding = Encoding.UTF8;
  1139. item.URL = url;
  1140. item.Postdata = content;
  1141. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  1142. stopwatch.Start();
  1143. HttpResult result = http.GetHtml(item);
  1144. stopwatch.Stop();
  1145. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  1146. stopwatch.Reset();
  1147. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1148. {
  1149. if (Xmlconfig.ReportXml(result.Html, "response", out string code, out string desc))
  1150. {
  1151. Log.WriteGetKeyLog("\r\nUpload copy status success:\r\nUsing time:" + usingtime + "ms\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  1152. return true;
  1153. }
  1154. else
  1155. {
  1156. CommonMethod.ReportErrormsg("Fail to parse copy result return", desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, sqliteConn);
  1157. Log.WriteErrorLog(code + "Error");
  1158. if (repeatupload)
  1159. {
  1160. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1161. }
  1162. return false;
  1163. }
  1164. }
  1165. else
  1166. {
  1167. if (repeatupload)
  1168. return false;
  1169. if (CommonMethod.HTTPChecker(item.URL))
  1170. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { order, item.URL, item.Postdata });
  1171. CommonMethod.ReportErrormsg("Fail to upload copy result", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, sqliteConn);
  1172. return true;
  1173. }
  1174. }
  1175. /// <summary>
  1176. /// 本地DB存储的异常重新上报
  1177. /// </summary>
  1178. /// <param name="reportType"></param>
  1179. /// <param name="reportData"></param>
  1180. /// <param name="sqliteConn"></param>
  1181. /// <returns></returns>
  1182. public static bool ReportErrormsg2(string url, string content, out bool mark)
  1183. {
  1184. mark = false;
  1185. HttpHelper http = new HttpHelper();
  1186. HttpItem item = new HttpItem()
  1187. {
  1188. URL = url,//URL这里都是测试URl   必需项
  1189. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1190. //Encoding = Encoding.Default,
  1191. Method = "post",//URL     可选项 默认为Get
  1192. Postdata = content,
  1193. PostEncoding = Encoding.UTF8,
  1194. KeepAlive = false
  1195. };
  1196. item.ContentType = "application/json;charset=utf-8";
  1197. HttpResult result = http.GetHtml(item);
  1198. string ParseText = result.Html;
  1199. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1200. {
  1201. mark = true;
  1202. Log.WriteGetKeyLog("\r\nReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1203. }
  1204. else
  1205. {
  1206. Log.WriteErrorLog("\r\nFail to ReportErrormsg:\r\n" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1207. return false;
  1208. }
  1209. try
  1210. {
  1211. JObject jObject = JObject.Parse(ParseText);
  1212. string message = jObject["message"].Value<string>();
  1213. string code = jObject["code"].Value<string>();
  1214. if (code == "1000")
  1215. return true;
  1216. else
  1217. {
  1218. Log.WriteErrorLog("\r\nError report return error:\r\n" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1219. return false;
  1220. }
  1221. }
  1222. catch (Exception ex)
  1223. {
  1224. Log.WriteErrorLog("\r\nFail to parse Error report return info," + ex.Message + ":" + ParseText + "\r\nAddress:" + TestMode.errorreportServerurl + "\r\nPostdata:" + content);
  1225. return false;
  1226. }
  1227. }
  1228. /// <summary>
  1229. /// 判断目标字符串是否只有数字和字母
  1230. /// </summary>
  1231. /// <param name="value">目标字符串</param>
  1232. /// <returns></returns>
  1233. public static bool IsNumberAndWord(string value)
  1234. {
  1235. if (value is null)
  1236. return false;
  1237. Regex r = new Regex(@"^[a-zA-Z0-9]+$");
  1238. return r.Match(value).Success;
  1239. }
  1240. public static bool InsertJsonConfig(string node, string key)
  1241. {
  1242. JObject jObject;
  1243. using (StreamReader file = new StreamReader(LocalPath.localpath + "\\Config.json"))
  1244. {
  1245. using (JsonTextReader reader = new JsonTextReader(file))
  1246. {
  1247. jObject = (JObject)JToken.ReadFrom(reader);
  1248. jObject[node][key] = true;
  1249. }
  1250. }
  1251. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  1252. {
  1253. writer.Write(jObject.ToString());
  1254. }
  1255. return true;
  1256. }
  1257. public static bool AddJsonConfig(string key, object value)
  1258. {
  1259. JObject jObject;
  1260. using (StreamReader file = new StreamReader(LocalPath.localpath + "\\Config.json"))
  1261. {
  1262. using (JsonTextReader reader = new JsonTextReader(file))
  1263. {
  1264. jObject = (JObject)JToken.ReadFrom(reader);
  1265. jObject.Add(key, (JToken)value);
  1266. }
  1267. }
  1268. using (StreamWriter writer = new StreamWriter(LocalPath.localpath + "\\Config.json"))
  1269. {
  1270. writer.Write(jObject.ToString());
  1271. }
  1272. return false;
  1273. }
  1274. /// <summary>
  1275. /// 预下载模式抄写的上报
  1276. /// </summary>
  1277. /// <param name="copydate">烧录时间</param>
  1278. /// <returns></returns>
  1279. public static bool UploadPreloadCopyResult(string host, string copydate, string SN, SQLiteConnection localDBNow, SQLiteConnection sqliteConn, bool repeatupload, string id, int timeout)
  1280. {
  1281. string ParseText = "";
  1282. bool status;
  1283. try
  1284. {
  1285. List<PostDateClass> postList = new List<PostDateClass>();
  1286. postList.Add(new PostDateClass("sn", SN));
  1287. postList.Add(new PostDateClass("date", copydate));
  1288. ParseText = FormPost.postFileMessage(host + "/reportlist.do?", postList, out status, timeout);
  1289. if (status)
  1290. {
  1291. JObject jObject = JObject.Parse(ParseText);
  1292. string des = CommonMethod.JSON_SeleteNode(jObject, "des");
  1293. string code = CommonMethod.JSON_SeleteNode(jObject, "code");
  1294. string data = JSON_SeleteNode(jObject, "data");
  1295. if (code == "1000")
  1296. {
  1297. //更新上报内容
  1298. if (SQLiteHelper.UpdateReportStatus(localDBNow, SN))
  1299. {
  1300. //更新成功
  1301. Log.WriteGetKeyLog("\r\nUpdateReportStatus success:\r\n" + host + "/reportlist.do?\r\n" + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText);
  1302. return true;
  1303. }
  1304. else
  1305. {
  1306. Log.WriteErrorLog("\r\nFail to UpdateReportStatus: " + SN);
  1307. return false;
  1308. }
  1309. }
  1310. else
  1311. {
  1312. CommonMethod.ReportErrormsg("Fail to UploadPreloadCopyResult", "Address: " + host + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate + "\r\n" + ParseText, sqliteConn);
  1313. Log.WriteErrorLog("\r\nUpdateReportStatus code return error,should be 1000 " + "\r\n" + SN + ":" + ParseText + "\r\nAddress:" + host + "/reportlist.do?" + "\r\nPostdata: " + "sn=" + SN + "\r\ndate=" + copydate);
  1314. if (repeatupload)
  1315. {
  1316. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1317. }
  1318. return false;
  1319. }
  1320. }
  1321. else
  1322. {
  1323. if (repeatupload)
  1324. return false;
  1325. if (CommonMethod.HTTPChecker(host))
  1326. SQLiteHelper.InsertDelayCopyReport(sqliteConn, new object[] { "preload_mode", host, SN + "@" + copydate });
  1327. return false;
  1328. }
  1329. }
  1330. catch (Exception ex)
  1331. {
  1332. Log.WriteErrorLog("\r\nFail to parse UpdateReportStatus return: " + ex.Message + "\r\n" + SN + ":" + ParseText);
  1333. return false;
  1334. }
  1335. }
  1336. /// <summary>
  1337. /// 预下载模式抄写的批量上报
  1338. /// </summary>
  1339. /// <param name="host"></param>
  1340. /// <param name="postList"></param>
  1341. /// <param name="localDBNow"></param>
  1342. /// <param name="sqliteConn"></param>
  1343. /// <param name="repeatupload"></param>
  1344. /// <param name="id"></param>
  1345. /// <returns></returns>
  1346. public static bool UploadPreloadCopyResultList(string host, List<PostDateClass> postList, SQLiteConnection localDBNow, SQLiteConnection sqliteConn, bool repeatupload, string id)
  1347. {
  1348. string ParseText = "";
  1349. bool status;
  1350. try
  1351. {
  1352. ParseText = FormPost.postFileMessage(host + "/reportlist.do?", postList, out status, 30000);
  1353. if (status)
  1354. {
  1355. JObject jObject = JObject.Parse(ParseText);
  1356. string des = CommonMethod.JSON_SeleteNode(jObject, "des");
  1357. string code = CommonMethod.JSON_SeleteNode(jObject, "code");
  1358. string data = JSON_SeleteNode(jObject, "data");
  1359. if (code == "1000")
  1360. {
  1361. Log.WriteGetKeyLog("UploadPreloadCopyResult successful\r\nAddress: " + host + "/reportlist.do?" + ParseText);
  1362. return true;
  1363. }
  1364. else
  1365. {
  1366. CommonMethod.ReportErrormsg("Fail to UploadPreloadCopyResult", "Address: " + host + "/reportlist.do?" + ParseText, sqliteConn);
  1367. Log.WriteErrorLog("\r\nUpdateReportStatus code return error,should be 1000 " + "\r\n" + ParseText + "\r\nAddress:" + host + "/reportlist.do?");
  1368. if (repeatupload)
  1369. {
  1370. SQLiteHelper.DeleteDelayCopyReport(sqliteConn, id);
  1371. }
  1372. return false;
  1373. }
  1374. }
  1375. else
  1376. {
  1377. Log.WriteErrorLog("Fail to UploadPreloadCopyResult\r\nAddress: " + host + "/reportlist.do?" + ParseText);
  1378. return false;
  1379. }
  1380. }
  1381. catch (Exception ex)
  1382. {
  1383. Log.WriteErrorLog("\r\nFail to parse UpdateReportStatus return: " + ex.Message + "\r\n" + ParseText);
  1384. return false;
  1385. }
  1386. }
  1387. /// <summary>
  1388. /// byte[]数组到int转换
  1389. /// </summary>
  1390. /// <param name="value"></param>
  1391. /// <returns></returns>
  1392. public static int BytelisttoInt(byte[] value)
  1393. {
  1394. Array.Reverse(value);
  1395. int result = 0;
  1396. for (int i = 0; i < value.Length; i++)
  1397. {
  1398. result += value[i] << i * 8;
  1399. }
  1400. return result;
  1401. }
  1402. /// <summary>
  1403. /// int类型到byte[]数组转换
  1404. /// </summary>
  1405. /// <param name="value"></param>
  1406. /// <returns></returns>
  1407. public static byte[] InttoBytelist(int value)
  1408. {
  1409. byte[] bytes = BitConverter.GetBytes(value);
  1410. Array.Reverse(bytes);
  1411. if (value == 0)
  1412. return bytes;
  1413. List<byte> list = new List<byte>();
  1414. list.AddRange(bytes);
  1415. while (true)
  1416. {
  1417. if (list[0] != 0)
  1418. break;
  1419. else
  1420. list.RemoveAt(0);
  1421. }
  1422. bytes = list.ToArray();
  1423. return bytes;
  1424. }
  1425. /// <summary>
  1426. /// 升级检查
  1427. /// </summary>
  1428. /// <param name="url"></param>
  1429. /// <param name="FactoryNum"></param>
  1430. /// <param name="sqliteConn"></param>
  1431. /// <param name="upmid"></param>
  1432. /// <returns></returns>
  1433. public static bool UpgradeCheck(string url, string FactoryNum, SQLiteConnection sqliteConn, out UpgradeMsgList upmid)
  1434. {
  1435. upmid = new UpgradeMsgList();
  1436. UpgradeCheckInput UpgradeCheckInput1 = new UpgradeCheckInput { appid = "SCBC_Factory_Tools", devicemodel = "SCBC_Factory_Tools", dnum = FactoryNum, ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), type = "pc" };//System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()
  1437. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(UpgradeCheckInput1, Newtonsoft.Json.Formatting.Indented);
  1438. HttpHelper http = new HttpHelper();
  1439. HttpItem item = new HttpItem()
  1440. {
  1441. URL = url,//URL这里都是测试URl   必需项
  1442. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1443. //Encoding = Encoding.Default,
  1444. Method = "post",//URL     可选项 默认为Get
  1445. Postdata = jsonorder,
  1446. PostEncoding = Encoding.UTF8,
  1447. KeepAlive = false
  1448. };
  1449. item.ContentType = "application/json;charset=utf-8";
  1450. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  1451. | SecurityProtocolType.Tls
  1452. | SecurityProtocolType.Tls11//(SecurityProtocolType)0x300 //Tls11
  1453. | SecurityProtocolType.Tls12;//(SecurityProtocolType)0xC00; //Tls12
  1454. HttpResult result = http.GetHtml(item);
  1455. string ParseText = result.Html;
  1456. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1457. {
  1458. Log.WriteGetKeyLog("\r\nUpgrade check," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1459. }
  1460. else
  1461. {
  1462. Log.WriteErrorLog("\r\nFail to upgrade check:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1463. ReportErrormsg("Fail to upgrade check", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1464. return false;
  1465. }
  1466. try
  1467. {
  1468. JObject jObject = JObject.Parse(ParseText);
  1469. upmid.servertime = jObject["servertime"].Value<string>();
  1470. upmid.callid = jObject["callid"].Value<string>();
  1471. upmid.state = jObject["state"].Value<string>();
  1472. upmid.note = jObject["note"].Value<string>();
  1473. upmid.language = jObject["language"].Value<string>();
  1474. upmid.apiversion = jObject["apiversion"].Value<string>();
  1475. upmid.upgrade = new UpgradeMsgList2()
  1476. {
  1477. type = JSON_SeleteNode(jObject, "type"),
  1478. appid = JSON_SeleteNode(jObject, "appid"),
  1479. apptype = JSON_SeleteNode(jObject, "apptype"),
  1480. version = JSON_SeleteNode(jObject, "version"),
  1481. verid = JSON_SeleteNode(jObject, "verid"),
  1482. md5 = JSON_SeleteNode(jObject, "md5"),
  1483. size = JSON_SeleteNode(jObject, "size"),
  1484. minicon = JSON_SeleteNode(jObject, "minicon"),
  1485. midicon = JSON_SeleteNode(jObject, "midicon"),
  1486. fileurl = JSON_SeleteNode(jObject, "fileurl"),
  1487. increment = JSON_SeleteNode(jObject, "increment"),
  1488. appendver = JSON_SeleteNode(jObject, "appendver"),
  1489. updatetime = JSON_SeleteNode(jObject, "updatetime")
  1490. };
  1491. if (upmid.state == "0000")
  1492. return true;
  1493. else
  1494. {
  1495. return false;
  1496. }
  1497. }
  1498. catch (Exception ex)
  1499. {
  1500. Log.WriteErrorLog("\r\nFail to parse upgrade check info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1501. ReportErrormsg("Fail to parse upgrade check info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1502. MessageBox.Show("Fail to parse upgrade check info\r\n " + ex.Message);
  1503. return false;
  1504. }
  1505. }
  1506. /// <summary>
  1507. /// 升级上报
  1508. /// </summary>
  1509. /// <param name="url"></param>
  1510. /// <param name="FactoryNum"></param>
  1511. /// <param name="Result"></param>
  1512. /// <param name="sqliteConn"></param>
  1513. /// <returns></returns>
  1514. public static bool UpgradeReport(string url, string FactoryNum, string Result, SQLiteConnection sqliteConn)
  1515. {
  1516. UpgradeReportInput UpgradeCheckInput1 = new UpgradeReportInput { appid = "SCBC_Factory_Tools", devmodel = "SCBC_Factory_Tools", dnum = FactoryNum, ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), type = "pc", result = Result };
  1517. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(UpgradeCheckInput1, Newtonsoft.Json.Formatting.Indented);
  1518. HttpHelper http = new HttpHelper();
  1519. HttpItem item = new HttpItem()
  1520. {
  1521. URL = url,//URL这里都是测试URl   必需项
  1522. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1523. //Encoding = Encoding.Default,
  1524. Method = "post",//URL     可选项 默认为Get
  1525. Postdata = jsonorder,
  1526. PostEncoding = Encoding.UTF8,
  1527. KeepAlive = false
  1528. };
  1529. item.ContentType = "application/json;charset=utf-8";
  1530. ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  1531. | SecurityProtocolType.Tls
  1532. | (SecurityProtocolType)0x300 //Tls11
  1533. | (SecurityProtocolType)0xC00; //Tls12
  1534. HttpResult result = http.GetHtml(item);
  1535. string ParseText = result.Html;
  1536. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1537. {
  1538. Log.WriteGetKeyLog("\r\nUpgrade report," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1539. }
  1540. else
  1541. {
  1542. Log.WriteErrorLog("\r\nFail to upgrade report:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1543. ReportErrormsg("Fail to upgrade report", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1544. return false;
  1545. }
  1546. try
  1547. {
  1548. JObject jObject = JObject.Parse(ParseText);
  1549. return true;
  1550. }
  1551. catch (Exception ex)
  1552. {
  1553. Log.WriteErrorLog("\r\nFail to parse upgrade report info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1554. ReportErrormsg("Fail to parse upgrade report info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1555. MessageBox.Show("Fail to parse upgrade report info\r\n " + ex.Message);
  1556. return false;
  1557. }
  1558. }
  1559. /// <summary>
  1560. /// 比较本地文件的MD5值
  1561. /// </summary>
  1562. /// <param name="filename"></param>
  1563. /// <param name="filetype"></param>
  1564. /// <param name="filemd5"></param>
  1565. /// <param name="error"></param>
  1566. /// <returns></returns>
  1567. public static bool CompareLocalkey(string filename, string filetype, string filemd5, out string error, out string filefullname)
  1568. {
  1569. error = "";
  1570. filefullname = "";
  1571. try
  1572. {
  1573. string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\" + filename + "\\" + filetype;
  1574. if (!Directory.Exists(filepath))
  1575. {
  1576. Directory.CreateDirectory(filepath);
  1577. }
  1578. DirectoryInfo Getkeyfile = new DirectoryInfo(filepath);
  1579. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  1580. {
  1581. string FileMD5 = GetMD5.GetMD5HashFromFile(keyfile.Name);
  1582. if (FileMD5 == filemd5)
  1583. {
  1584. Log.WriteGetKeyLog("Check md5file success,file md5:" + FileMD5 + "\r\nfile:" + keyfile.FullName);
  1585. filefullname = keyfile.FullName;
  1586. return true;
  1587. }
  1588. else
  1589. {
  1590. Log.WriteErrorLog("Check md5file error,delete file:" + keyfile.FullName + "\r\nfile md5" + FileMD5);
  1591. File.Delete(keyfile.FullName);
  1592. }
  1593. }
  1594. return false;
  1595. }
  1596. catch (Exception ex)
  1597. {
  1598. error = ex.Message;
  1599. return false;
  1600. }
  1601. }
  1602. /// <summary>
  1603. /// 数据MD5核对。当数据头为"MD5="时,key内容比对改为key内容MD5比对
  1604. /// </summary>
  1605. /// <param name="key"></param>
  1606. /// <param name="data"></param>
  1607. /// <param name="md5"></param>
  1608. /// <returns></returns>
  1609. public static bool CompareMD5(byte[] key, byte[] data)
  1610. {
  1611. string strdata = Encoding.ASCII.GetString(data);
  1612. Console.WriteLine(strdata);
  1613. if (strdata.StartsWith("MD5="))
  1614. {
  1615. string md5 = GetMD5.GetMD5Hash(key);
  1616. Console.WriteLine(md5);
  1617. return strdata.Remove(0, 4).Equals(md5);
  1618. }
  1619. return false;
  1620. }
  1621. /// <summary>
  1622. /// 将16进制字符串转为对应字节流写入生成key文件
  1623. /// </summary>
  1624. /// <param name="path"></param>
  1625. /// <param name="data"></param>
  1626. /// <param name="error"></param>
  1627. /// <returns></returns>
  1628. public static bool GetCreateHexkey(string path, string data, out string error)
  1629. {
  1630. error = "";
  1631. try
  1632. {
  1633. string filepath = path + ".bin";
  1634. byte[] bytedata = SerialInit.HexToByte(data);
  1635. //创建一个文件流
  1636. FileStream fs = new FileStream(filepath, FileMode.Create);
  1637. fs.Write(bytedata, 0, bytedata.Length);
  1638. fs.Close();
  1639. Log.WriteGetKeyLog("save key:" + filepath);
  1640. return true;
  1641. }
  1642. catch (Exception ex)
  1643. {
  1644. error = ex.Message;
  1645. Log.WriteErrorLog("save key error:" + error);
  1646. return false;
  1647. }
  1648. }
  1649. /// <summary>
  1650. /// 读取key文件内容转换为16进制字符串
  1651. /// </summary>
  1652. /// <param name="filepath"></param>
  1653. /// <param name="data"></param>
  1654. /// <param name="error"></param>
  1655. /// <returns></returns>
  1656. public static bool ReadHexFile(string filepath, out string data, out string error)
  1657. {
  1658. data = "";
  1659. error = "";
  1660. try
  1661. {
  1662. using FileStream fskey = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.None);
  1663. {
  1664. using BinaryReader brkey = new BinaryReader(fskey);
  1665. {
  1666. data = SerialInit.ByteToHex(brkey.ReadBytes((int)fskey.Length));
  1667. data = data.Replace(" ", "");
  1668. return true;
  1669. }
  1670. }
  1671. }
  1672. catch (Exception ex)
  1673. {
  1674. error = ex.Message;
  1675. return false;
  1676. }
  1677. }
  1678. /// <summary>
  1679. /// 账号登录
  1680. /// </summary>
  1681. /// <param name="url"></param>
  1682. /// <param name="account"></param>
  1683. /// <param name="password"></param>
  1684. /// <param name="pcmac"></param>
  1685. /// <param name="sqliteConn"></param>
  1686. /// <param name="loginOutput1"></param>
  1687. /// <returns></returns>
  1688. public static bool UserLogin(string url, string account, string password, string pcmac, SQLiteConnection sqliteConn, out LoginOutput loginOutput1)
  1689. {
  1690. loginOutput1 = new LoginOutput();
  1691. LoginInput LoginInput1 = new LoginInput { account = account, password = password, mac = pcmac };
  1692. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(LoginInput1, Newtonsoft.Json.Formatting.Indented);
  1693. HttpHelper http = new HttpHelper();
  1694. HttpItem item = new HttpItem()
  1695. {
  1696. URL = url,//URL这里都是测试URl   必需项
  1697. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1698. //Encoding = Encoding.Default,
  1699. Method = "post",//URL     可选项 默认为Get
  1700. Postdata = jsonorder,
  1701. PostEncoding = Encoding.UTF8,
  1702. KeepAlive = false
  1703. };
  1704. item.ContentType = "application/json;charset=utf-8";
  1705. ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;//
  1706. HttpResult result = http.GetHtml(item);
  1707. string ParseText = result.Html;
  1708. if (result.StatusCode == HttpStatusCode.OK)
  1709. {
  1710. Log.WriteGetKeyLog("\r\nLogin," + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1711. }
  1712. else
  1713. {
  1714. Log.WriteErrorLog("\r\nFail to login:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1715. ReportErrormsg("Fail to login", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1716. return false;
  1717. }
  1718. try
  1719. {
  1720. JObject jObject = JObject.Parse(ParseText);
  1721. loginOutput1.msg = jObject["msg"].Value<string>();
  1722. loginOutput1.code = jObject["code"].Value<string>();
  1723. loginOutput1.factoryname = JSON_SeleteNode(jObject, "factoryName");
  1724. loginOutput1.factorynum = JSON_SeleteNode(jObject, "factoryNum");
  1725. if (loginOutput1.code == "1000")
  1726. return true;
  1727. else
  1728. return false;
  1729. }
  1730. catch (Exception ex)
  1731. {
  1732. Log.WriteErrorLog("\r\nFail to login," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1733. ReportErrormsg("Fail to login", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1734. MessageBox.Show("Fail to parse login return\r\n " + ex.Message);
  1735. return false;
  1736. }
  1737. }
  1738. /// <summary>
  1739. /// 获取指定日期,在为一年中为第几周
  1740. /// </summary>
  1741. /// <param name="dt">指定时间</param>
  1742. /// <reutrn>返回第几周</reutrn>
  1743. public static int GetWeekOfYear(DateTime dt)
  1744. {
  1745. GregorianCalendar gc = new GregorianCalendar();
  1746. int weekOfYear = gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
  1747. return weekOfYear;
  1748. }
  1749. /// <summary>
  1750. /// 版本号比较:new_version>=old_version
  1751. /// </summary>
  1752. /// <param name="new_version">8.4.0</param>
  1753. /// <param name="old_version">8.3.10</param>
  1754. /// <returns></returns>
  1755. public static bool CompareVersion(string new_version, string old_version)
  1756. {
  1757. try
  1758. {
  1759. if (string.IsNullOrEmpty(new_version) || string.IsNullOrEmpty(old_version))
  1760. return false;
  1761. Version v_new = new Version(new_version);
  1762. Version v_old = new Version(old_version);
  1763. if (v_new >= v_old)
  1764. return true;
  1765. return false;
  1766. }
  1767. catch (Exception ex)
  1768. {
  1769. throw ex;
  1770. }
  1771. }
  1772. /// <summary>
  1773. /// 获取白平衡信息
  1774. /// </summary>
  1775. /// <param name="url"></param>
  1776. /// <param name="order"></param>
  1777. /// <param name="pcmac"></param>
  1778. /// <param name="sqliteConn"></param>
  1779. /// <param name="WBList"></param>
  1780. /// <returns></returns>
  1781. public static bool GetWhiteBalanceInfo(string url, string order, string pcmac, SQLiteConnection sqliteConn, out WhiteBalanceList WBList)
  1782. {
  1783. WBList = new WhiteBalanceList();
  1784. WhiteBalanceInput WBInput1 = new WhiteBalanceInput { bid = order, mac = pcmac };//"74-27-EA-6E-DA-9F"
  1785. string jsonorder = Newtonsoft.Json.JsonConvert.SerializeObject(WBInput1, Newtonsoft.Json.Formatting.Indented);
  1786. HttpHelper http = new HttpHelper();
  1787. HttpItem item = new HttpItem()
  1788. {
  1789. URL = url,//URL这里都是测试URl   必需项
  1790. Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
  1791. //Encoding = Encoding.Default,
  1792. Method = "post",//URL     可选项 默认为Get
  1793. Postdata = jsonorder,
  1794. PostEncoding = Encoding.UTF8,
  1795. KeepAlive = false
  1796. };
  1797. item.ContentType = "application/json;charset=utf-8";
  1798. System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
  1799. stopwatch.Start();
  1800. HttpResult result = http.GetHtml(item);
  1801. stopwatch.Stop();
  1802. string usingtime = stopwatch.ElapsedMilliseconds.ToString();
  1803. stopwatch.Reset();
  1804. string ParseText = result.Html;
  1805. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1806. {
  1807. Log.WriteGetKeyLog("\r\nGet WB info\r\nUsing time:" + usingtime + "ms\r\n" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1808. }
  1809. else
  1810. {
  1811. Log.WriteErrorLog("\r\nFail to WBinfo:" + result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1812. ReportErrormsg("Fail to WBinfo", result.StatusDescription + result.Html + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1813. return false;
  1814. }
  1815. try
  1816. {
  1817. JObject jObject = JObject.Parse(ParseText);
  1818. WBList.message = jObject["message"].Value<string>();
  1819. WBList.code = jObject["code"].Value<string>();
  1820. object a = jObject["obj"];
  1821. if (jObject["obj"].ToString().Length > 0)
  1822. WBList.whiteBalanceInfo = new WhiteBalanceInfo()
  1823. {
  1824. id = JSON_SeleteNode(jObject["obj"], "id"),
  1825. ordernum = JSON_SeleteNode(jObject["obj"], "ordernum"),
  1826. hdmirgain = JSON_SeleteNode(jObject["obj"], "hdmirgain"),
  1827. hdmiggain = JSON_SeleteNode(jObject["obj"], "hdmiggain"),
  1828. hdmibgain = JSON_SeleteNode(jObject["obj"], "hdmibgain"),
  1829. nrgain = JSON_SeleteNode(jObject["obj"], "nrgain"),
  1830. nggain = JSON_SeleteNode(jObject["obj"], "nggain"),
  1831. nbgain = JSON_SeleteNode(jObject["obj"], "nbgain"),
  1832. lrgain = JSON_SeleteNode(jObject["obj"], "lrgain"),
  1833. lggain = JSON_SeleteNode(jObject["obj"], "lggain"),
  1834. lbgain = JSON_SeleteNode(jObject["obj"], "lbgain"),
  1835. updatetime = JSON_SeleteNode(jObject["obj"], "updatetime"),
  1836. createtime = JSON_SeleteNode(jObject["obj"], "createtime"),
  1837. isdelete = JSON_SeleteNode(jObject["obj"], "isdelete"),
  1838. isExemption = JSON_SeleteNode(jObject["obj"], "isExemption")
  1839. };
  1840. if (WBList.code == "1000")
  1841. return true;
  1842. else
  1843. return false;
  1844. }
  1845. catch (Exception ex)
  1846. {
  1847. Log.WriteErrorLog("\r\nFail to parse WB info," + ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder);
  1848. ReportErrormsg("Fail to parse WB info", ex.Message + ":" + ParseText + "\r\nAddress:" + url + "\r\nPostdata:" + jsonorder, sqliteConn);
  1849. MessageBox.Show("Fail to parse WB info\r\n " + ex.Message);
  1850. return false;
  1851. }
  1852. }
  1853. private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  1854. {
  1855. return true;
  1856. }
  1857. public static bool AutoGetKey(string url, string order, string key, string keytype, SQLiteConnection errorDBNow)
  1858. {
  1859. string code;
  1860. string desc;
  1861. HttpHelper http = new HttpHelper();
  1862. HttpItem item = new HttpItem()
  1863. {
  1864. Encoding = Encoding.Default,
  1865. Method = "post",
  1866. ContentType = "application/x-www-form-urlencoded",
  1867. KeepAlive = false
  1868. };
  1869. url += "/autogetkey.do?";
  1870. item.URL = url;
  1871. item.Postdata = string.Format("order={0}&key={1}&keytype={2}&mac={3}", order, key, keytype, HttpUtility.UrlEncode(TestMode.MAC));
  1872. HttpResult result = http.GetHtml(item);
  1873. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1874. {
  1875. Log.WriteGetKeyLog("\r\nAutoquestKey :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  1876. if (Xmlconfig.AutoKeyStatusXml(result.Html, "response", out code, out desc))
  1877. {
  1878. return true;
  1879. }
  1880. else
  1881. {
  1882. CommonMethod.ReportErrormsg("Auto get key error", desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, errorDBNow);
  1883. return false;
  1884. }
  1885. }
  1886. else
  1887. {
  1888. CommonMethod.ReportErrormsg("Auto get key error", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, errorDBNow);
  1889. return false;
  1890. }
  1891. }
  1892. public static bool AutoGetKeyStatus(string url, string key, string keytype, SQLiteConnection errorDBNow)
  1893. {
  1894. string code;
  1895. string desc;
  1896. HttpHelper http = new HttpHelper();
  1897. HttpItem item = new HttpItem()
  1898. {
  1899. Encoding = Encoding.Default,
  1900. Method = "post",
  1901. ContentType = "application/x-www-form-urlencoded",
  1902. KeepAlive = false
  1903. };
  1904. url += "/autogetkeystatus.do?";
  1905. item.URL = url;
  1906. item.Postdata = string.Format("key={0}&keytype={1}", key, keytype);
  1907. HttpResult result = http.GetHtml(item);
  1908. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1909. {
  1910. Log.WriteGetKeyLog("\r\nAutoGetKeyStatus :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  1911. if (Xmlconfig.AutoKeyStatusXml(result.Html, "response", out code, out desc))
  1912. {
  1913. return true;
  1914. }
  1915. else
  1916. {
  1917. return false;
  1918. }
  1919. }
  1920. else
  1921. {
  1922. CommonMethod.ReportErrormsg("Auto get key status error", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata, errorDBNow);
  1923. return false;
  1924. }
  1925. }
  1926. public static bool GetFireTVDsn(string url, string ordernum, string deviceCode, string FSN, string PSN, out string dsn, out string error, SQLiteConnection errorDBNow)
  1927. {
  1928. // 计算函数耗时;
  1929. Stopwatch watch = new Stopwatch();
  1930. watch.Start();
  1931. dsn = "";
  1932. error = "";
  1933. HttpHelper http = new HttpHelper();
  1934. HttpItem item = new HttpItem()
  1935. {
  1936. Encoding = null, // 编码格式(utf-8,gb2312,gbk)可选项 默认类会自动识别Encoding = Encoding.Default,
  1937. Method = "GET", // URL 可选项 默认为Get
  1938. KeepAlive = false
  1939. };
  1940. item.URL = string.Format("{0}/getFiretvDsn.do?ordernum={1}&fsn={2}&psn={3}&deviceCode={4}&mac={5}", url, ordernum, FSN, PSN, deviceCode, HttpUtility.UrlEncode(TestMode.MAC));
  1941. HttpResult result = http.GetHtml(item);
  1942. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  1943. {
  1944. string code;
  1945. string desc;
  1946. Log.WriteGetKeyLog("\r\nGetFireTVDsn :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  1947. if (Xmlconfig.GetDSNXml(result.Html, "response", out code, out desc, out dsn))
  1948. {
  1949. Log.WriteGetKeyLog(string.Format("\r\nDSN= {0}\r\nHttp Get Dsn Elapsed = {1}", dsn, watch.Elapsed.TotalMilliseconds));
  1950. return true;
  1951. }
  1952. else
  1953. {
  1954. error = desc;
  1955. CommonMethod.ReportErrormsg("GetFireTVDsn error", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL, errorDBNow);
  1956. return false;
  1957. }
  1958. }
  1959. else
  1960. {
  1961. error = "Communication with server failed";
  1962. CommonMethod.ReportErrormsg("GetFireTVDsn error", result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL, errorDBNow);
  1963. return false;
  1964. }
  1965. }
  1966. /// <summary>
  1967. /// 导出日本的SN与ACASID到文本中;
  1968. /// </summary>
  1969. /// <param name="ordernum"></param>
  1970. /// <param name="sn"></param>
  1971. /// <param name="acasid"></param>
  1972. public static void ExportSNACASID(string ordernum, string sn, string acasid)
  1973. {
  1974. string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\SN-ACAS";
  1975. if (!Directory.Exists(path))
  1976. {
  1977. Directory.CreateDirectory(path);
  1978. }
  1979. if (!lastOrder.Equals(ordernum))
  1980. {
  1981. lastOrder = ordernum;
  1982. ListACASSN.Clear();
  1983. }
  1984. string msg = string.Format("{0};{1}", sn, acasid);
  1985. if (ListACASSN.Contains(msg))
  1986. {
  1987. return;
  1988. }
  1989. ListACASSN.Add(msg);
  1990. string fileName = path + "\\" + ordernum + ".txt";
  1991. var stream = new StreamWriter(fileName, true);
  1992. stream.WriteLine(msg);
  1993. stream.Close();
  1994. stream.Dispose();
  1995. }
  1996. /// <summary>
  1997. /// 是否抄写EDID;
  1998. /// </summary>
  1999. /// <param name="midInfo"></param>
  2000. /// <returns></returns>
  2001. public static bool IsEDIDCopy(MidList midInfo)
  2002. {
  2003. if (midInfo == null || midInfo.keytype == null)
  2004. return false;
  2005. if (!midInfo.keytype.ContainsKey("edid_pid") && !midInfo.keytype.ContainsKey("edid_model_name"))
  2006. return false;
  2007. return true;
  2008. }
  2009. #region G客户NTF功能:GetMaInfo
  2010. public static bool GetMaInfo(string sn, out MaInfo maInfo, SQLiteConnection errorDBNow)
  2011. {
  2012. string desc;
  2013. maInfo = null;
  2014. string strPost = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
  2015. "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
  2016. "<soap:Body>"+
  2017. "<GetMaInfo xmlns=\"http://tempuri.org/\">"+
  2018. "<sn>"+sn+"</sn>"+
  2019. "</GetMaInfo>"+
  2020. "</soap:Body>"+
  2021. "</soap:Envelope>";
  2022. HttpHelper http = new HttpHelper();
  2023. HttpItem item = new HttpItem()
  2024. {
  2025. Encoding = Encoding.Default,
  2026. Method = "post",
  2027. ContentType = "text/xml",
  2028. KeepAlive = false
  2029. };
  2030. item.URL = "http://smes-prd-app01.tclking.com:9002/SAPService.asmx";
  2031. item.Postdata = strPost;
  2032. HttpResult result = http.GetHtml(item);
  2033. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  2034. {
  2035. Log.WriteGetKeyLog("\r\nGetMaInfo :\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  2036. if (!Xmlconfig.GetMaInfoXml(result.Html, out maInfo, out desc))
  2037. {
  2038. Log.WriteGetKeyLog("GetMaInfo error:" + desc + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2039. return false;
  2040. }
  2041. }
  2042. else
  2043. {
  2044. Log.WriteGetKeyLog("GetMaInfo error:" + result.StatusDescription + "\r\n" + result.Html + "\r\nAddress: " + item.URL + "\r\nPostdata: " + item.Postdata);
  2045. return false;
  2046. }
  2047. return true;
  2048. }
  2049. #endregion
  2050. }
  2051. /// <summary>
  2052. /// CRC32
  2053. /// </summary>
  2054. class CRC32
  2055. {
  2056. //生成CRC32码表
  2057. static uint[] Crc32Table;
  2058. public static void GetCRC32Table()
  2059. {
  2060. uint Crc;
  2061. Crc32Table = new uint[256];
  2062. int i, j;
  2063. for (i = 0; i < 256; i++)
  2064. {
  2065. Crc = (uint)i;
  2066. for (j = 8; j > 0; j--)
  2067. {
  2068. if ((Crc & 1) == 1)
  2069. Crc = (Crc >> 1) ^ 0xEDB88320;
  2070. else
  2071. Crc >>= 1;
  2072. }
  2073. Crc32Table[i] = Crc;
  2074. }
  2075. }
  2076. public static uint GetCRC32(byte[] bytes)
  2077. {
  2078. GetCRC32Table();
  2079. uint value = 0xffffffff;
  2080. int len = bytes.Length;
  2081. for (int i = 0; i < len; i++)
  2082. {
  2083. value = (value >> 8) ^ Crc32Table[(value & 0xFF) ^ bytes[i]];
  2084. }
  2085. return value ^ 0xffffffff;
  2086. }
  2087. }
  2088. /// <summary>
  2089. /// 本地抄写模式方法
  2090. /// </summary>
  2091. public class GetLocalKeyCount
  2092. {
  2093. static string LocalKeyPath = AppDomain.CurrentDomain.BaseDirectory + "\\Keys\\";
  2094. public static bool GetLocalDidCount(string order, out int num, ref MidList localmidList, SQLiteConnection errorDBNow)
  2095. {
  2096. num = 0;
  2097. try
  2098. {
  2099. string DIDpath = LocalKeyPath + order + "\\DeviceID";
  2100. if (Directory.Exists(DIDpath) == false)
  2101. return false;
  2102. else
  2103. {
  2104. DirectoryInfo GetDIDfile = new DirectoryInfo(DIDpath);
  2105. foreach (FileInfo keyfile in GetDIDfile.GetFiles())
  2106. {
  2107. string filename = keyfile.FullName;
  2108. if (keyfile.Name.Contains("Online"))
  2109. {
  2110. if (localmidList.host == null)
  2111. {
  2112. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2113. {
  2114. ;
  2115. }
  2116. else
  2117. {
  2118. num = -1;
  2119. return true;
  2120. }
  2121. }
  2122. HttpHelper http = new HttpHelper();
  2123. HttpItem item = new HttpItem()
  2124. {
  2125. Encoding = Encoding.Default,
  2126. Method = "post",
  2127. ContentType = "application/x-www-form-urlencoded"
  2128. };
  2129. string url1 = localmidList.host + "/statdeviceid.do?";
  2130. item.URL = url1;
  2131. item.Postdata = "devicetype=tcl_unknown_model";
  2132. HttpResult result = http.GetHtml(item);
  2133. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  2134. {
  2135. Log.WriteGetKeyLog("\r\nGetDeviceID count:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  2136. if (Xmlconfig.KeyCountXml(result.Html, "response", "count", out string code, out string desc, out string count))
  2137. {
  2138. num = Convert.ToInt32(count.Replace(",", ""));
  2139. return true;
  2140. }
  2141. else
  2142. {
  2143. num = -1;
  2144. return true;
  2145. }
  2146. }
  2147. else
  2148. {
  2149. num = -1;
  2150. return true;
  2151. }
  2152. }
  2153. else
  2154. {
  2155. num = GetDIDfile.GetFiles().Length;
  2156. return true;
  2157. }
  2158. }
  2159. return true;
  2160. }
  2161. }
  2162. catch (Exception ex)
  2163. {
  2164. num = -1;
  2165. return true;
  2166. }
  2167. }
  2168. public static bool GetLocalMacCount(string order, out int num, ref MidList localmidList, SQLiteConnection errorDBNow)
  2169. {
  2170. num = 0;
  2171. try
  2172. {
  2173. string MACpath = LocalKeyPath + order + "\\MAC";
  2174. if (Directory.Exists(MACpath) == false)
  2175. return false;
  2176. else
  2177. {
  2178. DirectoryInfo GetMACfile = new DirectoryInfo(MACpath);
  2179. foreach (FileInfo keyfile in GetMACfile.GetFiles())
  2180. {
  2181. string filename = keyfile.FullName;
  2182. if (keyfile.Name.Contains("Online"))
  2183. {
  2184. if (localmidList.host == null)
  2185. {
  2186. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2187. {
  2188. ;
  2189. }
  2190. else
  2191. {
  2192. num = -1;
  2193. return true;
  2194. }
  2195. }
  2196. HttpHelper http = new HttpHelper();
  2197. HttpItem item = new HttpItem()
  2198. {
  2199. Encoding = Encoding.Default,
  2200. Method = "post",
  2201. ContentType = "application/x-www-form-urlencoded"
  2202. };
  2203. string url1 = localmidList.host + "/statmac.do?";
  2204. item.URL = url1;
  2205. item.Postdata = "typeString=" + localmidList.keytype["MAC"];
  2206. HttpResult result = http.GetHtml(item);
  2207. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  2208. {
  2209. Log.WriteGetKeyLog("\r\nGetMac count:\r\n" + item.URL + "\r\n" + item.Postdata + "\r\n" + result.Html);
  2210. if (Xmlconfig.KeyCountXml(result.Html, "response", "count", out string code, out string desc, out string count))
  2211. {
  2212. num = Convert.ToInt32(count.Replace(",", ""));
  2213. return true;
  2214. }
  2215. else
  2216. {
  2217. num = -1;
  2218. return true;
  2219. }
  2220. }
  2221. else
  2222. {
  2223. num = -1;
  2224. return true;
  2225. }
  2226. }
  2227. else
  2228. {
  2229. num = GetMACfile.GetFiles().Length;
  2230. return true;
  2231. }
  2232. }
  2233. return true;
  2234. }
  2235. }
  2236. catch (Exception ex)
  2237. {
  2238. num = -1;
  2239. return true;
  2240. }
  2241. }
  2242. public static bool GetLocalHDCPKEYCount(string order, out int num)
  2243. {
  2244. num = 0;
  2245. try
  2246. {
  2247. string HDCPKEYpath = LocalKeyPath + order + "\\HDCPKEY";
  2248. if (Directory.Exists(HDCPKEYpath) == false)
  2249. return false;
  2250. else
  2251. {
  2252. DirectoryInfo GetHDCPKEYfile = new DirectoryInfo(HDCPKEYpath);
  2253. num = GetHDCPKEYfile.GetFiles().Length;
  2254. return true;
  2255. }
  2256. }
  2257. catch (Exception ex)
  2258. {
  2259. num = -1;
  2260. return true;
  2261. }
  2262. }
  2263. public static bool GetLocalHDCPKEY22Count(string order, out int num)
  2264. {
  2265. num = 0;
  2266. try
  2267. {
  2268. string HDCPKEY22path = LocalKeyPath + order + "\\HDCPKEY22";
  2269. if (Directory.Exists(HDCPKEY22path) == false)
  2270. return false;
  2271. else
  2272. {
  2273. DirectoryInfo GetHDCPKEY22file = new DirectoryInfo(HDCPKEY22path);
  2274. num = GetHDCPKEY22file.GetFiles().Length;
  2275. return true;
  2276. }
  2277. }
  2278. catch (Exception ex)
  2279. {
  2280. num = -1;
  2281. return true;
  2282. }
  2283. }
  2284. public static bool GetLocalWiDiCount(string order, out int num)
  2285. {
  2286. num = 0;
  2287. try
  2288. {
  2289. string WiDipath = LocalKeyPath + order + "\\WiDi";
  2290. if (Directory.Exists(WiDipath) == false)
  2291. return false;
  2292. else
  2293. {
  2294. DirectoryInfo GetWiDifile = new DirectoryInfo(WiDipath);
  2295. num = GetWiDifile.GetFiles().Length;
  2296. return true;
  2297. }
  2298. }
  2299. catch (Exception ex)
  2300. {
  2301. num = -1;
  2302. return true;
  2303. }
  2304. }
  2305. public static bool GetLocalWidevineCount(string order, out int num)
  2306. {
  2307. num = 0;
  2308. try
  2309. {
  2310. string Widevinepath = LocalKeyPath + order + "\\Widevine";
  2311. if (Directory.Exists(Widevinepath) == false)
  2312. return false;
  2313. else
  2314. {
  2315. DirectoryInfo GetWidevinefile = new DirectoryInfo(Widevinepath);
  2316. num = GetWidevinefile.GetFiles().Length;
  2317. return true;
  2318. }
  2319. }
  2320. catch (Exception ex)
  2321. {
  2322. num = -1;
  2323. return true;
  2324. }
  2325. }
  2326. public static bool GetLocalESNCount(string order, out int num)
  2327. {
  2328. num = 0;
  2329. try
  2330. {
  2331. string ESNpath = LocalKeyPath + order + "\\ESN";
  2332. if (Directory.Exists(ESNpath) == false)
  2333. return false;
  2334. else
  2335. {
  2336. DirectoryInfo GetESNfile = new DirectoryInfo(ESNpath);
  2337. num = GetESNfile.GetFiles().Length;
  2338. return true;
  2339. }
  2340. }
  2341. catch (Exception ex)
  2342. {
  2343. num = -1;
  2344. return true;
  2345. }
  2346. }
  2347. public static bool GetLocalMGKCount(string order, out int num)
  2348. {
  2349. num = 0;
  2350. try
  2351. {
  2352. string MGKpath = LocalKeyPath + order + "\\MGK";
  2353. if (Directory.Exists(MGKpath) == false)
  2354. return false;
  2355. else
  2356. {
  2357. DirectoryInfo GetMGKfile = new DirectoryInfo(MGKpath);
  2358. num = GetMGKfile.GetFiles().Length;
  2359. return true;
  2360. }
  2361. }
  2362. catch (Exception ex)
  2363. {
  2364. num = -1;
  2365. return true;
  2366. }
  2367. }
  2368. public static bool GetLocalFairplayCount(string order, out int num)
  2369. {
  2370. num = 0;
  2371. try
  2372. {
  2373. string Fairplaypath = LocalKeyPath + order + "\\Fairplay";
  2374. if (Directory.Exists(Fairplaypath) == false)
  2375. return false;
  2376. else
  2377. {
  2378. DirectoryInfo GetFairplayfile = new DirectoryInfo(Fairplaypath);
  2379. num = GetFairplayfile.GetFiles().Length;
  2380. return true;
  2381. }
  2382. }
  2383. catch (Exception ex)
  2384. {
  2385. num = -1;
  2386. return true;
  2387. }
  2388. }
  2389. public static bool GetLocalFolderKeyCount(string order, string keyname, out int num)
  2390. {
  2391. num = 0;
  2392. try
  2393. {
  2394. string Fairplaypath = LocalKeyPath + order + "\\" + keyname;
  2395. if (Directory.Exists(Fairplaypath) == false)
  2396. return false;
  2397. else
  2398. {
  2399. DirectoryInfo GetFairplayfile = new DirectoryInfo(Fairplaypath);
  2400. num = GetFairplayfile.GetFiles().Length;
  2401. return true;
  2402. }
  2403. }
  2404. catch (Exception ex)
  2405. {
  2406. num = -1;
  2407. return true;
  2408. }
  2409. }
  2410. public static bool GetLocalAttestationCount(string order, out int num)
  2411. {
  2412. num = 0;
  2413. try
  2414. {
  2415. string Attestationpath = LocalKeyPath + order + "\\Attestation";
  2416. if (Directory.Exists(Attestationpath) == false)
  2417. return false;
  2418. else
  2419. {
  2420. DirectoryInfo GetAttestationfile = new DirectoryInfo(Attestationpath);
  2421. num = GetAttestationfile.GetFiles().Length;
  2422. return true;
  2423. }
  2424. }
  2425. catch (Exception ex)
  2426. {
  2427. num = -1;
  2428. return true;
  2429. }
  2430. }
  2431. public static bool GetLocalCIKEYCount(string order, out int num)
  2432. {
  2433. num = 0;
  2434. try
  2435. {
  2436. string CIKEYpath = LocalKeyPath + order + "\\CIKEY";
  2437. if (Directory.Exists(CIKEYpath) == false)
  2438. return false;
  2439. else
  2440. {
  2441. DirectoryInfo GetCIKEYfile = new DirectoryInfo(CIKEYpath);
  2442. num = GetCIKEYfile.GetFiles().Length;
  2443. return true;
  2444. }
  2445. }
  2446. catch (Exception ex)
  2447. {
  2448. num = -1;
  2449. return true;
  2450. }
  2451. }
  2452. public static bool GetLocalStringKeys(string order, string keytype, int filesize, out FileStream fskey, out StreamReader brkey, out string data, out string filename, out string error, SQLiteConnection errorDBNow, ref MidList localmidList, string SN)
  2453. {
  2454. data = "";
  2455. filename = "";
  2456. error = "";
  2457. brkey = null;
  2458. fskey = null;
  2459. try
  2460. {
  2461. string keypath = LocalKeyPath + order + "\\" + keytype;
  2462. if (Directory.Exists(keypath) == false)
  2463. {
  2464. error = "File not exist";
  2465. return false;
  2466. }
  2467. else
  2468. {
  2469. DirectoryInfo Getkeyfile = new DirectoryInfo(keypath);
  2470. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  2471. {
  2472. filename = keyfile.FullName;
  2473. if (keyfile.Name.Contains("Online"))
  2474. {
  2475. try
  2476. {
  2477. if (localmidList.host == null)
  2478. {
  2479. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2480. {
  2481. ;
  2482. }
  2483. else
  2484. {
  2485. return false;
  2486. }
  2487. }
  2488. if (keytype == "DeviceID")
  2489. localmidList.keytype["DeviceID"] = "devicetype=tcl_unknown_model";
  2490. else if (keytype == "MAC")
  2491. keytype = "MAC";
  2492. else if (keytype == "HDCPKEY")
  2493. keytype = "HDCP_KEY";
  2494. else if (keytype == "HDCPKEY22")
  2495. keytype = "HDCP2.2_KEY";
  2496. else if (keytype == "ESN")
  2497. keytype = "NETFILX_ESN";
  2498. else if (keytype == "Widevine")
  2499. keytype = "Widevine_KEY";
  2500. else if (keytype == "WiDi")
  2501. keytype = "WiDi";
  2502. else if (keytype == "CIKEY")
  2503. keytype = "CI_PLUS_KEY";
  2504. if (CommonMethod.GetKeys(localmidList.host, keytype, SN, localmidList.keytype[keytype], errorDBNow, out string did, out error, order, out string md5))
  2505. {
  2506. data = did;
  2507. Log.WriteGetKeyLog("\r\n" + keytype + "= " + data);
  2508. return true;
  2509. }
  2510. else
  2511. {
  2512. return false;
  2513. }
  2514. }
  2515. catch (Exception ex)
  2516. {
  2517. error = ex.Message;
  2518. return false;
  2519. }
  2520. }
  2521. else
  2522. {
  2523. try
  2524. {
  2525. fskey = new FileStream(keyfile.FullName.ToString(), FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  2526. {
  2527. brkey = new StreamReader(fskey);
  2528. {
  2529. if (fskey.Length == filesize || filesize == -1)
  2530. {
  2531. data = brkey.ReadToEnd();
  2532. return true;
  2533. }
  2534. else
  2535. {
  2536. error = "File size error.\r\nFilename:" + keyfile.Name + "\r\nFilesize:" + fskey.Length.ToString();
  2537. return false;
  2538. }
  2539. }
  2540. }
  2541. }
  2542. catch
  2543. {
  2544. ;
  2545. }
  2546. }
  2547. }
  2548. return false;
  2549. }
  2550. }
  2551. catch (Exception ex)
  2552. {
  2553. error = ex.Message;
  2554. return false;
  2555. }
  2556. }
  2557. public static bool GetLocalHexKeys(string order, string keytype, int filesize, out FileStream fskey, out BinaryReader brkey, out string data, out string filename, out string error, SQLiteConnection errorDBNow, ref MidList localmidList, string SN)
  2558. {
  2559. data = "";
  2560. filename = "";
  2561. error = "";
  2562. brkey = null;
  2563. fskey = null;
  2564. try
  2565. {
  2566. string keypath = LocalKeyPath + order + "\\" + keytype;
  2567. if (Directory.Exists(keypath) == false)
  2568. {
  2569. error = "File not exist";
  2570. return false;
  2571. }
  2572. else
  2573. {
  2574. DirectoryInfo Getkeyfile = new DirectoryInfo(keypath);
  2575. foreach (FileInfo keyfile in Getkeyfile.GetFiles())
  2576. {
  2577. filename = keyfile.FullName;
  2578. if (keyfile.Name.Contains("Online"))
  2579. {
  2580. try
  2581. {
  2582. if (localmidList.host == null)
  2583. {
  2584. if (CommonMethod.GetMidInfo(TestMode.serverurl, order, TestMode.MAC, "", "", errorDBNow, out localmidList))
  2585. {
  2586. ;
  2587. }
  2588. else
  2589. {
  2590. return false;
  2591. }
  2592. }
  2593. if (keytype == "DeviceID")
  2594. localmidList.keytype["DeviceID"] = "devicetype=tcl_unknown_model";
  2595. else if (keytype == "MAC")
  2596. keytype = "MAC";
  2597. else if (keytype == "HDCPKEY")
  2598. keytype = "HDCP_KEY";
  2599. else if (keytype == "HDCPKEY22")
  2600. keytype = "HDCP2.2_KEY";
  2601. else if (keytype == "ESN")
  2602. keytype = "NETFILX_ESN";
  2603. else if (keytype == "Widevine")
  2604. keytype = "Widevine_KEY";
  2605. else if (keytype == "WiDi")
  2606. keytype = "WiDi";
  2607. else if (keytype == "CIKEY")
  2608. keytype = "CI_PLUS_KEY";
  2609. else if (keytype == "MGK")
  2610. keytype = "MGK_KEY";
  2611. else if (keytype == "Attestation")
  2612. keytype = "Attestation";
  2613. else if (keytype == "Fairplay")
  2614. keytype = "Fairplay_KEY";
  2615. if (CommonMethod.GetKeys(localmidList.host, keytype, SN, localmidList.keytype[keytype], errorDBNow, out string did, out error, order, out string md5))
  2616. {
  2617. data = did;
  2618. Log.WriteGetKeyLog("\r\n" + keytype + "= " + data);
  2619. return true;
  2620. }
  2621. else
  2622. {
  2623. return false;
  2624. }
  2625. }
  2626. catch (Exception ex)
  2627. {
  2628. error = ex.Message;
  2629. return false;
  2630. }
  2631. }
  2632. else
  2633. {
  2634. try
  2635. {
  2636. fskey = new FileStream(keyfile.FullName.ToString(), FileMode.Open, FileAccess.Read, FileShare.None);
  2637. {
  2638. brkey = new BinaryReader(fskey);
  2639. {
  2640. if (fskey.Length == filesize || filesize == -1)
  2641. {
  2642. data = SerialInit.ByteToHex(brkey.ReadBytes((int)fskey.Length));
  2643. data = data.Replace(" ", "");
  2644. return true;
  2645. }
  2646. else
  2647. {
  2648. error = "File size error.\r\nFilename: " + keyfile.Name + "\r\nFilesize: " + fskey.Length.ToString();
  2649. return false;
  2650. }
  2651. }
  2652. }
  2653. }
  2654. catch
  2655. {
  2656. ;
  2657. }
  2658. }
  2659. }
  2660. return false;
  2661. }
  2662. }
  2663. catch (Exception ex)
  2664. {
  2665. error = ex.Message;
  2666. return false;
  2667. }
  2668. }
  2669. public static bool DeleteLocalKeys(ArrayList filelist, ArrayList Stream, ArrayList Reader)
  2670. {
  2671. try
  2672. {
  2673. if (filelist.Count == Reader.Count)
  2674. {
  2675. for (int i = 0; i < filelist.Count; i++)
  2676. {
  2677. if (filelist[i].ToString().Contains("DeviceID"))
  2678. ((StreamReader)Reader[i]).Dispose();
  2679. else
  2680. ((BinaryReader)Reader[i]).Dispose();
  2681. ((FileStream)Stream[i]).Dispose();
  2682. File.Delete(filelist[i].ToString());
  2683. }
  2684. return true;
  2685. }
  2686. else
  2687. return false;
  2688. }
  2689. catch (Exception ex)
  2690. {
  2691. for (int i = 0; i < Reader.Count; i++)
  2692. {
  2693. if (filelist[i].ToString().Contains("DeviceID"))
  2694. ((StreamReader)Reader[i]).Dispose();
  2695. else
  2696. ((BinaryReader)Reader[i]).Dispose();
  2697. ((FileStream)Stream[i]).Dispose();
  2698. }
  2699. return false;
  2700. }
  2701. }
  2702. public static bool UnlockLocalKeys(ArrayList filelist, ArrayList Stream, ArrayList Reader)
  2703. {
  2704. for (int i = 0; i < Reader.Count; i++)
  2705. {
  2706. if (filelist[i].ToString().Contains("DeviceID"))
  2707. ((StreamReader)Reader[i]).Dispose();
  2708. else
  2709. ((BinaryReader)Reader[i]).Dispose();
  2710. ((FileStream)Stream[i]).Dispose();
  2711. }
  2712. return true;
  2713. }
  2714. }
  2715. /// <summary>
  2716. /// 本地缓存记录
  2717. /// </summary>
  2718. public class LocalTxtRecord
  2719. {
  2720. public static bool LocalRecord(string record, string name)
  2721. {
  2722. try
  2723. {
  2724. string order = name + ".txt";
  2725. if (File.Exists(@order))
  2726. {
  2727. FileStream fs = new FileStream(@order, FileMode.Append);
  2728. byte[] data = System.Text.Encoding.Default.GetBytes("\r\n" + record);
  2729. fs.Write(data, 0, data.Length);
  2730. fs.Flush();
  2731. fs.Close();
  2732. }
  2733. else
  2734. {
  2735. FileStream fs = new FileStream(@order, FileMode.Create);
  2736. byte[] data = System.Text.Encoding.Default.GetBytes(record);
  2737. fs.Write(data, 0, data.Length);
  2738. fs.Flush();
  2739. fs.Close();
  2740. }
  2741. return true;
  2742. }
  2743. catch (Exception ex)
  2744. {
  2745. return false;
  2746. }
  2747. }
  2748. }
  2749. }