diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/job/DemoJob.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/job/DemoJob.java index 4696dca2..782acce1 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/job/DemoJob.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/job/DemoJob.java @@ -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 { diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/ProxyTunnelServer.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/ProxyTunnelServer.java index d45d2a25..286ed1f2 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/ProxyTunnelServer.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/core/ProxyTunnelServer.java @@ -63,9 +63,9 @@ public class ProxyTunnelServer implements EventListener { }); try { bootstrap.bind(proxyConfig.getTunnel().getPort()).sync(); - log.info("proxy server started,port:{}", proxyConfig.getTunnel().getPort()); + log.info("ProxyTunnelServer,port:{}", 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 { }); try { bootstrap.bind(proxyConfig.getTunnel().getSslPort()).sync(); - log.info("proxy server started,SSL port: {}", proxyConfig.getTunnel().getSslPort()); + log.info("ProxyTunnelServer,SSL port: {}", proxyConfig.getTunnel().getSslPort()); } catch (Exception e) { - log.error("proxy server error", e); + log.error("ProxyTunnelServer error", e); } } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpProxy.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpProxy.java index 7d165a0f..82dd3065 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpProxy.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpProxy.java @@ -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 { @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() { + bootstrap.group(bossGroup, workerGroup) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { @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 success!port:{}", 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(); } } + } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpVisitorChannelHandler.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpVisitorChannelHandler.java index fe0c0bfb..f5d4c503 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpVisitorChannelHandler.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpVisitorChannelHandler.java @@ -30,6 +30,11 @@ import java.net.InetSocketAddress; */ @Slf4j public class HttpVisitorChannelHandler extends SimpleChannelInboundHandler { + 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 { @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 { } 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() { - @Override - public void initChannel(SocketChannel ch) throws Exception { + bootstrap.group(bossGroup, workerGroup); + bootstrap.channel(NioServerSocketChannel.class); + bootstrap.childHandler(new ChannelInitializer() { + @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 started!port:{}", 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(); } } diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java index 909323ed..8791163d 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/security/HttpVisitorSecurityChannelHandler.java @@ -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() { - @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()); } // 继续传播 diff --git a/neutrino-proxy-server/src/main/resources/app.yml b/neutrino-proxy-server/src/main/resources/app.yml index 7e4e68a0..53f75922 100644 --- a/neutrino-proxy-server/src/main/resources/app.yml +++ b/neutrino-proxy-server/src/main/resources/app.yml @@ -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: diff --git a/neutrino-proxy-server/src/main/resources/sql/h2/port_pool.data.sql b/neutrino-proxy-server/src/main/resources/sql/h2/port_pool.data.sql index 056dc93f..136349f3 100644 --- a/neutrino-proxy-server/src/main/resources/sql/h2/port_pool.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/h2/port_pool.data.sql @@ -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()); diff --git a/neutrino-proxy-server/src/main/resources/sql/mariadb/port_pool.data.sql b/neutrino-proxy-server/src/main/resources/sql/mariadb/port_pool.data.sql index e4288b13..136349f3 100644 --- a/neutrino-proxy-server/src/main/resources/sql/mariadb/port_pool.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/mariadb/port_pool.data.sql @@ -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()); diff --git a/neutrino-proxy-server/src/main/resources/sql/mysql/port_pool.data.sql b/neutrino-proxy-server/src/main/resources/sql/mysql/port_pool.data.sql index e4288b13..136349f3 100644 --- a/neutrino-proxy-server/src/main/resources/sql/mysql/port_pool.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/mysql/port_pool.data.sql @@ -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());