2019-04-06 14:41:38 +03:00
|
|
|
package ru.gravit.launchserver.websocket;
|
|
|
|
|
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
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.socket.nio.NioServerSocketChannel;
|
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
|
|
import io.netty.handler.codec.http.HttpObjectAggregator;
|
|
|
|
import io.netty.handler.codec.http.HttpServerCodec;
|
|
|
|
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
|
|
|
|
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler;
|
|
|
|
import io.netty.handler.logging.LoggingHandler;
|
2019-05-03 19:07:37 +03:00
|
|
|
import ru.gravit.launcher.request.Request;
|
|
|
|
import ru.gravit.launcher.request.auth.AuthRequest;
|
|
|
|
import ru.gravit.launcher.request.websockets.StandartClientWebSocketService;
|
2019-04-06 14:41:38 +03:00
|
|
|
import ru.gravit.launchserver.LaunchServer;
|
|
|
|
import ru.gravit.launchserver.websocket.fileserver.FileServerHandler;
|
2019-05-03 19:07:37 +03:00
|
|
|
import ru.gravit.utils.helper.LogHelper;
|
2019-04-06 14:41:38 +03:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
|
|
public class LauncherNettyServer implements AutoCloseable {
|
|
|
|
public final ServerBootstrap serverBootstrap;
|
|
|
|
public final EventLoopGroup bossGroup;
|
|
|
|
public final EventLoopGroup workerGroup;
|
|
|
|
private static final String WEBSOCKET_PATH = "/api";
|
|
|
|
|
|
|
|
public LauncherNettyServer() {
|
|
|
|
LaunchServer.NettyConfig config = LaunchServer.server.config.netty;
|
|
|
|
bossGroup = new NioEventLoopGroup(config.performance.bossThread);
|
|
|
|
workerGroup = new NioEventLoopGroup(config.performance.workerThread);
|
|
|
|
serverBootstrap = new ServerBootstrap();
|
|
|
|
serverBootstrap.group(bossGroup, workerGroup)
|
|
|
|
.channel(NioServerSocketChannel.class)
|
2019-04-29 11:59:30 +03:00
|
|
|
.handler(new LoggingHandler(config.logLevel))
|
2019-04-06 14:41:38 +03:00
|
|
|
.childHandler(new ChannelInitializer<NioSocketChannel>() {
|
|
|
|
@Override
|
|
|
|
public void initChannel(NioSocketChannel ch) {
|
|
|
|
ChannelPipeline pipeline = ch.pipeline();
|
2019-05-09 16:00:10 +03:00
|
|
|
NettyConnectContext context = new NettyConnectContext();
|
2019-04-06 14:41:38 +03:00
|
|
|
//p.addLast(new LoggingHandler(LogLevel.INFO));
|
|
|
|
pipeline.addLast(new HttpServerCodec());
|
|
|
|
pipeline.addLast(new HttpObjectAggregator(65536));
|
|
|
|
pipeline.addLast(new WebSocketServerCompressionHandler());
|
|
|
|
pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
|
2019-04-28 15:06:12 +03:00
|
|
|
if (LaunchServer.server.config.netty.fileServerEnabled) pipeline.addLast(new FileServerHandler(LaunchServer.server.updatesDir, true));
|
2019-05-09 16:00:10 +03:00
|
|
|
if (LaunchServer.server.config.netty.ipForwarding) pipeline.addLast(new NettyIpForwardHandler(context));
|
|
|
|
pipeline.addLast(new WebSocketFrameHandler(context));
|
2019-04-06 14:41:38 +03:00
|
|
|
}
|
|
|
|
});
|
2019-05-04 09:59:42 +03:00
|
|
|
if(config.proxy != null && config.proxy.enabled)
|
2019-05-03 19:07:37 +03:00
|
|
|
{
|
|
|
|
LogHelper.info("Connect to main server %s");
|
|
|
|
Request.service = StandartClientWebSocketService.initWebSockets(config.proxy.address, false);
|
|
|
|
AuthRequest authRequest = new AuthRequest(config.proxy.login, config.proxy.password, config.proxy.auth_id, AuthRequest.ConnectTypes.PROXY);
|
|
|
|
authRequest.initProxy = true;
|
|
|
|
try {
|
|
|
|
authRequest.request();
|
|
|
|
} catch (Exception e) {
|
|
|
|
LogHelper.error(e);
|
|
|
|
}
|
|
|
|
}
|
2019-04-06 14:41:38 +03:00
|
|
|
}
|
|
|
|
public ChannelFuture bind(InetSocketAddress address)
|
|
|
|
{
|
|
|
|
return serverBootstrap.bind(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-20 01:14:02 +03:00
|
|
|
public void close() {
|
2019-04-06 14:41:38 +03:00
|
|
|
workerGroup.shutdownGracefully();
|
|
|
|
bossGroup.shutdownGracefully();
|
|
|
|
}
|
|
|
|
}
|