[*] 瓶颈

This commit is contained in:
acgist
2023-04-05 16:39:50 +08:00
parent efe3f2add2
commit 5efa09d3ce
36 changed files with 804 additions and 351 deletions

View File

@@ -10,7 +10,6 @@ import lombok.Setter;
/**
* 地址重写
* 内外网多网卡环境重写网络号保留主机号,通过重新地址实现多网互通。
*
* @author acgist
*/

View File

@@ -6,11 +6,7 @@ import lombok.Setter;
/**
* 重写规则
*
* @author acgist
*/
/**
* WebRTC STUN配置
* 没有配置内网地址等于网络号加上原始主机号
*
* @author acgist
*/
@@ -21,7 +17,9 @@ public class IpRewriteRuleProperties {
@Schema(title = "网络号", description = "网络号匹配终端IP")
private String network;
@Schema(title = "目标地址", description = "目标地址:没有配置等于网络号加上原始主机号")
private String targetHost;
@Schema(title = "内网地址", description = "内网地址")
private String innerHost;
@Schema(title = "外网地址", description = "外网地址")
private String outerHost;
}

View File

@@ -79,7 +79,9 @@ public final class NetUtils {
try {
final InetAddress sourceAddress = NetUtils.realAddress(sourceIp);
final InetAddress clientAddress = NetUtils.realAddress(clientIp);
if(NetUtils.localAddress(sourceAddress) && NetUtils.localAddress(clientAddress)) {
final boolean sourceLocal = NetUtils.localAddress(sourceAddress);
final boolean clientLocal = NetUtils.localAddress(clientAddress);
if(sourceLocal && clientLocal) {
final byte[] sourceBytes = sourceAddress.getAddress();
final byte[] clientBytes = clientAddress.getAddress();
final int length = (sourceBytes.length & clientBytes.length) * Byte.SIZE;
@@ -109,21 +111,25 @@ public final class NetUtils {
if(Boolean.FALSE.equals(NetUtils.ipRewriteProperties.getEnabled())) {
return sourceIp;
}
log.debug("重写地址:{} - {}", sourceIp, clientIp);
final IpRewriteRuleProperties rule = NetUtils.ipRewriteProperties.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);
if(NetUtils.localAddress(sourceAddress) && NetUtils.localAddress(clientAddress)) {
final IpRewriteRuleProperties rule = NetUtils.ipRewriteProperties.getRule().stream()
.filter(v -> NetUtils.subnetIp(v.getNetwork(), clientIp))
.findFirst()
.orElse(null);
if(rule == null) {
return sourceIp;
}
if(StringUtils.isNotEmpty(rule.getTargetHost())) {
return rule.getTargetHost();
final boolean sourceLocal = NetUtils.localAddress(sourceAddress);
final boolean clientLocal = NetUtils.localAddress(clientAddress);
if(sourceLocal && clientLocal) {
// 明确配置
if(StringUtils.isNotEmpty(rule.getInnerHost())) {
return rule.getInnerHost();
}
// 地址 = 网络号 + 主机号
final byte[] sourceBytes = sourceAddress.getAddress();
final byte[] clientBytes = clientAddress.getAddress();
final int length = (sourceBytes.length & clientBytes.length) * Byte.SIZE;
@@ -142,8 +148,16 @@ 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();
}
} catch (UnknownHostException e) {
log.error("IP地址转换异常:{}-{}", sourceIp, clientIp, e);
log.error("地址重写异常:{}-{}", sourceIp, clientIp, e);
}
return sourceIp;
}