yangys
2024-10-30 25db770e621f1259b8d5b7fd514207f7481c2d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
    }
}