diff --git a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ClientChannelHandler.java b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ClientChannelHandler.java index 12155b07..8290de15 100644 --- a/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ClientChannelHandler.java +++ b/neutrino-proxy-client/src/main/java/fun/asgc/neutrino/proxy/client/core/ClientChannelHandler.java @@ -29,6 +29,7 @@ import fun.asgc.neutrino.core.util.LockUtil; import fun.asgc.neutrino.proxy.client.util.ProxyUtil; import fun.asgc.neutrino.proxy.core.*; import io.netty.channel.*; +import io.netty.handler.timeout.IdleStateEvent; import lombok.extern.slf4j.Slf4j; /** @@ -97,4 +98,22 @@ public class ClientChannelHandler extends SimpleChannelInboundHandler findEnableListByLicenseId(Integer licenseId); + + @Update("update `port_mapping` set is_online = :isOnline,update_time = :updateTime where license_id = :licenseId and server_port = :serverPort") + void updateOnlineStatus(@Param("licenseId") Integer licenseId, @Param("serverPort") Integer serverPort, @Param("isOnline") Integer isOnline, @Param("updateTime") Date updateTime); + + @Update("update `port_mapping` set is_online = :isOnline,update_time = :updateTime where license_id = :licenseId") + void updateOnlineStatus(@Param("licenseId") Integer licenseId, @Param("isOnline") Integer isOnline, @Param("updateTime") Date updateTime); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ProxyServerRunner.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ProxyServerRunner.java index 708d91c3..1fc3e49b 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ProxyServerRunner.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ProxyServerRunner.java @@ -28,7 +28,6 @@ import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.NonIntercept; import fun.asgc.neutrino.core.context.ApplicationRunner; import fun.asgc.neutrino.core.util.FileUtil; -import fun.asgc.neutrino.proxy.core.IdleCheckHandler; import fun.asgc.neutrino.proxy.core.ProxyMessageDecoder; import fun.asgc.neutrino.proxy.core.ProxyMessageEncoder; import fun.asgc.neutrino.proxy.server.base.proxy.ProxyConfig; @@ -39,6 +38,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.ssl.SslHandler; +import io.netty.handler.timeout.IdleStateHandler; import lombok.extern.slf4j.Slf4j; import javax.net.ssl.*; @@ -137,7 +137,7 @@ public class ProxyServerRunner implements ApplicationRunner { proxyConfig.getProtocol().getLengthFieldOffset(), proxyConfig.getProtocol().getLengthFieldLength(), proxyConfig.getProtocol().getLengthAdjustment(), proxyConfig.getProtocol().getInitialBytesToStrip())); ch.pipeline().addLast(new ProxyMessageEncoder()); - ch.pipeline().addLast(new IdleCheckHandler(proxyConfig.getProtocol().getReadIdleTime(), proxyConfig.getProtocol().getWriteIdleTime(), proxyConfig.getProtocol().getAllIdleTimeSeconds())); + ch.pipeline().addLast(new IdleStateHandler(proxyConfig.getProtocol().getReadIdleTime(), proxyConfig.getProtocol().getWriteIdleTime(), proxyConfig.getProtocol().getAllIdleTimeSeconds())); ch.pipeline().addLast(new ServerChannelHandler()); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ServerChannelHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ServerChannelHandler.java index 3ba2cd9b..dbe91058 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ServerChannelHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/core/ServerChannelHandler.java @@ -27,15 +27,20 @@ import fun.asgc.neutrino.core.base.Dispatcher; import fun.asgc.neutrino.core.util.BeanManager; import fun.asgc.neutrino.core.util.LockUtil; import fun.asgc.neutrino.proxy.core.*; +import fun.asgc.neutrino.proxy.server.proxy.domain.CmdChannelAttachInfo; +import fun.asgc.neutrino.proxy.server.service.ProxyMutualService; import fun.asgc.neutrino.proxy.server.util.ProxyUtil; import io.netty.buffer.Unpooled; import io.netty.channel.*; +import io.netty.handler.timeout.IdleStateEvent; +import lombok.extern.slf4j.Slf4j; /** * * @author: aoshiguchen * @date: 2022/6/16 */ +@Slf4j public class ServerChannelHandler extends SimpleChannelInboundHandler { private static volatile Dispatcher dispatcher; @@ -77,13 +82,17 @@ public class ServerChannelHandler extends SimpleChannelInboundHandler // 用户连接断开,从控制连接中移除 String userId = ProxyUtil.getVisitorChannelUserId(userChannel); - ProxyUtil.removeUserChannelFromCmdChannel(cmdChannel, userId); + ProxyUtil.removeVisitorChannelFromCmdChannel(cmdChannel, userId); Channel proxyChannel = userChannel.attr(Constants.NEXT_CHANNEL).get(); if (proxyChannel != null && proxyChannel.isActive()) { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageAuthHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageAuthHandler.java index 226c8a8c..e0189e78 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageAuthHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageAuthHandler.java @@ -143,9 +143,9 @@ public class ProxyMessageAuthHandler implements ProxyMessageHandler { for (PortMappingDO portMapping : portMappingList) { try { + proxyMutualService.bindServerPort(cmdChannelAttachInfo, portMapping.getServerPort()); bootstrap.bind(portMapping.getServerPort()).get(); log.info("绑定用户端口: {}", portMapping.getServerPort()); - proxyMutualService.bindServerPort(cmdChannelAttachInfo, portMapping.getServerPort()); } catch (Exception ex) { // BindException表示该端口已经绑定过 if (!(ex.getCause() instanceof BindException)) { diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageDisconnectHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageDisconnectHandler.java index a297c319..5ae602aa 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageDisconnectHandler.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/proxy/handler/ProxyMessageDisconnectHandler.java @@ -29,7 +29,6 @@ import fun.asgc.neutrino.proxy.core.Constants; import fun.asgc.neutrino.proxy.core.ProxyDataTypeEnum; import fun.asgc.neutrino.proxy.core.ProxyMessage; import fun.asgc.neutrino.proxy.core.ProxyMessageHandler; -import fun.asgc.neutrino.proxy.server.proxy.domain.VisitorChannelAttachInfo; import fun.asgc.neutrino.proxy.server.util.ProxyUtil; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; @@ -55,7 +54,7 @@ public class ProxyMessageDisconnectHandler implements ProxyMessageHandler { } // 代理连接没有连上服务器由控制连接发送用户端断开连接消息 String visitorId = proxyMessage.getInfo(); - Channel userChannel = ProxyUtil.removeUserChannelFromCmdChannel(ctx.channel(), visitorId); + Channel userChannel = ProxyUtil.removeVisitorChannelFromCmdChannel(ctx.channel(), visitorId); if (null != userChannel) { // 数据发送完成后再关闭连接,解决http1.0数据传输问题 userChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java index 32cd32ab..25aaf1ad 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/PortMappingService.java @@ -186,7 +186,7 @@ public class PortMappingService { ParamCheckUtil.checkExpression(!licenseDO.getUserId().equals(1), ExceptionConstant.NO_PERMISSION_VISIT); } - portMappingMapper.updateEnableStatus(req.getId(), req.getEnable()); + portMappingMapper.updateEnableStatus(req.getId(), req.getEnable(), new Date()); return new PortMappingUpdateEnableStatusRes(); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/ProxyMutualService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/ProxyMutualService.java index bbf13449..bb2688ac 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/ProxyMutualService.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/ProxyMutualService.java @@ -24,10 +24,14 @@ package fun.asgc.neutrino.proxy.server.service; import fun.asgc.neutrino.core.annotation.Autowired; import fun.asgc.neutrino.core.annotation.Component; import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.proxy.server.constant.OnlineStatusEnum; +import fun.asgc.neutrino.proxy.server.dal.LicenseMapper; import fun.asgc.neutrino.proxy.server.dal.PortMappingMapper; import fun.asgc.neutrino.proxy.server.proxy.domain.CmdChannelAttachInfo; import lombok.extern.slf4j.Slf4j; +import java.util.Date; + /** * 代理交互服务 * @author: aoshiguchen @@ -39,6 +43,8 @@ import lombok.extern.slf4j.Slf4j; public class ProxyMutualService { @Autowired private PortMappingMapper portMappingMapper; + @Autowired + private LicenseMapper licenseMapper; /** * 绑定服务端端口处理 @@ -46,8 +52,21 @@ public class ProxyMutualService { * @param serverPort */ public void bindServerPort(CmdChannelAttachInfo attachInfo, Integer serverPort) { - // TODO + Date now = new Date(); + portMappingMapper.updateOnlineStatus(attachInfo.getLicenseId(), serverPort, OnlineStatusEnum.ONLINE.getStatus(), now); + licenseMapper.updateOnlineStatus(attachInfo.getLicenseId(), OnlineStatusEnum.ONLINE.getStatus(), now); log.info("绑定服务端端口 licenseId:{},ip:{},serverPort:{}", attachInfo.getLicenseId(), attachInfo.getIp(), serverPort); } + /** + * 客户端下线 + * @param attachInfo + */ + public void offline(CmdChannelAttachInfo attachInfo) { + Date now = new Date(); + portMappingMapper.updateOnlineStatus(attachInfo.getLicenseId(), OnlineStatusEnum.OFFLINE.getStatus(), now); + licenseMapper.updateOnlineStatus(attachInfo.getLicenseId(), OnlineStatusEnum.OFFLINE.getStatus(), now); + log.info("客户端下线 licenseId:{},ip:{}", attachInfo.getLicenseId(), attachInfo.getIp()); + } + } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ProxyUtil.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ProxyUtil.java index 61d8ce01..738f8b76 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ProxyUtil.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/util/ProxyUtil.java @@ -190,7 +190,7 @@ public class ProxyUtil { } } - public static Channel removeUserChannelFromCmdChannel(Channel cmdChannel, String visitorId) { + public static Channel removeVisitorChannelFromCmdChannel(Channel cmdChannel, String visitorId) { if (null == getAttachInfo(cmdChannel) || null == ((CmdChannelAttachInfo)getAttachInfo(cmdChannel)).getVisitorChannelMap().get(visitorId)) { return null; } diff --git a/neutrino-proxy-server/src/main/resources/application.yml b/neutrino-proxy-server/src/main/resources/application.yml index cdd67312..f767f813 100644 --- a/neutrino-proxy-server/src/main/resources/application.yml +++ b/neutrino-proxy-server/src/main/resources/application.yml @@ -17,8 +17,8 @@ neutrino: length-field-length: 4 initial-bytes-to-strip: 0 length-adjustment: 0 - read-idle-time: 60 - write-idle-time: 40 + read-idle-time: 40 + write-idle-time: 10 all-idle-time-seconds: 0 server: port: 9000