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 normalizeList(List values, boolean upperCase) { List 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 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(); } }