|
@@ -1,6 +1,6 @@
|
|
|
package com.xiaobao.gateway.protocol.proxy.forward;
|
|
package com.xiaobao.gateway.protocol.proxy.forward;
|
|
|
|
|
|
|
|
-import com.xiaobao.gateway.protocol.dto.MediaServerDTO;
|
|
|
|
|
|
|
+import com.xiaobao.gateway.protocol.config.MediaServer;
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
import io.netty.channel.ChannelOption;
|
|
import io.netty.channel.ChannelOption;
|
|
@@ -26,47 +26,41 @@ public class ForwardProxyServer {
|
|
|
|
|
|
|
|
private static final InternalLogger log = InternalLoggerFactory.getInstance(ForwardProxyServer.class);
|
|
private static final InternalLogger log = InternalLoggerFactory.getInstance(ForwardProxyServer.class);
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 反向代理网关端口
|
|
|
|
|
- */
|
|
|
|
|
- private static final int REMOTE_PORT = 9000;
|
|
|
|
|
- /**
|
|
|
|
|
- * 反向代理网关地址
|
|
|
|
|
- */
|
|
|
|
|
- private static final String REMOTE_HOST = "127.0.0.1";
|
|
|
|
|
-
|
|
|
|
|
- public static void main(String[] args) throws InterruptedException {
|
|
|
|
|
- List<MediaServerDTO> serverList = MediaServerDTO.getServerList();
|
|
|
|
|
- ExecutorService executorService = Executors.newFixedThreadPool(serverList.size());
|
|
|
|
|
- for (MediaServerDTO server : serverList) {
|
|
|
|
|
- executorService.submit(() -> {
|
|
|
|
|
- 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();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- throw new RuntimeException(e);
|
|
|
|
|
- } finally {
|
|
|
|
|
- bossGroup.shutdownGracefully();
|
|
|
|
|
- workerGroup.shutdownGracefully();
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ public static void bootstrap(String reverseHost, int reversePort, List<MediaServer> mediaServerList) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ExecutorService executorService = Executors.newFixedThreadPool(mediaServerList.size());
|
|
|
|
|
+ for (MediaServer server : mediaServerList) {
|
|
|
|
|
+ executorService.submit(() -> {
|
|
|
|
|
+ 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(reverseHost, reversePort, server));
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .childOption(ChannelOption.AUTO_READ, false)
|
|
|
|
|
+ .bind(proxyPort).sync().channel().closeFuture().sync();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ bossGroup.shutdownGracefully();
|
|
|
|
|
+ workerGroup.shutdownGracefully();
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ executorService.shutdown();
|
|
|
|
|
+ executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("启动正向代理出错", e);
|
|
|
}
|
|
}
|
|
|
- executorService.shutdown();
|
|
|
|
|
- executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|