天眼封禁优化

back_recharge
mengchengzhen 2021-06-08 10:41:51 +08:00
parent 4de7b94119
commit 962f0641b7
5 changed files with 121 additions and 67 deletions

View File

@ -1,6 +1,7 @@
package com.ljsd.jieling.netty.handler;
import com.ljsd.jieling.netty.server.SkyEyeClient;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoop;
@ -9,45 +10,50 @@ import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rpc.protocols.ChatProto;
import java.util.concurrent.TimeUnit;
@ChannelHandler.Sharable
public class SkyEyeHandler extends SimpleChannelInboundHandler {
private static final Logger logger = LoggerFactory.getLogger(SkyEyeHandler.class);
long start = 0;
private long heartbeatCount = 0;
private SkyEyeClient cilent;
public SkyEyeHandler(SkyEyeClient cilent){
this.cilent = cilent;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.info("天眼激活......");
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("掉线了"+(System.currentTimeMillis()-start)/1000);
logger.info("天眼channelInactive......");
EventLoop loop = ctx.channel().eventLoop();
loop.schedule(() ->{
logger.info("天眼reconnect......");
cilent.connect();
},1, TimeUnit.SECONDS);
ctx.fireChannelInactive();
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println();
ByteBuf in = (ByteBuf)msg;
int length = in.readableBytes();
if(length<=6){
return;
}
byte[] b = new byte[length-6];
in.readBytes(6);
for(int i =0;i<length-6;i++){
b[i] = in.readByte();
}
ChatProto.ChatV3 v3 = ChatProto.ChatV3.parseFrom(b);
}
// @Override
// public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
//// ctx.channel().writeAndFlush("TCX心跳");
//// System.out.println("发送心跳"+(System.currentTimeMillis()-start)/1000);
// super.userEventTriggered(ctx, evt);
//// if (evt instanceof IdleStateEvent) {
//// IdleStateEvent event = (IdleStateEvent) evt;
//// if (event.state().equals(IdleState.READER_IDLE)) {
//// System.out.println("长期没收到服务器推送数据");
//// //可以选择重新连接
//// } else if (event.state().equals(IdleState.WRITER_IDLE)) {
//// System.out.println("长期未向服务器发送数据");
//// } else if (event.state().equals(IdleState.ALL_IDLE)) {
//// System.out.println("ALL");
//// }
//// }
// }
}

View File

@ -19,7 +19,6 @@ public class SkyEyeClient {
private static Bootstrap bootstrap = null;
public static Channel channel;
private EventLoopGroup group = new NioEventLoopGroup();
protected final HashedWheelTimer timer = new HashedWheelTimer();
private final int port = 8741;
private final String host = "api.gamegs.cn";
@ -30,49 +29,43 @@ public class SkyEyeClient {
public void doConnect(EventLoopGroup group){
// ChannelFuture f = null;
ChannelFuture future=null;
Bootstrap bootstrap = new Bootstrap();
bootstrap = new Bootstrap();
bootstrap.group(group)
.option(ChannelOption.SO_KEEPALIVE,true)
.channel(NioSocketChannel.class);
final ConnectionWatchdog watchdog = new ConnectionWatchdog(bootstrap, timer, port,host, true) {
@Override
public ChannelHandler[] handlers() {
return new ChannelHandler[] {
this,
new SkyEyeHandler()
};
}
};
try {
synchronized (bootstrap){
bootstrap.remoteAddress(host,port);
bootstrap.handler(new ChannelInitializer<Channel>() {
//初始化channel
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(watchdog.handlers());
ch.pipeline().addLast(new SkyEyeHandler(SkyEyeClient.this));
}
});
future = bootstrap.connect(host,port);
}
future.sync();
// f = bootstrap.connect();
// f.addListener(new ConnectionListener());
channel = future.channel();
connect();
// channel.closeFuture().sync();
} catch (Throwable t) {
LOGGER.error("客户端连接失败------------,ip为{},端口为{}",host,port);
if (null != future) {
try {
timer.newTimeout(watchdog,60, TimeUnit.SECONDS);
} catch (Exception e1) {
LOGGER.error("客户端连接失败------------,ip为{},端口为{}",host,port);
e1.printStackTrace();
}
}
}finally {
// group.shutdownGracefully();
public void connect(){
synchronized (bootstrap) {
ChannelFuture future = bootstrap.connect(host, port);
channel = future.channel();
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture f) throws Exception {
boolean succeed = f.isSuccess();
Channel channel = f.channel();
if (!succeed) {
LOGGER.info("重连失败");
f.channel().pipeline().fireChannelInactive();
} else {
LOGGER.info("重连成功");
}
}
});
}
}

View File

@ -0,0 +1,16 @@
package com.ljsd.jieling.config.skyeye;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(SkyEyeProperties.class)
public class SkyEyeAutoConfigration {
@Autowired
private SkyEyeProperties skyEyeProperties;
public SkyEyeProperties getSkyEyeProperties() {
return skyEyeProperties;
}
}

View File

@ -0,0 +1,28 @@
package com.ljsd.jieling.config.skyeye;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "skyeye")
public class SkyEyeProperties {
private String appId;
private String gmAddress;
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getGmAddress() {
return gmAddress;
}
public void setGmAddress(String gmAddress) {
this.gmAddress = gmAddress;
}
}

View File

@ -1,5 +1,6 @@
package com.ljsd.jieling.util;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.protobuf.ByteString;
import com.google.protobuf.GeneratedMessage;
@ -7,6 +8,8 @@ import com.googlecode.protobuf.format.JsonFormat;
import com.ljsd.GameApplication;
import com.ljsd.SkyEyeService;
import com.ljsd.jieling.chat.messge.MessageCache;
import com.ljsd.jieling.config.skyeye.SkyEyeAutoConfigration;
import com.ljsd.jieling.config.skyeye.SkyEyeProperties;
import com.ljsd.jieling.core.GlobalsDef;
import com.ljsd.jieling.core.SimpleTransaction;
import com.ljsd.jieling.db.redis.RedisKey;
@ -36,6 +39,7 @@ import java.util.*;
public class MessageUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageUtil.class);
private static HashMap<String, String> skyEyeExtraMap = new HashMap<>();
public static byte[] wrappedBuffer(int uid, int token, int index, int result, int msgId, GeneratedMessage generatedMessage) {
byte[] backMessage;
@ -79,8 +83,9 @@ public class MessageUtil {
return bytes1;
}
public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage) {
byte[] idByte = short2byte((short) 0xd6ac);
public static byte[] wrappedBufferSE(GeneratedMessage generatedMessage,SkyEyeProperties properties) {
int b1 = Integer.parseInt(properties.getAppId(),16);
byte[] idByte = short2byte((short) b1);
byte[] backMessage = generatedMessage.toByteArray();
byte[] verifyByte = new byte[2+4+backMessage.length];
byte[] lengthByte =int2bytes(backMessage.length);
@ -457,8 +462,10 @@ public class MessageUtil {
if(uid != 0){
he = UserManager.getUser(uid);
}
ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel);
byte[] byteBuf = wrappedBufferSE(v3.build());
SkyEyeAutoConfigration netServerConfig = ConfigurableApplicationContextManager.getBean(SkyEyeAutoConfigration.class);
SkyEyeProperties properties = netServerConfig.getSkyEyeProperties();
ChatProto.ChatV3.Builder v3 = buildChatV3(msg,me,he,channel,properties);
byte[] byteBuf = wrappedBufferSE(v3.build(),properties);
// session.writeAndFlush(byteBuf);
// session.putBackMassageToMap(byteBuf);
ByteBuf bb = PooledByteBufAllocator.DEFAULT.heapBuffer();
@ -466,7 +473,7 @@ public class MessageUtil {
SkyEyeService.sendMsg(bb);
}
public static ChatProto.ChatV3.Builder buildChatV3(String msg,User me,User he,int type){
public static ChatProto.ChatV3.Builder buildChatV3(String msg,User me,User he,int type,SkyEyeProperties properties){
ChatProto.ChatV3.Builder v3 = ChatProto.ChatV3.newBuilder();
String uuid = UUID.randomUUID().toString().replaceAll("-","");
v3.setId(uuid);
@ -479,6 +486,10 @@ public class MessageUtil {
ISession iSession = OnlineUserManager.getSessionByUid(me.getId());
String ip = iSession.getChannel().getLocalAddress().getHostString();
v3.setIp(ip);
skyEyeExtraMap.clear();
String url = properties.getGmAddress();
skyEyeExtraMap.put("url",url);
v3.setExtra(JSON.toJSONString(skyEyeExtraMap));
return v3;
}
@ -491,7 +502,7 @@ public class MessageUtil {
v3.setVipLevel(user.getPlayerInfoManager().getVipLevel());
v3.setZoneId(String.valueOf(GameApplication.serverId));
v3.setZoneName(String.valueOf(GameApplication.serverId));
v3.setServerId(1+"");
v3.setServerId(user.getPlayerInfoManager().getChannel_id());
return v3;
}
//高位在前,低位在后