This commit is contained in:
wujiawei 2023-02-12 10:34:46 +08:00
parent fae329ea3e
commit 6cb17fbd58
20 changed files with 837 additions and 831 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>netty-proxy</artifactId> <artifactId>netty-proxy</artifactId>

View File

@ -1,8 +1,5 @@
package com.luck.client; package com.luck.client;
import org.springframework.boot.SpringApplication;
public class ClientStart { public class ClientStart {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@ -1,32 +1,44 @@
package com.luck.client; package com.luck.client;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Constant { public class Constant {
/** 代理服务channel */ /**
public static Channel proxyChannel = null; * 绑定访客id
*/
/** 绑定访客id */
public static final AttributeKey<String> VID = AttributeKey.newInstance("vid"); public static final AttributeKey<String> VID = AttributeKey.newInstance("vid");
/**
/** 访客代理服务channel */ * 代理服务channel
*/
public static Channel proxyChannel = null;
/**
* 访客代理服务channel
*/
public static Map<String, Channel> vpc = new ConcurrentHashMap<>(); public static Map<String, Channel> vpc = new ConcurrentHashMap<>();
/** 访客真实服务channel */ /**
* 访客真实服务channel
*/
public static Map<String, Channel> vrc = new ConcurrentHashMap<>(); public static Map<String, Channel> vrc = new ConcurrentHashMap<>();
/** 真实服务端口 */ /**
* 真实服务端口
*/
public static int realPort = 8080; public static int realPort = 8080;
/** 服务端口 */ /**
* 服务端口
*/
public static int serverPort = 16001; public static int serverPort = 16001;
/** 服务IP */ /**
* 服务IP
*/
public static String serverIp = "127.0.0.1"; public static String serverIp = "127.0.0.1";
/** /**

View File

@ -1,7 +1,6 @@
package com.luck.client; package com.luck.client;
import com.luck.msg.MyMsg; import com.luck.msg.MyMsg;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;

View File

@ -1,32 +1,26 @@
package com.luck.client; package com.luck.client;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.luck.msg.MyMsg; import com.luck.msg.MyMsg;
import com.luck.msg.MyMsgDecoder; import com.luck.msg.MyMsgDecoder;
import com.luck.msg.MyMsgEncoder; import com.luck.msg.MyMsgEncoder;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; import io.netty.channel.*;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
public class ProxySocket { import java.util.concurrent.Executors;
private static EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/** 重连代理服务 */ public class ProxySocket {
/**
* 重连代理服务
*/
private static final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor(); private static final ScheduledExecutorService reconnectExecutor = Executors.newSingleThreadScheduledExecutor();
private static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
public static Channel connectProxyServer() throws Exception { public static Channel connectProxyServer() throws Exception {
reconnectExecutor.scheduleAtFixedRate(() -> { reconnectExecutor.scheduleAtFixedRate(() -> {

View File

@ -1,7 +1,6 @@
package com.luck.client; package com.luck.client;
import com.luck.msg.MyMsg; import com.luck.msg.MyMsg;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;

View File

@ -1,13 +1,7 @@
package com.luck.client; package com.luck.client;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; import io.netty.channel.*;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;

View File

@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.GetMapping;
public class ClientController { public class ClientController {
@GetMapping("/version") @GetMapping("/version")
public Result version(){ public Result version() {
return ResultFactory.successOf("客户端版本"); return ResultFactory.successOf("客户端版本");
} }
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>netty-proxy</artifactId> <artifactId>netty-proxy</artifactId>

View File

@ -4,22 +4,34 @@ import java.util.Arrays;
public class MyMsg { public class MyMsg {
/** 心跳 */ /**
* 心跳
*/
public static final byte TYPE_HEARTBEAT = 0X00; public static final byte TYPE_HEARTBEAT = 0X00;
/** 连接成功 */ /**
* 连接成功
*/
public static final byte TYPE_CONNECT = 0X01; public static final byte TYPE_CONNECT = 0X01;
/** 数据传输 */ /**
* 数据传输
*/
public static final byte TYPE_TRANSFER = 0X02; public static final byte TYPE_TRANSFER = 0X02;
/** 连接断开 */ /**
* 连接断开
*/
public static final byte TYPE_DISCONNECT = 0X09; public static final byte TYPE_DISCONNECT = 0X09;
/** 数据类型 */ /**
* 数据类型
*/
private byte type; private byte type;
/** 消息传输数据 */ /**
* 消息传输数据
*/
private byte[] data; private byte[] data;
public byte getType() { public byte getType() {

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <parent>
<artifactId>netty-proxy</artifactId> <artifactId>netty-proxy</artifactId>

View File

@ -1,15 +1,9 @@
package com.luck.server; package com.luck.server;
import com.luck.msg.MyMsg; import com.luck.msg.MyMsg;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.Channel; import io.netty.channel.*;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
@ -65,7 +59,7 @@ public class ClientHandler extends SimpleChannelInboundHandler<MyMsg> {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
String vid = ctx.channel().attr(Constant.VID).get(); String vid = ctx.channel().attr(Constant.VID).get();
if(StringUtil.isNullOrEmpty(vid)) { if (StringUtil.isNullOrEmpty(vid)) {
super.channelWritabilityChanged(ctx); super.channelWritabilityChanged(ctx);
return; return;
} }

View File

@ -1,29 +1,39 @@
package com.luck.server; package com.luck.server;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Constant { public class Constant {
/** 客户端服务channel */ /**
public static Channel clientChannel = null; * 绑定channel_id
*/
/** 绑定channel_id */
public static final AttributeKey<String> VID = AttributeKey.newInstance("vid"); public static final AttributeKey<String> VID = AttributeKey.newInstance("vid");
/**
/** 访客客户服务channel */ * 客户端服务channel
*/
public static Channel clientChannel = null;
/**
* 访客客户服务channel
*/
public static Map<String, Channel> vcc = new ConcurrentHashMap<>(); public static Map<String, Channel> vcc = new ConcurrentHashMap<>();
/** 访客访客服务channel */ /**
* 访客访客服务channel
*/
public static Map<String, Channel> vvc = new ConcurrentHashMap<>(); public static Map<String, Channel> vvc = new ConcurrentHashMap<>();
/** 服务代理端口 */ /**
* 服务代理端口
*/
public static int visitorPort = 16002; public static int visitorPort = 16002;
/** 服务端口 */ /**
* 服务端口
*/
public static int serverPort = 16001; public static int serverPort = 16001;
/** /**

View File

@ -2,13 +2,8 @@ package com.luck.server;
import com.luck.msg.MyMsgDecoder; import com.luck.msg.MyMsgDecoder;
import com.luck.msg.MyMsgEncoder; import com.luck.msg.MyMsgEncoder;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.*;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
@ -21,6 +16,7 @@ public class ServerSocket {
/** /**
* 启动服务端 * 启动服务端
*
* @throws Exception * @throws Exception
*/ */
public static void startServer() throws Exception { public static void startServer() throws Exception {

View File

@ -1,9 +1,6 @@
package com.luck.server; package com.luck.server;
import java.util.UUID;
import com.luck.msg.MyMsg; import com.luck.msg.MyMsg;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
@ -11,6 +8,8 @@ import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.util.UUID;
public class VisitorHandler extends SimpleChannelInboundHandler<ByteBuf> { public class VisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override @Override

View File

@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.GetMapping;
public class ServerController { public class ServerController {
@GetMapping("/version") @GetMapping("/version")
public Result version(){ public Result version() {
return ResultFactory.successOf("服务端端版本"); return ResultFactory.successOf("服务端端版本");
} }
} }

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging> <packaging>pom</packaging>