LicenseService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.qmth.ops.biz.service;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.qmth.boot.core.solar.model.AppLicense;
  4. import com.qmth.boot.core.solar.model.OrgInfo;
  5. import com.qmth.boot.tools.codec.CodecUtils;
  6. import com.qmth.boot.tools.crypto.AES;
  7. import com.qmth.boot.tools.crypto.RSA;
  8. import com.qmth.boot.tools.crypto.RsaHelper;
  9. import com.qmth.boot.tools.device.DeviceInfo;
  10. import com.qmth.boot.tools.models.ByteArray;
  11. import com.qmth.ops.biz.domain.Deploy;
  12. import com.qmth.ops.biz.domain.Org;
  13. import com.qmth.ops.biz.query.OrgQuery;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.annotation.Resource;
  18. import javax.validation.constraints.NotNull;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. @Service
  26. public class LicenseService {
  27. private RsaHelper privateKey;
  28. @Resource
  29. private OrgService orgService;
  30. @Resource
  31. private FileService fileService;
  32. public LicenseService(@Value("${admin.private-key}") String privateKeyFile) throws IOException {
  33. privateKey = RSA.getPrivateKey(ByteArray.fromFile(new File(privateKeyFile)).value());
  34. }
  35. public DeviceInfo parseDeviceInfo(MultipartFile file) throws Exception {
  36. return new ObjectMapper().readValue(privateKey.decrypt(file.getBytes()).value(), DeviceInfo.class);
  37. }
  38. public void buildLicense(@NotNull Deploy deploy, String deviceId, String version, @NotNull OutputStream ous)
  39. throws Exception {
  40. //build deploy info
  41. AppLicense license = new AppLicense();
  42. license.setId(deploy.getId());
  43. license.setName(deploy.getName());
  44. license.setControl(deploy.getControl());
  45. license.setOrgs(new ArrayList<>());
  46. //TODO
  47. //后续需要补充app.code填充
  48. //build org info list
  49. OrgQuery query = new OrgQuery();
  50. query.setDeployId(deploy.getId());
  51. query.setEnable(true);
  52. query.setPageNumber(1);
  53. query.setPageSize(100);
  54. while (true) {
  55. List<Org> orgs = orgService.listByQuery(query);
  56. if (orgs == null || orgs.isEmpty()) {
  57. break;
  58. }
  59. for (Org org : orgs) {
  60. OrgInfo info = org.buildOrgInfo(fileService.getServer());
  61. info.setLogo(fileService.getOrgLogo(org).toBase64());
  62. license.getOrgs().add(info);
  63. }
  64. query.setPageNumber(query.getPageNumber() + 1);
  65. }
  66. byte[] data = new ObjectMapper().writeValueAsBytes(license);
  67. ByteArrayOutputStream temp = new ByteArrayOutputStream();
  68. temp.write(CodecUtils.md5(data));
  69. temp.write(privateKey.encrypt(data).value());
  70. //不限设备与版本
  71. if (deviceId == null && version == null) {
  72. ous.write((byte) 0);
  73. ous.write(temp.toByteArray());
  74. }
  75. //限制设备,不限版本
  76. else if (deviceId != null && version == null) {
  77. ous.write((byte) 1);
  78. ous.write(AES.encrypt(temp.toByteArray(), deviceId.substring(0, 16), deviceId.substring(16)).value());
  79. }
  80. //不限设置,限制版本
  81. else if (deviceId == null && version != null) {
  82. String versionMd5 = ByteArray.md5(version).toHexString();
  83. ous.write((byte) 2);
  84. ous.write(AES.encrypt(temp.toByteArray(), versionMd5.substring(0, 16), versionMd5.substring(16)).value());
  85. }
  86. //限制设备与版本
  87. else {
  88. String versionMd5 = ByteArray.md5(version).toHexString();
  89. ous.write((byte) 3);
  90. ous.write(AES.encrypt(
  91. AES.encrypt(temp.toByteArray(), deviceId.substring(0, 16), deviceId.substring(16)).value(),
  92. versionMd5.substring(0, 16), versionMd5.substring(16)).value());
  93. }
  94. ous.close();
  95. }
  96. }