perf: 重写部分代码

This commit is contained in:
2023-05-31 04:33:29 +08:00
parent 5851272688
commit e557202ea3
7 changed files with 50 additions and 21 deletions

View File

@@ -31,8 +31,8 @@ public final class ServerConfig {
map = new Yaml().load(stream);
}
host = (String) map.get("host");
port = (int) map.get("port");
host = map.getOrDefault("host", "localhost").toString();
port = (int) map.getOrDefault("port", 58888);
eventLoopThread = (int) map.getOrDefault("event-loop-thread", 5);
enableAcceptList = (boolean) map.get("enable-accept-list");
acceptList = (List<String>) map.get("accept-list");

View File

@@ -20,7 +20,7 @@ public class BallServerChannelHandler extends SimpleChannelInboundHandler<String
@Override
protected void channelRead0(ChannelHandlerContext context, String message) {
if ("ping".equals(message)) {
context.writeAndFlush("pong");
context.channel().writeAndFlush("pong");
return;
}
try {

View File

@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.ball.common.data.BallMessageInfo;
import cn.hamster3.mc.plugin.ball.server.config.ServerConfig;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
@@ -14,12 +14,13 @@ import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class BallServerChannelInitializer extends ChannelInitializer<NioSocketChannel> {
public class BallServerChannelInitializer extends ChannelInitializer<SocketChannel> {
public static final List<Channel> CHANNELS = new ArrayList<>();
private static final Logger LOGGER = LoggerFactory.getLogger("BallServerChannelInitializer");
@@ -36,13 +37,26 @@ public class BallServerChannelInitializer extends ChannelInitializer<NioSocketCh
}
@Override
protected void initChannel(@NotNull NioSocketChannel channel) {
LOGGER.info("远程地址 {} 请求建立连接.", channel.remoteAddress().toString());
protected void initChannel(@NotNull SocketChannel channel) {
InetSocketAddress remoteAddress = channel.remoteAddress();
LOGGER.info("远程地址 {} 请求建立连接.", remoteAddress.toString());
String hostAddress = channel.remoteAddress().getAddress().getHostAddress();
String hostAddress = remoteAddress.getAddress().getHostAddress();
if (ServerConfig.isEnableAcceptList() && !ServerConfig.getAcceptList().contains(hostAddress)) {
channel.disconnect();
LOGGER.warn("{} 不在白名单列表中, 已断开连接!", hostAddress);
LOGGER.warn("{} 不在白名单列表中, 断开连接!", hostAddress);
channel.pipeline()
.addLast(new LengthFieldPrepender(8))
.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 8, 0, 8))
.addLast(new StringDecoder(StandardCharsets.UTF_8))
.addLast(new StringEncoder(StandardCharsets.UTF_8));
try {
channel.writeAndFlush("connection refused").await();
channel.disconnect().await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return;
}