| | |
| | | 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 { |
| | | |
| | | /** |
| | |
| | | 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(); |
| | | } |
| | | |
| | | } |