RPCShell.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using Dicom.Log;
  2. using JsonRpcLite.Network;
  3. using JsonRpcLite.Rpc;
  4. using JsonRpcLite.Services;
  5. using MyMD5;
  6. using Newtonsoft.Json;
  7. using NPOI;
  8. using Syncfusion.CompoundFile.DocIO.Native;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Vinno.IUS.Common.Utilities;
  16. using vStation.Database;
  17. using vStation.Infrastructure;
  18. using vStation.Module.Workstation;
  19. using vStation.Utilities;
  20. using WingInterfaceLibrary.DTO.Management;
  21. using WingInterfaceLibrary.DTO.User;
  22. using WingInterfaceLibrary.Enum;
  23. using WingInterfaceLibrary.Interface;
  24. using WingInterfaceLibrary.Request.Management;
  25. using WingInterfaceLibrary.Request.User;
  26. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  27. using Logger = vStation.Infrastructure.Logger;
  28. using Platform = WingInterfaceLibrary.Enum.Platform;
  29. namespace vStation.URMStationRelevant
  30. {
  31. public static class RPCShell
  32. {
  33. private static JsonRpcClient _client;
  34. private static JsonRpcHttpClientEngine _clientEngine;
  35. private static IManagementService _managementService;
  36. private static ILoginService _loginService;
  37. private static IUserService _userService;
  38. private static Module.UserModule.IUserService _localUserService;
  39. private static Module.Login.ILoginService _localLoginService;
  40. private static string _fixedPassword = "vinno123";
  41. private static string _oldAdminPassword;
  42. private static string _oldClientPassword;
  43. static RPCShell() {
  44. if (_client == null)
  45. {
  46. _client = new JsonRpcClient();
  47. }
  48. if (_clientEngine == null)
  49. {
  50. _clientEngine = new JsonRpcHttpClientEngine(URMConfig.RPCAddress);
  51. }
  52. _client.UseEngine(_clientEngine);
  53. _managementService = _client?.CreateProxy<IManagementService>();
  54. _loginService = _client?.CreateProxy<ILoginService>();
  55. _userService = _client?.CreateProxy<IUserService>();
  56. _localUserService = AppManager.Instance.GetFunction<Module.UserModule.IUserService>();
  57. _localLoginService = AppManager.Instance.GetFunction<Module.Login.ILoginService>();
  58. }
  59. ///对称加密
  60. public static string SymmetryEncrypt(string text)
  61. {
  62. if (string.IsNullOrEmpty(text))
  63. {
  64. return "";
  65. }
  66. try
  67. {
  68. List<string> base64Code = new List<string>
  69. {
  70. "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/", "a", "b", "c", "d",
  71. "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
  72. "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
  73. "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "="
  74. };
  75. var empty =new byte();
  76. List<byte> byteMessage = Encoding.UTF8.GetBytes(text).ToList();
  77. StringBuilder outmessage;
  78. int messageLen = byteMessage.Count;
  79. int page = messageLen / 3;
  80. int use = 0;
  81. if ((use = messageLen % 3) > 0)
  82. {
  83. for (int i = 0; i < 3 - use; i++) byteMessage.Add(empty);
  84. page++;
  85. }
  86. outmessage = new StringBuilder();
  87. for (int i = 0; i < page; i++)
  88. {
  89. List<int> instr = new List<int>
  90. {
  91. byteMessage[i * 3],
  92. byteMessage[i * 3 + 1],
  93. byteMessage[i * 3 + 2]
  94. };
  95. int[] outstr = new int[4];
  96. outstr[0] = instr[0] >> 2;
  97. outstr[1] = ((instr[0] & 0x03) << 4) ^ (instr[1] >> 4);
  98. if (instr[1] != empty)
  99. {
  100. outstr[2] = ((instr[1] & 0x0f) << 2) ^ (instr[2] >> 6);
  101. }
  102. else
  103. {
  104. outstr[2] = 64;
  105. }
  106. if (instr[2] != empty)
  107. {
  108. outstr[3] = (instr[2] & 0x3f);
  109. }
  110. else
  111. {
  112. outstr[3] = 64;
  113. }
  114. outmessage.Append(base64Code[outstr[0]]);
  115. outmessage.Append(base64Code[outstr[1]]);
  116. outmessage.Append(base64Code[outstr[2]]);
  117. outmessage.Append(base64Code[outstr[3]]);
  118. }
  119. var finalStr = "Symmetry_" + outmessage.ToString();
  120. //Console.WriteLine(finalStr);
  121. return finalStr;
  122. }
  123. catch (Exception e)
  124. {
  125. throw e;
  126. }
  127. }
  128. public static async Task<string> AdminLogin(string account,string pwd)
  129. {
  130. try
  131. {
  132. var adminPWD=SymmetryEncrypt(pwd);
  133. var lonInRequest = new LoginRequest()
  134. {
  135. AdminName = account.ToLower(),
  136. Password = adminPWD
  137. };
  138. var result =
  139. await _managementService.AdminLogin(lonInRequest);
  140. if(result == null)
  141. {
  142. return "SignOrLoginFail";
  143. }
  144. if (result.LoginState == LoginStateEnum.Succeed)
  145. {
  146. URMConfig.ManagementURL = result?.ManagementUrl;
  147. URMConfig.URMUserType = URMUserType.Admin;
  148. URMConfig.Token = result?.Token;
  149. var tryLoginResult = _localLoginService.Login(account.ToLower(), pwd, false);
  150. if (tryLoginResult == Module.Workstation.LoginResult.LoginSuccess)
  151. {
  152. return "success";
  153. }
  154. if (tryLoginResult == Module.Workstation.LoginResult.InvalidPassowrd)
  155. {
  156. _localUserService.UpdatePasswordForAdmin(account.ToLower(), pwd, pwd);
  157. }
  158. if (tryLoginResult == Module.Workstation.LoginResult.PasswordNotReset)
  159. {
  160. _oldAdminPassword = pwd;
  161. }
  162. return "success";
  163. }
  164. if (result.LoginState == LoginStateEnum.PasswordIncorrect)
  165. {
  166. return "PasswordIncorrect";
  167. }
  168. return "SignOrLoginFail";
  169. }
  170. catch (Exception ex)
  171. {
  172. if (ex is RpcException rpcException)
  173. {
  174. Logger.WriteLineWarn($"Error happened while client login for urm work station {rpcException}");
  175. if (rpcException.ErrorCode == 6037)
  176. {
  177. return "Usernotregistered";
  178. }
  179. else if(rpcException.ErrorCode == 9020)
  180. {
  181. return "NoFoundDongle";//未读取到有效的License信息
  182. }
  183. else if (rpcException.ErrorCode == 6062)
  184. {
  185. return "AccountIsLocked";//密码错误次数过多,账号已锁定,次日解锁
  186. }
  187. }
  188. else
  189. {
  190. Logger.WriteLineError($"Error happened while client login for urm work station {ex}");
  191. }
  192. return "SignOrLoginFail";
  193. }
  194. }
  195. public static async Task<Module.Workstation.LoginResult> DongleLogin(string anyCode, string labDongle, string anyAccount)
  196. {
  197. try
  198. {
  199. var lonInRequest = new CommonLoginRequest()
  200. {
  201. AnyAccount = anyAccount,
  202. AnyCode = anyCode,
  203. LabDongle = labDongle,
  204. Platform = Platform.Windows,
  205. LoginSource = LoginSource.PC,
  206. LanguageCode = GetLanguageCode()
  207. };
  208. var result =
  209. await _loginService.CommonLoginAsync(lonInRequest);
  210. if (result == null)
  211. {
  212. Logger.WriteLineError($"Login result is null");
  213. return Module.Workstation.LoginResult.UserNotExist;
  214. }
  215. if (result.LoginState == LoginStateEnum.Succeed)
  216. {
  217. URMConfig.Token = result.Token;
  218. var userInfoResult =
  219. await _userService.GetUserInfoAsync(new GetUserInfoRequest()
  220. {
  221. Token = URMConfig.Token
  222. });
  223. URMConfig.UserName = userInfoResult.UserName;
  224. URMConfig.Pwd =_fixedPassword;
  225. URMConfig.Code = userInfoResult.UserCode;
  226. try
  227. {
  228. if (File.Exists(URMConfig.StatesPath))
  229. {
  230. var statesText = File.ReadAllText(URMConfig.StatesPath);
  231. var oldState = JsonConvert.DeserializeObject<userData>(statesText);
  232. oldState.user.fullName = userInfoResult.FullName;
  233. oldState.user.account = userInfoResult.UserName;
  234. oldState.user.password = URMConfig.Pwd;
  235. oldState.user.isAutoLogin = true;
  236. oldState.user.isAgreeClause = true;
  237. oldState.user.code = userInfoResult.UserCode;
  238. oldState.user.token = result.Token;
  239. File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(oldState));
  240. }
  241. else
  242. {
  243. var newState = new userData();
  244. newState.app = new app()
  245. {
  246. locale = new List<string> { "zh", "CN" },
  247. issFollowSystemLocale = false,
  248. enableVidLoopPlayback = false
  249. };
  250. newState.user = new user()
  251. {
  252. fullName = userInfoResult.FullName,
  253. account = userInfoResult.UserName,
  254. password = _fixedPassword,
  255. isAutoLogin = true,
  256. isAgreeClause = true,
  257. code = userInfoResult.UserCode,
  258. token = result.Token
  259. };
  260. File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
  261. }
  262. }
  263. catch (Exception ex)
  264. {
  265. Logger.WriteLineError("Error hanppened while read states {ex}");
  266. var newState = new userData();
  267. newState.app = new app()
  268. {
  269. locale = new List<string> { "zh", "CN" },
  270. issFollowSystemLocale = false,
  271. enableVidLoopPlayback = false
  272. };
  273. newState.user = new user()
  274. {
  275. fullName = userInfoResult.FullName,
  276. account = userInfoResult.UserName,
  277. password = _fixedPassword,
  278. isAutoLogin = true,
  279. isAgreeClause = true,
  280. code = userInfoResult.UserCode,
  281. token = result.Token
  282. };
  283. File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
  284. }
  285. var user = new Database.User
  286. {
  287. Account = userInfoResult.UserName,
  288. Description = userInfoResult.FullName,
  289. FullName = userInfoResult.FullName,
  290. Type = UserType.Normal,
  291. Status = StateType.Active,
  292. Password = Md5Builder.GetMd5Code(_fixedPassword),
  293. DigitalSignature = null
  294. };
  295. var tryLoginState = _localLoginService.Login(URMConfig.UserName, URMConfig.Pwd, false,false);
  296. if (tryLoginState == Module.Workstation.LoginResult.LoginSuccess)
  297. {
  298. return Module.Workstation.LoginResult.LoginSuccess;
  299. }
  300. else
  301. {
  302. var addUserState = _localUserService.CreateUser(user);
  303. if (addUserState == Module.Workstation.CreateUserResult.Successful)
  304. {
  305. var tryLoginStateAfter = _localLoginService.Login(URMConfig.UserName, URMConfig.Pwd, false, false);
  306. if (tryLoginStateAfter == Module.Workstation.LoginResult.LoginSuccess)
  307. {
  308. Logger.WriteLineInfo("Dongle Login local success");
  309. }
  310. else
  311. {
  312. Logger.WriteLineInfo("Dongle Login local failed");
  313. }
  314. return Module.Workstation.LoginResult.LoginSuccess;
  315. }
  316. }
  317. return Module.Workstation.LoginResult.LoginSuccess;
  318. }
  319. else
  320. {
  321. Logger.WriteLineError($"Server dongle login failed");
  322. return Module.Workstation.LoginResult.UserNotExist;
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. Logger.WriteLineError($"Error happened while do {ex}");
  328. return Module.Workstation.LoginResult.Unknown;
  329. }
  330. }
  331. public static async Task<string> ClientLogin(string account, string pwd)
  332. {
  333. try
  334. {
  335. if (account.ToLower() == "admin")
  336. {
  337. return "AdminError";//admin 不正确
  338. }
  339. var clientPWD = SymmetryEncrypt(pwd);
  340. var lonInRequest = new CommonLoginRequest()
  341. {
  342. AnyAccount = account,
  343. Password = clientPWD,
  344. Platform=Platform.Windows,
  345. LoginSource=LoginSource.PC,
  346. LanguageCode=GetLanguageCode()
  347. };
  348. var result =
  349. await _loginService.CommonLoginAsync(lonInRequest);
  350. if (result == null)
  351. {
  352. return "ErrorException";//异常错误
  353. }
  354. if (result.LoginState == LoginStateEnum.Succeed)
  355. {
  356. URMConfig.UserName = account;
  357. URMConfig.Pwd= pwd;
  358. URMConfig.Token = result.Token;
  359. var result2 =
  360. await _userService.GetUserInfoAsync(new GetUserInfoRequest()
  361. {
  362. Token = URMConfig.Token
  363. });
  364. URMConfig.Code = result2.UserCode;
  365. var tryLoginState = _localLoginService.Login(account, pwd, false);
  366. var needCreateState = false;
  367. if (File.Exists(URMConfig.StatesPath))
  368. {
  369. var statesText = File.ReadAllText(URMConfig.StatesPath);
  370. try
  371. {
  372. var oldState = JsonConvert.DeserializeObject<userData>(statesText);
  373. oldState.user.fullName = result2.FullName;
  374. oldState.user.account = account.ToLower();
  375. oldState.user.password = pwd;
  376. oldState.user.isAutoLogin = true;
  377. oldState.user.isAgreeClause = true;
  378. oldState.user.code = result2.UserCode;
  379. oldState.user.token = result.Token.Substring(1,5);
  380. File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(oldState));
  381. }
  382. catch (Exception ex)
  383. {
  384. Logger.WriteLineError($"Error happened while converting states {ex}");
  385. File.Delete(URMConfig.StatesPath);
  386. needCreateState = true;
  387. }
  388. }
  389. if (needCreateState)
  390. {
  391. var newState = new userData();
  392. newState.app = new app()
  393. {
  394. locale = new List<string> { "zh", "CN" },
  395. issFollowSystemLocale = false,
  396. enableVidLoopPlayback = false
  397. };
  398. newState.user = new user()
  399. {
  400. fullName = result2.FullName,
  401. account = account.ToLower(),
  402. password = pwd,
  403. isAutoLogin = true,
  404. isAgreeClause = true,
  405. code = result2.UserCode,
  406. token = result.Token
  407. };
  408. File.WriteAllText(URMConfig.StatesPath, JsonConvert.SerializeObject(newState));
  409. }
  410. var user = new Database.User
  411. {
  412. Account = account,
  413. Description = result2.FullName,
  414. FullName = result2.FullName,
  415. Type = UserType.Normal,
  416. Status = StateType.Active,
  417. Password = Md5Builder.GetMd5Code(pwd),
  418. DigitalSignature = new byte[1],
  419. };
  420. if (tryLoginState ==Module.Workstation.LoginResult.UserNotExist)
  421. {
  422. _localUserService.CreateUser(user);
  423. }
  424. if (tryLoginState == Module.Workstation.LoginResult.InvalidPassowrd)
  425. {
  426. var forceUpdatePasswordResult = _localUserService.ForceUpdatePasswordByName(account, pwd);
  427. if (forceUpdatePasswordResult == DbOperationResult.UpdateSuccess)
  428. {
  429. return "success";
  430. }
  431. else
  432. {
  433. return "PasswordIncorrect";
  434. }
  435. }
  436. return "success";
  437. }
  438. else if(result.LoginState == LoginStateEnum.PasswordIncorrect)
  439. {
  440. return "PasswordIncorrect";
  441. }
  442. else if (result.LoginState == LoginStateEnum.SignOrLoginFail)
  443. {
  444. return "SignOrLoginFail";
  445. }
  446. return "SignOrLoginFail";
  447. }
  448. catch (Exception ex)
  449. {
  450. if(ex is RpcException rpcException)
  451. {
  452. Logger.WriteLineWarn($"Error happened while client login for urm work station {rpcException}");
  453. if (rpcException.ErrorCode== 6037)
  454. {
  455. return "Usernotregistered";
  456. }
  457. }
  458. else
  459. {
  460. Logger.WriteLineError($"Error happened while client login for urm work station {ex}");
  461. }
  462. return "SignOrLoginFail";
  463. }
  464. }
  465. private static string GetLanguageCode()
  466. {
  467. return TranslateHelper.CurrentLanguage;
  468. }
  469. internal static async Task<bool> ResetPassword(string account, string newPassword, string oldPassword="abc123")
  470. {
  471. try {
  472. var isAdmin = account.ToLower() == "admin";
  473. if (isAdmin)
  474. {
  475. var result = await _managementService.ModifyAdminPassword(new ModifyAdminPasswordRequest
  476. {
  477. Token = URMConfig.Token,
  478. NewPassword = SymmetryEncrypt(newPassword),
  479. OldPassword = SymmetryEncrypt(_oldAdminPassword)
  480. });
  481. if (result)
  482. {
  483. var resultLogin = await AdminLogin(account, newPassword);
  484. if (resultLogin == "Success")
  485. {
  486. return true;
  487. }
  488. }
  489. return result;
  490. }
  491. else
  492. {
  493. var result = await _loginService.ModifyPasswordAsync(new ModifyPasswordRequest()
  494. {
  495. Token = URMConfig.Token,
  496. Password = SymmetryEncrypt(URMConfig.Pwd),
  497. NewPassword = SymmetryEncrypt(newPassword),
  498. AnyAccount = account,
  499. });
  500. if (result)
  501. {
  502. Logger.WriteLineInfo("Reset password success");
  503. var resultLogin = await ClientLogin(account, newPassword);
  504. if (resultLogin== "success")
  505. {
  506. return true;
  507. }
  508. }
  509. return result;
  510. }
  511. }
  512. catch (Exception ex)
  513. {
  514. Logger.WriteLineError($"Error happened while Admin login for urm work station {ex}");
  515. return false;
  516. }
  517. }
  518. }
  519. }