2
0

fix: license防篡改系统时间

This commit is contained in:
caiyuchao
2025-04-27 16:35:00 +08:00
parent ffd27558d1
commit 2da336a69f
3 changed files with 40 additions and 6 deletions

View File

@@ -184,7 +184,7 @@ public class CustomLicenseManager extends LicenseManager {
private boolean verityTimeFile() { private boolean verityTimeFile() {
try { try {
// 取出时间文件 // 取出时间文件
BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH)); BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME));
String fileJson = reader.readLine(); String fileJson = reader.readLine();
reader.close(); reader.close();
String decryptedJson = LicenseUtils.decrypt(fileJson); String decryptedJson = LicenseUtils.decrypt(fileJson);

View File

@@ -6,7 +6,9 @@ package org.wfc.common.license.domain;
*/ */
public class LicenseConstants { public class LicenseConstants {
public final static String TIME_PATH = "/var/lib/pro/etc/serve/production.json"; public final static String TIME_PATH = "/var/lib/pro/etc/server/";
public final static String TIME_FILE_NAME = "production.json";
public static final String KEY = "wfcwanfiAdmin6666"; public static final String KEY = "wfcwanfiAdmin6666";
} }

View File

@@ -1,5 +1,6 @@
package org.wfc.common.license.runner; package org.wfc.common.license.runner;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data; import lombok.Data;
@@ -15,9 +16,11 @@ import org.wfc.common.license.domain.LicenseVerifyParam;
import org.wfc.common.license.serverinfo.AbstractServerInfos; import org.wfc.common.license.serverinfo.AbstractServerInfos;
import org.wfc.common.license.utils.LicenseUtils; import org.wfc.common.license.utils.LicenseUtils;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@@ -40,7 +43,7 @@ public class LicenseCheckRunner implements ApplicationRunner {
// 启动定时任务 // 启动定时任务
public void startTimer() { public void startTimer() {
scheduledFuture = scheduler.scheduleAtFixedRate(this::timer, 0, 30, TimeUnit.SECONDS); scheduledFuture = scheduler.scheduleAtFixedRate(this::timer, 0, 30, TimeUnit.SECONDS);
scheduler.scheduleAtFixedRate(this::fileTimer, 0, 60, TimeUnit.SECONDS); scheduler.scheduleAtFixedRate(this::fileTimer, 0, 40, TimeUnit.SECONDS);
} }
// 停止定时任务 // 停止定时任务
@@ -103,7 +106,7 @@ public class LicenseCheckRunner implements ApplicationRunner {
* 5秒检测一次不能太快也不能太慢 * 5秒检测一次不能太快也不能太慢
*/ */
protected void fileTimer() { protected void fileTimer() {
createTimeFile(); createAndVerifyTimeFile();
} }
private void install() { private void install() {
@@ -165,11 +168,39 @@ public class LicenseCheckRunner implements ApplicationRunner {
} }
} }
private void createTimeFile() { private void createAndVerifyTimeFile() {
boolean isCreate = false;
String decryptedJson = "";
try {
// 取出时间文件
BufferedReader reader = new BufferedReader(new FileReader(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME));
String fileJson = reader.readLine();
reader.close();
decryptedJson = LicenseUtils.decrypt(fileJson);
} catch (Exception e) {
isCreate = true;
log.debug("first read time file error {}", e.getMessage());
}
if (StrUtil.isBlank(decryptedJson)) {
isCreate = true;
}
if (isCreate) {
// 不存在则创建
FileUtil.mkdir(LicenseConstants.TIME_PATH);
updateTimeFile();
} else {
// 存在则验证时间是否被篡改,不被篡改则更新当前时间
if (Long.parseLong(decryptedJson) < System.currentTimeMillis()) {
updateTimeFile();
}
}
}
private void updateTimeFile() {
try { try {
String currentTime = System.currentTimeMillis() + ""; String currentTime = System.currentTimeMillis() + "";
String encryptedJson = LicenseUtils.encrypt(currentTime); String encryptedJson = LicenseUtils.encrypt(currentTime);
FileWriter writer = new FileWriter(LicenseConstants.TIME_PATH); FileWriter writer = new FileWriter(LicenseConstants.TIME_PATH + LicenseConstants.TIME_FILE_NAME);
writer.write(encryptedJson); writer.write(encryptedJson);
writer.close(); writer.close();
} catch (Exception e) { } catch (Exception e) {
@@ -223,6 +254,7 @@ public class LicenseCheckRunner implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
generate(); generate();
createAndVerifyTimeFile();
install(); install();
startTimer(); startTimer();
} }