[*] 日常优化
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",
|
||||
|
||||
@@ -91,7 +91,7 @@ public final class ErrorUtils {
|
||||
*/
|
||||
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;
|
||||
@@ -231,11 +231,11 @@ public final class ErrorUtils {
|
||||
.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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -31,7 +32,7 @@ public final class FileUtils {
|
||||
* @return 文件大小
|
||||
*/
|
||||
public static final String formatSize(Long size) {
|
||||
return formatSize(size, "B");
|
||||
return FileUtils.formatSize(size, "B");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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,7 +38,9 @@ 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() {
|
||||
}
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
/**
|
||||
* 地址重写
|
||||
*/
|
||||
@@ -46,10 +46,10 @@ public final class NetUtils {
|
||||
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;
|
||||
}
|
||||
try {
|
||||
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))
|
||||
.filter(v -> NetUtils.subnetIP(v.getNetwork(), clientIP))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if(rule == null) {
|
||||
return sourceIp;
|
||||
return sourceIP;
|
||||
}
|
||||
log.debug("地址重写:{} - {} - {}", sourceIp, clientIp, rule.getNetwork());
|
||||
try {
|
||||
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) {
|
||||
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())) {
|
||||
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();
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("地址重写异常:{}-{}", sourceIp, clientIp, e);
|
||||
}
|
||||
return sourceIp;
|
||||
// 公网服务 && 内网设备
|
||||
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);
|
||||
}
|
||||
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,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