|
|
@@ -1,15 +1,21 @@
|
|
|
package com.xiaobao.gateway.protocol.proxy.forward;
|
|
|
|
|
|
+import com.xiaobao.gateway.protocol.dto.MediaServerDTO;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
+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.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
import io.netty.handler.logging.LogLevel;
|
|
|
import io.netty.handler.logging.LoggingHandler;
|
|
|
import io.netty.util.internal.logging.InternalLogger;
|
|
|
import io.netty.util.internal.logging.InternalLoggerFactory;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* 正向代理(代理客户端)
|
|
|
*/
|
|
|
@@ -17,27 +23,45 @@ public class ForwardProxyServer {
|
|
|
|
|
|
private static final InternalLogger log = InternalLoggerFactory.getInstance(ForwardProxyServer.class);
|
|
|
|
|
|
- private static final int LOCAL_PORT = 1935;
|
|
|
+ /**
|
|
|
+ * 反向代理网关端口
|
|
|
+ */
|
|
|
+ private static final int REMOTE_PORT = 9000;
|
|
|
+ /**
|
|
|
+ * 反向代理网关地址
|
|
|
+ */
|
|
|
private static final String REMOTE_HOST = "127.0.0.1";
|
|
|
- private static final int REMOTE_PORT = 1936;
|
|
|
-
|
|
|
- public static void main(String[] args) throws Exception {
|
|
|
- log.info("Forward Proxying *:{} to {}:{}", LOCAL_PORT, REMOTE_HOST, REMOTE_PORT);
|
|
|
|
|
|
- // Configure the bootstrap.
|
|
|
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
|
|
- EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
- try {
|
|
|
- ServerBootstrap b = new ServerBootstrap();
|
|
|
- b.group(bossGroup, workerGroup)
|
|
|
- .channel(NioServerSocketChannel.class)
|
|
|
- .handler(new LoggingHandler(LogLevel.INFO))
|
|
|
- .childHandler(new ForwardProxyServerInitializer(REMOTE_HOST, REMOTE_PORT))
|
|
|
- .childOption(ChannelOption.AUTO_READ, false)
|
|
|
- .bind(LOCAL_PORT).sync().channel().closeFuture().sync();
|
|
|
- } finally {
|
|
|
- bossGroup.shutdownGracefully();
|
|
|
- workerGroup.shutdownGracefully();
|
|
|
+ public static void main(String[] args) {
|
|
|
+ List<MediaServerDTO> serverList = MediaServerDTO.getMediaServerList();
|
|
|
+ for (MediaServerDTO server : serverList) {
|
|
|
+ new Thread(() -> {
|
|
|
+ int proxyPort = server.getProxyPort();
|
|
|
+ EventLoopGroup bossGroup = new NioEventLoopGroup(1);
|
|
|
+ EventLoopGroup workerGroup = new NioEventLoopGroup();
|
|
|
+ try {
|
|
|
+ ServerBootstrap b = new ServerBootstrap();
|
|
|
+ b.group(bossGroup, workerGroup)
|
|
|
+ .channel(NioServerSocketChannel.class)
|
|
|
+ .handler(new LoggingHandler(LogLevel.INFO))
|
|
|
+ .childHandler(new ChannelInitializer<SocketChannel>() {
|
|
|
+ @Override
|
|
|
+ public void initChannel(SocketChannel ch) {
|
|
|
+ ChannelPipeline pipeline = ch.pipeline();
|
|
|
+ pipeline.addLast(new LoggingHandler(LogLevel.INFO));
|
|
|
+ pipeline.addLast(new ForwardProxyServerInHandler(REMOTE_HOST, REMOTE_PORT, server));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .childOption(ChannelOption.AUTO_READ, false)
|
|
|
+ .bind(proxyPort).sync().channel().closeFuture().sync();
|
|
|
+ log.info("启动服务端口" + proxyPort + "成功");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("启动服务端口" + proxyPort + "出错", e);
|
|
|
+ } finally {
|
|
|
+ bossGroup.shutdownGracefully();
|
|
|
+ workerGroup.shutdownGracefully();
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
}
|
|
|
}
|
|
|
}
|