修改参数

This commit is contained in:
=
2023-11-14 17:04:10 +08:00
parent 4569165993
commit f2005b3e74
7 changed files with 42 additions and 28 deletions
@@ -62,7 +62,6 @@ public class ProxyMessageConnectHandler implements ProxyMessageHandler {
realServerChannel.config().setOption(ChannelOption.AUTO_READ, true);
ProxyUtil.addRealServerChannel(visitorId, realServerChannel);
ProxyUtil.setRealServerChannelVisitorId(realServerChannel, visitorId);
ProxyUtil.setChannelSecurity(channel);
}
@Override
@@ -24,11 +24,13 @@ public class ProxyMessageSecureKeyHandler implements ProxyMessageHandler {
byte[] decryptedData = EncryptUtil.decryptByAes(secureKey, data);
String m = new String(decryptedData);
if ("ok".equals(m)) {
// 设置当前链路为安全,之后使用该链路传输的消息均会加密
// 设置当前cmd通道为安全,之后使用该通道传输的消息均会加密
Attribute<Boolean> booleanAttribute = ctx.attr(Constants.IS_SECURITY);
booleanAttribute.set(true);
// 设置代理通道为安全,之后使用代理通道传输的消息均会加密
ProxyUtil.setSecureKey(secureKey);
ProxyUtil.setProxyChannelSecurity();
log.info("Encrypted link established successfully");
} else {
@@ -83,7 +83,9 @@ public class ProxyUtil {
tcpProxyTunnelBootstrap.connect().addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
borrowListener.success(future.channel());
Channel newChannel = future.channel();
setChannelSecurity(newChannel);
borrowListener.success(newChannel);
} else {
borrowListener.error(future.cause());
}
@@ -115,7 +117,9 @@ public class ProxyUtil {
tcpProxyTunnelBootstrap.connect().addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
borrowListener.success(future.channel());
Channel newChannel = future.channel();
setChannelSecurity(newChannel);
borrowListener.success(newChannel);
} else {
borrowListener.error(future.cause());
}
@@ -230,8 +234,30 @@ public class ProxyUtil {
}
public static void setChannelSecurity(Channel channel) {
if (null == secureKey) {
return;
}
channel.attr(Constants.IS_SECURITY).set(true);
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);
});
}
}
@@ -98,8 +98,6 @@ public class ProxyMessageDecoder extends LengthFieldBasedFrameDecoder {
Attribute<byte[]> secureKeyAttr = ctx.attr(SECURE_KEY);
byte[] secureKey = secureKeyAttr.get();
// 解密
log.info("DecoderKey:{}", HexUtil.encodeHexStr(secureKey));
log.info("DecoderBytes:{}", HexUtil.encodeHexStr(encryptedBytes));
byte[] decryptedData = EncryptUtil.decryptByAes(secureKey, encryptedBytes);
buf = Unpooled.wrappedBuffer(decryptedData);
@@ -94,7 +94,7 @@ public class ProxyMessageConnectHandler implements ProxyMessageHandler {
}
// 设置加密
ProxyUtil.setChannelSecurity(licenseDO.getId(), visitorChannel);
ProxyUtil.setChannelSecurity(licenseDO.getId(), ctx.channel());
}
@Override
@@ -66,11 +66,6 @@ public class ProxyMessageSecureKeyHandler implements ProxyMessageHandler {
Integer licenseId = ctx.attr(Constants.LICENSE_ID).get();
ProxyUtil.setSecureKey(licenseId, secureKey);
ProxyUtil.setChannelSecurity(licenseId, ctx.channel());
Map<String, Channel> channelMap = ProxyUtil.getVisitorChannels(ctx.channel());
channelMap.values().forEach(channel -> ProxyUtil.setChannelSecurity(licenseId, channel));
if (licenseId != null) {
ProxyUtil.setLicenseIdRelativeChannelSecurity(licenseId);
}
}
@Override
@@ -38,15 +38,15 @@ public class ProxyUtil {
/**
* 服务端口 -> 指令通道映射
*/
private static Map<Integer, Channel> serverPortToCmdChannelMap = new ConcurrentHashMap<>();
private static final Map<Integer, Channel> serverPortToCmdChannelMap = new ConcurrentHashMap<>();
/**
* license -> 指令通道映射
*/
private static Map<Integer, Channel> licenseToCmdChannelMap = new ConcurrentHashMap<>();
private static final Map<Integer, Channel> licenseToCmdChannelMap = new ConcurrentHashMap<>();
/**
* 服务端口 -> 访问通道映射
*/
private static Map<Integer, Channel> serverPortToVisitorChannel = new ConcurrentHashMap<>();
private static final Map<Integer, Channel> serverPortToVisitorChannel = new ConcurrentHashMap<>();
/**
* cmdChannelAttachInfo.getUserChannelMap() 读写锁
@@ -55,21 +55,21 @@ public class ProxyUtil {
/**
* 访问者ID生成器
*/
private static AtomicLong visitorIdProducer = new AtomicLong(0);
private static final AtomicLong visitorIdProducer = new AtomicLong(0);
/**
* 代理 - connect附加映射
*/
private static Map<String, ProxyAttachment> proxyConnectAttachmentMap = new HashMap<>();
private static final Map<String, ProxyAttachment> proxyConnectAttachmentMap = new HashMap<>();
/**
* 子域名 - 服务端端口映射
*/
private static Map<String, Integer> subdomainToServerPort = new HashMap<>();
private static final Map<String, Integer> subdomainToServerPort = new HashMap<>();
/**
* licenseId - 客户端Id映射
*/
private static Map<Integer, String> licenseIdToClientIdMap = new HashMap<>();
private static final Map<Integer, String> licenseIdToClientIdMap = new HashMap<>();
private static Map<Integer, byte[]> licenseIdToSecureKeyMap = new ConcurrentHashMap<>();
private static final Map<Integer, byte[]> licenseIdToSecureKeyMap = new ConcurrentHashMap<>();
/**
* 初始化代理信息
@@ -146,9 +146,6 @@ public class ProxyUtil {
cmdChannelAttachInfo.getServerPorts().addAll(serverPorts);
}
// 添加安全信息
setChannelSecurity(licenseId, cmdChannel);
licenseToCmdChannelMap.put(licenseId, cmdChannel);
}
@@ -431,13 +428,10 @@ public class ProxyUtil {
licenseIdToSecureKeyMap.put(licenseId, key);
}
public static void setLicenseIdRelativeChannelSecurity(Integer licenseId) {
public static void setLicenseIdRelativeProxyChannelSecurity(Integer licenseId) {
Set<Integer> portSet = licenseToServerPortMap.get(licenseId);
for(Integer port : portSet) {
Channel cmdChannel = serverPortToCmdChannelMap.get(port);
setChannelSecurity(licenseId, cmdChannel);
Channel visitorChannel = serverPortToVisitorChannel.get(port);
setChannelSecurity(licenseId,visitorChannel);
// TODO 代理客户端
}
}