修复加密过程中的通道编码异常问题

This commit is contained in:
az
2023-11-14 21:31:08 +08:00
parent f2005b3e74
commit 8c14727fba
6 changed files with 19 additions and 29 deletions
@@ -56,12 +56,15 @@ public class ProxyMessageConnectHandler implements ProxyMessageHandler {
channel.attr(Constants.NEXT_CHANNEL).set(realServerChannel);
realServerChannel.attr(Constants.NEXT_CHANNEL).set(channel);
// 远程绑定
// 通知服务端进行远程绑定,此绑定信息不加密,该条消息为身份标识
channel.writeAndFlush(ProxyMessage.buildConnectMessage(visitorId + "@" + proxyConfig.getTunnel().getLicenseKey()));
realServerChannel.config().setOption(ChannelOption.AUTO_READ, true);
ProxyUtil.addRealServerChannel(visitorId, realServerChannel);
ProxyUtil.setRealServerChannelVisitorId(realServerChannel, visitorId);
// 连接信息发送后,将该通道设置为加密
ProxyUtil.setChannelSecurity(channel);
}
@Override
@@ -28,9 +28,8 @@ public class ProxyMessageSecureKeyHandler implements ProxyMessageHandler {
Attribute<Boolean> booleanAttribute = ctx.attr(Constants.IS_SECURITY);
booleanAttribute.set(true);
// 设置代理通道为安全,之后使用代理通道传输的消息均会加密
// 全局存储密钥
ProxyUtil.setSecureKey(secureKey);
ProxyUtil.setProxyChannelSecurity();
log.info("Encrypted link established successfully");
} else {
@@ -46,6 +46,9 @@ public class UdpProxyMessageConnectHandler implements ProxyMessageHandler {
.setTargetIp(udpBaseInfo.getTargetIp())
.setTargetPort(udpBaseInfo.getTargetPort())
).setData(proxyConfig.getTunnel().getLicenseKey().getBytes()));
// connect类型的消息不加密,用于标识身份,发送标识消息后,再将通道设置加密标识
ProxyUtil.setChannelSecurity(channel);
}
@Override
@@ -83,9 +83,7 @@ public class ProxyUtil {
tcpProxyTunnelBootstrap.connect().addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
Channel newChannel = future.channel();
setChannelSecurity(newChannel);
borrowListener.success(newChannel);
borrowListener.success(future.channel());
} else {
borrowListener.error(future.cause());
}
@@ -93,6 +91,10 @@ public class ProxyUtil {
}
public static void returnTcpProxyChanel(Channel proxyChanel) {
if (proxyChanel != null) {
proxyChanel.attr(Constants.IS_SECURITY).set(null);
proxyChanel.attr(Constants.SECURE_KEY).set(null);
}
if (tcpProxyChannelPool.size() > MAX_POOL_SIZE) {
proxyChanel.close();
} else {
@@ -117,9 +119,7 @@ public class ProxyUtil {
tcpProxyTunnelBootstrap.connect().addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
Channel newChannel = future.channel();
setChannelSecurity(newChannel);
borrowListener.success(newChannel);
borrowListener.success(future.channel());
} else {
borrowListener.error(future.cause());
}
@@ -127,6 +127,10 @@ public class ProxyUtil {
}
public static void returnUdpProxyChanel(Channel proxyChanel) {
if (proxyChanel != null) {
proxyChanel.attr(Constants.IS_SECURITY).set(null);
proxyChanel.attr(Constants.SECURE_KEY).set(null);
}
if (udpProxyChannelPool.size() > MAX_POOL_SIZE) {
proxyChanel.close();
} else {
@@ -241,23 +245,4 @@ public class ProxyUtil {
channel.attr(Constants.SECURE_KEY).set(secureKey);
}
/**
* 设置代理通道为安全
*/
public static void setProxyChannelSecurity() {
if (null == secureKey) {
return;
}
tcpProxyChannelPool.forEach(channel -> {
channel.attr(Constants.IS_SECURITY).set(true);
channel.attr(Constants.SECURE_KEY).set(secureKey);
});
udpProxyChannelPool.forEach(channel -> {
channel.attr(Constants.IS_SECURITY).set(true);
channel.attr(Constants.SECURE_KEY).set(secureKey);
});
}
}
@@ -111,7 +111,6 @@ public class ProxyMessageDecoder extends LengthFieldBasedFrameDecoder {
long sn = buf.readLong();
proxyMessage.setSerialNumber(sn);
proxyMessage.setType(type);
int infoLength = buf.readInt();
@@ -22,6 +22,7 @@
package org.dromara.neutrinoproxy.core;
import cn.hutool.core.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;