package com.qianwen.smartman.common.utils;
|
|
import com.alibaba.fastjson.JSONObject;
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileWriter;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.nio.charset.StandardCharsets;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Scanner;
|
import org.smartboot.license.client.License;
|
import org.smartboot.license.client.LicenseEntity;
|
import org.smartboot.license.client.LicenseException;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.SpringUtil;
|
import com.qianwen.smartman.modules.system.dto.LicenseDetailDTO;
|
import com.qianwen.smartman.modules.system.entity.Tenant;
|
import com.qianwen.smartman.modules.system.mapper.TenantMapper;
|
import org.springframework.util.DigestUtils;
|
|
|
public class LicenseUtil {
|
private static final String CPU_LINUX = "dmidecode -t processor | grep ID | head -n 1";
|
private static final String MainBORD_LINUX = "dmidecode | grep 'Serial Number' | head -n 1";
|
private static Map<String, String> licenseMap = new HashMap<>();
|
private static License license = new License();
|
private static final String OS = System.getProperty("os.name");
|
private static final TenantMapper tenantMapper = SpringUtil.getBean(TenantMapper.class);
|
private static final String PROFILE = SpringUtil.getContext().getEnvironment().getActiveProfiles()[0];
|
private static final String DEVICE_MAC = getMac();
|
|
|
public static String getMac() {
|
if (DEVICE_MAC != null) {
|
return DEVICE_MAC;
|
}
|
try {
|
String str = getCpuId() + getMainBordId();
|
if ("".equals(str)) {
|
return null;
|
}
|
return DigestUtils.md5DigestAsHex(str.getBytes(StandardCharsets.UTF_8)).toUpperCase();
|
} catch (IOException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
public static String getCpuId() throws IOException {
|
String result;
|
if (OS.contains("Windows")) {
|
String[] windows = {"wmic", "cpu", "get", "ProcessorId"};
|
System.getProperty("os.name");
|
Process process = Runtime.getRuntime().exec(windows);
|
process.getOutputStream().close();
|
Scanner sc = new Scanner(process.getInputStream(), "utf-8");
|
sc.next();
|
result = sc.next();
|
} else {
|
result = execCommandLinux(CPU_LINUX);
|
}
|
System.out.println("CPU ID:" + result);
|
return result;
|
}
|
|
public static String getMainBordId() {
|
String result = "";
|
if (OS.contains("Windows")) {
|
try {
|
File file = File.createTempFile("realhowto", ".vbs");
|
file.deleteOnExit();
|
FileWriter fw = new FileWriter(file);
|
fw.write("Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\nSet colItems = objWMIService.ExecQuery _ \n (\"Select * from Win32_BaseBoard\") \nFor Each objItem in colItems \n Wscript.Echo objItem.SerialNumber \n exit for ' do the first cpu only! \nNext \n");
|
fw.close();
|
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
|
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
while (true) {
|
String line = input.readLine();
|
if (line == null) {
|
break;
|
}
|
result = result + line;
|
}
|
input.close();
|
} catch (Exception e) {
|
System.out.print("获取主板信息错误");
|
}
|
} else {
|
result = execCommandLinux(MainBORD_LINUX);
|
}
|
System.out.println("Main BordID:" + result);
|
return result.trim();
|
}
|
|
public static String execCommandLinux(String command) {
|
String result = "";
|
try {
|
Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", command});
|
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
String line = br.readLine();
|
if (line != null) {
|
result = result + line;
|
}
|
br.close();
|
} catch (IOException e) {
|
System.out.print("命令执行错误");
|
}
|
return result;
|
}
|
|
public static String getEncryptCode(String tenantId) {
|
if (licenseMap.get(tenantId) != null) {
|
return licenseMap.get(tenantId);
|
}
|
Tenant tenant = tenantMapper.selectOne(Lambda.eq(Tenant::getTenantId, tenantId));
|
if (Func.isNotEmpty(tenant.getBladeLicense())) {
|
licenseMap.put(Func.toStr(tenant.getTenantId()), tenant.getBladeLicense());
|
}
|
return tenant.getBladeLicense();
|
}
|
|
public static void setEncryptCode(String tenantId, String encrypt) throws LicenseException {
|
LicenseEntity licenseEntity = license.loadLicense(encrypt);
|
macValid(licenseEntity);
|
licenseMap.put(tenantId, encrypt);
|
Tenant tenant = new Tenant();
|
tenant.setTenantId(tenantId);
|
tenant.setBladeLicense(encrypt);
|
tenantMapper.update(tenant, Lambda.eq(Tenant::getTenantId, tenant.getTenantId()));
|
}
|
|
public static LicenseDetailDTO loadLicense(String tenantId) throws LicenseException {
|
if (!"dev".equals(PROFILE)) {
|
String encryptCode = getEncryptCode(tenantId);
|
LicenseEntity entity = license.loadLicense(encryptCode);
|
LicenseDetailDTO detailDTO = macValid(entity);
|
return detailDTO;
|
}
|
return null;
|
}
|
|
public static LicenseDetailDTO macValid(LicenseEntity entity) {
|
String json = new String(entity.getData());
|
LicenseDetailDTO detailDTO = (LicenseDetailDTO) JSONObject.parseObject(json, LicenseDetailDTO.class);
|
if (detailDTO.getMac() == null || !detailDTO.getMac().equals(DEVICE_MAC)) {
|
throw new LicenseException(MessageUtils.message("system.license.device.err", new Object[0]));
|
}
|
return detailDTO;
|
}
|
|
public static Map<String, String> getLicenseMap() {
|
return licenseMap;
|
}
|
}
|