服务端直接转发到服务端端口 待完成

This commit is contained in:
xj
2024-03-10 23:02:46 +08:00
parent e2c6490fae
commit 446390743c
10 changed files with 75 additions and 153 deletions
@@ -13,7 +13,7 @@ import org.noear.solon.annotation.Component;
@Slf4j
@Component
@JobHandler(name = "DemoJob", cron = "0/10 * * * * ?", param = "{\"a\":1}")
public class DemoJob implements IJobHandler {
public class DemoJob implements IJobHandler {
@Override
public void execute(String param) throws Exception {
@@ -63,9 +63,9 @@ public class ProxyTunnelServer implements EventListener<AppLoadEndEvent> {
});
try {
bootstrap.bind(proxyConfig.getTunnel().getPort()).sync();
log.info("proxy server startedport{}", proxyConfig.getTunnel().getPort());
log.info("ProxyTunnelServerport{}", proxyConfig.getTunnel().getPort());
} catch (Exception e) {
log.error("proxy server error", e);
log.error("ProxyTunnelServer error", e);
}
}
@@ -84,9 +84,9 @@ public class ProxyTunnelServer implements EventListener<AppLoadEndEvent> {
});
try {
bootstrap.bind(proxyConfig.getTunnel().getSslPort()).sync();
log.info("proxy server startedSSL port {}", proxyConfig.getTunnel().getSslPort());
log.info("ProxyTunnelServerSSL port {}", proxyConfig.getTunnel().getSslPort());
} catch (Exception e) {
log.error("proxy server error", e);
log.error("ProxyTunnelServer error", e);
}
}
@@ -7,6 +7,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.neutrinoproxy.server.base.proxy.ProxyConfig;
import org.dromara.neutrinoproxy.server.proxy.core.BytesMetricsHandler;
@@ -18,6 +19,9 @@ import org.noear.solon.annotation.Inject;
import org.noear.solon.core.event.AppLoadEndEvent;
import org.noear.solon.core.event.EventListener;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
/**
* 应用加载完成事件(即启动完成)- 判断是否配置域名-配置了域名则启动HTTP代理
* 默认端口 80
@@ -30,36 +34,54 @@ public class HttpProxy implements EventListener<AppLoadEndEvent> {
@Inject
private ProxyConfig proxyConfig;
/**
* NioServerSocketChannel对应的future
*/
protected ChannelFuture httpFuture;
@Override
public void onEvent(AppLoadEndEvent appLoadEndEvent) throws Throwable {
if (null == proxyConfig.getServer().getTcp().getHttpProxyPort()) {
log.info("no config domain name,nonsupport http proxy.");
return;
}
this.start();
}
private void start() {
// 处理网络连接---接受请求
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
// 进行socketChannel的网络读写
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (null != proxyConfig.getServer().getTcp().getTransferLogEnable() &&
proxyConfig.getServer().getTcp().getTransferLogEnable()) {
ch.pipeline().addFirst(new LoggingHandler(HttpProxy.class));
}
ch.pipeline().addFirst(new BytesMetricsHandler())
if (null != proxyConfig.getServer().getTcp().getTransferLogEnable() &&
proxyConfig.getServer().getTcp().getTransferLogEnable()) {
ch.pipeline().addFirst(new LoggingHandler(HttpProxy.class));
}
ch.pipeline().addFirst(new BytesMetricsHandler())
.addLast(new HttpVisitorSecurityChannelHandler())
.addLast("flowLimiter",new VisitorFlowLimiterChannelHandler())
.addLast(new HttpVisitorChannelHandler());
.addLast(new HttpVisitorChannelHandler(ch));
}
});
bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpProxyPort()).sync();
httpFuture = bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpProxyPort()).sync();
log.info("Http proxy server start successport:{}", proxyConfig.getServer().getTcp().getHttpProxyPort());
//添加关闭重启的监听器,3秒后尝试重启
// httpFuture.channel().closeFuture().addListener(genericFutureListener);
// httpFuture.channel().closeFuture().sync();
} catch (Exception e) {
log.error("http proxy start err!", e);
} finally {
// bossGroup.shutdownGracefully();
// workerGroup.shutdownGracefully();
// httpFuture.channel().close();
}
}
}
@@ -30,6 +30,11 @@ import java.net.InetSocketAddress;
*/
@Slf4j
public class HttpVisitorChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
private SocketChannel sch;
public HttpVisitorChannelHandler(SocketChannel sch) {
this.sch = sch;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
@@ -60,6 +65,7 @@ public class HttpVisitorChannelHandler extends SimpleChannelInboundHandler<ByteB
// 根据域名拿到绑定的映射对应的cmdChannel
Integer serverPort = ctx.channel().attr(Constants.SERVER_PORT).get();
Channel cmdChannel = ProxyUtil.getCmdChannelByServerPort(serverPort);
if (null == cmdChannel) {
ctx.channel().close();
return;
@@ -36,6 +36,10 @@ import java.security.KeyStore;
public class HttpsProxy implements EventListener<AppLoadEndEvent> {
@Inject
private ProxyConfig proxyConfig;
/**
* NioServerSocketChannel对应的future
*/
private ChannelFuture httpsFuture;
@Override
public void onEvent(AppLoadEndEvent appLoadEndEvent) throws Throwable {
if (null == proxyConfig.getServer().getTcp().getHttpsProxyPort() ||
@@ -48,26 +52,38 @@ public class HttpsProxy implements EventListener<AppLoadEndEvent> {
}
private void start() {
// 处理网络连接---接受请求
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
// 进行socketChannel的网络读写
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
if (null != proxyConfig.getServer().getTcp().getTransferLogEnable() && proxyConfig.getServer().getTcp().getTransferLogEnable()) {
ch.pipeline().addFirst(new LoggingHandler(HttpsProxy.class));
}
ch.pipeline().addLast(createSslHandler());
ch.pipeline().addFirst(new BytesMetricsHandler());
ch.pipeline().addLast(new HttpVisitorSecurityChannelHandler());
ch.pipeline().addLast("flowLimiter",new VisitorFlowLimiterChannelHandler());
ch.pipeline().addLast(new HttpVisitorChannelHandler());
}
});
bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpsProxyPort()).sync();
ch.pipeline().addLast(createSslHandler())
.addFirst(new BytesMetricsHandler())
.addLast(new HttpVisitorSecurityChannelHandler())
.addLast("flowLimiter", new VisitorFlowLimiterChannelHandler())
.addLast(new HttpVisitorChannelHandler(ch));
}
});
httpsFuture = bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpsProxyPort()).sync();
log.info("Https proxy server startedport:{}", proxyConfig.getServer().getTcp().getHttpsProxyPort());
//添加关闭重启的监听器,3秒后尝试重启
// httpsFuture.channel().closeFuture().addListener(genericFutureListener);
// httpsFuture.channel().closeFuture().sync();
} catch (Exception e) {
log.error("https proxy start err!", e);
} finally {
// bossGroup.shutdownGracefully();
// workerGroup.shutdownGracefully();
// httpsFuture.channel().close();
}
}
@@ -24,40 +24,8 @@ import org.noear.solon.Solon;
*/
@Slf4j
public class HttpVisitorSecurityChannelHandler extends ChannelInboundHandlerAdapter {
private final SecurityGroupService securityGroupService = Solon.context().getBean(SecurityGroupService.class);
private final PortMappingService portMappingService = Solon.context().getBean(PortMappingService.class);
private Bootstrap bootstrap;
/**
* 内部转发处理器
*/
class ProxyInnerHandler extends ChannelInboundHandlerAdapter {
private Channel channel;
public ProxyInnerHandler(Channel channel) {
bootstrap = new Bootstrap();
this.channel = channel;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf readBuffer = (ByteBuf) msg;
readBuffer.retain();
channel.writeAndFlush(readBuffer);
}
}
private Channel getClientChannel(SocketChannel ch) throws InterruptedException {
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast("clientHandler", new ProxyInnerHandler(ch));
}
});
// 转发地址
ChannelFuture sync = bootstrap.connect("127.0.0.1", 9527);
return sync.channel();
}
private SecurityGroupService securityGroupService = Solon.context().getBean(SecurityGroupService.class);
private PortMappingService portMappingService = Solon.context().getBean(PortMappingService.class);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
@@ -95,7 +63,7 @@ public class HttpVisitorSecurityChannelHandler extends ChannelInboundHandlerAdap
}
ctx.channel().attr(Constants.REAL_REMOTE_IP).set(ip);
ctx.channel().attr(Constants.SERVER_PORT).set(dm.getId());
ctx.channel().attr(Constants.LICENSE_ID).set(dm.getLicenseId());
// ctx.channel().attr(Constants.LICENSE_ID).set(dm.getLicenseId());
}
// 继续传播
@@ -61,8 +61,8 @@ neutrino:
# 如果不配置,则不支持域名映射
domain-name: ${DOMAIN_NAME:server.com}
# https证书配置
key-store-password: ${HTTPS_STORE_PASS:}
jks-path: ${HTTPS_JKS_PATH:}
key-store-password: ${HTTPS_STORE_PASS:123456}
jks-path: ${HTTPS_JKS_PATH:classpath:/test.jks}
# 是否开启代理服务报文日志(日志级别为debug时开启才有效)
transfer-log-enable: ${SERVER_LOG:false}
udp:
@@ -9,33 +9,3 @@ INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update
(4, 1, 9104, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(5, 1, 9105, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(6, 1, 9106, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(7, 1, 9107, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(8, 1, 9108, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(9, 1, 9109, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(10, 1, 9110, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(11, 1, 9111, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(12, 1, 9112, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(13, 1, 9113, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(14, 1, 9114, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(15, 1, 9115, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(16, 1, 9116, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(17, 1, 9117, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(18, 1, 9118, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(19, 1, 9119, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(20, 1, 9120, 1, now(), now());
@@ -9,33 +9,3 @@ INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update
(4, 1, 9104, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(5, 1, 9105, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(6, 1, 9106, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(7, 1, 9107, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(8, 1, 9108, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(9, 1, 9109, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(10, 1, 9110, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(11, 1, 9111, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(12, 1, 9112, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(13, 1, 9113, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(14, 1, 9114, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(15, 1, 9115, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(16, 1, 9116, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(17, 1, 9117, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(18, 1, 9118, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(19, 1, 9119, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(20, 1, 9120, 1, now(), now());
@@ -9,33 +9,3 @@ INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update
(4, 1, 9104, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(5, 1, 9105, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(6, 1, 9106, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(7, 1, 9107, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(8, 1, 9108, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(9, 1, 9109, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(10, 1, 9110, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(11, 1, 9111, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(12, 1, 9112, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(13, 1, 9113, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(14, 1, 9114, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(15, 1, 9115, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(16, 1, 9116, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(17, 1, 9117, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(18, 1, 9118, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(19, 1, 9119, 1, now(), now());
INSERT INTO port_pool(`id`, `group_id`, `port`, `enable`, `create_time`, `update_time`) VALUES
(20, 1, 9120, 1, now(), now());