[fix] 通道数据添加appKey、appSecret、originalIp验证

This commit is contained in:
wujiawei
2024-10-19 22:31:41 +08:00
parent 55ce3ff359
commit b7d571ccc1
25 changed files with 538 additions and 77 deletions

View File

@ -1,11 +1,11 @@
package org.framework.lazy.cloud.network.heartbeat.common;
import io.netty.channel.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -29,17 +29,21 @@ public class ChannelContext {
// 如果客户端已经存在 移除
if (channelIdClientChannelDTOConcurrentHashMap.containsKey(clientId)) {
// clear(clientId);
List<Channel> channels = channelIdClientChannelDTOConcurrentHashMap.get(clientId);
for (Channel existChannel : channels) {
if (existChannel != null && !existChannel.isActive()) {
existChannel.close();
}else {
channels.remove(existChannel);
List<Channel> existChannelList = new ArrayList<>();
List<Channel> oldChannels = channelIdClientChannelDTOConcurrentHashMap.get(clientId);
for (Channel existChannel : oldChannels) {
if (existChannel != null) {
if(existChannel.isActive()){
existChannelList.add(existChannel);
}else {
existChannel.close();
}
}
}
channels.add(channel);
existChannelList.add(channel);
channelIdClientChannelDTOConcurrentHashMap.put(clientId, existChannelList);
}else {
channelIdClientChannelDTOConcurrentHashMap.putIfAbsent(clientId, List.of(channel));
channelIdClientChannelDTOConcurrentHashMap.putIfAbsent(clientId, Collections.synchronizedList(new ArrayList<>(List.of(channel))));
}
}

View File

@ -3,6 +3,7 @@ package org.framework.lazy.cloud.network.heartbeat.common;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.wu.framework.core.utils.ObjectUtils;
import java.nio.charset.StandardCharsets;
@ -13,8 +14,8 @@ import java.nio.charset.StandardCharsets;
@Setter
@Getter
public class NettyProxyMsg {
// body 长度 type 1 clientId 4 clientTargetIp 4 clientTargetPort 4 visitorPort 4 visitorId 4 data 4
public static final int bodyLength = 1 + 1 + 4 + 4 + 4 + 4 + 4 + 4;
// body 长度 type 1 isSsl 1 appKey 4 appSecret 4 clientId 4 originalIp 4 clientTargetIp 4 clientTargetPort 4 visitorPort 4 visitorId 4 data 4
public static final int bodyLength = 1 + 1 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4 + 4;
/**
@ -30,8 +31,29 @@ public class NettyProxyMsg {
* byte 长度 1
* 1 true
* 0 false
* @since 1.2.8
*/
private byte isSsl = 0;
/**
* 令牌key
* byte[] 长度 4
* @since 1.2.8
*/
private byte[] appKey;
/**
* 令牌密钥
* byte[] 长度 4
*
* @since 1.2.9
*/
private byte[] appSecret;
/**
* 原始IP
* byte[] 长度 4
*
* @since 1.2.9
*/
private byte[] originalIp;
/**
* 客户端ID
* byte[] 长度 4
@ -84,6 +106,78 @@ public class NettyProxyMsg {
}
public void setAppKeyString(String appKey) {
if (ObjectUtils.isEmpty(appKey)) {
this.appKey = null;
} else {
this.appKey = appKey.getBytes(StandardCharsets.UTF_8);
}
}
public void setAppSecretString(String appSecret) {
if (ObjectUtils.isEmpty(appSecret)) {
this.appSecret = null;
} else {
this.appSecret = appSecret.getBytes(StandardCharsets.UTF_8);
}
}
/**
* 设置原始IP
*
* @param originalIp 原始IP
*/
public void setOriginalIpString(String originalIp) {
if (ObjectUtils.isEmpty(originalIp)) {
this.originalIp = null;
} else {
this.originalIp = originalIp.getBytes(StandardCharsets.UTF_8);
}
}
/**
* 获取应用密钥
*
* @return 应用密钥
*/
public String getAppSecretString() {
if (ObjectUtils.isEmpty(appSecret)) {
return null;
}
return new String(appSecret, StandardCharsets.UTF_8);
}
/**
* 获取应用key
*
* @return 应用key
*/
public String getAppKeyString() {
if (ObjectUtils.isEmpty(appKey)) {
return null;
}
return new String(appKey, StandardCharsets.UTF_8);
}
/**
* 获取原始IP字符串
*
* @return 原始IP字符串
*/
public String getOriginalIpString() {
if (ObjectUtils.isEmpty(originalIp)) {
return null;
}
return new String(originalIp, StandardCharsets.UTF_8);
}
public String getClientIdString() {
if (ObjectUtils.isEmpty(clientId)) {
return null;
}
return new String(clientId, StandardCharsets.UTF_8);
}
public void setClientTargetIp(byte[] clientTargetIp) {
this.clientTargetIp = clientTargetIp;
}

View File

@ -112,6 +112,22 @@ public class NettyProxyMsgDecoder extends LengthFieldBasedFrameDecoder {
byte isSsl = in.readByte();
nettyProxyMsg.setIsSsl(isSsl);
int appKeyLength = in.readInt();
byte[] appKeyBytes = new byte[appKeyLength];
in.readBytes(appKeyBytes);
nettyProxyMsg.setAppKey(appKeyBytes);
int appSecretLength = in.readInt();
byte[] appSecretBytes = new byte[appSecretLength];
in.readBytes(appSecretBytes);
nettyProxyMsg.setAppSecret(appSecretBytes);
int originalIpLength = in.readInt();
byte[] originalIpBytes = new byte[originalIpLength];
in.readBytes(originalIpBytes);
nettyProxyMsg.setOriginalIp(originalIpBytes);
int clientIdLength = in.readInt();
byte[] clientIdBytes = new byte[clientIdLength];
in.readBytes(clientIdBytes);
@ -140,6 +156,9 @@ public class NettyProxyMsgDecoder extends LengthFieldBasedFrameDecoder {
nettyProxyMsg.setVisitorId(visitorIdBytes);
byte[] data = new byte[bodyLength - NettyProxyMsg.bodyLength -
appKeyLength -
appSecretLength -
originalIpLength -
clientIdLength -
clientTargetIpLength -
clientTargetPortLength -

View File

@ -24,6 +24,9 @@ public class NettyProxyMsgEncoder extends MessageToByteEncoder<NettyProxyMsg> {
int bodyLength = NettyProxyMsg.bodyLength;
byte typeBytes = msg.getType();
byte isSsl = msg.getIsSsl();
byte[] appKey = msg.getAppKey();
byte[] appSecret = msg.getAppSecret();
byte[] originalIp = msg.getOriginalIp();
byte[] clientIdBytes = msg.getClientId();
byte[] clientTargetIpBytes = msg.getClientTargetIp();
byte[] clientTargetPortBytes = msg.getClientTargetPort();
@ -31,6 +34,15 @@ public class NettyProxyMsgEncoder extends MessageToByteEncoder<NettyProxyMsg> {
byte[] visitorIdBytes = msg.getVisitorId();
byte[] msgDataBytes = msg.getData();
if (appKey != null) {
bodyLength += appKey.length;
}
if (appSecret != null) {
bodyLength += appSecret.length;
}
if (originalIp != null) {
bodyLength += originalIp.length;
}
if (clientIdBytes != null) {
bodyLength += clientIdBytes.length;
}
@ -56,6 +68,32 @@ public class NettyProxyMsgEncoder extends MessageToByteEncoder<NettyProxyMsg> {
out.writeByte(typeBytes);
out.writeByte(isSsl);
// 防止数据读错位置 令牌key
if (appKey != null) {
out.writeInt(appKey.length);
out.writeBytes(appKey);
} else {
// 防止令牌key 未填写
out.writeInt(0x00);
}
// 防止数据读错位置 令牌密钥
if (appSecret != null) {
out.writeInt(appSecret.length);
out.writeBytes(appSecret);
} else {
// 防止令牌密钥 未填写
out.writeInt(0x00);
}
// 防止数据读错位置 原始IP
if (originalIp != null) {
out.writeInt(originalIp.length);
out.writeBytes(originalIp);
} else {
// 防止原始IP 未填写
out.writeInt(0x00);
}
// 防止数据读错位置 clientId
if (clientIdBytes != null) {
out.writeInt(clientIdBytes.length);

View File

@ -11,6 +11,9 @@ public class ChannelAttributeKeyUtils {
private static final AttributeKey<String> VISITOR_ID = AttributeKey.newInstance("visitorId");
private static final AttributeKey<Integer> VISITOR_PORT = AttributeKey.newInstance("visitorPort");
private static final AttributeKey<String> CLIENT_ID = AttributeKey.newInstance("clientId");
private static final AttributeKey<String> APP_KEY = AttributeKey.newInstance("appKey");
private static final AttributeKey<String> APP_SECRET = AttributeKey.newInstance("appSecret");
private static final AttributeKey<String> ORIGINAL_IP = AttributeKey.newInstance("originalIp");
private static final AttributeKey<Integer> OUT_FLOW = AttributeKey.newInstance("outFlow");
private static final AttributeKey<Integer> IN_FLOW = AttributeKey.newInstance("inFlow");
@ -174,4 +177,62 @@ public class ChannelAttributeKeyUtils {
public static Channel getTransferNextChannel(Channel channel) {
return channel.attr(TRANSFER_NEXT_CHANNEL).get();
}
/**
* 为通道绑定 通道中访客端口
*
* @param channel 通道
* @param appKey 应用key
*/
public static void buildAppKey(Channel channel, String appKey) {
channel.attr(APP_KEY).set(appKey);
}
/**
* 获取 通道中 应用key
*
* @param channel 通道
*/
public static String getAppKey(Channel channel) {
return channel.attr(APP_KEY).get();
}
/**
* 为通道绑定 应用密钥
*
* @param channel 通道
* @param appSecret 应用密钥
*/
public static void buildAppSecret(Channel channel, String appSecret) {
channel.attr(APP_SECRET).set(appSecret);
}
/**
* 获取 通道中 应用密钥
*
* @param channel 通道
*/
public static String getAppSecret(Channel channel) {
return channel.attr(APP_SECRET).get();
}
/**
* 为通道绑定 原始IP
*
* @param channel 通道
* @param originalIp 原始IP
*/
public static void buildOriginalIp(Channel channel, String originalIp) {
channel.attr(ORIGINAL_IP).set(originalIp);
}
/**
* 获取 通道中 原始IP
*
* @param channel 通道
*/
public static String getOriginalIp(Channel channel) {
return channel.attr(ORIGINAL_IP).get();
}
}