[*] 日常优化
This commit is contained in:
@@ -134,7 +134,7 @@ const defaultRTCPeerConnectionConfig = {
|
||||
// ICE代理服务器
|
||||
iceServers : [
|
||||
{
|
||||
// { "url": "stun:stun1.l.google.com:19302" },
|
||||
// { "urls": "stun:stun1.l.google.com:19302" },
|
||||
urls: [
|
||||
"stun:stun1.l.google.com:19302",
|
||||
"stun:stun2.l.google.com:19302",
|
||||
|
||||
@@ -12,37 +12,37 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public final class CloseableUtils {
|
||||
|
||||
private CloseableUtils() {
|
||||
}
|
||||
private CloseableUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭资源
|
||||
*
|
||||
* @param closeable 资源
|
||||
*/
|
||||
public static final void close(Closeable closeable) {
|
||||
try {
|
||||
if(closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("关闭资源异常", e);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 关闭资源
|
||||
*
|
||||
* @param closeable 资源
|
||||
*/
|
||||
public static final void close(Closeable closeable) {
|
||||
try {
|
||||
if(closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("关闭资源异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭资源
|
||||
*
|
||||
* @param closeable 资源
|
||||
*/
|
||||
public static final void close(AutoCloseable closeable) {
|
||||
try {
|
||||
if(closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("关闭资源异常", e);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 关闭资源
|
||||
*
|
||||
* @param closeable 资源
|
||||
*/
|
||||
public static final void close(AutoCloseable closeable) {
|
||||
try {
|
||||
if(closeable != null) {
|
||||
closeable.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("关闭资源异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public final class DateUtils {
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
private DateStyle(String format) {
|
||||
this.format = format;
|
||||
this.format = format;
|
||||
this.dateTimeFormatter = DateTimeFormatter.ofPattern(format);
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public final class DateUtils {
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
private TimeStyle(String format) {
|
||||
this.format = format;
|
||||
this.format = format;
|
||||
this.dateTimeFormatter = DateTimeFormatter.ofPattern(format);
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ public final class DateUtils {
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
private DateTimeStyle(String format) {
|
||||
this.format = format;
|
||||
this.format = format;
|
||||
this.dateTimeFormatter = DateTimeFormatter.ofPattern(format);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,249 +30,249 @@ import lombok.extern.slf4j.Slf4j;
|
||||
@Slf4j
|
||||
public final class ErrorUtils {
|
||||
|
||||
private ErrorUtils() {
|
||||
}
|
||||
private ErrorUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常映射
|
||||
*/
|
||||
private static final Map<Class<?>, MessageCode> CODE_MAPPING = new LinkedHashMap<>();
|
||||
/**
|
||||
* 异常映射
|
||||
*/
|
||||
private static final Map<Class<?>, MessageCode> CODE_MAPPING = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* 错误地址
|
||||
*/
|
||||
public static final String ERROR_PATH = "/error";
|
||||
/**
|
||||
* Servlet错误编码
|
||||
*/
|
||||
public static final String SERVLET_STATUS_CODE = "javax.servlet.error.status_code";
|
||||
/**
|
||||
* Servlet错误地址
|
||||
*/
|
||||
public static final String SERVLET_REQUEST_URI = "javax.servlet.error.request_uri";
|
||||
/**
|
||||
* Servlet错误异常
|
||||
*/
|
||||
public static final String EXCEPTION_SERVLET = "javax.servlet.error.exception";
|
||||
/**
|
||||
* SpringBoot异常
|
||||
*/
|
||||
public static final String EXCEPTION_SPRINGBOOT = "org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR";
|
||||
/**
|
||||
* 错误地址
|
||||
*/
|
||||
public static final String ERROR_PATH = "/error";
|
||||
/**
|
||||
* Servlet错误编码
|
||||
*/
|
||||
public static final String SERVLET_STATUS_CODE = "javax.servlet.error.status_code";
|
||||
/**
|
||||
* Servlet错误地址
|
||||
*/
|
||||
public static final String SERVLET_REQUEST_URI = "javax.servlet.error.request_uri";
|
||||
/**
|
||||
* Servlet错误异常
|
||||
*/
|
||||
public static final String EXCEPTION_SERVLET = "javax.servlet.error.exception";
|
||||
/**
|
||||
* SpringBoot异常
|
||||
*/
|
||||
public static final String EXCEPTION_SPRINGBOOT = "org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR";
|
||||
|
||||
/**
|
||||
* 注册异常(注意继承顺序)
|
||||
*
|
||||
* @param messageCode 状态编码
|
||||
* @param clazz 异常类型
|
||||
*/
|
||||
public static final void register(MessageCode messageCode, Class<?> clazz) {
|
||||
log.debug("注册状态编码异常映射:{} - {}", messageCode, clazz);
|
||||
synchronized (CODE_MAPPING) {
|
||||
CODE_MAPPING.put(clazz, messageCode);
|
||||
/**
|
||||
* 注册异常(注意继承顺序)
|
||||
*
|
||||
* @param messageCode 状态编码
|
||||
* @param clazz 异常类型
|
||||
*/
|
||||
public static final void register(MessageCode messageCode, Class<?> clazz) {
|
||||
log.debug("注册状态编码异常映射:{} - {}", messageCode, clazz);
|
||||
synchronized (CODE_MAPPING) {
|
||||
CODE_MAPPING.put(clazz, messageCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 错误消息
|
||||
*/
|
||||
public static final Message message(HttpServletRequest request, HttpServletResponse response) {
|
||||
return ErrorUtils.message(null, request, response);
|
||||
}
|
||||
/**
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 错误消息
|
||||
*/
|
||||
public static final Message message(HttpServletRequest request, HttpServletResponse response) {
|
||||
return ErrorUtils.message(null, request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param t 异常
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 错误消息
|
||||
*/
|
||||
public static final Message message(Throwable t, HttpServletRequest request, HttpServletResponse response) {
|
||||
final Message message;
|
||||
// 错误状态编码
|
||||
int status = ErrorUtils.globalStatus(request, response);
|
||||
// 全局异常
|
||||
final Object globalError = t == null ? ErrorUtils.globalError(request) : t;
|
||||
// 原始异常
|
||||
final Object rootError = ErrorUtils.rootException(globalError);
|
||||
if(rootError instanceof MessageCodeException messageCodeException) {
|
||||
// 状态编码异常
|
||||
final MessageCode messageCode = messageCodeException.getMessageCode();
|
||||
status = messageCode.getStatus();
|
||||
message = Message.fail(messageCode, messageCodeException.getMessage());
|
||||
} else if(rootError instanceof Throwable throwable) {
|
||||
// 未知异常:异常转换
|
||||
final MessageCode messageCode = ErrorUtils.messageCode(status, throwable);
|
||||
status = messageCode.getStatus();
|
||||
message = Message.fail(messageCode, ErrorUtils.message(messageCode, throwable));
|
||||
} else {
|
||||
// 没有异常
|
||||
final MessageCode messageCode = MessageCode.of(status);
|
||||
message = Message.fail(messageCode);
|
||||
}
|
||||
// 状态编码
|
||||
if(status == HttpServletResponse.SC_OK) {
|
||||
status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
response.setStatus(status);
|
||||
// 请求地址
|
||||
final String path = ErrorUtils.getFirstParams(
|
||||
request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI),
|
||||
request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH),
|
||||
request.getAttribute(SERVLET_REQUEST_URI),
|
||||
request.getServletPath()
|
||||
);
|
||||
// 请求参数
|
||||
final String query = ErrorUtils.getFirstParams(
|
||||
request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING),
|
||||
request.getQueryString()
|
||||
);
|
||||
// 请求方法
|
||||
final String method = request.getMethod();
|
||||
if(globalError instanceof Throwable) {
|
||||
log.error("""
|
||||
请求异常
|
||||
请求地址:{}
|
||||
请求参数:{}
|
||||
请求方法:{}
|
||||
错误信息:{}
|
||||
响应状态:{}
|
||||
""", path, query, method, message, status, globalError);
|
||||
} else {
|
||||
log.warn("""
|
||||
请求错误
|
||||
请求地址:{}
|
||||
请求参数:{}
|
||||
请求方法:{}
|
||||
错误信息:{}
|
||||
响应状态:{}
|
||||
原始信息:{}
|
||||
""", path, query, method, message, status, globalError);
|
||||
}
|
||||
final Map<String, String> body = new HashMap<>();
|
||||
body.put("path", path);
|
||||
body.put("query", query);
|
||||
body.put("method", method);
|
||||
message.setBody(body);
|
||||
return message;
|
||||
}
|
||||
/**
|
||||
* @param t 异常
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 错误消息
|
||||
*/
|
||||
public static final Message message(Throwable t, HttpServletRequest request, HttpServletResponse response) {
|
||||
final Message message;
|
||||
// 状态编码
|
||||
int status = ErrorUtils.globalStatus(request, response);
|
||||
// 全局异常
|
||||
final Object globalError = t == null ? ErrorUtils.globalError(request) : t;
|
||||
// 原始异常
|
||||
final Object rootError = ErrorUtils.rootException(globalError);
|
||||
if(rootError instanceof MessageCodeException messageCodeException) {
|
||||
// 状态编码异常
|
||||
final MessageCode messageCode = messageCodeException.getMessageCode();
|
||||
status = messageCode.getStatus();
|
||||
message = Message.fail(messageCode, messageCodeException.getMessage());
|
||||
} else if(rootError instanceof Throwable throwable) {
|
||||
// 未知异常:异常转换
|
||||
final MessageCode messageCode = ErrorUtils.messageCode(status, throwable);
|
||||
status = messageCode.getStatus();
|
||||
message = Message.fail(messageCode, ErrorUtils.message(messageCode, throwable));
|
||||
} else {
|
||||
// 没有异常
|
||||
final MessageCode messageCode = MessageCode.of(status);
|
||||
message = Message.fail(messageCode);
|
||||
}
|
||||
// 状态编码
|
||||
if(status == HttpServletResponse.SC_OK) {
|
||||
status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
|
||||
}
|
||||
response.setStatus(status);
|
||||
// 请求地址
|
||||
final String path = ErrorUtils.getFirstParams(
|
||||
request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI),
|
||||
request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH),
|
||||
request.getAttribute(SERVLET_REQUEST_URI),
|
||||
request.getServletPath()
|
||||
);
|
||||
// 请求参数
|
||||
final String query = ErrorUtils.getFirstParams(
|
||||
request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING),
|
||||
request.getQueryString()
|
||||
);
|
||||
// 请求方法
|
||||
final String method = request.getMethod();
|
||||
if(globalError instanceof Throwable) {
|
||||
log.error("""
|
||||
请求异常
|
||||
请求地址:{}
|
||||
请求参数:{}
|
||||
请求方法:{}
|
||||
错误信息:{}
|
||||
响应状态:{}
|
||||
""", path, query, method, message, status, globalError);
|
||||
} else {
|
||||
log.warn("""
|
||||
请求错误
|
||||
请求地址:{}
|
||||
请求参数:{}
|
||||
请求方法:{}
|
||||
错误信息:{}
|
||||
响应状态:{}
|
||||
原始信息:{}
|
||||
""", path, query, method, message, status, globalError);
|
||||
}
|
||||
final Map<String, String> body = new HashMap<>();
|
||||
body.put("path", path);
|
||||
body.put("query", query);
|
||||
body.put("method", method);
|
||||
message.setBody(body);
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 状态编码
|
||||
*/
|
||||
public static final int globalStatus(HttpServletRequest request, HttpServletResponse response) {
|
||||
final Object status = request.getAttribute(SERVLET_STATUS_CODE);
|
||||
if(status instanceof Integer) {
|
||||
return (Integer) status;
|
||||
}
|
||||
return response.getStatus();
|
||||
}
|
||||
/**
|
||||
* @param request 请求
|
||||
* @param response 响应
|
||||
*
|
||||
* @return 状态编码
|
||||
*/
|
||||
public static final int globalStatus(HttpServletRequest request, HttpServletResponse response) {
|
||||
final Object status = request.getAttribute(SERVLET_STATUS_CODE);
|
||||
if(status instanceof Integer) {
|
||||
return (Integer) status;
|
||||
}
|
||||
return response.getStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param request 请求
|
||||
*
|
||||
* @return 异常
|
||||
*/
|
||||
public static final Object globalError(HttpServletRequest request) {
|
||||
// Servlet错误异常
|
||||
Object throwable = request.getAttribute(EXCEPTION_SERVLET);
|
||||
if(throwable != null) {
|
||||
return throwable;
|
||||
}
|
||||
// SpringBoot异常
|
||||
throwable = request.getAttribute(EXCEPTION_SPRINGBOOT);
|
||||
if(throwable != null) {
|
||||
return throwable;
|
||||
}
|
||||
return throwable;
|
||||
}
|
||||
/**
|
||||
* @param request 请求
|
||||
*
|
||||
* @return 异常
|
||||
*/
|
||||
public static final Object globalError(HttpServletRequest request) {
|
||||
// Servlet错误异常
|
||||
Object throwable = request.getAttribute(EXCEPTION_SERVLET);
|
||||
if(throwable != null) {
|
||||
return throwable;
|
||||
}
|
||||
// SpringBoot异常
|
||||
throwable = request.getAttribute(EXCEPTION_SPRINGBOOT);
|
||||
if(throwable != null) {
|
||||
return throwable;
|
||||
}
|
||||
return throwable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param status 原始状态编码
|
||||
* @param throwable 异常
|
||||
*
|
||||
* @return 状态编码
|
||||
*
|
||||
* @see ResponseEntityExceptionHandler
|
||||
* @see DefaultHandlerExceptionResolver
|
||||
*/
|
||||
public static final MessageCode messageCode(int status, Throwable throwable) {
|
||||
final Class<?> clazz = throwable.getClass();
|
||||
return CODE_MAPPING.entrySet().stream()
|
||||
.filter(entry -> {
|
||||
final Class<?> mappingClazz = entry.getKey();
|
||||
return mappingClazz.equals(clazz) || mappingClazz.isAssignableFrom(clazz);
|
||||
})
|
||||
.map(Map.Entry::getValue)
|
||||
.findFirst()
|
||||
.orElse(MessageCode.of(status));
|
||||
}
|
||||
/**
|
||||
* @param status 原始状态编码
|
||||
* @param throwable 异常
|
||||
*
|
||||
* @return 状态编码
|
||||
*
|
||||
* @see ResponseEntityExceptionHandler
|
||||
* @see DefaultHandlerExceptionResolver
|
||||
*/
|
||||
public static final MessageCode messageCode(int status, Throwable throwable) {
|
||||
final Class<?> clazz = throwable.getClass();
|
||||
return CODE_MAPPING.entrySet().stream()
|
||||
.filter(entry -> {
|
||||
final Class<?> mappingClazz = entry.getKey();
|
||||
return mappingClazz.equals(clazz) || mappingClazz.isAssignableFrom(clazz);
|
||||
})
|
||||
.map(Map.Entry::getValue)
|
||||
.findFirst()
|
||||
.orElse(MessageCode.of(status));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
* @param throwable 异常
|
||||
*
|
||||
* @return 异常信息
|
||||
*/
|
||||
public static final String message(MessageCode messageCode, Throwable throwable) {
|
||||
if(throwable instanceof BindException bindException) {
|
||||
return bindException.getAllErrors().stream()
|
||||
.map(ObjectError::getDefaultMessage)
|
||||
.collect(Collectors.joining(" && "));
|
||||
}
|
||||
if(throwable instanceof ConstraintViolationException violationException) {
|
||||
return violationException.getConstraintViolations().stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.joining(" && "));
|
||||
}
|
||||
final String message = throwable.getMessage();
|
||||
// 不是未知系统异常
|
||||
if(StringUtils.isNotEmpty(message) && messageCode != MessageCode.CODE_9999) {
|
||||
return message;
|
||||
}
|
||||
// 少量信息返回异常信息
|
||||
if(StringUtils.isNotEmpty(message) && message.length() <= Byte.MAX_VALUE) {
|
||||
return message;
|
||||
}
|
||||
// 其他情况不能直接返回异常信息
|
||||
return messageCode.getMessage();
|
||||
}
|
||||
/**
|
||||
* @param messageCode 状态编码
|
||||
* @param throwable 异常
|
||||
*
|
||||
* @return 异常信息
|
||||
*/
|
||||
public static final String message(MessageCode messageCode, Throwable throwable) {
|
||||
if(throwable instanceof BindException bindException) {
|
||||
return bindException.getAllErrors().stream()
|
||||
.map(ObjectError::getDefaultMessage)
|
||||
.collect(Collectors.joining(" && "));
|
||||
}
|
||||
if(throwable instanceof ConstraintViolationException violationException) {
|
||||
return violationException.getConstraintViolations().stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.joining(" && "));
|
||||
}
|
||||
final String message = throwable.getMessage();
|
||||
// 自定义的系统异常
|
||||
if(StringUtils.isNotEmpty(message) && messageCode != MessageCode.CODE_9999) {
|
||||
return message;
|
||||
}
|
||||
// 少量信息直接返回
|
||||
if(StringUtils.isNotEmpty(message) && message.length() <= Byte.MAX_VALUE) {
|
||||
return message;
|
||||
}
|
||||
// 其他情况不能直接返回异常信息
|
||||
return messageCode.getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param t 异常
|
||||
*
|
||||
* @return 原始异常
|
||||
*
|
||||
* @see #rootException(Throwable)
|
||||
*/
|
||||
public static final Object rootException(Object t) {
|
||||
if(t instanceof Throwable throwable) {
|
||||
return ErrorUtils.rootException(throwable);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
/**
|
||||
* @param t 异常
|
||||
*
|
||||
* @return 原始异常
|
||||
*
|
||||
* @see #rootException(Throwable)
|
||||
*/
|
||||
public static final Object rootException(Object t) {
|
||||
if(t instanceof Throwable throwable) {
|
||||
return ErrorUtils.rootException(throwable);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param t 异常
|
||||
*
|
||||
* @return 原始异常
|
||||
*/
|
||||
public static final Throwable rootException(Throwable t) {
|
||||
Throwable cause = t;
|
||||
do {
|
||||
// 直接返回状态编码异常
|
||||
if(cause instanceof MessageCodeException) {
|
||||
return cause;
|
||||
}
|
||||
} while(cause != null && (cause = cause.getCause()) != null);
|
||||
// 返回原始异常
|
||||
return t;
|
||||
}
|
||||
/**
|
||||
* @param t 异常
|
||||
*
|
||||
* @return 原始异常
|
||||
*/
|
||||
public static final Throwable rootException(Throwable t) {
|
||||
Throwable cause = t;
|
||||
do {
|
||||
// 直接返回状态编码异常
|
||||
if(cause instanceof MessageCodeException) {
|
||||
return cause;
|
||||
}
|
||||
} while(cause != null && (cause = cause.getCause()) != null);
|
||||
// 返回原始异常
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param params 参数列表
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.acgist.taoyao.boot.utils;
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
@@ -13,48 +14,48 @@ import org.apache.commons.lang3.ArrayUtils;
|
||||
*/
|
||||
public final class FileUtils {
|
||||
|
||||
private FileUtils() {
|
||||
}
|
||||
private FileUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据进制
|
||||
*/
|
||||
private static final int SCALE = 1024;
|
||||
/**
|
||||
* 文件大小单位
|
||||
*/
|
||||
private static final String[] UNITS = {"B", "KB", "MB", "GB", "TB", "PB"};
|
||||
/**
|
||||
* 数据进制
|
||||
*/
|
||||
private static final int SCALE = 1024;
|
||||
/**
|
||||
* 文件大小单位
|
||||
*/
|
||||
private static final String[] UNITS = { "B", "KB", "MB", "GB", "TB", "PB" };
|
||||
|
||||
/**
|
||||
* @param size 文件大小
|
||||
*
|
||||
* @return 文件大小
|
||||
*/
|
||||
public static final String formatSize(Long size) {
|
||||
return formatSize(size, "B");
|
||||
}
|
||||
/**
|
||||
* @param size 文件大小
|
||||
*
|
||||
* @return 文件大小
|
||||
*/
|
||||
public static final String formatSize(Long size) {
|
||||
return FileUtils.formatSize(size, "B");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param size 文件大小
|
||||
* @param unit 当前单位
|
||||
*
|
||||
* @return 文件大小
|
||||
*/
|
||||
public static final String formatSize(Long size, String unit) {
|
||||
if(size == null || size <= 0L) {
|
||||
return "0B";
|
||||
}
|
||||
int index = ArrayUtils.indexOf(UNITS, unit);
|
||||
double value = size;
|
||||
while(value >= SCALE) {
|
||||
if(++index >= UNITS.length) {
|
||||
index = UNITS.length - 1;
|
||||
break;
|
||||
}
|
||||
value /= SCALE;
|
||||
}
|
||||
return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_EVEN) + UNITS[index];
|
||||
}
|
||||
/**
|
||||
* @param size 文件大小
|
||||
* @param unit 当前单位
|
||||
*
|
||||
* @return 文件大小
|
||||
*/
|
||||
public static final String formatSize(Long size, String unit) {
|
||||
if(size == null || size <= 0L) {
|
||||
return "0B";
|
||||
}
|
||||
int index = ArrayUtils.indexOf(UNITS, unit);
|
||||
double value = size;
|
||||
while(value >= SCALE) {
|
||||
if(++index >= UNITS.length) {
|
||||
index = UNITS.length - 1;
|
||||
break;
|
||||
}
|
||||
value /= SCALE;
|
||||
}
|
||||
return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_EVEN) + UNITS[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否Linux平台
|
||||
@@ -66,7 +67,7 @@ public final class FileUtils {
|
||||
/**
|
||||
* 创建目录
|
||||
*
|
||||
* @param path
|
||||
* @param path 目录
|
||||
*/
|
||||
public static final void mkdirs(String path) {
|
||||
final File file = new File(path);
|
||||
@@ -76,4 +77,13 @@ public final class FileUtils {
|
||||
file.mkdirs();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建上级目录
|
||||
*
|
||||
* @param path 目录
|
||||
*/
|
||||
public static final void mkdirsParent(String path) {
|
||||
FileUtils.mkdirs(Paths.get(path).getParent().toFile().getAbsolutePath());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,13 +38,15 @@ public final class HTTPUtils {
|
||||
/**
|
||||
* 无效IP验证
|
||||
*/
|
||||
private static final Function<String, Boolean> NEXT_IP = ip -> StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip);
|
||||
private static final Function<String, Boolean> NEXT_IP
|
||||
=
|
||||
ip -> StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip);
|
||||
|
||||
private HTTPUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param timeout 超时时间
|
||||
* @param timeout 超时时间
|
||||
* @param executor 线程池
|
||||
*/
|
||||
public static final void init(long timeout, Executor executor) {
|
||||
@@ -71,7 +73,7 @@ public final class HTTPUtils {
|
||||
*
|
||||
* @return IP地址
|
||||
*/
|
||||
public static final String httpIp(HttpServletRequest request) {
|
||||
public static final String clientIP(HttpServletRequest request) {
|
||||
String ip = request.getHeader("X-Forwarded-For");
|
||||
if (NEXT_IP.apply(ip)) {
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
@@ -91,7 +93,9 @@ public final class HTTPUtils {
|
||||
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
|
||||
sslContext.init(
|
||||
new KeyManager[0],
|
||||
new X509TrustManager[] { TaoyaoTrustManager.INSTANCE },
|
||||
new X509TrustManager[] {
|
||||
TaoyaoTrustManager.INSTANCE
|
||||
},
|
||||
new SecureRandom()
|
||||
);
|
||||
return sslContext;
|
||||
|
||||
@@ -141,6 +141,29 @@ public final class JSONUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转Map
|
||||
*
|
||||
* @param <K> K类型
|
||||
* @param <V> V类型
|
||||
* @param json JSON
|
||||
* @param kClazz Java类型
|
||||
* @param vClass Java类型
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
public static final <K, V> Map<K, V> toMap(String json, Class<K> kClazz, Class<V> vClass) {
|
||||
if (Objects.isNull(json)) {
|
||||
return Map.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, new TypeReference<Map<K, V>>() {
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw MessageCodeException.of(e, "JSON转Map失败:" + json);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON转List
|
||||
*
|
||||
@@ -202,7 +225,7 @@ public final class JSONUtils {
|
||||
private static final Module buildCustomModule() {
|
||||
final SimpleModule customModule = new SimpleModule("CustomModule");
|
||||
// 注意不能转换Long类型数据:请求数据类型变化
|
||||
// customModule.addSerializer(Long.class, ToStringSerializer.instance);
|
||||
// customModule.addSerializer(Long.class, ToStringSerializer.instance);
|
||||
return customModule;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,11 +28,11 @@ public final class NetUtils {
|
||||
/**
|
||||
* 本机IP
|
||||
*/
|
||||
private static String localIp;
|
||||
private static String localIP;
|
||||
/**
|
||||
* 环回IP
|
||||
*/
|
||||
private static String loopbackIp;
|
||||
private static String loopbackIP;
|
||||
/**
|
||||
* 地址重写
|
||||
*/
|
||||
@@ -43,13 +43,13 @@ public final class NetUtils {
|
||||
*/
|
||||
public static final void init(RewriteProperties rewriteProperties) {
|
||||
try {
|
||||
NetUtils.rewriteProperties = rewriteProperties;
|
||||
NetUtils.rewriteProperties = rewriteProperties;
|
||||
final InetAddress localHost = InetAddress.getLocalHost();
|
||||
final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
|
||||
NetUtils.localIp = localHost.getHostAddress();
|
||||
NetUtils.loopbackIp = loopbackAddress.getHostAddress();
|
||||
log.info("本机IP:{}", NetUtils.localIp);
|
||||
log.info("环回IP:{}", NetUtils.loopbackIp);
|
||||
NetUtils.localIP = localHost.getHostAddress();
|
||||
NetUtils.loopbackIP = loopbackAddress.getHostAddress();
|
||||
log.info("本机IP:{}", NetUtils.localIP);
|
||||
log.info("环回IP:{}", NetUtils.loopbackIP);
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("加载网络配置异常", e);
|
||||
}
|
||||
@@ -58,29 +58,29 @@ public final class NetUtils {
|
||||
/**
|
||||
* @return 本机IP
|
||||
*/
|
||||
public static final String localIp() {
|
||||
return NetUtils.localIp;
|
||||
public static final String localIP() {
|
||||
return NetUtils.localIP;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 环回IP
|
||||
*/
|
||||
public static final String loopbackIp() {
|
||||
return NetUtils.loopbackIp;
|
||||
public static final String loopbackIP() {
|
||||
return NetUtils.loopbackIP;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内网是否相同网段
|
||||
*
|
||||
* @param sourceIp 原始IP
|
||||
* @param clientIp 终端IP
|
||||
* @param sourceIP 原始IP
|
||||
* @param clientIP 终端IP
|
||||
*
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public static final boolean subnetIp(final String sourceIp, final String clientIp) {
|
||||
public static final boolean subnetIP(final String sourceIP, final String clientIP) {
|
||||
try {
|
||||
final InetAddress sourceAddress = NetUtils.realAddress(sourceIp);
|
||||
final InetAddress clientAddress = NetUtils.realAddress(clientIp);
|
||||
final InetAddress sourceAddress = NetUtils.realAddress(sourceIP);
|
||||
final InetAddress clientAddress = NetUtils.realAddress(clientIP);
|
||||
final boolean sourceLocal = NetUtils.localAddress(sourceAddress);
|
||||
final boolean clientLocal = NetUtils.localAddress(clientAddress);
|
||||
if(sourceLocal && clientLocal) {
|
||||
@@ -96,37 +96,38 @@ public final class NetUtils {
|
||||
return source.equals(client);
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("IP地址转换异常:{}-{}", sourceIp, clientIp, e);
|
||||
log.error("IP地址转换异常:{} - {}", sourceIP, clientIP, e);
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写地址
|
||||
*
|
||||
* @param sourceIp 原始IP
|
||||
* @param clientIp 终端IP
|
||||
* @param sourceIP 原始IP
|
||||
* @param clientIP 终端IP
|
||||
*
|
||||
* @return 替换IP
|
||||
* @return 重写IP
|
||||
*/
|
||||
public static final String rewriteIp(final String sourceIp, final String clientIp) {
|
||||
public static final String rewriteIP(final String sourceIP, final String clientIP) {
|
||||
if(Boolean.FALSE.equals(NetUtils.rewriteProperties.getEnabled())) {
|
||||
return sourceIp;
|
||||
return sourceIP;
|
||||
}
|
||||
final RewriteRuleProperties rule = NetUtils.rewriteProperties.getRule().stream()
|
||||
.filter(v -> NetUtils.subnetIp(v.getNetwork(), clientIp))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(rule == null) {
|
||||
return sourceIp;
|
||||
}
|
||||
log.debug("地址重写:{} - {} - {}", sourceIp, clientIp, rule.getNetwork());
|
||||
try {
|
||||
final InetAddress sourceAddress = NetUtils.realAddress(sourceIp);
|
||||
final InetAddress clientAddress = NetUtils.realAddress(clientIp);
|
||||
final InetAddress sourceAddress = NetUtils.realAddress(sourceIP);
|
||||
final InetAddress clientAddress = NetUtils.realAddress(clientIP);
|
||||
final boolean sourceLocal = NetUtils.localAddress(sourceAddress);
|
||||
final boolean clientLocal = NetUtils.localAddress(clientAddress);
|
||||
// 内网服务 && 内网设备
|
||||
if(sourceLocal && clientLocal) {
|
||||
final RewriteRuleProperties rule = NetUtils.rewriteProperties.getRule().stream()
|
||||
.filter(v -> NetUtils.subnetIP(v.getNetwork(), clientIP))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(rule == null) {
|
||||
return sourceIP;
|
||||
}
|
||||
log.debug("地址重写:{} - {} - {}", sourceIP, clientIP, rule.getNetwork());
|
||||
// 明确配置
|
||||
if(StringUtils.isNotEmpty(rule.getInnerHost())) {
|
||||
return rule.getInnerHost();
|
||||
@@ -150,18 +151,36 @@ public final class NetUtils {
|
||||
return InetAddress.getByAddress(bytes).getHostAddress();
|
||||
}
|
||||
}
|
||||
// 公网服务 && 内网设备
|
||||
if(sourceLocal && !clientLocal && StringUtils.isNotEmpty(rule.getInnerHost())) {
|
||||
return rule.getInnerHost();
|
||||
}
|
||||
// 内网服务 && 公网设备
|
||||
if(!sourceLocal && clientLocal && StringUtils.isNotEmpty(rule.getOuterHost())) {
|
||||
return rule.getOuterHost();
|
||||
if(sourceLocal && !clientLocal) {
|
||||
final RewriteRuleProperties rule = NetUtils.rewriteProperties.getRule().stream()
|
||||
.filter(v -> NetUtils.subnetIP(v.getNetwork(), sourceIP))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(rule == null) {
|
||||
return sourceIP;
|
||||
}
|
||||
if(StringUtils.isNotEmpty(rule.getOuterHost())) {
|
||||
return rule.getOuterHost();
|
||||
}
|
||||
}
|
||||
// 公网服务 && 内网设备
|
||||
if(!sourceLocal && clientLocal) {
|
||||
final RewriteRuleProperties rule = NetUtils.rewriteProperties.getRule().stream()
|
||||
.filter(v -> NetUtils.subnetIP(v.getNetwork(), clientIP))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(rule == null) {
|
||||
return sourceIP;
|
||||
}
|
||||
if(StringUtils.isNotEmpty(rule.getInnerHost())) {
|
||||
return rule.getInnerHost();
|
||||
}
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("地址重写异常:{}-{}", sourceIp, clientIp, e);
|
||||
log.error("地址重写异常:{} - {}", sourceIP, clientIP, e);
|
||||
}
|
||||
return sourceIp;
|
||||
return sourceIP;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,7 +194,7 @@ public final class NetUtils {
|
||||
final InetAddress address = InetAddress.getByName(ip);
|
||||
if(NetUtils.localAnyOrLoopAddress(address)) {
|
||||
// 通配地址或者换回地址使用本机地址
|
||||
return InetAddress.getByName(NetUtils.localIp);
|
||||
return InetAddress.getByName(NetUtils.localIP);
|
||||
}
|
||||
return address;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="false" scanPeriod="60 seconds" debug="false">
|
||||
|
||||
<springProperty scope="context" name="log.name" source="spring.application.name" />
|
||||
<springProperty scope="context" name="log.name" source="spring.application.name" />
|
||||
|
||||
<contextName>${log.name}</contextName>
|
||||
<contextName>${log.name}</contextName>
|
||||
|
||||
<property name="log.path" value="logs" />
|
||||
<property name="log.queue" value="2048" />
|
||||
<property name="log.buffer" value="8192" />
|
||||
<property name="log.history" value="30" />
|
||||
<property name="log.charset" value="UTF-8" />
|
||||
<property name="log.pattern" value="[${log.name}] %d{YYYY-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %file:%line - %m%n" />
|
||||
<property name="log.path" value="logs" />
|
||||
<property name="log.queue" value="2048" />
|
||||
<property name="log.buffer" value="8192" />
|
||||
<property name="log.history" value="30" />
|
||||
<property name="log.charset" value="UTF-8" />
|
||||
<property name="log.pattern" value="[${log.name}] %d{YYYY-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %file:%line - %m%n" />
|
||||
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
@@ -22,90 +22,90 @@
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="fileDebug" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.debug.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.debug.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>DEBUG</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileDebugAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileDebug" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
<appender name="fileDebug" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.debug.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.debug.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>DEBUG</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileDebugAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileDebug" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
|
||||
<appender name="fileInfo" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.info.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.info.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>INFO</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileInfoAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileInfo" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
<appender name="fileInfo" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.info.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.info.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>INFO</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileInfoAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileInfo" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
|
||||
<appender name="fileError" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.error.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.error.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>ERROR</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileErrorAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileError" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
<appender name="fileError" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/${log.name}.error.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<maxHistory>${log.history}</maxHistory>
|
||||
<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/${log.name}.error.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
</rollingPolicy>
|
||||
<bufferSize>${log.buffer}</bufferSize>
|
||||
<immediateFlush>false</immediateFlush>
|
||||
<encoder>
|
||||
<charset>${log.charset}</charset>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>ERROR</level>
|
||||
</filter>
|
||||
</appender>
|
||||
<appender name="fileErrorAsync" class="ch.qos.logback.classic.AsyncAppender">
|
||||
<appender-ref ref="fileError" />
|
||||
<queueSize>${log.queue}</queueSize>
|
||||
<includeCallerData>true</includeCallerData>
|
||||
<discardingThreshold>0</discardingThreshold>
|
||||
</appender>
|
||||
|
||||
<springProfile name="uat | prd">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="fileDebugAsync" />
|
||||
<appender-ref ref="fileInfoAsync" />
|
||||
<appender-ref ref="fileErrorAsync" />
|
||||
</root>
|
||||
</springProfile>
|
||||
<springProfile name="uat | prd">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="fileDebugAsync" />
|
||||
<appender-ref ref="fileInfoAsync" />
|
||||
<appender-ref ref="fileErrorAsync" />
|
||||
</root>
|
||||
</springProfile>
|
||||
|
||||
<springProfile name="!(uat | prd)">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="console" />
|
||||
<appender-ref ref="fileDebugAsync" />
|
||||
<appender-ref ref="fileInfoAsync" />
|
||||
<appender-ref ref="fileErrorAsync" />
|
||||
</root>
|
||||
</springProfile>
|
||||
<springProfile name="!(uat | prd)">
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="console" />
|
||||
<appender-ref ref="fileDebugAsync" />
|
||||
<appender-ref ref="fileInfoAsync" />
|
||||
<appender-ref ref="fileErrorAsync" />
|
||||
</root>
|
||||
</springProfile>
|
||||
|
||||
</configuration>
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.acgist.taoyao.boot.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -19,6 +21,7 @@ public class MessageTest {
|
||||
.body(Map.of("1", "2"))
|
||||
.build();
|
||||
final String json = JSONUtils.toJSON(message);
|
||||
assertEquals("{\"code\":\"0000\",\"message\":\"acgist\",\"body\":{\"1\":\"2\"}}", json);
|
||||
log.info("{}", json);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,34 +20,43 @@ public class NetUtilsTest {
|
||||
private void init() {
|
||||
final RewriteRuleProperties rewriteRuleProperties1 = new RewriteRuleProperties();
|
||||
rewriteRuleProperties1.setNetwork("192.168.1.0");
|
||||
final RewriteRuleProperties rewriteRuleProperties8 = new RewriteRuleProperties();
|
||||
rewriteRuleProperties8.setNetwork("192.168.8.0");
|
||||
rewriteRuleProperties8.setInnerHost("192.168.8.88");
|
||||
rewriteRuleProperties8.setOuterHost("8.8.8.8");
|
||||
final RewriteRuleProperties rewriteRuleProperties10 = new RewriteRuleProperties();
|
||||
rewriteRuleProperties10.setNetwork("192.168.10.0");
|
||||
final RewriteProperties rewriteProperties = new RewriteProperties();
|
||||
rewriteProperties.setEnabled(true);
|
||||
rewriteProperties.setPrefix(24);
|
||||
rewriteProperties.setRule(List.of(rewriteRuleProperties1, rewriteRuleProperties10));
|
||||
rewriteProperties.setRule(List.of(rewriteRuleProperties1, rewriteRuleProperties8, rewriteRuleProperties10));
|
||||
NetUtils.init(rewriteProperties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubnetIp() {
|
||||
this.init();
|
||||
assertTrue(NetUtils.subnetIp("192.168.8.1", "192.168.8.100"));
|
||||
assertTrue(NetUtils.subnetIp("192.168.100.1", "192.168.100.100"));
|
||||
assertFalse(NetUtils.subnetIp("192.168.1.1", "192.168.8.100"));
|
||||
assertFalse(NetUtils.subnetIp("192.168.80.1", "192.168.8.100"));
|
||||
assertTrue(NetUtils.subnetIp("fe80::9ff9:2da9:9759:17e9", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertTrue(NetUtils.subnetIp("fe80::9ff9:2da9:9759:17ee", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertFalse(NetUtils.subnetIp("fe81::9ff9:2da9:9759:17e9", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertFalse(NetUtils.subnetIp("fe81::9ff9:2da9:9759:17ee", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertFalse(NetUtils.subnetIP("192.168.1.1", "114.114.114.114"));
|
||||
assertTrue(NetUtils.subnetIP("192.168.8.1", "192.168.8.100"));
|
||||
assertTrue(NetUtils.subnetIP("192.168.100.1", "192.168.100.100"));
|
||||
assertFalse(NetUtils.subnetIP("192.168.1.1", "192.168.8.100"));
|
||||
assertFalse(NetUtils.subnetIP("192.168.80.1", "192.168.8.100"));
|
||||
assertTrue(NetUtils.subnetIP("fe80::9ff9:2da9:9759:17e9", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertTrue(NetUtils.subnetIP("fe80::9ff9:2da9:9759:17ee", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertFalse(NetUtils.subnetIP("fe81::9ff9:2da9:9759:17e9", "fe80::9ff9:2da9:9759:17e9"));
|
||||
assertFalse(NetUtils.subnetIP("fe81::9ff9:2da9:9759:17ee", "fe80::9ff9:2da9:9759:17e9"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRewriteIp() {
|
||||
this.init();
|
||||
assertNotEquals("192.168.1.0", NetUtils.rewriteIp("0.0.0.0", "192.168.1.1"));
|
||||
assertEquals("192.168.1.100", NetUtils.rewriteIp("192.168.8.100", "192.168.1.1"));
|
||||
assertEquals("192.168.10.100", NetUtils.rewriteIp("192.168.8.100", "192.168.10.1"));
|
||||
assertNotEquals("192.168.1.0", NetUtils.rewriteIP("0.0.0.0", "192.168.1.1"));
|
||||
assertEquals("192.168.1.100", NetUtils.rewriteIP("192.168.8.100", "192.168.1.1"));
|
||||
assertEquals("192.168.10.100", NetUtils.rewriteIP("192.168.8.100", "192.168.10.1"));
|
||||
assertEquals("114.114.114.114", NetUtils.rewriteIP("114.114.114.114", "192.168.10.1"));
|
||||
assertEquals("192.168.2.100", NetUtils.rewriteIP("192.168.2.100", "114.114.114.114"));
|
||||
assertEquals("192.168.8.88", NetUtils.rewriteIP("114.114.114.114", "192.168.8.100"));
|
||||
assertEquals("8.8.8.8", NetUtils.rewriteIP("192.168.8.100", "114.114.114.114"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,15 +64,15 @@ public class NetUtilsTest {
|
||||
this.init();
|
||||
long a = System.currentTimeMillis();
|
||||
for (int index = 0; index < 100000; index++) {
|
||||
assertTrue(NetUtils.subnetIp("192.168.100.1", "192.168.100.100"));
|
||||
assertFalse(NetUtils.subnetIp("192.168.1.1", "192.168.8.100"));
|
||||
assertTrue(NetUtils.subnetIP("192.168.100.1", "192.168.100.100"));
|
||||
assertFalse(NetUtils.subnetIP("192.168.1.1", "192.168.8.100"));
|
||||
}
|
||||
long z = System.currentTimeMillis();
|
||||
log.info("耗时:{}", z - a);
|
||||
a = System.currentTimeMillis();
|
||||
for (int index = 0; index < 100000; index++) {
|
||||
assertEquals("192.168.1.100", NetUtils.rewriteIp("192.168.8.100", "192.168.1.1"));
|
||||
assertEquals("192.168.10.100", NetUtils.rewriteIp("192.168.8.100", "192.168.10.1"));
|
||||
assertEquals("192.168.1.100", NetUtils.rewriteIP("192.168.8.100", "192.168.1.1"));
|
||||
assertEquals("192.168.10.100", NetUtils.rewriteIP("192.168.8.100", "192.168.10.1"));
|
||||
}
|
||||
z = System.currentTimeMillis();
|
||||
log.info("耗时:{}", z - a);
|
||||
|
||||
@@ -1,13 +1,21 @@
|
||||
package com.acgist.taoyao.boot.utils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class ScriptUtilsTest {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
ScriptUtils.execute("ls");
|
||||
ScriptUtils.execute("netstat -ano");
|
||||
void test() throws InterruptedException {
|
||||
assertNotEquals(0, ScriptUtils.execute("ls").getCode());
|
||||
assertEquals(0, ScriptUtils.execute("netstat -ano").getCode());
|
||||
log.info("{}", ScriptUtils.execute("ls").getResult());
|
||||
log.info("{}", ScriptUtils.execute("netstat -ano").getResult());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ public class MediaTransportPlainProtocol extends ProtocolRoomAdapter {
|
||||
// 媒体服务返回IP
|
||||
final String mediaIp = (String) body.get(Constant.IP);
|
||||
if(StringUtils.isNotEmpty(mediaIp)) {
|
||||
final String rewriteIp = NetUtils.rewriteIp(mediaIp, clientIp);
|
||||
final String rewriteIp = NetUtils.rewriteIP(mediaIp, clientIp);
|
||||
log.debug("重写地址:{} + {} -> {}", mediaIp, clientIp, rewriteIp);
|
||||
body.put(Constant.IP, rewriteIp);
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ public class MediaTransportWebRtcCreateProtocol extends ProtocolRoomAdapter {
|
||||
// 媒体服务返回IP
|
||||
final String mediaIp = (String) map.get(Constant.IP);
|
||||
if(StringUtils.isNotEmpty(mediaIp)) {
|
||||
final String rewriteIp = NetUtils.rewriteIp(mediaIp, clientIp);
|
||||
final String rewriteIp = NetUtils.rewriteIP(mediaIp, clientIp);
|
||||
log.debug("重写地址:{} + {} -> {}", mediaIp, clientIp, rewriteIp);
|
||||
map.put(Constant.IP, rewriteIp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user