#
Junjie
2 天以前 686fe55892de7bf8d206cddbead77a5fbdb0e091
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
package com.zy.system.entity.license;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
 
public class LicenseUtils {
 
    /**
     * 获取当前服务器需要额外校验的License参数
     */
    public static LicenseCheck getServerInfos(){
        //操作系统类型
        String osName = System.getProperty("os.name").toLowerCase();
        osName = osName.toLowerCase();
        AbstractServerInfos abstractServerInfos = null;
 
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        }else{//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }
 
        return abstractServerInfos.getServerInfos();
    }
 
    /**
     * 生成请求码,请求码中包含许可证名称和当前服务器的硬件信息。
     */
    public static String buildRequestCode(String subject) {
        return buildRequestCode(subject, getServerInfos());
    }
 
    public static String buildRequestCode(String subject, LicenseCheck licenseCheck) {
        if (isBlank(subject)) {
            throw new IllegalArgumentException("许可证名称不能为空");
        }
 
        LicenseCheck normalized = normalizeLicenseCheck(licenseCheck == null ? new LicenseCheck() : licenseCheck);
        JSONObject payload = new JSONObject(true);
        payload.put("version", 2);
        payload.put("subject", subject);
        payload.put("licenseBind", buildBindModel(normalized));
        return Base64.getEncoder().encodeToString(JSON.toJSONString(payload).getBytes(StandardCharsets.UTF_8));
    }
 
    private static JSONObject buildBindModel(LicenseCheck licenseCheck) {
        JSONObject bindModel = new JSONObject(true);
        bindModel.put("version", 2);
        bindModel.put("bindMode", "MULTI_NODE");
        bindModel.put("matchMode", "ANY");
 
        JSONObject node = new JSONObject(true);
        node.put("nodeId", getNodeId());
        node.put("ipAddress", toJsonArray(licenseCheck.getIpAddress()));
        node.put("macAddress", toJsonArray(licenseCheck.getMacAddress()));
        node.put("cpuSerial", trimToEmpty(licenseCheck.getCpuSerial()));
        node.put("mainBoardSerial", trimToEmpty(licenseCheck.getMainBoardSerial()));
 
        JSONArray nodes = new JSONArray();
        nodes.add(node);
        bindModel.put("nodes", nodes);
        return bindModel;
    }
 
    private static LicenseCheck normalizeLicenseCheck(LicenseCheck source) {
        LicenseCheck target = new LicenseCheck();
        target.setIpAddress(normalizeList(source.getIpAddress(), false));
        target.setMacAddress(normalizeList(source.getMacAddress(), true));
        target.setCpuSerial(trimToEmpty(source.getCpuSerial()));
        target.setMainBoardSerial(trimToEmpty(source.getMainBoardSerial()));
        return target;
    }
 
    private static List<String> normalizeList(List<String> values, boolean upperCase) {
        List<String> result = new ArrayList<>();
        if (values == null || values.isEmpty()) {
            return result;
        }
        for (String value : values) {
            if (isBlank(value)) {
                continue;
            }
            String normalized = value.trim();
            normalized = upperCase ? normalized.toUpperCase() : normalized.toLowerCase();
            if (!result.contains(normalized)) {
                result.add(normalized);
            }
        }
        Collections.sort(result);
        return result;
    }
 
    private static JSONArray toJsonArray(List<String> values) {
        JSONArray array = new JSONArray();
        if (values != null && !values.isEmpty()) {
            array.addAll(values);
        }
        return array;
    }
 
    private static String getNodeId() {
        try {
            String hostName = InetAddress.getLocalHost().getHostName();
            if (!isBlank(hostName)) {
                return hostName.trim();
            }
        } catch (Exception ignored) {
        }
        return "node-1";
    }
 
    private static boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }
 
    private static String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
 
}