123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.qmth.ops.biz.service;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.qmth.boot.core.solar.model.AppLicense;
- import com.qmth.boot.core.solar.model.OrgInfo;
- import com.qmth.boot.tools.codec.CodecUtils;
- import com.qmth.boot.tools.crypto.AES;
- import com.qmth.boot.tools.crypto.RSA;
- import com.qmth.boot.tools.crypto.RsaHelper;
- import com.qmth.boot.tools.device.DeviceInfo;
- import com.qmth.boot.tools.models.ByteArray;
- import com.qmth.ops.biz.domain.Deploy;
- import com.qmth.ops.biz.domain.Org;
- import com.qmth.ops.biz.query.OrgQuery;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import javax.validation.constraints.NotNull;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.ArrayList;
- import java.util.List;
- @Service
- public class LicenseService {
- private RsaHelper privateKey;
- @Resource
- private AppService appService;
- @Resource
- private OrgService orgService;
- @Resource
- private FileService fileService;
- public LicenseService(@Value("${admin.private-key}") String privateKeyFile) throws IOException {
- privateKey = RSA.getPrivateKey(ByteArray.fromFile(new File(privateKeyFile)).value());
- }
- public DeviceInfo parseDeviceInfo(MultipartFile file) throws Exception {
- return new ObjectMapper().readValue(privateKey.decrypt(file.getBytes()).value(), DeviceInfo.class);
- }
- public void buildLicense(@NotNull Deploy deploy, String deviceId, String version, @NotNull OutputStream ous)
- throws Exception {
- //build deploy info
- AppLicense license = new AppLicense();
- license.setId(deploy.getId());
- license.setName(deploy.getName());
- license.setControl(deploy.getControl());
- license.setOrgs(new ArrayList<>());
- //build org info list
- OrgQuery query = new OrgQuery();
- query.setDeployId(deploy.getId());
- query.setEnable(true);
- query.setPageNumber(1);
- query.setPageSize(100);
- while (true) {
- List<Org> orgs = orgService.listByQuery(query);
- if (orgs == null || orgs.isEmpty()) {
- break;
- }
- for (Org org : orgs) {
- OrgInfo info = org.buildOrgInfo(fileService.getServer());
- info.setLogo(fileService.getOrgLogo(org).toBase64());
- license.getOrgs().add(info);
- }
- query.setPageNumber(query.getPageNumber() + 1);
- }
- byte[] data = new ObjectMapper().writeValueAsBytes(license);
- ByteArrayOutputStream temp = new ByteArrayOutputStream();
- temp.write(CodecUtils.md5(data));
- temp.write(privateKey.encrypt(data).value());
- //不限设备与版本
- if (deviceId == null && version == null) {
- ous.write((byte) 0);
- ous.write(temp.toByteArray());
- }
- //限制设备,不限版本
- else if (deviceId != null && version == null) {
- ous.write((byte) 1);
- ous.write(AES.encrypt(temp.toByteArray(), deviceId.substring(0, 16), deviceId.substring(16)).value());
- }
- //不限设置,限制版本
- else if (deviceId == null && version != null) {
- String versionMd5 = ByteArray.md5(version).toHexString();
- ous.write((byte) 2);
- ous.write(AES.encrypt(temp.toByteArray(), versionMd5.substring(0, 16), versionMd5.substring(16)).value());
- }
- //限制设备与版本
- else {
- String versionMd5 = ByteArray.md5(version).toHexString();
- ous.write((byte) 3);
- ous.write(AES.encrypt(
- AES.encrypt(temp.toByteArray(), deviceId.substring(0, 16), deviceId.substring(16)).value(),
- versionMd5.substring(0, 16), versionMd5.substring(16)).value());
- }
- ous.close();
- }
- }
|