新增端口映射支持选择UDP协议
This commit is contained in:
+3
-3
@@ -77,17 +77,17 @@ public class ProxyConfiguration implements LifecycleBean {
|
||||
}
|
||||
|
||||
@Bean("udpServerBossGroup")
|
||||
private NioEventLoopGroup udpBossGroup(@Inject ProxyConfig proxyConfig) {
|
||||
private NioEventLoopGroup udpServerBossGroup(@Inject ProxyConfig proxyConfig) {
|
||||
return new NioEventLoopGroup(proxyConfig.getServer().getUdp().getBossThreadCount());
|
||||
}
|
||||
|
||||
@Bean("udpServerWorkerGroup")
|
||||
private NioEventLoopGroup udpWorkerGroup(@Inject ProxyConfig proxyConfig) {
|
||||
private NioEventLoopGroup udpServerWorkerGroup(@Inject ProxyConfig proxyConfig) {
|
||||
return new NioEventLoopGroup(proxyConfig.getServer().getUdp().getWorkThreadCount());
|
||||
}
|
||||
|
||||
@Bean("udpServerBootstrap")
|
||||
public Bootstrap udpBootstrap(@Inject("udpServerBossGroup") NioEventLoopGroup udpServerBossGroup,
|
||||
public Bootstrap udpServerBootstrap(@Inject("udpServerBossGroup") NioEventLoopGroup udpServerBossGroup,
|
||||
@Inject("udpServerWorkerGroup") NioEventLoopGroup udpServerWorkerGroup,
|
||||
@Inject ProxyConfig proxyConfig) {
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
|
||||
+3
-1
@@ -4,6 +4,8 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import io.netty.channel.socket.DatagramPacket;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2023/9/16
|
||||
@@ -12,7 +14,7 @@ public class UdpVisitorChannelHandler extends SimpleChannelInboundHandler<Datagr
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
|
||||
|
||||
System.out.println("服务端接收到消息 \nsender:" + datagramPacket.sender().toString() + "内容\n" + datagramPacket.content().toString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ public class ProtocalService {
|
||||
return Lists.newArrayList(
|
||||
new ProtocalListRes().setName("TCP").setEnable(Boolean.TRUE).setRemark("支持一切TCP之上的协议"),
|
||||
new ProtocalListRes().setName("HTTP(S)").setEnable(Boolean.TRUE).setRemark("支持绑定子域名,未绑定时等价于时使用TCP。 若配置了证书,则同时支持HTTPS。"),
|
||||
new ProtocalListRes().setName("UDP").setEnable(Boolean.FALSE).setRemark("暂不支持")
|
||||
new ProtocalListRes().setName("UDP").setEnable(Boolean.TRUE).setRemark("暂不支持")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+14
-4
@@ -3,8 +3,10 @@ package org.dromara.neutrinoproxy.server.service;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import org.dromara.neutrinoproxy.core.Constants;
|
||||
import org.dromara.neutrinoproxy.server.constant.EnableStatusEnum;
|
||||
import org.dromara.neutrinoproxy.server.constant.NetworkProtocolEnum;
|
||||
import org.dromara.neutrinoproxy.server.dal.LicenseMapper;
|
||||
import org.dromara.neutrinoproxy.server.dal.PortMappingMapper;
|
||||
import org.dromara.neutrinoproxy.server.dal.PortPoolMapper;
|
||||
@@ -43,6 +45,8 @@ import java.util.stream.Collectors;
|
||||
public class VisitorChannelService {
|
||||
@Inject("tcpServerBootstrap")
|
||||
private ServerBootstrap tcpServerBootstrap;
|
||||
@Inject("udpServerBootstrap")
|
||||
private Bootstrap udpServerBootstrap;
|
||||
@Inject
|
||||
private ProxyMutualService proxyMutualService;
|
||||
@Db
|
||||
@@ -219,14 +223,20 @@ public class VisitorChannelService {
|
||||
|
||||
for (PortMappingDO portMapping : portMappingList) {
|
||||
if (EnableStatusEnum.DISABLE.getStatus().equals(portMapping.getEnable())) {
|
||||
// 端口映射被禁用了,忽略 TODO 端口被禁用了也需要处理
|
||||
// 端口映射被禁用了,忽略 TODO 映射没被禁用,但端口被禁用了也需要处理
|
||||
continue;
|
||||
}
|
||||
// TODO 此处切入,TCP/UDP代理
|
||||
try {
|
||||
proxyMutualService.bindServerPort(cmdChannelAttachInfo, portMapping.getServerPort());
|
||||
tcpServerBootstrap.bind(portMapping.getServerPort()).get();
|
||||
log.info("绑定用户端口: {}", portMapping.getServerPort());
|
||||
|
||||
NetworkProtocolEnum networkProtocolEnum = NetworkProtocolEnum.of(portMapping.getProtocal());
|
||||
if (networkProtocolEnum == NetworkProtocolEnum.UDP) {
|
||||
udpServerBootstrap.bind(portMapping.getServerPort()).get();
|
||||
log.info("绑定UDP用户端口: {}", portMapping.getServerPort());
|
||||
} else {
|
||||
tcpServerBootstrap.bind(portMapping.getServerPort()).get();
|
||||
log.info("绑定TCP用户端口: {}", portMapping.getServerPort());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
// BindException表示该端口已经绑定过
|
||||
if (!(ex.getCause() instanceof BindException)) {
|
||||
|
||||
Reference in New Issue
Block a user