Commit 66c50f7d by 宋祥

1.过滤许可证校验

parent ebd59c88
package com.baosight.iplat4j.core.license;
import com.baosight.iplat4j.core.log.Logger;
import com.baosight.iplat4j.core.log.LoggerFactory;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
/**
* 重写许可证校验类,无正式许可证的情况不做校验
*
* @author:songx
* @date:2023/6/28,16:00
*/
public class LicenseStub {
private static final int CHECK_INTERVAL = 86400000;
public static String ERROR;
public static boolean OK = false;
public static String AUTHORIZED_ENTERPRISE = null;
private static Logger LOG = LoggerFactory.getLogger(LicenseStub.class);
private static String LICENSE_FILE = "license.xml";
private static String LICENSE_FILE_PATH = null;
private static boolean flag = false;
private static LicenseManager LICENSE;
private static long lastCheckTime = 0L;
public LicenseStub() {
}
public static synchronized void init() {
File f = new File(LICENSE_FILE_PATH);
if (!f.exists()) {
ERROR = "许可证文件[" + LICENSE_FILE_PATH + "]不存在!";
LOG.error(ERROR);
}
LICENSE = LicenseManager.getInstance(LICENSE_FILE_PATH);
}
public static String[] getLicenseInfo() {
String[] info = new String[2];
if (checkLicense() && LICENSE != null) {
info[0] = "0";
info[1] = "许可证文件: " + LICENSE_FILE_PATH + "\n" + LICENSE.getAllInfo();
} else {
info[0] = "1";
info[1] = "许可证文件: " + LICENSE_FILE_PATH + "\n" + (ERROR == null ? "" : ERROR);
}
return info;
}
public static String[] checkLicense2() {
// 无需校验许可证 modify by songx at 20230628
return new String[]{"true", "", "9999999", "true"};
}
public static boolean checkLicense() {
String[] ret = checkLicense2();
return "true".equals(ret[0]);
}
public static void setLicenseDir(String dir) {
if (LICENSE_FILE_PATH == null) {
LOG.info("[LICENSE] check environment <PLAT_LICENSE>...");
String platLicense = System.getenv("PLAT_LICENSE");
if (platLicense != null) {
LOG.info("[LICENSE] environment <PLAT_LICENSE> defined as <" + platLicense + ">.");
File f = new File(platLicense);
if (f.isFile()) {
LICENSE_FILE_PATH = platLicense;
LOG.info("[LICENSE] use <" + LICENSE_FILE_PATH + ">.");
return;
}
LOG.info("[LICENSE] file <" + LICENSE_FILE_PATH + "> not exist.");
} else {
LOG.info("[LICENSE] environment <PLAT_LICENSE> not defined.");
}
LICENSE_FILE_PATH = dir + File.separator + "license" + File.separator + LICENSE_FILE;
LOG.info("[LICENSE] use <" + LICENSE_FILE_PATH + "> instead.");
}
}
private static void setError(String msg) {
OK = false;
ERROR = msg;
LOG.error("[LICENSE] " + msg);
LOG.error(msg);
}
static String checkMac(String macInLicense) {
if (macInLicense != null && macInLicense.trim().length() != 0) {
String[] macs = macInLicense.trim().split("(,|;|\\s)");
Set<String> hostMacs = getHostMac();
if (hostMacs == null) {
return "许可证验证失败,请在Java6或者以上环境运行!";
} else {
String[] var3 = macs;
int var4 = macs.length;
for (int var5 = 0; var5 < var4; ++var5) {
String mac = var3[var5];
String nmac = mac.replaceAll("(:|-)", ":").toUpperCase();
if (hostMacs.contains(nmac)) {
return null;
}
}
StringBuilder sb = new StringBuilder();
sb.append("许可的MAC地址不符:");
sb.append(Arrays.toString(macs));
sb.append("; ");
sb.append("本机MAC地址:");
sb.append(hostMacs);
return sb.toString();
}
} else {
return null;
}
}
private static Set<String> getHostMac() {
Enumeration interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException var14) {
LOG.error(var14.getMessage(), var14);
return Collections.EMPTY_SET;
}
HashSet macs = new HashSet();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = (NetworkInterface) interfaces.nextElement();
Object var3 = null;
byte[] address;
try {
Method getHardwareAddress = ni.getClass().getDeclaredMethod("getHardwareAddress", (Class[]) null);
address = (byte[]) ((byte[]) getHardwareAddress.invoke(ni, (Object[]) null));
} catch (NoSuchMethodException var9) {
LOG.error(var9.getMessage(), var9);
return null;
} catch (SecurityException var10) {
LOG.error(var10.getMessage(), var10);
return null;
} catch (IllegalAccessException var11) {
LOG.error(var11.getMessage(), var11);
return null;
} catch (IllegalArgumentException var12) {
LOG.error(var12.getMessage(), var12);
return null;
} catch (InvocationTargetException var13) {
LOG.error(var13.getMessage(), var13);
return null;
}
if (address != null && address.length != 0) {
StringBuilder sb = new StringBuilder(18);
byte[] var5 = address;
int var6 = address.length;
for (int var7 = 0; var7 < var6; ++var7) {
byte b = var5[var7];
if (sb.length() > 0) {
sb.append(':');
}
sb.append(String.format("%02x", b).toUpperCase());
}
String mac = sb.toString().trim();
if (mac.length() > 0) {
LOG.info("[LICENSE] local MAC: " + mac);
macs.add(mac);
}
}
}
return macs;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment