#
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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.zy.system.entity.license;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public final class LicenseBindingSupport {
 
    private LicenseBindingSupport() {
    }
 
    public static boolean isV2Extra(Object extra) {
        if (extra == null) {
            return false;
        }
        if (extra instanceof LicenseBindModel) {
            return true;
        }
        if (extra instanceof JSONObject) {
            return isV2Json((JSONObject) extra);
        }
        if (extra instanceof String) {
            String text = ((String) extra).trim();
            if (isBlank(text) || !text.startsWith("{")) {
                return false;
            }
            return isV2Json(JSON.parseObject(text));
        }
        return false;
    }
 
    public static LicenseBindModel parseBindModel(Object extra) {
        if (extra == null) {
            return null;
        }
        if (extra instanceof LicenseBindModel) {
            return normalizeBindModel((LicenseBindModel) extra);
        }
        if (extra instanceof JSONObject) {
            return normalizeBindModel(((JSONObject) extra).toJavaObject(LicenseBindModel.class));
        }
        if (extra instanceof String) {
            String text = ((String) extra).trim();
            if (isBlank(text)) {
                return null;
            }
            return normalizeBindModel(JSON.parseObject(text, LicenseBindModel.class));
        }
        return normalizeBindModel(JSON.parseObject(JSON.toJSONString(extra), LicenseBindModel.class));
    }
 
    public static LicenseCheck parseLegacyLicenseCheck(Object extra) {
        if (extra == null) {
            return null;
        }
        if (extra instanceof LicenseCheck) {
            return (LicenseCheck) extra;
        }
        if (extra instanceof JSONObject) {
            return ((JSONObject) extra).toJavaObject(LicenseCheck.class);
        }
        if (extra instanceof String) {
            String text = ((String) extra).trim();
            if (isBlank(text)) {
                return null;
            }
            return JSON.parseObject(text, LicenseCheck.class);
        }
        return JSON.parseObject(JSON.toJSONString(extra), LicenseCheck.class);
    }
 
    public static boolean matches(LicenseBindModel licenseBind, LicenseCheck serverCheck) {
        if (licenseBind == null) {
            return false;
        }
        if ("UNLIMITED".equalsIgnoreCase(trimToEmpty(licenseBind.getBindMode()))) {
            return true;
        }
        List<LicenseNodeCheck> nodes = licenseBind.getNodes();
        if (nodes == null || nodes.isEmpty()) {
            return true;
        }
        for (LicenseNodeCheck node : nodes) {
            if (matchesNode(node, serverCheck)) {
                return true;
            }
        }
        return false;
    }
 
    private static boolean isV2Json(JSONObject jsonObject) {
        if (jsonObject == null) {
            return false;
        }
        return jsonObject.containsKey("nodes")
                || jsonObject.containsKey("bindMode")
                || jsonObject.containsKey("matchMode")
                || Integer.valueOf(2).equals(jsonObject.getInteger("version"));
    }
 
    private static LicenseBindModel normalizeBindModel(LicenseBindModel source) {
        if (source == null) {
            return null;
        }
        LicenseBindModel target = new LicenseBindModel();
        target.setVersion(source.getVersion() == null ? 2 : source.getVersion());
        target.setBindMode(isBlank(source.getBindMode()) ? "MULTI_NODE" : source.getBindMode().trim());
        target.setMatchMode(isBlank(source.getMatchMode()) ? "ANY" : source.getMatchMode().trim());
 
        List<LicenseNodeCheck> normalizedNodes = new ArrayList<>();
        if (source.getNodes() != null) {
            for (LicenseNodeCheck node : source.getNodes()) {
                if (node == null) {
                    continue;
                }
                LicenseNodeCheck normalizedNode = new LicenseNodeCheck();
                normalizedNode.setNodeId(trimToEmpty(node.getNodeId()));
                normalizedNode.setIpAddress(normalizeList(node.getIpAddress(), false));
                normalizedNode.setMacAddress(normalizeList(node.getMacAddress(), true));
                normalizedNode.setCpuSerial(trimToEmpty(node.getCpuSerial()));
                normalizedNode.setMainBoardSerial(trimToEmpty(node.getMainBoardSerial()));
                normalizedNodes.add(normalizedNode);
            }
        }
        target.setNodes(normalizedNodes);
        return target;
    }
 
    private static boolean matchesNode(LicenseNodeCheck node, LicenseCheck serverCheck) {
        if (node == null || serverCheck == null) {
            return false;
        }
        if (!checkList(node.getIpAddress(), serverCheck.getIpAddress())) {
            return false;
        }
        if (!checkList(node.getMacAddress(), serverCheck.getMacAddress())) {
            return false;
        }
        if (!checkSerial(node.getMainBoardSerial(), serverCheck.getMainBoardSerial())) {
            return false;
        }
        return checkSerial(node.getCpuSerial(), serverCheck.getCpuSerial());
    }
 
    private static boolean checkList(List<String> expectedList, List<String> actualList) {
        if (expectedList == null || expectedList.isEmpty()) {
            return true;
        }
        if (actualList == null || actualList.isEmpty()) {
            return false;
        }
        for (String expected : expectedList) {
            if (!isBlank(expected) && actualList.contains(expected.trim())) {
                return true;
            }
        }
        return false;
    }
 
    private static boolean checkSerial(String expectedSerial, String actualSerial) {
        if (isBlank(expectedSerial)) {
            return true;
        }
        return !isBlank(actualSerial) && expectedSerial.equals(actualSerial);
    }
 
    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 boolean isBlank(String value) {
        return value == null || value.trim().isEmpty();
    }
 
    private static String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }
}