From f0a61a1d30d610a1f2fb830d01ade2d00e9e2f0b Mon Sep 17 00:00:00 2001 From: xiaojie <550345579@qq.com> Date: Tue, 25 Jun 2024 10:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0=E9=9C=80=E9=83=A8=E7=BD=B2=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E5=BE=85=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 服务端直接转发功能 后续将通过集成nginx来实现服务端转发 --- .gitignore | 1 + README.md | 2 +- neutrino-proxy-admin/config/dev.env.js | 2 +- neutrino-proxy-admin/config/index.js | 4 ++-- neutrino-proxy-admin/config/prod.env.js | 2 +- .../src/views/proxy/license.vue | 6 +++-- .../server/dal/LicenseMapper.java | 3 ++- .../server/proxy/enhance/HttpProxy.java | 24 ++++++++----------- .../enhance/HttpVisitorChannelHandler.java | 18 ++++---------- .../server/proxy/enhance/HttpsProxy.java | 14 +++++++---- .../HttpVisitorSecurityChannelHandler.java | 9 +++---- .../server/service/LicenseService.java | 18 +++++++------- .../main/resources/sql/h2/license.data.sql | 2 +- 13 files changed, 48 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index cfef0ef9..66e68a11 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ logs data **/cert/** neutrino-proxy-server/src/main/resources/static/** +/neutrino-proxy-server/src/main/resources/static/ diff --git a/README.md b/README.md index 3097b4a8..8f22c490 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # what is new? 在原版的基础上新增了【域名解析】功能,域名解析【无需】绑定【本地端口】

- +

diff --git a/neutrino-proxy-admin/config/dev.env.js b/neutrino-proxy-admin/config/dev.env.js index 799ec796..6bb64a3f 100644 --- a/neutrino-proxy-admin/config/dev.env.js +++ b/neutrino-proxy-admin/config/dev.env.js @@ -1,7 +1,7 @@ module.exports = { NODE_ENV: '"development"', ENV_CONFIG: '"dev"', - BASE_API: '"https://server.com"', + BASE_API: '"http://localhost:8888/neutrino-proxy-server"', USER_NAME: '"visitor"', USER_PWD: '"123456"' } diff --git a/neutrino-proxy-admin/config/index.js b/neutrino-proxy-admin/config/index.js index e6160568..99bb914b 100644 --- a/neutrino-proxy-admin/config/index.js +++ b/neutrino-proxy-admin/config/index.js @@ -55,8 +55,8 @@ module.exports = { assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', - // you can set by youself according to actual condition - assetsPublicPath: './', + // you can set by youself according to actual condition + assetsPublicPath: '/', /** * Source Maps diff --git a/neutrino-proxy-admin/config/prod.env.js b/neutrino-proxy-admin/config/prod.env.js index 0c43ea73..0fde97f7 100644 --- a/neutrino-proxy-admin/config/prod.env.js +++ b/neutrino-proxy-admin/config/prod.env.js @@ -1,5 +1,5 @@ module.exports = { NODE_ENV: '"production"', ENV_CONFIG: '"prod"', - BASE_API: '"https://api-prod"' + BASE_API: '"http://np.myone.cc"' // 后端访问地址与 server配置一致 } diff --git a/neutrino-proxy-admin/src/views/proxy/license.vue b/neutrino-proxy-admin/src/views/proxy/license.vue index 401b0af1..2d0f1b59 100644 --- a/neutrino-proxy-admin/src/views/proxy/license.vue +++ b/neutrino-proxy-admin/src/views/proxy/license.vue @@ -63,11 +63,13 @@ diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/LicenseMapper.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/LicenseMapper.java index 99fccb27..e163def4 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/LicenseMapper.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/dal/LicenseMapper.java @@ -71,9 +71,10 @@ public interface LicenseMapper extends BaseMapper { ); } + +// .ne(LicenseDO::getId, 1) default void updateOnlineStatus(Integer isOnline, Date updateTime) { this.update(null, new LambdaUpdateWrapper() - .ne(LicenseDO::getId, 1) .set(LicenseDO::getIsOnline, isOnline) .set(LicenseDO::getUpdateTime, updateTime) ); 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 82dd3065..a0db65a1 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 @@ -1,17 +1,16 @@ package org.dromara.neutrinoproxy.server.proxy.enhance; -import cn.hutool.core.util.StrUtil; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; 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; -import org.dromara.neutrinoproxy.server.proxy.core.ProxyTunnelServer; import org.dromara.neutrinoproxy.server.proxy.security.HttpVisitorSecurityChannelHandler; import org.dromara.neutrinoproxy.server.proxy.security.VisitorFlowLimiterChannelHandler; import org.noear.solon.annotation.Component; @@ -19,9 +18,6 @@ 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 @@ -60,18 +56,18 @@ public class HttpProxy implements EventListener { .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(ch)); + .addLast(new HttpVisitorChannelHandler()); } }); httpFuture = bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpProxyPort()).sync(); - log.info("Http proxy server start success!port:{}", proxyConfig.getServer().getTcp().getHttpProxyPort()); + log.info("Http proxy server started success!port:{}", proxyConfig.getServer().getTcp().getHttpProxyPort()); //添加关闭重启的监听器,3秒后尝试重启 // httpFuture.channel().closeFuture().addListener(genericFutureListener); // httpFuture.channel().closeFuture().sync(); 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 f5d4c503..f05e8f3d 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 @@ -1,24 +1,19 @@ package org.dromara.neutrinoproxy.server.proxy.enhance; -import io.netty.bootstrap.Bootstrap; -import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; -import io.netty.channel.*; -import io.netty.channel.socket.SocketChannel; -import io.netty.handler.proxy.ProxyHandler; +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelOption; +import io.netty.channel.SimpleChannelInboundHandler; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.dromara.neutrinoproxy.core.Constants; import org.dromara.neutrinoproxy.core.ProxyMessage; -import org.dromara.neutrinoproxy.core.util.HttpUtil; import org.dromara.neutrinoproxy.server.constant.NetworkProtocolEnum; -import org.dromara.neutrinoproxy.server.proxy.domain.DomainMapping; import org.dromara.neutrinoproxy.server.proxy.domain.ProxyAttachment; -import org.dromara.neutrinoproxy.server.proxy.domain.ProxyMapping; import org.dromara.neutrinoproxy.server.proxy.domain.VisitorChannelAttachInfo; import org.dromara.neutrinoproxy.server.service.FlowReportService; import org.dromara.neutrinoproxy.server.util.ProxyUtil; -import org.h2.store.DataHandler; import org.noear.solon.Solon; import java.net.InetSocketAddress; @@ -30,11 +25,6 @@ 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 { diff --git a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpsProxy.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpsProxy.java index 99538743..db4765a7 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpsProxy.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/proxy/enhance/HttpsProxy.java @@ -1,8 +1,10 @@ package org.dromara.neutrinoproxy.server.proxy.enhance; -import cn.hutool.core.util.StrUtil; import io.netty.bootstrap.ServerBootstrap; -import io.netty.channel.*; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; @@ -13,7 +15,6 @@ import org.apache.commons.lang3.StringUtils; import org.dromara.neutrinoproxy.core.util.FileUtil; import org.dromara.neutrinoproxy.server.base.proxy.ProxyConfig; import org.dromara.neutrinoproxy.server.proxy.core.BytesMetricsHandler; -import org.dromara.neutrinoproxy.server.proxy.core.ProxyTunnelServer; import org.dromara.neutrinoproxy.server.proxy.security.HttpVisitorSecurityChannelHandler; import org.dromara.neutrinoproxy.server.proxy.security.VisitorFlowLimiterChannelHandler; import org.noear.solon.annotation.Component; @@ -21,7 +22,10 @@ import org.noear.solon.annotation.Inject; import org.noear.solon.core.event.AppLoadEndEvent; import org.noear.solon.core.event.EventListener; -import javax.net.ssl.*; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.TrustManager; import java.io.InputStream; import java.security.KeyStore; @@ -70,7 +74,7 @@ public class HttpsProxy implements EventListener { .addFirst(new BytesMetricsHandler()) .addLast(new HttpVisitorSecurityChannelHandler()) .addLast("flowLimiter", new VisitorFlowLimiterChannelHandler()) - .addLast(new HttpVisitorChannelHandler(ch)); + .addLast(new HttpVisitorChannelHandler()); } }); httpsFuture = bootstrap.bind("0.0.0.0", proxyConfig.getServer().getTcp().getHttpsProxyPort()).sync(); 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 8791163d..d87311cc 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 @@ -1,11 +1,8 @@ package org.dromara.neutrinoproxy.server.proxy.security; -import cn.hutool.core.util.StrUtil; -import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; -import io.netty.channel.*; -import io.netty.channel.socket.SocketChannel; -import io.netty.handler.proxy.ProxyHandler; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.dromara.neutrinoproxy.core.Constants; @@ -63,7 +60,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/java/org/dromara/neutrinoproxy/server/service/LicenseService.java b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/LicenseService.java index 88cf9c77..1becd487 100644 --- a/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/LicenseService.java +++ b/neutrino-proxy-server/src/main/java/org/dromara/neutrinoproxy/server/service/LicenseService.java @@ -205,18 +205,18 @@ public class LicenseService implements LifecycleBean { */ @Tran public LicenseUpdateEnableStatusRes updateEnableStatus(LicenseUpdateEnableStatusReq req) { - // 服务端免通道 直接转发,所以无需建立客户端通道。启用即在线,禁用即离线 - if (req.getId() == 1) { - licenseMapper.update(null, new LambdaUpdateWrapper() - .eq(LicenseDO::getId, req.getId()) - .set(LicenseDO::getEnable, req.getEnable()) - .set(LicenseDO::getIsOnline, req.getEnable()) - .set(LicenseDO::getUpdateTime, new Date())); - } else { + // 服务端免通道 直接转发,所以无需建立客户端通道。启用即在线,禁用即离线 20240311 设计取消 +// if (req.getId() == 1) { +// licenseMapper.update(null, new LambdaUpdateWrapper() +// .eq(LicenseDO::getId, req.getId()) +// .set(LicenseDO::getEnable, req.getEnable()) +// .set(LicenseDO::getIsOnline, req.getEnable()) +// .set(LicenseDO::getUpdateTime, new Date())); +// } else { licenseMapper.updateEnableStatus(req.getId(), req.getEnable(), new Date()); // 更新VisitorChannel visitorChannelService.updateVisitorChannelByLicenseId(req.getId(), req.getEnable()); - } +// } return new LicenseUpdateEnableStatusRes(); } diff --git a/neutrino-proxy-server/src/main/resources/sql/h2/license.data.sql b/neutrino-proxy-server/src/main/resources/sql/h2/license.data.sql index 990d6806..529ae9d5 100644 --- a/neutrino-proxy-server/src/main/resources/sql/h2/license.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/h2/license.data.sql @@ -1,3 +1,3 @@ #license INSERT INTO license(`id`, `name`, `key`, `user_id`, `is_online`, `enable`, `create_time`, `update_time`) VALUES -(1, '服务端(不可编辑删除,可禁用)', 'b0a907332b474b25897c4dcb31fc7eb6', 1, 1, 1, now(), now()); +(1, '测试客户端', 'b0a907332b474b25897c4dcb31fc7eb6', 1, 1, 1, now(), now());